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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.policy.PolicyDetail;
import com.gree.mall.manager.bean.policy.PolicyVo;
import com.gree.mall.manager.commonmapper.PolicyCMapper;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.AdminWebsit;
import com.gree.mall.manager.plus.entity.Policy;
import com.gree.mall.manager.plus.entity.PolicyRange;
import com.gree.mall.manager.plus.entity.PolicyWebsit;
import com.gree.mall.manager.plus.service.AdminWebsitService;
import com.gree.mall.manager.plus.service.PolicyRangeService;
import com.gree.mall.manager.plus.service.PolicyService;
import com.gree.mall.manager.plus.service.PolicyWebsitService;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PolicyLogic {

    @Autowired
    PolicyCMapper policyCMapper;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    PolicyService policyService;

    @Autowired
    PolicyRangeService policyRangeService;

    @Autowired
    PolicyWebsitService policyWebsitService;

    @Autowired
    AdminWebsitService adminWebsitService;



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

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

        IPage<PolicyVo> policyVoIPage = policyCMapper.list(page, zfireParam,adminCompanyIds);
        return policyVoIPage;
    }

    public PolicyDetail detail(String id) {

        Policy policy = policyService.getById(id);

        PolicyDetail policyDetail = BeanUtil.toBean(policy, PolicyDetail.class);

        List<PolicyRange> policyRanges = policyRangeService.lambdaQuery().eq(PolicyRange::getPolicyId, policyDetail.getId()).list();
        List<PolicyWebsit> policyWebsits = policyWebsitService.lambdaQuery().eq(PolicyWebsit::getPolicyId, policyDetail.getId()).list();


        policyDetail.setPolicyRanges(policyRanges);
        policyDetail.setPolicyWebsits(policyWebsits);

        return policyDetail;

    }

    @Transactional(rollbackFor = Exception.class)
    public void add(PolicyDetail policyDetail) {

        AdminUserCom adminUser = commonLogic.getAdminUser();

        if (policyService.lambdaQuery().eq(Policy::getPolicyName,policyDetail.getPolicyName()).count() > 0)
            throw new RemoteServiceException("保险名称不能重复");

        policyDetail.setCompanyWechatId(adminUser.getCompanyWechatId());
        policyDetail.setCompanyWechatName(adminUser.getCompanyName());
        policyDetail.setEndTime(DateUtil.offsetMillisecond(DateUtil.endOfDay(policyDetail.getEndTime()),-999));
        policyDetail.setStartTime(DateUtil.beginOfDay(policyDetail.getStartTime()));

        policyDetail.insert();

        for (PolicyRange policyRange : policyDetail.getPolicyRanges()) {
            policyRange.setPolicyId(policyDetail.getId());
        }

        for (PolicyWebsit policyWebsit : policyDetail.getPolicyWebsits()) {

            AdminWebsit adminWebsit = adminWebsitService.getById(policyWebsit.getWebsitId());

            policyWebsit.setPolicyId(policyDetail.getId());
            policyWebsit.setWebsitName(adminWebsit.getName());
            policyWebsit.setBelongCompany(adminWebsit.getBelongCompany());
            policyWebsit.setBelongCompanyCode(adminWebsit.getBelongCompanyCode());
        }


        policyRangeService.saveBatch(policyDetail.getPolicyRanges());
        policyWebsitService.saveBatch(policyDetail.getPolicyWebsits());
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(PolicyDetail policyDetail) {

        if (policyService.lambdaQuery().eq(Policy::getPolicyName,policyDetail.getPolicyName())
                .ne(Policy::getId,policyDetail.getId()).count() > 0)
            throw new RemoteServiceException("保险名称不能重复");

        policyDetail.updateById();

        policyRangeService.lambdaUpdate().eq(PolicyRange::getPolicyId,policyDetail.getId()).remove();
        policyWebsitService.lambdaUpdate().eq(PolicyWebsit::getPolicyId,policyDetail.getId()).remove();

        for (PolicyRange policyRange : policyDetail.getPolicyRanges()) {
            policyRange.setPolicyId(policyDetail.getId());
        }

        for (PolicyWebsit policyWebsit : policyDetail.getPolicyWebsits()) {
            AdminWebsit adminWebsit = adminWebsitService.getById(policyWebsit.getWebsitId());

            policyWebsit.setPolicyId(policyDetail.getId());
            policyWebsit.setWebsitName(adminWebsit.getName());
            policyWebsit.setBelongCompany(adminWebsit.getBelongCompany());
            policyWebsit.setBelongCompanyCode(adminWebsit.getBelongCompanyCode());
        }


        policyRangeService.saveBatch(policyDetail.getPolicyRanges());
        policyWebsitService.saveBatch(policyDetail.getPolicyWebsits());
    }

    public void del(String id) {

        policyService.removeById(id);

        policyRangeService.lambdaUpdate().eq(PolicyRange::getPolicyId,id).remove();
        policyWebsitService.lambdaUpdate().eq(PolicyWebsit::getPolicyId,id).remove();
    }
}
