package com.diagbot.facade;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.client.UserServiceClient;
import com.diagbot.dto.AuthDetailDTO;
import com.diagbot.dto.ProductAuthProgressDTO;
import com.diagbot.dto.ProductOrderExportDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.UserOrgDTO;
import com.diagbot.dto.WaitExamOrderCouDTO;
import com.diagbot.entity.OrderDetailStatus;
import com.diagbot.entity.OrderDetails;
import com.diagbot.entity.ProductOrder;
import com.diagbot.entity.ProductOrderIndex;
import com.diagbot.entity.wrapper.OrderDetailsWapper;
import com.diagbot.entity.wrapper.ProductOrderWrapper;
import com.diagbot.enums.AuditStatusEnum;
import com.diagbot.enums.VisibleIdTypeEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.idc.VisibleIdCreater;
import com.diagbot.service.impl.ProductOrderServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.ExportBeanExcelUtil;
import com.diagbot.util.GsonUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.AddOrderVO;
import com.diagbot.vo.OrderDetialsVO;
import com.diagbot.vo.ProductOrderExportVO;
import com.diagbot.vo.ProductOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单业务
 *
 * @author Administrator
 */
@Component
public class ProductOrderFacade extends ProductOrderServiceImpl {
    @Autowired
    private VisibleIdCreater visibleIdCreater;
    @Autowired
    private OrderDetailsFacade orderDetailsFacade;
    @Autowired
    private ServiceInfoFacade serviceInfoFacade;
    @Autowired
    private LantoneProductFacade lantoneProductFacade;
    @Autowired
    private ProductServiceFacade productServiceFacade;
    @Autowired
    private UserServiceClient userServiceClient;
    @Autowired
    private ProductOrderFacade productOrderFacade;
    @Autowired
    private OpenedProductsFacade openedProductsFacade;


    /**
     * 获取产品的认证进度
     *
     * @return 包含产品当前的认证状态
     */
    public List<ProductAuthProgressDTO> productAuthProgress() {
        List<ProductAuthProgressDTO> retList = new ArrayList<ProductAuthProgressDTO>();
        Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
        List<ProductOrder> orderList = baseMapper.getUserProductOrder(userId);
        List<String> nums = new ArrayList<>();
        for (ProductOrder i : orderList) {
            nums.add(i.getNum());
        }
        
        if(nums.size()==0){
        	return retList;
        }
        
        List<AuthDetailDTO> detailList = baseMapper.getOrderDetailsByNums(nums);
        List<AuthDetailDTO> products = null;
        Map<String, List<AuthDetailDTO>> detailMap = detailList.stream().collect(Collectors.groupingBy(AuthDetailDTO::getNum));
        for (String num : detailMap.keySet()) {
            ProductAuthProgressDTO productAuthProgressDTO = new ProductAuthProgressDTO();
            productAuthProgressDTO.setNum(num);
            products = detailMap.get(num);
            for(AuthDetailDTO i : products){
            	i.setAuditStatusMsg(AuditStatusEnum.getName(i.getAuditStatus()));
            }
            productAuthProgressDTO.setProducts(products);
            retList.add(productAuthProgressDTO);
        }

        return retList;
    }


