package com.pusong.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.pusong.business.domain.*;
import com.pusong.business.domain.bo.*;
import com.pusong.business.domain.vo.*;
import com.pusong.business.enums.ApproverTypeEnum;
import com.pusong.business.enums.CommonStatusEnum;
import com.pusong.business.enums.ContractStatusEnum;
import com.pusong.business.enums.CustomerStatusEnum;
import com.pusong.business.mapper.*;
import com.pusong.business.service.IPsApproverRecordService;
import com.pusong.business.service.IPsCompanyInfoService;
import com.pusong.business.service.approver.ApproverContainer;
import com.pusong.business.service.approver.ApproverService;
import com.pusong.common.core.exception.ServiceException;
import com.pusong.common.core.utils.DateUtils;
import com.pusong.common.core.utils.MapstructUtils;
import com.pusong.common.core.utils.StringUtils;
import com.pusong.common.mybatis.core.page.TableDataInfo;
import com.pusong.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pusong.common.satoken.utils.LoginHelper;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pusong.business.service.IPsCustomInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 客户基本信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-07-25
 */
@RequiredArgsConstructor
@Service
public class PsCustomInfoServiceImpl implements IPsCustomInfoService {

    private final PsCustomInfoMapper baseMapper;
    private final PsCustomPriceMapper priceMapper;
    private final PsCustomCallbackMapper callbackMapper;
    private final PsContractInfoMapper contractInfoMapper;
    private final PsCompanyInfoMapper companyInfoMapper;
    @Resource
    private final IPsCompanyInfoService companyInfoService;

    private final ApproverContainer approverContainer;
    private final IPsApproverRecordService approverRecordService;

    /**
     * 根据id查询客户
     * @return 客户id和姓名
     */
    @Override
    public PsCustomInfo listById(Long id) {
        return baseMapper.selectById(id);

    }

    /**
     * 查询登陆人下所有的客户名称和id
     * @return 客户id和姓名
     */
    @Override
    public List<Map<String,Object>> listByLoginUser(String name) {

        //查询客户基本信息
        List<PsCustomInfo> list = baseMapper.selectList(new QueryWrapper<PsCustomInfo>().select("id", "custom_name","custom_mobile")
                .lambda().eq(PsCustomInfo::getDelFlag, 0).isNotNull(PsCustomInfo::getCustomName).ne(PsCustomInfo::getCustomName,"").ne(PsCustomInfo::getCustomName,"无")
                .and(StringUtils.isNotBlank(name),wq->wq.like(PsCustomInfo::getCustomName,name).or().like(PsCustomInfo::getCustomMobile,name)));
        List<Map<String,Object>> listmap = new ArrayList<>();
        list.forEach(item->{
            Map<String,Object> map = new HashMap<>();
            map.put("id",item.getId());
            map.put("customName",item.getCustomName());
            map.put("custom_mobile",item.getCustomMobile());
            listmap.add(map);
        });

        return listmap;
    }

    /**
     * 分页查询客户基本信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 客户基本信息分页列表
     */
    @Override
    public TableDataInfo<PsCustomInfoVo> queryPageList(PsCustomInfoBo bo, PageQuery pageQuery) {
        //查询客户基本信息

        bo.setBlack(CommonStatusEnum.N.getCode());
        if(bo.getType() == null ||  bo.getType() == 1){//任务列表
            bo.setNotCustomStatus(CustomerStatusEnum.getPublic());
        }else if(bo.getType() == 2){//黑名单
            bo.setBlack(CommonStatusEnum.Y.getCode());
            bo.setNotCustomStatus(CustomerStatusEnum.getPublic());
        }else if(bo.getType() == 3){//公海
            bo.setInCustomStatus(CustomerStatusEnum.getPublic());
        }
        Wrapper<PsCustomInfo> lqw = buildQueryWrapper(bo);
        Page<PsCustomInfoVo> result = new Page<>();
        if(bo.getType() == 3){//公海数据不用加数据权限
            result = baseMapper.selectPagePublicCustomerList(pageQuery.build(), lqw);
        }else{
            result = baseMapper.selectPageCustomerList(pageQuery.build(), lqw);
        }

        if(CollectionUtils.isEmpty(result.getRecords())){
            return TableDataInfo.build(result);
        }
        List<Long> ids = result.getRecords().stream().map(PsCustomInfoVo::getId).collect(Collectors.toList());

        //查询报价金额详细
        List<PsCustomPriceVo> pices = priceMapper.selectVoList(Wrappers.<PsCustomPrice>lambdaQuery()
                        .in(PsCustomPrice::getCustomId,ids).eq(PsCustomPrice::getDelFlag,0));
        Map<Long, List<PsCustomPriceVo>> id_piceMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(pices)){
            //分组
            id_piceMap = pices.stream().collect((Collectors.groupingBy(PsCustomPriceVo::getCustomId)));
            //排序
            id_piceMap.entrySet().forEach(entry->{
                entry.setValue(entry.getValue().stream().sorted(Comparator.comparingInt(PsCustomPriceVo::getPriceBatch)).collect(Collectors.toList()));
            });
        }

