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.ProductLineDTO;
import com.diagbot.dto.RenewalsInfosDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.UserOrgDTO;
import com.diagbot.entity.UserRenewals;
import com.diagbot.entity.wrapper.UserRenewalsWrapper;
import com.diagbot.enums.CancelRenewalsEnum;
import com.diagbot.enums.RenewalsEnum;
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.UserRenewalsServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.AddRenewalsInfoVO;
import com.diagbot.vo.DeleteRenewalsInfoVO;
import com.diagbot.vo.RenewalsInfosVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: wangyu
 * @time: 2018/10/12 15:31
 */
@Component
public class UserRenewalsFacade extends UserRenewalsServiceImpl {

    @Autowired
    private VisibleIdCreater visibleIdCreater;
    @Autowired
    private UserServiceClient userServiceClient;
    @Autowired
    private LantoneProductFacade lantoneProductFacade;
    @Autowired
    private OpenedProductsFacade openedProductsFacade;

    public RespDTO<Boolean> addRenewalsInfos(AddRenewalsInfoVO addRenewalsInfoVO){
        UserRenewals userRenewals =new UserRenewals();
        Map map =new HashMap();
        String user = UserUtils.getCurrentPrincipleID();
        map.put("productId",addRenewalsInfoVO.getProductId());
        map.put("userId",user);
        Long userId = Long.parseLong(user);
        userRenewals.setCreator(user);
        userRenewals.setUserId(userId);
        userRenewals.setApplyTime(DateUtil.now());
        userRenewals.setGmtCreate(DateUtil.now());
        userRenewals.setRenewalsNo(visibleIdCreater.getNextId(VisibleIdTypeEnum.IS_RENEWALS.getKey()).toString());
        if(openedProductsFacade.selectOpenedProducts(map).size() == 0){
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "该产品未开通，不能续费");
        }
        if(this.selectProductIdByUserId(map).contains(addRenewalsInfoVO.getProductId())){
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "请勿重复提交申请");
        }
        userRenewals.setProductId(addRenewalsInfoVO.getProductId());
        return RespDTO.onSuc(this.save(userRenewals));
    }


    public IPage renewalsInfo(RenewalsInfosVO renewalsInfosVO){
        Page page =new Page();
        BeanUtil.copyProperties(renewalsInfosVO,page);
        UserRenewalsWrapper userRenewalsWrapper =new UserRenewalsWrapper();
        BeanUtil.copyProperties(renewalsInfosVO,userRenewalsWrapper);
        userRenewalsWrapper.setCancelRenewals(CancelRenewalsEnum.NOT_CANCEL.getKey());
        if(renewalsInfosVO.getOrgName()!=null && renewalsInfosVO.getOrgName()!=""){
            RespDTO<List<Long>> rlist = userServiceClient.getUserByOrgName(renewalsInfosVO.getOrgName());
            userRenewalsWrapper.setUserId(rlist.data);
        }
        List<RenewalsInfosDTO> list = this.selectUserRenewals(page,userRenewalsWrapper).getRecords();
        List<ProductLineDTO> productList =lantoneProductFacade.productLines();
        List<Long> userIds =new ArrayList<>();
        for (RenewalsInfosDTO renewalsInfosDTO:list) {
            Long userId = renewalsInfosDTO.getUserId();
            userIds.add(userId);
        }
        RespDTO<Map<Long, UserOrgDTO>> mapRespDTO = userServiceClient.getUserAndOrg(userIds);
        if(mapRespDTO == null || !"0".equals(mapRespDTO.code) ) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                    "获取用户和机构信息失败");
        }
        Map<Long, UserOrgDTO> dataMap = mapRespDTO.data;
        for(RenewalsInfosDTO bean : list) {
            UserOrgDTO uo = dataMap.get(bean.getUserId());
            bean.setRenewalsStatusName(RenewalsEnum.getName(bean.getRenewalsStatus()));
            for (ProductLineDTO productLineDTO:productList) {
                if (productLineDTO.getId()==bean.getProductId()){
                    bean.setProductName(productLineDTO.getName());
                }
            }
            if(uo != null) {
                bean.setUsername(uo.getUsername());
                bean.setLinkman(uo.getLinkman());
                bean.setOrgName(uo.getOrgName());
            }
        }
        return page;
    }

    public Boolean deleteRenewalsInfo(DeleteRenewalsInfoVO deleteRenewalsInfoVO){
        UserRenewals userRenewals =this.getById(deleteRenewalsInfoVO.getId());
        userRenewals.setIsDeleted("Y");
        userRenewals.setModifier(UserUtils.getCurrentPrincipleID());
        userRenewals.setGmtModified(DateUtil.now());
        if(!this.updateById(userRenewals)){
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                    "删除失败");
        }
        return true;
    }

    public Boolean cancelRenewalsInfo(DeleteRenewalsInfoVO deleteRenewalsInfoVO){
        UserRenewals userRenewals =this.getById(deleteRenewalsInfoVO.getId());
        userRenewals.setCancelRenewals(CancelRenewalsEnum.CANCEL_RENEWALS_ENUM.getKey());
        userRenewals.setModifier(UserUtils.getCurrentPrincipleID());
        userRenewals.setGmtModified(DateUtil.now());
        if(!this.updateById(userRenewals)){
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                    "取消失败");
        }
        return true;
    }
}