    /**
     * 根据用户选择的产品生成订单
     *
     * @param addOrderVO 需要提供参数（产品id）
     * @return Boolean true
     */
    @Transactional
    public Boolean addOrders(AddOrderVO addOrderVO) {
        Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
        List<Long> opendId = openedProductsFacade.getOpendCountByUserId(userId);
        List<Long> orderDetailsId = orderDetailsFacade.getOrderDetailsByUser(userId);
        for (int i =0; i<addOrderVO.getProductId().size();i++){
            for (int j =0; j<opendId.size();j++){
                if(addOrderVO.getProductId().get(i)==opendId.get(j)){
                    throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "请勿重复提交已开通产品");
                }
            }
            for (int k =0; k<orderDetailsId.size();k++){
                if(addOrderVO.getProductId().get(i)==orderDetailsId.get(k)){
                    throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "产品正在审核中请勿重复提交");
                }
            }
        }
        ProductOrder productOrder = new ProductOrder();
        productOrder.setCreator(UserUtils.getCurrentPrincipleID());
        productOrder.setGmtCreate(DateUtil.now());
        String orderNum = visibleIdCreater.getNextId(VisibleIdTypeEnum.IS_ORDER.getKey()).toString();
        productOrder.setNum(orderNum);
        productOrder.setUserId(userId);
        productOrder.setTime(DateUtil.now());
        if (!save(productOrder)) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "添加订单失败");
        }
        OrderDetails orderDetails = null;
        List<OrderDetails> list =new ArrayList();
        List<Long> lantoneProductId =new ArrayList<>();
        for (Long o : addOrderVO.getProductId()) {
            lantoneProductId.add(o);
            orderDetails = new OrderDetails();
            orderDetails.setUserId(userId);
            orderDetails.setOrderNum(orderNum);
            orderDetails.setGmtCreate(DateUtil.now());
            orderDetails.setCreator(UserUtils.getCurrentPrincipleID());
            orderDetails.setProductId(o);
            list.add(orderDetails);
        }
        if (0 == lantoneProductFacade.listByIds(lantoneProductId).size()) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "获取产品信息失败");
        }
        if(orderDetailsFacade.saveOrderDetails(list)){
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "添加订单失败");
        }
        return true;
    }


    /**
     * 根据订单号查询订单信息
     *
     * @param productOrder 参数
     * @return 订单信息
     */
    public ProductOrder selectByOrderNum(ProductOrder productOrder) {
        return selctOrderTimeByOrderNum(productOrder);
    }


    /**
     * 计算待审核的订单数量
     *
     * @return 待审核的订单数量
     */
    public WaitExamOrderCouDTO waitExamOrderCou() {
        WaitExamOrderCouDTO waitExamOrderCouDTO = new WaitExamOrderCouDTO();
        waitExamOrderCouDTO.setCount(baseMapper.waitExamOrderCou());
        return waitExamOrderCouDTO;
    }


	/**
	 * 产品申请查询所有订单信息
     *
	 * @param productOrderVO （订单编号，机构名称，申请状态）
	 * @return 所有订单信息
	 */
	public RespDTO<IPage<ProductOrderWrapper>> selectAllProductOrder(ProductOrderVO productOrderVO){
	    Page page =new Page();
	    BeanUtil.copyProperties(productOrderVO,page);
		ProductOrderIndex productOrderIndex =new ProductOrderIndex();
		BeanUtil.copyProperties(productOrderVO, productOrderIndex);
		//TODO
        if(productOrderVO.getOrgName()!=null&&productOrderVO.getOrgName()!=""){
            RespDTO<List<Long>> rlist = userServiceClient.getUserByOrgName(productOrderVO.getOrgName());
            productOrderIndex.setUserId(rlist.data);
        }
		List<ProductOrderWrapper> list =productOrderFacade.selectAllProductOrder(page,productOrderIndex).getRecords();
		List<Long> list1 =new ArrayList<>();
		List<String> orderNoList =new ArrayList<>();
		for (ProductOrderWrapper productOrderWrapper:list) {
			Long longs = productOrderWrapper.getUserId();
			String orderNo =productOrderWrapper.getNum();
			list1.add(longs);
			orderNoList.add(orderNo);
		}
        OrderDetialsVO orderDetialsVO =new OrderDetialsVO();
		orderDetialsVO.setOrderNum(orderNoList);
		List<OrderDetailsWapper> orderDetilList = orderDetailsFacade.getAllOrderDetials(orderDetialsVO);
		RespDTO<Map<Long, UserOrgDTO>> mapRespDTO = userServiceClient.getUserAndOrg(list1);
		if(mapRespDTO == null || !"0".equals(mapRespDTO.code) ) {
			throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
					"获取用户和机构信息失败");
		}
		Map<Long, UserOrgDTO> dataMap = mapRespDTO.data;
		for(ProductOrderWrapper bean : list) {
			UserOrgDTO uo = dataMap.get(bean.getUserId());
			if(uo != null) {
				bean.setUsername(uo.getUsername());
				bean.setLinkman(uo.getLinkman());
				bean.setEmail(uo.getEmail());
				bean.setOrgId(uo.getOrgId());
				bean.setOrgName(uo.getOrgName());
			}
			for(int i = 0; i<orderDetilList.size();i++){
			    if(orderDetilList.get(i).getOrderNum().equals(bean.getNum())){
                    bean.setOrderDetial(orderDetilList);
                }
            }
		}
		return RespDTO.onSuc(page);
	}


    /**
     * 修改订单审核状态
     *
     * @param orderNum 参数
     * @return Boolean true
     */
	public Boolean updateProductOrderStatus(String orderNum){
        OrderDetailStatus  orderDetailStatus = orderDetailsFacade.selectAllOrderDetialsByOrderNum(orderNum);
        ProductOrder productOrder=new ProductOrder();
        productOrder.setNum(orderNum);
        productOrder= this.selctOrderTimeByOrderNum(productOrder);
        int satus =0;
        if(orderDetailStatus.getCt() == orderDetailStatus.getNotAudit()){
            satus = AuditStatusEnum.NotAudit.getKey();
            // ct == sh 待审核
        }else if(orderDetailStatus.getCt() > orderDetailStatus.getNotAudit() &&orderDetailStatus.getNotAudit() != 0){
            satus = AuditStatusEnum.Audit_Part.getKey();
            // ct > sh && sh != 0 部分审核
        }else if(orderDetailStatus.getCt() == orderDetailStatus.getPass()){
            satus = AuditStatusEnum.Adopt.getKey();
            // ct == pass 通过
        }else if(orderDetailStatus.getCt() == orderDetailStatus.getNotPass()){
            satus = AuditStatusEnum.Veto.getKey();
            // ct == not_pass 不通过
        }else {
            satus = AuditStatusEnum.Adopt_Part.getKey();
            // 其他 部分通过
        }
        productOrder.setAuditStatus(satus);
        productOrder.setGmtModified(DateUtil.now());
        productOrder.setModifier(UserUtils.getCurrentPrincipleID());
        if(!this.updateById(productOrder)){
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                    "用户订单审核状态修改失败");
        }
        return true;
    }

    /**
     *  导出产品订单相关信息
     * @param productOrderExportVO
     * @param response
     */
    public void export(ProductOrderExportVO productOrderExportVO,HttpServletResponse response){
	    ProductOrderVO productOrderVO =new ProductOrderVO();
	    BeanUtil.copyProperties(productOrderExportVO,productOrderVO);
	    if(DateUtil.getDaysBetweenTwoDate(DateUtil.parseDate(productOrderVO.getStartTime()), DateUtil.parseDate(productOrderVO.getEndTime()))>30 || DateUtil.getDaysBetweenTwoDate(DateUtil.parseDate(productOrderVO.getStartTime()), DateUtil.parseDate(productOrderVO.getEndTime())) < 0){
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                    "日期超出限制，请重新输入");
        }
	    productOrderVO.setCurrent(1L);
        productOrderVO.setSize(999999999999999999L);
        Map<String, Object> userMap = new HashMap<String, Object>();
        userMap.put("startTime", productOrderExportVO.getStartTime());
        userMap.put("endTime", productOrderExportVO.getEndTime());
        RespDTO<IPage<ProductOrderWrapper>> datas = this.selectAllProductOrder(productOrderVO);
        GsonUtil.toJson(datas.data.getRecords());
        List<String> listName = new ArrayList<>();

        listName.add("订单id");
        listName.add("申请时间");
        listName.add("订单号");
        listName.add("申请机构");
        listName.add("申请人");
        listName.add("手机号");
        listName.add("邮箱");
        listName.add("审核状态");

        List<String> listId = new ArrayList<>();
        listId.add("id");//订单ID
        listId.add("time");//申请时间
        listId.add("num");// 订单号
        listId.add("orgName");//申请机构
        listId.add("linKman");// 申请人
        listId.add("userName");//手机号
        listId.add("email");//邮箱
        listId.add("auditStatus");//审核状态


        List<ProductOrderExportDTO> list = new ArrayList<>();
        for (int i = 0; i < datas.data.getRecords().size(); i++) {
            ProductOrderWrapper userInfo = datas.data.getRecords().get(i);
            list.add(new ProductOrderExportDTO(String.valueOf(userInfo.getId()),
                    DateUtil.formatDate(userInfo.getTime()),
                    userInfo.getNum(),
                    userInfo.getOrgName(),
                    userInfo.getLinkman(),
                    userInfo.getUsername(),
                    userInfo.getEmail(),
                    String.valueOf(userInfo.getAuditStatus())));
        }
        response.setContentType("text/html;charset=UTF-8");
        /*String filePath = productOrderExportVO.getFilePath();//文件路径
        String fileName = productOrderExportVO.getFileName();//文件名
        ExportBeanExcelUtil ex = new ExportBeanExcelUtil();
        RespDTO resul = ex.exportExcel("测试POI导出EXCEL文档", listName, listId, list, filePath, fileName);*/
        ExportBeanExcelUtil ex = new ExportBeanExcelUtil();
        ex.exportExcelNew("产品申请相关信息", listName, listId, list,response);
    }
}


