package com.diagbot.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.diagbot.annotation.SysLogger;
import com.diagbot.dto.EnShowOptionDTO;
import com.diagbot.dto.GetConsoleOnTrialDTO;
import com.diagbot.dto.GetConsoleOpenedDTO;
import com.diagbot.dto.OpenUpOnTrialDTO;
import com.diagbot.dto.ProductAuthProgressDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.UserAndProdutDTO;
import com.diagbot.dto.WaitExamOrderCouDTO;
import com.diagbot.entity.OpenedProducts;
import com.diagbot.entity.wrapper.OrderDetailsWapper;
import com.diagbot.entity.wrapper.ProductOrderWrapper;
import com.diagbot.facade.LantoneProductFacade;
import com.diagbot.facade.OpenedProductsFacade;
import com.diagbot.facade.ProductOrderFacade;
import com.diagbot.util.DateUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.AuditStatusVO;
import com.diagbot.vo.ModifyOpeningTimeVO;
import com.diagbot.vo.OpenUpOnTrialVO;
import com.diagbot.vo.OrderDetialsVO;
import com.diagbot.vo.ProductOrderVO;
import com.diagbot.vo.StartAndendByUserIdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 
 * @author rgb
 *
 */
@Api(value="控制台-产品操作API", tags={"控制台-产品操作API"})
@RestController
@RequestMapping("/productOrder")
@SuppressWarnings("unchecked")
public class ProductOrderController {
	@Autowired
	private LantoneProductFacade lantoneProductFacade;

	@Autowired
	private ProductOrderFacade productOrderFacade;
	@Autowired
	private OpenedProductsFacade openedProductsFacade;

	@ApiOperation(value = "控制台-产品认证进度(示例)[by:rengb]",notes="控制台-产品认证进度")
	@PostMapping("/productAuthProgress")
	@SysLogger("productAuthProgress")
	public RespDTO<List<ProductAuthProgressDTO>> productAuthProgress() {
		return RespDTO.onSuc(productOrderFacade.productAuthProgress());
	}
	
	@ApiOperation(value = "控制台-已开通产品(示例)[by:rengb]",notes="控制台-已开通产品")
	@PostMapping("/getConsoleOpened")
	@SysLogger("getConsoleOpened")
	public RespDTO<List<GetConsoleOpenedDTO>> getConsoleOpened() {
		return RespDTO.onSuc(openedProductsFacade.getConsoleOpened());
	}
	
	@ApiOperation(value = "控制台-可试用产品(示例)[by:rengb]",notes="控制台-可试用产品")
	@PostMapping("/getConsoleOnTrial")
	@SysLogger("getConsoleOnTrial")
	public RespDTO<List<GetConsoleOnTrialDTO>> getConsoleOnTrial() {
		return RespDTO.onSuc(openedProductsFacade.getConsoleOnTrial());
	}
	
	@ApiOperation(value = "控制台-产品立即试用接口(示例)[by:rengb]",notes="控制台-产品立即试用接口")
	@PostMapping("/openUpOnTrial")
	@SysLogger("openUpOnTrial")
	public RespDTO<OpenUpOnTrialDTO> openUpOnTrial(OpenUpOnTrialVO openUpOnTrialVO) {
		return RespDTO.onSuc(openedProductsFacade.openUpOnTrial(openUpOnTrialVO));
	}
	
	@ApiOperation(value = "控制台-可展示的选项卡(示例)[by:rengb]",notes="控制台-可展示的选项卡")
	@PostMapping("/enShowOption")
	@SysLogger("enShowOption")
	public RespDTO<EnShowOptionDTO> enShowOption() {
		return RespDTO.onSuc(openedProductsFacade.enShowOption());
	}
	
	@ApiOperation(value = "控制台-待审核订单个数(示例)[by:rengb]",notes="控制台-待审核订单个数")
	@PostMapping("/waitExamOrderCou")
	@SysLogger("waitExamOrderCou")
	public RespDTO<WaitExamOrderCouDTO> waitExamOrderCou() {
		return RespDTO.onSuc(productOrderFacade.waitExamOrderCou());
	}

	@ApiOperation(value = "根据用户id查询已开通的功能[by:wangfeng]", notes = "已开通信息")
    @PostMapping("/getInformationAvailableByUserId")
    @SysLogger("getInformationAvailableByUserId")
    public RespDTO  getInformationAvailableByUserId(@RequestParam Long userId){
    	List<UserAndProdutDTO> UserAndProdutData = openedProductsFacade.getInformationAvailableByUserId(userId);
    	return RespDTO.onSuc(UserAndProdutData);
    }