        //查询回访记录详细
        List<PsCustomCallbackVo> callbacks = callbackMapper.selectVoList(Wrappers.<PsCustomCallback>lambdaQuery()
                .in(PsCustomCallback::getCustomId, ids)
                .eq(PsCustomCallback::getDelFlag, 0));
        Map<Long, List<PsCustomCallbackVo>> id_callbackMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(callbacks)){
            //分组
            id_callbackMap = callbacks.stream().collect((Collectors.groupingBy(PsCustomCallbackVo::getCustomId)));
            //排序
            id_callbackMap.entrySet().forEach(entry->{
                entry.setValue(entry.getValue().stream().sorted(Comparator.comparingLong(PsCustomCallbackVo::getId).reversed()).collect(Collectors.toList()));
            });
        }

        //查询公司信息
        List<PsCompanyInfoVo> companyInfoVos = companyInfoMapper.selectVoList(Wrappers.<PsCompanyInfo>lambdaQuery()
                .in(PsCompanyInfo::getCustomId, ids)
                .eq(PsCompanyInfo::getDelFlag, 0)
                .eq(PsCompanyInfo::getCompanyType, "1"));
        Map<Long, List<PsCompanyInfoVo>> id_companyMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(companyInfoVos)){
            //分组
            id_companyMap = companyInfoVos.stream().collect((Collectors.groupingBy(PsCompanyInfoVo::getCustomId)));
            //排序
            id_companyMap.entrySet().forEach(entry->{
                entry.setValue(entry.getValue().stream().sorted(Comparator.comparingLong(PsCompanyInfoVo::getId)).collect(Collectors.toList()));
            });
        }

        for (PsCustomInfoVo info : result.getRecords()) {
            //装填回访记录vo
            info.setPsCustomCallbackVo(id_callbackMap.get(info.getId()));
            //装填报价金额vo
            info.setPsCustomPriceVo(id_piceMap.get(info.getId()));
            //装填公司信息vo
            info.setPsCompanyInfoVos(id_companyMap.get(info.getId()));
            //报价金额
            if(CollectionUtils.isNotEmpty(info.getPsCustomPriceVo())){
                info.setPrice(info.getPsCustomPriceVo().stream().map(PsCustomPriceVo::getServicePrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));
            }
            //最新的回访信息
            if(CollectionUtils.isNotEmpty(info.getPsCustomCallbackVo())){
                info.setCallBack(info.getPsCustomCallbackVo().get(0).getCallbackContent());
            }
            //最早的公司名称
            if(CollectionUtils.isNotEmpty(info.getPsCompanyInfoVos())){
                info.setCompanyName(info.getPsCompanyInfoVos().get(0).getCompanyName());
                info.setCompanyNum(info.getPsCompanyInfoVos().size());
            }else{
                info.setCompanyNum(0);
            }
            //任务进行天数
            info.setNumOfDat(info.getStartDate() == null?0:DateUtils.calWorkDate(info.getStartDate(),new Date()));
            //公海审批失败
            if(bo.getType() == 3 && StringUtils.equals(CustomerStatusEnum.PUBLIC.getCode(),info.getCustomStatus())){
                info.setPublicDesc(approverRecordService.getLastFail(info.getId()+"",null,List.of(ApproverTypeEnum.PUBLIC.getCode())));
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的客户基本信息列表
     *
     * @param bo 查询条件
     * @return 客户基本信息列表
     */
    @Override
    public List<PsCustomInfoVo> queryList(PsCustomInfoBo bo) {
        Wrapper<PsCustomInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectCustomerList(lqw);
    }

    private Wrapper<PsCustomInfo> buildQueryWrapper(PsCustomInfoBo bo) {
        QueryWrapper<PsCustomInfo> qw = Wrappers.query();
        //客户信息表条件
        qw.eq( "info.del_flag", 0);//删除标志
        qw.like(bo.getId() != null, "info.id", bo.getId());//id
        qw.like(StringUtils.isNotBlank(bo.getCustomName()), "info.custom_name", bo.getCustomName());//姓名
        qw.eq(StringUtils.isNotBlank(bo.getCustomSource()), "info.custom_source", bo.getCustomSource());//来源
        qw.eq(StringUtils.isNotBlank(bo.getCustomLevel()), "info.custom_level", bo.getCustomLevel());//客户级别
        qw.like(StringUtils.isNotBlank(bo.getCustomMobile()), "info.custom_mobile", bo.getCustomMobile());//客户手机号
        qw.in(CollectionUtils.isNotEmpty(bo.getInCustomStatus()), "info.custom_status", bo.getInCustomStatus());//客户状态
        qw.notIn(CollectionUtils.isNotEmpty(bo.getNotCustomStatus()), "info.custom_status", bo.getNotCustomStatus());//客户状态
        qw.eq(StringUtils.isNotBlank(bo.getBlack()), "info.black", bo.getBlack());//拉黑标识
        //用户信息表条件
        qw.like(StringUtils.isNotBlank(bo.getCustomManagerName()), "usr.nick_name", bo.getCustomManagerName());//销售经理名称
        //关联客户信息表条件
        qw.like(StringUtils.isNotBlank(bo.getCustomIntroducerName()), "psinfo.custom_name", bo.getCustomIntroducerName());//介绍人姓名
        //公司名称
        qw.exists(StringUtils.isNotBlank(bo.getCompanyName()), "select 1 from ps_company_info com where info.id = com.custom_id and com.company_name like '%"+bo.getCompanyName()+"%'");
        qw.exists(bo.getHaveContract() != null && bo.getHaveContract() == 1,"SELECT 1 FROM ps_contract_info psci WHERE psci.custom_id = info.id and psci.contract_status != '10' and psci.is_cancel != '03'");
        qw.notExists(bo.getHaveContract() != null && bo.getHaveContract() == 0,"SELECT 1 FROM ps_contract_info psci WHERE psci.custom_id = info.id and psci.contract_status != '10' and psci.is_cancel != '03'");
        qw.orderByDesc("info.create_time");//创建时间排序
        return qw;
    }

    /**
     * 新增客户基本信息
     *
     * @param bo 客户基本信息
     * @return 是否新增成功
     */
    @Override
    @Transactional
    public Long insertByBo(PsCustomInfoBo bo) {
        PsCustomInfo add = MapstructUtils.convert(bo, PsCustomInfo.class);
        validEntityBeforeSave(add);
        //客户经理就是登录用户
        add.setCustomManager(LoginHelper.getUserId());
        add.setBlack(CommonStatusEnum.N.getCode());
        add.setCustomStatus(CustomerStatusEnum.INIT.getCode());
        add.setAcceptDate(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if(CollectionUtils.isNotEmpty(bo.getPriceBos())){
            bo.getPriceBos().forEach(item->item.setCustomId(add.getId()));
            this.insertPiceByBo(bo.getPriceBos());
        }
        if(CollectionUtils.isNotEmpty(bo.getCallbackBos())){
            bo.getCallbackBos().forEach(item->item.setCustomId(add.getId()));
            this.insertCallbackByBo(bo.getCallbackBos());
        }
        //新增公司信息
        if(StringUtils.isNotBlank(bo.getCompanyName())){
            PsCompanyInfoBo update = new PsCompanyInfoBo();
            update.setCustomId(add.getId());
            update.setCompanyName(bo.getCompanyName());
            companyInfoService.saveBo(update);
        }
        return add.getId();
    }

    /**
     * 修改客户基本信息
     *
     * @param bo 客户基本信息
     * @return 是否修改成功
     */
    @Override

    public Boolean updateByBo(PsCustomInfoBo bo) {
        PsCustomInfo update = MapstructUtils.convert(bo, PsCustomInfo.class);
        validEntityBeforeSave(update);
         baseMapper.updateById(update);
        //修改公司信息
        if(CollectionUtils.isNotEmpty(bo.getCompanyInfoBos())){
            bo.getCompanyInfoBos().forEach(item->{item.setCustomId(bo.getId());item.setCompanyType("1");});
            companyInfoService.saveBatchBo(bo.getCompanyInfoBos());
        }
        if(CollectionUtils.isNotEmpty(bo.getCompanyIds())){
            companyInfoService.deleteWithValidByIds(bo.getCompanyIds(),true);
        }
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PsCustomInfo entity){
        //TODO 校验状态
    }

    /**
     * 修改客户状态
     *
     * @param id     主键id
     * @param type 操作类型
     * @return 是否删除成功
     */
    @Override
    public Boolean updateCustomerStatus(Long id, Integer type,String desc) {
        PsCustomInfo psCustomInfo = baseMapper.selectById(id);
        if(true){
            //todo 校验原数据状态是否可以更改
        }
        LambdaUpdateWrapper<PsCustomInfo> wq = Wrappers.<PsCustomInfo>lambdaUpdate().eq(PsCustomInfo::getId, id);
        switch (type){
            case 1://移入公海
                psCustomInfo.setCustomStatus(CustomerStatusEnum.PUBLIC.getCode());
                //拼装历史的所属客户经理
                if(StringUtils.isBlank(psCustomInfo.getHistoryCustomManager())){
                    psCustomInfo.setHistoryCustomManager(psCustomInfo.getCustomManager()+"");
                }else {
                    psCustomInfo.setHistoryCustomManager(psCustomInfo.getHistoryCustomManager()+","+psCustomInfo.getCustomManager());
                }
                wq.set(PsCustomInfo::getCustomManager,null);
                psCustomInfo.setCustomManager(null);
                break;
            case 2://拉黑
                psCustomInfo.setBlack(CommonStatusEnum.Y.getCode());
                psCustomInfo.setBlackDesc(desc);
                break;
        }

        return baseMapper.update(psCustomInfo,wq) > 0;
    }

    /**
     * 新增客户报价信息
     *
     * @param bos 客户报价信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertPiceByBo(List<PsCustomPriceBo> bos) {
        List<PsCustomPrice> adds = MapstructUtils.convert(bos, PsCustomPrice.class);
        PsCustomPrice price = priceMapper.selectOne(new QueryWrapper<PsCustomPrice>().select("max(price_batch) price_batch")
                .lambda().eq(PsCustomPrice::getDelFlag, 0).eq(PsCustomPrice::getCustomId, bos.get(0).getCustomId()).last("limit 1"));
        adds.forEach(add->{
            add.setPriceBatch((price != null && price.getPriceBatch() != null) ? price.getPriceBatch() + 1 : 1);
        });

        return priceMapper.insertBatch(adds);
    }

    /**
     * 新增客户回访记录
     *
     * @param bos 客户回访记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertCallbackByBo(List<PsCustomCallbackBo> bos){
        List<PsCustomCallback> adds = MapstructUtils.convert(bos, PsCustomCallback.class);
        return callbackMapper.insertBatch(adds);
    }


    /**
     * 已完成客户（最开始 客户档案和已完成客户是一个接口，后续分开了，此接口只用来查已完成客户列表）
     * @param queryBo
     * @return
     */
    public TableDataInfo<PsCustomerRecordVo> getCustomerRecordList(PsCustomRecordQueryBo queryBo, PageQuery pageQuery) {
        QueryWrapper<PsCustomInfo> qw = Wrappers.query();
        qw.like(StringUtils.isNotBlank(queryBo.getCompanyName()), "com.company_name", queryBo.getCompanyName());
        qw.like(StringUtils.isNotBlank(queryBo.getFinanceAccountNo()), "com.finance_account_no", queryBo.getFinanceAccountNo());
        qw.like(StringUtils.isNotBlank(queryBo.getLegalPersonName()), "com.legal_person_name", queryBo.getLegalPersonName());

        qw.eq(queryBo.getId() != null, "custom.id", queryBo.getId());
        qw.like(StringUtils.isNotBlank(queryBo.getName()), "custom.custom_name", queryBo.getName());
        qw.like(StringUtils.isNotBlank(queryBo.getMobile()), "custom.custom_mobile", queryBo.getMobile());
        qw.eq(StringUtils.isNotBlank(queryBo.getCustomSource()), "custom.custom_source", queryBo.getCustomSource());
        qw.notIn("con.contract_status", ContractStatusEnum.isValid());
        qw.exists(StringUtils.isNotBlank(queryBo.getCustomIntroducerName()), "select 1 from ps_custom_info pc where pc.id = custom.custom_introducer and pc.custom_name like '%" + queryBo.getCustomIntroducerName() + "%'");
        qw.exists(StringUtils.isNotBlank(queryBo.getCustomManagerName()), "select 1 from sys_user su where su.user_id = custom.custom_manager and su.nick_name like '%" + queryBo.getCustomManagerName() + "%'");
        if (queryBo.getType() != null && queryBo.getType() == 2) {
//            qw.eq("con.contract_status", ContractStatusEnum.SUCCESS.getCode());
            //已完成合同列表：客户下不存在未完成的合同信息（不包括作废和暂存的合同）
            qw.notExists("select 1 from ps_contract_info pscon where pscon.custom_id = custom.id and pscon.contract_status not in ('" + ContractStatusEnum.SUCCESS.getCode() + "','"
                    + ContractStatusEnum.INIT.getCode() + "' and pscon.is_cancel != '03')");
        } /*else if (queryBo.getType() != null && queryBo.getType() == 1) {
            //托管类服务类型编码集合
            qw.exists(queryBo.getServiceType() != null && queryBo.getServiceType() == 1, "select 1 from ps_contract_business bus " +
                    "where bus.contract_code = con.contract_code and bus.business_type = 2");
            //非托管服务类型编码集合
            qw.notExists(queryBo.getServiceType() != null && queryBo.getServiceType() == 2, "select 1 from ps_contract_business bus " +
                    "where bus.contract_code = con.contract_code and bus.business_type = 2");
            //服务项目编码集合
            qw.exists(CollectionUtils.isNotEmpty(queryBo.getServiceProjects()), "select 1 from ps_contract_business detail " +
                    "where detail.contract_code = con.contract_code and detail.business_type in (" + StringUtils.join(queryBo.getServiceProjects(), ",") + ")");//服务项目编码集合
        }*/
        Page<PsCustomerRecordVo> list = baseMapper.selectPageCustomerRecordList(pageQuery.build(), qw);
        if (CollectionUtils.isEmpty(list.getRecords())) {
            return TableDataInfo.build(list);
        }

        Map<Long, List<PsCustomCallbackVo>> id_callbackMap = new HashMap<>();
        //已完成客户的回访记录
        if (queryBo.getType() != null && queryBo.getType() == 2) {
            List<Long> ids = list.getRecords().stream().map(PsCustomerRecordVo::getId).collect(Collectors.toList());
            //查询回访记录详细
            List<PsCustomCallbackVo> callbacks = callbackMapper.selectVoList(Wrappers.<PsCustomCallback>lambdaQuery()
                    .in(PsCustomCallback::getCustomId, ids)
                    .eq(PsCustomCallback::getDelFlag, 0));
            if (CollectionUtils.isNotEmpty(callbacks)) {
                //分组
                id_callbackMap = callbacks.stream().collect((Collectors.groupingBy(PsCustomCallbackVo::getCustomId)));
                //排序
                id_callbackMap.entrySet().forEach(entry -> {
                    entry.setValue(entry.getValue().stream().sorted(Comparator.comparingLong(PsCustomCallbackVo::getId).reversed()).collect(Collectors.toList()));
                });
            }
        }
        for (PsCustomerRecordVo item : list.getRecords()){
            //免费工单审批
            item.setFreeFailDesc(approverRecordService.getLastFail(item.getContractCode(), item.getContractCode(), List.of(ApproverTypeEnum.FREE.getCode())));
            //装填回访记录vo
            item.setPsCustomCallbackVo(id_callbackMap.get(item.getId()));
        }
        return TableDataInfo.build(list);
    }

    /**
     * 移除黑名单
     * @param customerId 客户id
     */
    public void removeblack(Long customerId){
        PsCustomInfo psCustomInfo = baseMapper.selectById(customerId);
        if(!StringUtils.equals(CommonStatusEnum.Y.getCode(),psCustomInfo.getBlack()) || psCustomInfo.getDelFlag()!=0){
            throw new ServiceException("数据状态已变更，请刷新后重试");
        }
        psCustomInfo.setBlack(CommonStatusEnum.N.getCode());
        baseMapper.updateById(psCustomInfo);
    }

    /**
     * 删除
     * @param customerId 客户id
     */
    public void delete(Long customerId){
        PsCustomInfo psCustomInfo = baseMapper.selectById(customerId);
        if(!StringUtils.equals(CommonStatusEnum.Y.getCode(),psCustomInfo.getBlack()) || psCustomInfo.getDelFlag()!=0){
            throw new ServiceException("数据状态已变更，请刷新后重试");
        }
        psCustomInfo.setDelFlag(2);
        baseMapper.updateById(psCustomInfo);
    }

    /**
     * 终止合作
     * @param customerId 客户id
     */
    public void stop(Long customerId){
        PsCustomInfo psCustomInfo = baseMapper.selectById(customerId);
        if(StringUtils.equals(CommonStatusEnum.Y.getCode(),psCustomInfo.getBlack()) || psCustomInfo.getDelFlag()!=0){
            throw new ServiceException("数据状态已变更，请刷新后重试");
        }
        Long num = contractInfoMapper.selectCount(Wrappers.<PsContractInfo>lambdaQuery().eq(PsContractInfo::getCustomId, customerId)
                .notIn(PsContractInfo::getContractStatus, List.of(ContractStatusEnum.INIT.getCode(), ContractStatusEnum.SUCCESS.getCode()))
                .ne(PsContractInfo::getIsCancel, CommonStatusEnum.SUCCESS.getCode()));
        if(num>0){
            throw new ServiceException("有未完成的合同，无法终止合作");
        }
        psCustomInfo.setCustomStatus(CustomerStatusEnum.STOP.getCode());
        baseMapper.updateById(psCustomInfo);
    }

    /**
     * 主动认领
     * @param customerId 客户id
     */
    @Transactional
    public void claim(Long customerId){
        PsCustomInfo psCustomInfo = baseMapper.selectById(customerId);
        psCustomInfo.setCustomStatus(CustomerStatusEnum.CLAIMAPPROVER.getCode());
//        psCustomInfo.setCustomManager(LoginHelper.getUserId());
        baseMapper.updateById(psCustomInfo);
        //发起审批
        ApproverService service = approverContainer.getService(ApproverTypeEnum.PUBLIC.getCode());
        service.apply(customerId+"",null,null,LoginHelper.getUserId()+"");
    }

    /**
     * 指派
     * @param customerId 客户id
     * @param userId 被指派者
     */
    public void assign(Long customerId,Long userId){
        PsCustomInfo psCustomInfo = baseMapper.selectById(customerId);
        psCustomInfo.setCustomStatus(CustomerStatusEnum.INIT.getCode());
        psCustomInfo.setCustomManager(userId);
        psCustomInfo.setAcceptDate(new Date());
        baseMapper.updateById(psCustomInfo);
    }

}
