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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.sql.Order;
import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.policy.WorkerManagerBean;
import com.gree.mall.manager.bean.policy.WorkerManagerDetail;
import com.gree.mall.manager.enums.*;
import com.gree.mall.manager.enums.workorder.OrderBaseStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorkerManagerLogic {

    @Autowired
    WebsitUserService websitUserService;

    @Autowired
    PgOrderBaseService pgOrderBaseService;

    @Autowired
    WorkerImgService workerImgService;

    @Autowired
    WorkerPolicyService workerPolicyService;

    @Autowired
    UserService userService;

    @Autowired
    AdminWebsitService adminWebsitService;

    @Autowired
    CommonLogic commonLogic;


    @Autowired
    PolicyOrderService policyOrderService;


    public void stop(String id) {

        WebsitUser websitUserBean = websitUserService.getById(id);
        if (!websitUserBean.getExamineStatus().equals(ExamineWorkerStatusEnum.OK.getKey()))
            throw new RemoteServiceException("师傅不处于审核通过");

        WebsitUser websitUser = new WebsitUser();
        websitUser.setId(id);
        websitUser.setExamineStatus(ExamineWorkerStatusEnum.STOP.getKey());
        websitUser.updateById();
    }

    public void activation(String id) {

        WebsitUser websitUserBean = websitUserService.getById(id);
        if (!websitUserBean.getExamineStatus().equals(ExamineWorkerStatusEnum.STOP.getKey()))
            throw new RemoteServiceException("师傅不处于暂停状态");

        WebsitUser websitUser = new WebsitUser();
        websitUser.setId(id);
        websitUser.setExamineStatus(ExamineWorkerStatusEnum.POLICY_WAIT.getKey());
        websitUser.updateById();
    }

    @Transactional(rollbackFor = Exception.class)
    public void quit(String id) {

        WebsitUser websitUserBean = websitUserService.getById(id);
        if (!websitUserBean.getExamineStatus().equals(ExamineWorkerStatusEnum.OK.getKey()))
            throw new RemoteServiceException("师傅不处于审核通过");


        if (pgOrderBaseService.lambdaQuery()
                .and(item -> item.eq(PgOrderBase::getWorkerId,websitUserBean.getUserId()).or().like(PgOrderBase::getWorkerIdList,websitUserBean.getUserId()))
                .eq(PgOrderBase::getWebsitId,websitUserBean.getWebsitId())
                .notIn(PgOrderBase::getOrderStatus, OrderBaseStatusEnum.YQX.getKey(),OrderBaseStatusEnum.YJS.getKey(),
                        OrderBaseStatusEnum.YWG.getKey(),OrderBaseStatusEnum.FWQX.getKey(),OrderBaseStatusEnum.YWGO.getKey(),
                        OrderBaseStatusEnum.YGBGD.getKey(),OrderBaseStatusEnum.FGFZF.getKey(),OrderBaseStatusEnum.YZFGD.getKey(),
                        OrderBaseStatusEnum.FZXBWG.getKey(),OrderBaseStatusEnum.FBBWG.getKey(),OrderBaseStatusEnum.LSXX.getKey(),
                        OrderBaseStatusEnum.PLGB.getKey(),OrderBaseStatusEnum.ZCGB.getKey()
                        ).count() > 1) {

            throw new RemoteServiceException("师傅存在未完工工单，请改派后再离职");
        }


        WebsitUser websitUser = new WebsitUser();
        websitUser.setId(id);
        websitUser.setExamineStatus(ExamineWorkerStatusEnum.QUIT.getKey());
        websitUser.updateById();
    }

    public void againQuit(String id) {

        WebsitUser websitUserBean = websitUserService.getById(id);
        if (!websitUserBean.getExamineStatus().equals(ExamineWorkerStatusEnum.QUIT.getKey()))
            throw new RemoteServiceException("师傅不处于离职状态");

        WebsitUser websitUser = new WebsitUser();
        websitUser.setId(id);
        websitUser.setExamineStatus(ExamineWorkerStatusEnum.POLICY_WAIT.getKey());
        websitUser.updateById();
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(WorkerManagerBean workerManagerBean) {
        workerManagerBean.setCreateTime(null);

        User user = userService.getById(workerManagerBean.getUserId());

        if (!StringUtil.isEmpty(user.getWorkerNumber())){
            if (!workerManagerBean.getWorkerNumber().equals(user.getWorkerNumber())) {
                throw new RemoteServiceException("师傅编号已经录入不允许更改");
            }
        }
        workerManagerBean.updateById();


        workerManagerBean.getUser().updateById();

        workerImgService.lambdaUpdate().eq(WorkerImg::getWorkerId,workerManagerBean.getUserId())
                .remove();


        for (WorkerImg workerImg : workerManagerBean.getWorkerImgs()) {
            workerImg.setWorkerId(workerManagerBean.getUserId());
        }


        workerImgService.saveOrUpdateBatch(workerManagerBean.getWorkerImgs());

        workerPolicyService.lambdaUpdate().eq(WorkerPolicy::getWebsitUserId,workerManagerBean.getId()).remove();


        for (WorkerPolicy workerPolicy : workerManagerBean.getWorkerPolicies()) {
            workerPolicy.setWorkerId(workerManagerBean.getUserId());
            workerPolicy.setWebsitUserId(workerManagerBean.getId());
        }

        workerPolicyService.saveOrUpdateBatch(workerManagerBean.getWorkerPolicies());


    }

    @Transactional(rollbackFor = Exception.class)
    public WorkerManagerDetail detail(String id) {

        WebsitUser websitUser = websitUserService.getById(id);

        WorkerManagerDetail workerManagerDetail = BeanUtil.toBean(websitUser, WorkerManagerDetail.class);

        workerManagerDetail.setUser(userService.getById(workerManagerDetail.getUserId()));

        List<WorkerImg> list = workerImgService.lambdaQuery().eq(WorkerImg::getWorkerId, websitUser.getUserId()).list();

        List<WorkerPolicy> workerPolicies = workerPolicyService.lambdaQuery().eq(WorkerPolicy::getWebsitUserId, websitUser.getId()).list();

        workerManagerDetail.setWorkerImgs(list);
        workerManagerDetail.setWorkerPolicies(workerPolicies);

        return workerManagerDetail;

    }

    @Transactional(rollbackFor = Exception.class)
    public void examineWait(WorkerManagerBean workerManagerBean) {

        AdminUserCom adminUser = commonLogic.getAdminUser();

        WebsitUser websitUser = websitUserService.getById(workerManagerBean.getId());

        if (!websitUser.getExamineStatus().equals(ExamineWorkerStatusEnum.WAIT.getKey()))
            throw new RemoteServiceException("状态不为待审核状态");

        workerManagerBean.setCreateTime(null);

        workerManagerBean.setExamineBy(adminUser.getNickName());
        workerManagerBean.setExamineTime(new Date());

        workerManagerBean.setWorkerNumber(workerManagerBean.getUser().getWorkerNumber());
        User user = userService.getById(workerManagerBean.getUserId());

        if (!StringUtil.isEmpty(user.getWorkerNumber())){
            if (!workerManagerBean.getWorkerNumber().equals(user.getWorkerNumber())) {
                throw new RemoteServiceException("师傅编号已经录入不允许更改");
            }
        }
        if (StringUtil.isEmpty(workerManagerBean.getWorkerNumber()))
            throw new RemoteServiceException("师傅编号不能为空");

        workerManagerBean.updateById();

        workerManagerBean.getUser().updateById();

        workerImgService.lambdaUpdate().eq(WorkerImg::getWorkerId,workerManagerBean.getUserId())
                .remove();


        for (WorkerImg workerImg : workerManagerBean.getWorkerImgs()) {
            workerImg.setWorkerId(workerManagerBean.getUserId());
        }


        workerImgService.saveOrUpdateBatch(workerManagerBean.getWorkerImgs());

        workerPolicyService.lambdaUpdate().eq(WorkerPolicy::getWebsitUserId,workerManagerBean.getId()).remove();


        for (WorkerPolicy workerPolicy : workerManagerBean.getWorkerPolicies()) {
            workerPolicy.setWorkerId(workerManagerBean.getUserId());
            workerPolicy.setWebsitUserId(workerManagerBean.getId());
        }

        workerPolicyService.saveOrUpdateBatch(workerManagerBean.getWorkerPolicies());




        if (workerManagerBean.getExamineStatus().equals(ExamineWorkerStatusEnum.FAIL.getKey()))
            return;


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

        if (!CollectionUtils.isEmpty(workerManagerBean.getWorkerPolicies())) {

            for (WorkerPolicy workerPolicy : workerManagerBean.getWorkerPolicies()) {
                if (workerPolicy.getEndTime().after(new Date()) && workerPolicy.getStartTime().before(new Date())) {
                    workerPolicy.setStatus("BZZ");
                } else {
                    workerPolicy.setStatus("DSX");
                }
                if (workerPolicy.getEndTime().before(new Date())) {
                    workerPolicy.setStatus("YSX");
                }
            }
        }

        //非自建需要判断齐全保险
        if (!adminWebsit.getAttr().equals(WebsitAttrEnum.SELF.getKey())) {

            List<WorkerPolicy> workerPolicies = workerManagerBean.getWorkerPolicies().stream().filter(item -> item.getPolicyType().equals("AC")).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(workerPolicies))
                throw new RemoteServiceException("请填工程师意外险");


            if (workerPolicies.stream().filter(item -> item.getStatus().equals("BZZ")).count() < 1) {
                throw new RemoteServiceException("意外险必须有生效的保险");
            }

            Boolean wp = this.hasOverlap(workerPolicies);

            if (wp)
                throw new RemoteServiceException("意外险有相同覆盖范围内的保险");


            if (adminWebsit.getInsureType().equals("意外险,工伤险")) {
                List<WorkerPolicy> workerPoliciesIn = workerManagerBean.getWorkerPolicies().stream()
                        .filter(item -> item.getPolicyType().equals("IN")).collect(Collectors.toList());

                Boolean in = this.hasOverlap(workerPoliciesIn);

                if (in)
                    throw new RemoteServiceException("工伤险有相同覆盖范围内的保险");


                if (workerPoliciesIn.stream().filter(item -> item.getStatus().equals("BZZ")).count() > 0) {
                    throw new RemoteServiceException("工伤险必须有生效的保险");
                }

            } else {
                List<WorkerPolicy> workerPoliciesEm = workerManagerBean.getWorkerPolicies().stream()
                        .filter(item -> item.getPolicyType().equals("EM")).collect(Collectors.toList());

                Boolean em = this.hasOverlap(workerPoliciesEm);

                if (em)
                    throw new RemoteServiceException("雇主险有相同覆盖范围内的保险");


                if (workerPoliciesEm.stream().filter(item -> item.getStatus().equals("BZZ")).count() < 1) {
                    throw new RemoteServiceException("雇主险必须有生效的保险");
                }
            }
            workerManagerBean.setExamineStatus(ExamineWorkerStatusEnum.OK.getKey());
        }else {

            List<WorkerPolicy> workerPolicies = workerManagerBean.getWorkerPolicies().stream().filter(item -> item.getPolicyType().equals("AC")).collect(Collectors.toList());

            Boolean wp = this.hasOverlap(workerPolicies);

            if (wp)
                throw new RemoteServiceException("意外险有相同覆盖范围内的保险");

            if (workerPolicies.stream().filter(item -> item.getStatus().equals("BZZ")).count() < 1)
                workerManagerBean.setExamineStatus(ExamineWorkerStatusEnum.WAIT_WORKER.getKey());


            if (adminWebsit.getInsureType().equals("意外险,工伤险")) {
                List<WorkerPolicy> workerPoliciesIn = workerManagerBean.getWorkerPolicies().stream()
                        .filter(item -> item.getPolicyType().equals("IN")).collect(Collectors.toList());

                Boolean in = this.hasOverlap(workerPoliciesIn);

                if (in)
                    throw new RemoteServiceException("工伤险有相同覆盖范围内的保险");

                if (workerPoliciesIn.stream().filter(item -> item.getStatus().equals("BZZ") || item.getStatus().equals("DSX")).count() < 1) {
                    workerManagerBean.setExamineStatus(ExamineWorkerStatusEnum.WAIT_WORKER.getKey());
                    this.scGongShang(workerManagerBean);
                }

                if (workerManagerBean.getWorkerPolicies().stream().filter(item -> item.getStatus().equals("BZZ")).count() > 1) {
                    workerManagerBean.setExamineStatus(ExamineWorkerStatusEnum.OK.getKey());
                }



            } else {
                List<WorkerPolicy> workerPoliciesEm = workerManagerBean.getWorkerPolicies().stream()
                        .filter(item -> item.getPolicyType().equals("EM")).collect(Collectors.toList());

                Boolean em = this.hasOverlap(workerPoliciesEm);

                if (em)
                    throw new RemoteServiceException("雇主险有相同覆盖范围内的保险");

                if (workerPoliciesEm.stream().filter(item -> item.getStatus().equals("BZZ")).count() < 1) {
                    workerManagerBean.setExamineStatus(ExamineWorkerStatusEnum.WAIT_WORKER.getKey());
                    this.scGuZhu(workerManagerBean);
                }

                if (workerManagerBean.getWorkerPolicies().stream().filter(item -> item.getStatus().equals("BZZ")).count() > 1) {
                    workerManagerBean.setExamineStatus(ExamineWorkerStatusEnum.OK.getKey());
                }

            }


        }

        workerManagerBean.updateById();

        workerPolicyService.saveOrUpdateBatch(workerManagerBean.getWorkerPolicies());

    }

    private void scGuZhu(WorkerManagerBean workerManagerBean) {

        AdminUserCom adminUser = commonLogic.getAdminUser();

        //找有无可替换，有替换，无生成
        List<PolicyOrder> policyOrders = policyOrderService.lambdaQuery().eq(PolicyOrder::getWebsitId, workerManagerBean.getWebsitId())
                .eq(PolicyOrder::getPolicyOrderStatus, PolicyOrderEmStutasEnum.KTH.getKey()).list();

        if (!CollectionUtils.isEmpty(policyOrders)) {

            User user = userService.getById(workerManagerBean.getUserId());

            policyOrderService.lambdaUpdate()
                    .eq(PolicyOrder::getId,policyOrders.get(0).getId())
                    .set(PolicyOrder::getPolicyOrderStatus,PolicyOrderEmStutasEnum.YSX.getKey())
                    .set(PolicyOrder::getEndTime,new Date()).update();

            workerPolicyService.lambdaUpdate()
                    .eq(WorkerPolicy::getOrderId,policyOrders.get(0).getId())
                    .eq(WorkerPolicy::getWebsitUserId,workerManagerBean.getId())
                    .set(WorkerPolicy::getStatus, "YSX")
                    .set(WorkerPolicy::getEndTime,new Date()).update();

            PolicyOrder policyOrder = policyOrders.get(0);
            policyOrder.setId(IdWorker.getIdStr());
            policyOrder.setReplaceName(policyOrder.getWorkerName());
            policyOrder.setReplacePhone(policyOrder.getWorkerMobile());
            policyOrder.setReplaceNumber(policyOrder.getWorkerNumber());
            policyOrder.setReplaceIdcard(policyOrder.getWorkerIdcard());

            policyOrder.setWorkerIdcard(user.getIdCard());
            policyOrder.setWorkerMobile(user.getMobile());
            policyOrder.setWorkerName(workerManagerBean.getUser().getNickName());
            policyOrder.setWorkerNumber(workerManagerBean.getUser().getWorkerNumber());

            policyOrder.setPolicyOrderStatus(PolicyOrderEmStutasEnum.DTH.getKey());

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



        }else {

            AdminWebsit adminWebsit = adminWebsitService.getById(workerManagerBean.getWebsitId());
            PolicyOrder policyOrder = new PolicyOrder();
            policyOrder.setBelongCompany(adminWebsit.getBelongCompany());
            policyOrder.setBelongCompanyCode(adminWebsit.getBelongCompanyCode());
            policyOrder.setPolicyOrderStatus(PolicyOrderEmStutasEnum.XG.getKey());
            policyOrder.setPolicyName("雇主险");
            policyOrder.setCompanyWechatId(adminWebsit.getCompanyWechatId());
            policyOrder.setCompanyWechatName(adminWebsit.getCompanyWechatName());
            policyOrder.setPolicyId(IdWorker.getIdStr());
            policyOrder.setId(policyOrder.getPolicyId());
            policyOrder.setWebsitId(adminWebsit.getWebsitId());
            policyOrder.setWebsitName(adminWebsit.getName());
            policyOrder.setWorkerNumber(workerManagerBean.getWorkerNumber());
            policyOrder.setWorkerName(workerManagerBean.getUser().getNickName());
            policyOrder.setWorkerIdcard(workerManagerBean.getUser().getIdCard());
            policyOrder.setWorkerMobile(workerManagerBean.getUser().getMobile());
            policyOrder.setType("EM");
            policyOrder.insert();
        }
    }

    private void scGongShang(WorkerManagerBean workerManagerBean) {


        AdminWebsit adminWebsit = adminWebsitService.getById(workerManagerBean.getWebsitId());
        PolicyOrder policyOrder = new PolicyOrder();
        policyOrder.setBelongCompany(adminWebsit.getBelongCompany());
        policyOrder.setBelongCompanyCode(adminWebsit.getBelongCompanyCode());
        policyOrder.setPolicyOrderStatus(PolicyOrderInStutasEnum.DGM.getKey());
        policyOrder.setPolicyName("工伤险");
        policyOrder.setCompanyWechatId(adminWebsit.getCompanyWechatId());
        policyOrder.setCompanyWechatName(adminWebsit.getCompanyWechatName());
        policyOrder.setPolicyId(IdWorker.getIdStr());
        policyOrder.setId(policyOrder.getPolicyId());
        policyOrder.setWebsitId(adminWebsit.getWebsitId());
        policyOrder.setWebsitName(adminWebsit.getName());
        policyOrder.setWorkerNumber(workerManagerBean.getWorkerNumber());
        policyOrder.setWorkerName(workerManagerBean.getUser().getNickName());
        policyOrder.setWorkerIdcard(workerManagerBean.getUser().getIdCard());
        policyOrder.setWorkerMobile(workerManagerBean.getUser().getMobile());
        policyOrder.setBuyUnit(adminWebsit.getInsureSteadUnit());
        policyOrder.setBuyUnit(adminWebsit.getInsureSteadUnit());
        policyOrder.setType("IN");
        policyOrder.insert();

    }

    public Boolean hasOverlap(List<WorkerPolicy> workerPolicies){

        for (int i= 0; i<workerPolicies.size();i++){
            WorkerPolicy workerPolicy = workerPolicies.get(i);
            for (int j = i+1; j <workerPolicies.size() ; j++) {
                WorkerPolicy workerPolicy2 = workerPolicies.get(j);
                if (workerPolicy.getEndTime().after(workerPolicy2.getStartTime()) && workerPolicy2.getEndTime().after(workerPolicy.getStartTime())){
                    return true;
                }
            }
        }

        return false;

    }


    @Transactional(rollbackFor = Exception.class)
    public void examine(WorkerManagerBean workerManagerBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        workerManagerBean.setPolicyExamineBy(adminUser.getNickName());
        workerManagerBean.setPolicyExamineTime(new Date());

        WebsitUser websitUser = websitUserService.getById(workerManagerBean.getId());

        if (!websitUser.getExamineStatus().equals(ExamineWorkerStatusEnum.POLICY_WAIT.getKey()))
            throw new RemoteServiceException("状态不为保险待审核状态");

        workerManagerBean.setCreateTime(null);
        workerManagerBean.updateById();


        workerManagerBean.getUser().updateById();

        workerImgService.lambdaUpdate().eq(WorkerImg::getWorkerId,workerManagerBean.getUserId())
                .remove();


        for (WorkerImg workerImg : workerManagerBean.getWorkerImgs()) {
            workerImg.setWorkerId(workerManagerBean.getUserId());
        }


        workerImgService.saveOrUpdateBatch(workerManagerBean.getWorkerImgs());

        workerPolicyService.lambdaUpdate().eq(WorkerPolicy::getWebsitUserId,workerManagerBean.getId()).remove();


        for (WorkerPolicy workerPolicy : workerManagerBean.getWorkerPolicies()) {
            workerPolicy.setWorkerId(workerManagerBean.getUserId());
            workerPolicy.setWebsitUserId(workerManagerBean.getId());
        }

        workerPolicyService.saveOrUpdateBatch(workerManagerBean.getWorkerPolicies());

        if (workerManagerBean.getExamineStatus().equals(ExamineWorkerStatusEnum.FAIL.getKey()))
            return;


        //检测保险情况 自建

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

        List<WorkerPolicy> workerPolicies = workerManagerBean.getWorkerPolicies().stream().filter(item -> item.getPolicyType()
                .equals("AC")).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(workerPolicies))
            throw new RemoteServiceException("请填工程师意外险");

        for (WorkerPolicy workerPolicy : workerPolicies) {
            if (workerPolicy.getEndTime().after(new Date()) && workerPolicy.getStartTime().before(new Date())) {
                workerPolicy.setStatus("BZZ");
            }else {
                workerPolicy.setStatus("DSX");
            }
            if (workerPolicy.getEndTime().before(new Date())) {
                workerPolicy.setStatus("YSX");
            }
        }
        if (workerPolicies.stream().filter(item -> item.getStatus().equals("BZZ")).count() < 1) {
            throw new RemoteServiceException("意外险必须有生效的保险");
        }


        Boolean wp = this.hasOverlap(workerPolicies);

        if (wp)
            throw new RemoteServiceException("意外险有相同覆盖范围内的保险");


        if (adminWebsit.getInsureType().equals("意外险,工伤险")) {
            List<WorkerPolicy> workerPoliciesIn = workerManagerBean.getWorkerPolicies().stream()
                    .filter(item -> item.getPolicyType().equals("IN")).collect(Collectors.toList());

            Boolean in = this.hasOverlap(workerPoliciesIn);

            if (in)
                throw new RemoteServiceException("工伤险有相同覆盖范围内的保险");

            for (WorkerPolicy workerPolicy : workerPoliciesIn) {
                if (workerPolicy.getEndTime().after(new Date()) && workerPolicy.getStartTime().before(new Date())) {
                    workerPolicy.setStatus("BZZ");
                }else {
                    workerPolicy.setStatus("DSX");
                }
                if (workerPolicy.getEndTime().before(new Date())) {
                    workerPolicy.setStatus("YSX");
                }
            }

            if (workerPolicies.stream().filter(item -> item.getStatus().equals("BZZ")).count() < 1) {
                throw new RemoteServiceException("工伤险必须有生效的保险");
            }

        } else {
            List<WorkerPolicy> workerPoliciesEm = workerManagerBean.getWorkerPolicies().stream()
                    .filter(item -> item.getPolicyType().equals("EM")).collect(Collectors.toList());

            Boolean em = this.hasOverlap(workerPoliciesEm);

            if (em)
                throw new RemoteServiceException("雇主险有相同覆盖范围内的保险");


            for (WorkerPolicy workerPolicy : workerPoliciesEm) {
                if (workerPolicy.getEndTime().after(new Date()) && workerPolicy.getStartTime().before(new Date())) {
                    workerPolicy.setStatus("BZZ");
                }else {
                    workerPolicy.setStatus("DSX");
                }
                if (workerPolicy.getEndTime().before(new Date())) {
                    workerPolicy.setStatus("YSX");
                }
            }

            if (workerPolicies.stream().filter(item -> item.getStatus().equals("BZZ")).count() < 1) {
                throw new RemoteServiceException("雇主险必须有生效的保险");
            }
        }


        workerPolicyService.saveOrUpdateBatch(workerManagerBean.getWorkerPolicies());

}
}
