package com.gree.mall.manager.logic.policy;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.policy.PolicyOrderEmVO;
import com.gree.mall.manager.bean.policy.PolicyOrderInVO;
import com.gree.mall.manager.bean.policy.PolicyOrderVO;
import com.gree.mall.manager.bean.policy.PolicyVo;
import com.gree.mall.manager.commonmapper.PolicyCMapper;
import com.gree.mall.manager.enums.IsYesNoEnum;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.AdminWebsitService;
import com.gree.mall.manager.plus.service.PolicyOrderService;
import com.gree.mall.manager.plus.service.WebsitUserService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class PolicyOrderLogic {


    @Autowired
    PolicyCMapper policyCMapper;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    PolicyOrderService policyOrderService;

    @Autowired
    AdminWebsitService adminWebsitService;

    @Autowired
    WebsitUserService websitUserService;

    public IPage<PolicyOrderVO> listPolicyOrder(Page page, ZfireParamBean zfireParam) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, PolicyOrderVO.class, adminUser);

        IPage<PolicyOrderVO> policyOrderVOIPage = policyCMapper.listPolicyOrder(page, zfireParam, adminCompanyIds);
        return policyOrderVOIPage;
    }

    public IPage<PolicyOrderEmVO> listEm(Page page, ZfireParamBean zfireParam) {

        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, PolicyOrderEmVO.class, adminUser);

        IPage<PolicyOrderEmVO> policyOrderVOIPage = policyCMapper.listEm(page, zfireParam, adminCompanyIds);

        return policyOrderVOIPage;
    }


    public IPage<PolicyOrderInVO> listIn(Page page, ZfireParamBean zfireParam) {

        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, PolicyOrderInVO.class, adminUser);

        IPage<PolicyOrderInVO> policyOrderVOIPage = policyCMapper.listIn(page, zfireParam, adminCompanyIds);

        return policyOrderVOIPage;
    }

    public PolicyOrder detail(String id) {
        PolicyOrder byId = policyOrderService.getById(id);
        return byId;
    }

    public void update(PolicyOrder policyOrder) {
        policyOrder.updateById();
    }

    public void importData(List<Object> objects) {

        for (Object o : objects) {
            List<Object> row = (List<Object>) o;


            PolicyOrder policyOrder = policyOrderService.lambdaQuery()
                    .eq(PolicyOrder::getWebsitId, (String) row.get(1))
                    .eq(PolicyOrder::getWorkerNumber, (String) row.get(3))
                    .eq(PolicyOrder::getType, "IN")
                    .eq(PolicyOrder::getPolicyOrderStatus, "DGM").one();

            if (policyOrder == null)
                throw new RemoteAccessException((String) row.get(1)+"网点下工程师未找到"+(String) row.get(3));


            if (policyOrder != null) {
                WorkerPolicy workerPolicy = new WorkerPolicy();
                workerPolicy.setWorkerId(policyOrder.getWorkerNumber());
                workerPolicy.setType("IN");
                workerPolicy.setOrderId(policyOrder.getId());
                workerPolicy.setPolicyName(policyOrder.getPolicyName());
                workerPolicy.setCompany(policyOrder.getBelongCompany());
                workerPolicy.setPolicyOrder(policyOrder.getPolicyNumber());
                workerPolicy.setPolicyType("IN");
                workerPolicy.setStartTime(DateUtil.parse((String) row.get(9), "yyyy-MM-dd"));
                workerPolicy.setEndTime(DateUtil.endOfDay(DateUtil.parse((String) row.get(10), "yyyy-MM-dd")));
                workerPolicy.setWebsitUserId(policyOrder.getId());
                workerPolicy.setWebsitId(policyOrder.getWebsitId());
                workerPolicy.setWebsitName(policyOrder.getWebsitName());
                workerPolicy.setWorkerName(policyOrder.getWorkerName());

                if (new Date().after(workerPolicy.getStartTime()) && new Date().before(workerPolicy.getEndTime()))
                    workerPolicy.setStatus("BZZ");
                else if (new Date().after(policyOrder.getEndTime()))
                    workerPolicy.setStatus("YSX");
                else {
                    workerPolicy.setStatus("DSX");
                }
                workerPolicy.insert();
            }

            policyOrder.setStartTime(DateUtil.parse((String) row.get(9), "yyyy-MM-dd"));
            policyOrder.setEndTime(DateUtil.endOfDay(DateUtil.parse((String) row.get(10), "yyyy-MM-dd")));
            policyOrder.setClassWebsitId((String) row.get(11));
            policyOrder.setClassWebsitName((String) row.get(12));
            policyOrder.setPayAmount(Convert.toBigDecimal(row.get(8)));


            if (new Date().after(policyOrder.getStartTime()) && new Date().before(policyOrder.getEndTime()))
                policyOrder.setPolicyOrderStatus("BZZ");
            else if (new Date().after(policyOrder.getEndTime()))
                policyOrder.setPolicyOrderStatus("YSX");
            else {
                policyOrder.setPolicyOrderStatus("DSX");
            }

            policyOrder.updateById();


        }
    }

    public void importEm(List<Object> objects) {

        for (Object o : objects) {
            List<Object> row = (List<Object>) o;

            AdminWebsit adminWebsit = adminWebsitService.lambdaQuery().eq(AdminWebsit::getWebsitNumber, (String) row.get(1)).one();

            WebsitUser websitUser = websitUserService.lambdaQuery().eq(WebsitUser::getWebsitId, (String) row.get(1))
                    .eq(WebsitUser::getWorkerNumber, (String) row.get(3)).one();

            PolicyOrder policyOrder = new PolicyOrder();
            policyOrder.setId("PW"+ IdWorker.getIdStr());

            policyOrder.setPolicyName("雇主险");
            policyOrder.setPolicyId(policyOrder.getId());
            policyOrder.setPolicyNumber(policyOrder.getId());

            policyOrder.setWebsitUserId(websitUser.getId());

            policyOrder.setStartTime(DateUtil.parse((String) row.get(6), "yyyy-MM-dd"));
            policyOrder.setEndTime(DateUtil.parse((String) row.get(7), "yyyy-MM-dd"));

            policyOrder.setIsSend(IsYesNoEnum.NO.getKey());
            policyOrder.setIsReceipt(IsYesNoEnum.NO.getKey());


            policyOrder.setBelongCompany(adminWebsit.getBelongCompany());
            policyOrder.setBelongCompanyCode(adminWebsit.getBelongCompanyCode());

            policyOrder.setCompanyWechatId(adminWebsit.getCompanyWechatId());
            policyOrder.setCompanyWechatName(adminWebsit.getCompanyWechatName());

            policyOrder.setWebsitId(adminWebsit.getWebsitId());
            policyOrder.setWebsitName(adminWebsit.getName());

            policyOrder.setWorkerIdcard((String) row.get(4));
            policyOrder.setWorkerMobile((String) row.get(5));
            policyOrder.setWorkerName((String) row.get(2));
            policyOrder.setWorkerNumber((String) row.get(3));

            policyOrder.setType("EM");



            if (new Date().after(policyOrder.getStartTime()) && new Date().before(policyOrder.getEndTime()))
                policyOrder.setPolicyOrderStatus("BZZ");
            else if (new Date().after(policyOrder.getEndTime()))
                policyOrder.setPolicyOrderStatus("YSX");
            else {
                policyOrder.setPolicyOrderStatus("DSX");
            }

            policyOrder.insert();


            WorkerPolicy workerPolicy = new WorkerPolicy();
            workerPolicy.setWorkerId(websitUser.getWorkerNumber());
            workerPolicy.setType("EM");
            workerPolicy.setOrderId(policyOrder.getId());
            workerPolicy.setPolicyName(policyOrder.getPolicyName());
            workerPolicy.setCompany(policyOrder.getBelongCompany());
            workerPolicy.setPolicyOrder(policyOrder.getPolicyNumber());
            workerPolicy.setPolicyType("LINE");
            workerPolicy.setStartTime(DateUtil.parse((String) row.get(9), "yyyy-MM-dd"));
            workerPolicy.setEndTime(DateUtil.endOfDay(DateUtil.parse((String) row.get(10), "yyyy-MM-dd")));
            workerPolicy.setWebsitUserId(policyOrder.getId());
            workerPolicy.setWebsitId(policyOrder.getWebsitId());
            workerPolicy.setWebsitName(policyOrder.getWebsitName());
            workerPolicy.setWorkerName(policyOrder.getWorkerName());

            if (new Date().after(workerPolicy.getStartTime()) && new Date().before(workerPolicy.getEndTime()))
                workerPolicy.setStatus("BZZ");
            else if (new Date().after(policyOrder.getEndTime()))
                workerPolicy.setStatus("YSX");
            else {
                workerPolicy.setStatus("DSX");
            }
            workerPolicy.insert();





        }
    }
}