	@ApiOperation(value = "根据用户id和产品id，启用和停用功能[by:wangfeng]", notes = "开通信息")
	@PostMapping("/startAndendByuserId")
	@SysLogger("startAndendByuserId")
	@Transactional
	public RespDTO<StartAndendByUserIdVO> startAndendByuserId(@RequestBody @Valid StartAndendByUserIdVO startAndendByUserIdVO) {
		OpenedProducts openedProducts = new OpenedProducts();
		openedProducts.setUserId(startAndendByUserIdVO.getUserId());
		openedProducts.setProductId(startAndendByUserIdVO.getProductId());
		openedProducts.setCreator(UserUtils.getCurrentPrincipleID());
		openedProducts.setGmtCreate(DateUtil.now());
		openedProducts.setServiceStatus(startAndendByUserIdVO.getServiceStatus());
		boolean res =openedProductsFacade.startAndendByuserId(openedProducts);
		if(res !=true  ){
			return RespDTO.onSuc("操作失败!");
		}
		return RespDTO.onSuc("操作成功!");
	}

	@ApiOperation(value = "根据用户id和产品id，删除开通信息[by:wangfeng]", notes = "删除开通信息")
	@PostMapping("/delInformationAvailable")
	@SysLogger("delInformationAvailable")
	@Transactional
	public RespDTO delInformationAvailable(@RequestParam Long userId, Long productId) {
		OpenedProducts openedProducts = new OpenedProducts();
		openedProducts.setUserId(userId);
		openedProducts.setProductId(productId);
		openedProducts.setCreator(UserUtils.getCurrentPrincipleID());
		openedProducts.setGmtCreate(DateUtil.now());
		boolean res =openedProductsFacade.delInformationAvailable(openedProducts);
		if(res !=true  ){
			return RespDTO.onSuc("操作失败!");
		}
		return RespDTO.onSuc("操作成功!");
	}

	@ApiOperation(value = "根据用户id和产品id，修改开通时间[by:wangfeng]", notes = "修改开通时间")
	@PostMapping("/modifyOpeningTime")
	@SysLogger("modifyOpeningTime")
	@Transactional
	public RespDTO<ModifyOpeningTimeVO> modifyOpeningTime(@RequestBody @Valid ModifyOpeningTimeVO modifyOpeningTimeVO) {
		OpenedProducts openedProducts = new OpenedProducts();
		openedProducts.setUserId(modifyOpeningTimeVO.getUserId());
		openedProducts.setProductId(modifyOpeningTimeVO.getProductId());
		openedProducts.setStartTime(DateUtil.parseDate(modifyOpeningTimeVO.getStartTime()));
		openedProducts.setEndTime(DateUtil.parseDate(modifyOpeningTimeVO.getEndTime()));
		//TODO wangfeng openedProducts.setCreator(UserUtils.getCurrentPrincipleID());
		openedProducts.setGmtCreate(DateUtil.now());
		boolean res = openedProductsFacade.modifyOpeningTime(openedProducts);
		if(res !=true  ){
			return RespDTO.onSuc("操作失败!");
		}
		return RespDTO.onSuc("操作成功!");
	}

	@ApiOperation(value = "查询所有订单明细[by:wangyu]",
			notes =
					"current:页数<br>"+
							"size:每页显示条数<br>"+
							"status:订单状态（0.未付款1.已付款）")
	@PostMapping("/getAllOrderDetials")
	@SysLogger("getAllOrderDetials")
	public RespDTO<List<OrderDetailsWapper>> getAllOrderDetials(OrderDetialsVO orderDetialsVO) {
		List<OrderDetailsWapper> list = productOrderFacade.getAllOrderDetials(orderDetialsVO);
		return RespDTO.onSuc(list);
	}

	@ApiOperation(value = "产品申请显示所有订单接口[by:wangyu]",
			notes =
					"current:页数<br>"+
							"size:每页显示条数<br>"+
							"status:订单状态（0.未付款1.已付款）")
	@PostMapping("/getAllProductOrder")
	@SysLogger("getAllProductOrder")
	public RespDTO<List<ProductOrderWrapper>> getAllProductOrder(ProductOrderVO productOrderVO) {
		List<ProductOrderWrapper> list = productOrderFacade.selectAllProductOrder(productOrderVO);
		return RespDTO.onSuc(list);
	}

	@ApiOperation(value = "产品申请审核接口[by:wangyu]",
			notes =
					"id:订单明细id，根据订单明细id修改审核状态，必填<br>")
	@PostMapping("/auditStatus")
	@SysLogger("auditStatus")
	public RespDTO<Boolean> auditStatus(AuditStatusVO auditStatusVO) {
		return RespDTO.onSuc(productOrderFacade.updateAuditStatus(auditStatusVO));
	}
}
