package cn.superdesk.uniorder.service.organisation.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.data.extract.enums.opCenter.OpCenterBizPlatformEnum;
import cn.superdesk.uniorder.common.enums.common.CommonEnabledEnum;
import cn.superdesk.uniorder.common.enums.common.CommonServiceNoEnum;
import cn.superdesk.uniorder.common.enums.common.CommonSoftDeleteEnum;
import cn.superdesk.uniorder.common.enums.common.CommonSourceFromEnum;
import cn.superdesk.uniorder.common.enums.thirdpart.qchjc.QchjcStorageStatusEnum;
import cn.superdesk.uniorder.common.exception.OpenApiException;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.organisation.UnifiedSupplier;
import cn.superdesk.uniorder.dao.entity.organisation.UnifiedSupplierExample;
import cn.superdesk.uniorder.dao.entity.sys.SysUser;
import cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.accounting.QchjcUserEntity;
import cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.accounting.QchjcUserEntityExample;
import cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.organisation.QchjcSupplierEntity;
import cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.organisation.QchjcSupplierEntityExample;
import cn.superdesk.uniorder.dao.mapper.organisation.UnifiedSupplierMapper;
import cn.superdesk.uniorder.dao.mapper.organisation.UnifiedSupplierMapperEx;
import cn.superdesk.uniorder.dao.mapper.thirdpart.qchjc.accounting.QchjcUserEntityMapper;
import cn.superdesk.uniorder.dao.mapper.thirdpart.qchjc.organisation.QchjcSupplierEntityMapper;
import cn.superdesk.uniorder.dto.param.organisation.SysSupplierQueryParam;
import cn.superdesk.uniorder.dto.param.organisation.UnifiedSupplierQueryCriteria;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.SlaveSupplierReceiveParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskmall.SuperMallSupplierReceiveParam;
import cn.superdesk.uniorder.dto.vo.organisation.UnifiedSupplierVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSelectVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSupplierVo;
import cn.superdesk.uniorder.service.organisation.SysSupplierService;
import cn.superdesk.uniorder.service.sys.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * @Description: 商户/供应商 对账主体
 * @Author: linjinyu
 * @CreateDate: 8/5/20 5:06 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 8/5/20 5:06 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class SysSupplierServiceImpl implements SysSupplierService {


    @Autowired
    private UnifiedSupplierMapper     unifiedSupplierMapper;
    @Autowired
    private UnifiedSupplierMapperEx   unifiedSupplierMapperEx;
    @Autowired
    private QchjcSupplierEntityMapper qchjcSupplierEntityMapper;
    @Autowired
    private QchjcUserEntityMapper     qchjcUserEntityMapper;
    @Autowired
    private SysUserService            sysUserService;


    @Override
    public int insertSelective(UnifiedSupplier unifiedSupplier) {
        return unifiedSupplierMapper.insertSelective(unifiedSupplier);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedSupplier unifiedSupplier) {
        return unifiedSupplierMapper.updateByPrimaryKeySelective(unifiedSupplier);
    }

    @Override
    public void deleteObj(Long id) {

    }


    @Override
    public UnifiedSupplier findById(Long id) {
        return unifiedSupplierMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UnifiedSupplier> findAll(Map<String, Object> queryMap) {

//		log.info("{}", AnJsonUtils.toJson(queryMap));
//
//		UnifiedSupplierCriteria unifiedSupplierCriteria = AnBeanUtils.mapToBean(queryMap, UnifiedSupplierCriteria.class);
//
//		log.info("入参: {}\n转换: {}", AnJsonUtils.toJson(queryMap), AnJsonUtils.toJson(unifiedSupplierCriteria));
//
//		return unifiedSupplierMapperEx.findAll(unifiedSupplierCriteria);
        return null;
    }

    @Override
    public PageInfo<UnifiedSupplier> findByPaging(Map<String, Object> params, Integer pageNo, Integer pageSize) {
        return null;
    }


    @Override
    public List<UnifiedSupplierVo> findByCriteria(UnifiedSupplierQueryCriteria unifiedSupplierCriteria) {

        log.info("入参: {}", AnJsonUtils.toJson(unifiedSupplierCriteria));

        return unifiedSupplierMapperEx.findAll(unifiedSupplierCriteria);
    }


    /****************************************** extends BaseService 部分   end ******************************************/


    @Override
    public int addUnifiedSupplier(UnifiedSupplier entity, LoginSysUser loginSysUser) {
        return unifiedSupplierMapper.insertSelective(entity);
    }

    @Override
    public int updateUnifiedSupplier(UnifiedSupplier entity, LoginSysUser loginSysUser) {

        UnifiedSupplier originEntity = unifiedSupplierMapper.selectByPrimaryKey(entity.getId());

        AnBeanUtils.copy(entity, originEntity);

        return unifiedSupplierMapper.updateByPrimaryKeySelective(originEntity);
    }

    @Override
    public int deleteUnifiedSupplier(Long id, LoginSysUser loginSysUser) {
        return unifiedSupplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UnifiedSupplier findUnifiedSupplierById(Long id) {
        UnifiedSupplier entity = unifiedSupplierMapper.selectByPrimaryKey(id);
        return entity;
    }

    @Override
    public R<List<UnifiedSupplier>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

        // 样例 保留
//		UnifiedSupplier unifiedSupplierConditions = BeanUtils.mapToBean(conditions,UnifiedSupplier.class);


        PageHelper.startPage(pageNun, pageSize);
        UnifiedSupplierExample          example  = new UnifiedSupplierExample();
        UnifiedSupplierExample.Criteria criteria = example.createCriteria();

        List<UnifiedSupplier>     resultList = unifiedSupplierMapper.selectByExample(example);
        PageInfo<UnifiedSupplier> page       = new PageInfo<UnifiedSupplier>(resultList);
        PageHelper.clearPage();

        return new R<List<UnifiedSupplier>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    @Override
    public R<List<UnifiedSupplierVo>> pageQuery(
            UnifiedSupplierQueryCriteria queryCriteria,
            LoginSysUser loginSysUser) {

        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
        List<UnifiedSupplierVo>     resultList = unifiedSupplierMapperEx.findAll(queryCriteria);
        PageInfo<UnifiedSupplierVo> page       = new PageInfo<UnifiedSupplierVo>(resultList);
        PageHelper.clearPage();

        // 附加信息
        additionalInfoGetDataByPage(resultList);

        return new R<List<UnifiedSupplierVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);
    }

    private void additionalInfoGetDataByPage(List<UnifiedSupplierVo> resultList) {

        if (CollUtil.isEmpty(resultList)) {
            return;
        }

        for (UnifiedSupplierVo vo : resultList) {

            // 补 订单类型-订单来源-引用enum CommonServiceNoEnum
            if (ObjectUtil.isNotNull(vo.getServiceNo())) {
                vo.setServiceNoName(CommonServiceNoEnum.getByServiceNo(vo.getServiceNo()).getServiceName());
            } else {
                vo.setServiceNoName(CommonServiceNoEnum.UNKNOW.getServiceName());
            }
            // 补 操作来源-引用enum CommonSourceFromEnum
            if (ObjectUtil.isNotNull(vo.getSourceFrom())) {
                vo.setSourceFromName(CommonSourceFromEnum.getBySourceFromCode(vo.getSourceFrom()).getSourceFromName());
            } else {
                vo.setSourceFromName(CommonSourceFromEnum.UNKNOW.getSourceFromName());
            }
        }
    }


    /****************************************** 工具方法  start ******************************************/

    @Override
    public List<UnifiedSupplier> findByOutSaleIds(List<Long> outSaleIds) {

        UnifiedSupplierExample example = new UnifiedSupplierExample();
        example.createCriteria()
                .andSaleIdIn(outSaleIds);
        List<UnifiedSupplier> resultList = unifiedSupplierMapper.selectByExample(example);
        return resultList;

    }

    public Map<Long, UnifiedSupplier> convertOutSaleIds2MapOutSaleId2Entity(List<Long> outSaleIds) {

        Map<Long, UnifiedSupplier> result = new HashMap<>();

        if (CollUtil.isNotEmpty(outSaleIds)) {

            List<UnifiedSupplier> resultList = findByOutSaleIds(outSaleIds);

            if (CollUtil.isNotEmpty(resultList)) {

                result = resultList
                        .stream()
                        .filter(AnLamdaUtils.distinctByKey(o -> o.getSaleId()))
                        .collect(Collectors.toMap(UnifiedSupplier::getSaleId, v -> v, (k, v) -> v));
            }
        }

        return result;

    }


    /****************************************** 工具方法   end ******************************************/


    private void convertSlaveSupplierReceiveParam2Entity4SuperdeskMall(SuperMallSupplierReceiveParam superMallSupplierReceiveParam, UnifiedSupplier unifiedSupplier) {

        unifiedSupplier.setSaleId(superMallSupplierReceiveParam.getSaleId());
        unifiedSupplier.setSaleName(superMallSupplierReceiveParam.getSaleName());

        unifiedSupplier.setServiceNo(superMallSupplierReceiveParam.getServiceNo());

        unifiedSupplier.setContactsName(superMallSupplierReceiveParam.getContactsName());
        unifiedSupplier.setContactsPhone(superMallSupplierReceiveParam.getContactsPhone());
        unifiedSupplier.setContactsAdd(superMallSupplierReceiveParam.getContactsAdd());

        if(CommonServiceNoEnum.服务之窗.getServiceNo().equals(unifiedSupplier.getServiceNo())){
            unifiedSupplier.setBizPlatformCode(OpCenterBizPlatformEnum.服务之窗.getCode());
            unifiedSupplier.setBizPlatformName(OpCenterBizPlatformEnum.服务之窗.getName());
        }else if(CommonServiceNoEnum.企业福利.getServiceNo().equals(unifiedSupplier.getServiceNo())){
            unifiedSupplier.setBizPlatformCode(OpCenterBizPlatformEnum.福利商城.getCode());
            unifiedSupplier.setBizPlatformName(OpCenterBizPlatformEnum.福利商城.getName());
        }else if(CommonServiceNoEnum.企业采购.getServiceNo().equals(unifiedSupplier.getServiceNo())){
            unifiedSupplier.setBizPlatformCode(OpCenterBizPlatformEnum.企业采购.getCode());
            unifiedSupplier.setBizPlatformName(OpCenterBizPlatformEnum.企业采购.getName());
        }else if(CommonServiceNoEnum.代客下单.getServiceNo().equals(unifiedSupplier.getServiceNo())){
            unifiedSupplier.setBizPlatformCode(OpCenterBizPlatformEnum.代客下单.getCode());
            unifiedSupplier.setBizPlatformName(OpCenterBizPlatformEnum.代客下单.getName());
        }


        if (null == unifiedSupplier.getId()) {
            unifiedSupplier.setCreateTime(DateUtil.date());
            unifiedSupplier.setCreateUserId(1L);
        } else {
            unifiedSupplier.setUpdateTime(DateUtil.date());
            unifiedSupplier.setUpdateUserId(1L);
        }

        if (null == superMallSupplierReceiveParam.getEnabled()) {
            unifiedSupplier.setEnabled(CommonEnabledEnum.启用.getKey());
        } else {
            unifiedSupplier.setEnabled(CommonEnabledEnum.get(superMallSupplierReceiveParam.getEnabled()).getKey());
        }

        if (null == superMallSupplierReceiveParam.getDeleted()) {
            unifiedSupplier.setDeleted(CommonSoftDeleteEnum.正常.getKey());
        } else {
            unifiedSupplier.setDeleted(CommonSoftDeleteEnum.get(superMallSupplierReceiveParam.getDeleted()).getKey());
        }

        log.info("\n转换??:\n{}\n{}", AnJsonUtils.toJson(superMallSupplierReceiveParam), AnJsonUtils.toJson(unifiedSupplier));

    }

    private void convertSlaveSupplierReceiveParam2Entity4SuperdeskMallFLSC_QCH(SlaveSupplierReceiveParam slaveSupplierReceiveParam, UnifiedSupplier unifiedSupplier) {

        unifiedSupplier.setSaleId(slaveSupplierReceiveParam.getSaleId());
        unifiedSupplier.setSaleName(slaveSupplierReceiveParam.getSaleName());

        // 侨城汇 福利商城
        unifiedSupplier.setServiceNo(CommonServiceNoEnum.企业福利_侨城汇.getServiceNo());

        unifiedSupplier.setContactsName(slaveSupplierReceiveParam.getContactsName());
        unifiedSupplier.setContactsPhone(slaveSupplierReceiveParam.getContactsPhone());
        unifiedSupplier.setContactsAdd(slaveSupplierReceiveParam.getContactsAdd());

        if (ObjectUtil.isNotNull(unifiedSupplier.getId())) {
            unifiedSupplier.setCreateTime(DateUtil.date());
            unifiedSupplier.setCreateUserId(1L);
        } else {
            unifiedSupplier.setUpdateTime(DateUtil.date());
            unifiedSupplier.setUpdateUserId(1L);
        }

        /****************************************** 20220119 start ******************************************/

        if (ObjectUtil.isNotNull(slaveSupplierReceiveParam.getEnabled())) {
            unifiedSupplier.setEnabled(slaveSupplierReceiveParam.getEnabled());
        } else {
            unifiedSupplier.setEnabled(CommonEnabledEnum.启用.getKey());
        }

        if (ObjectUtil.isNotNull(slaveSupplierReceiveParam.getDeleted())) {
            unifiedSupplier.setDeleted(slaveSupplierReceiveParam.getDeleted());
        } else {
            unifiedSupplier.setDeleted(CommonSoftDeleteEnum.正常.getKey());
        }

    }

    /**
     * 代客专用 合并 供应商(商户) 信息
     *
     * @param saleId
     * @param saleName
     * @param serviceNo
     */
    @Override
    public void mergeUnifiedSupplier(Long saleId, String saleName, String serviceNo) {

        UnifiedSupplier processUnifiedSupplier = null;

        UnifiedSupplierExample example = new UnifiedSupplierExample();
        example.createCriteria()
                .andSaleIdEqualTo(saleId)
                // 区分serviceNo
                .andServiceNoEqualTo(serviceNo);

        List<UnifiedSupplier> unifiedSupplierList = unifiedSupplierMapper.selectByExample(example);

        if (CollUtil.isEmpty(unifiedSupplierList)) {

            processUnifiedSupplier = new UnifiedSupplier();
            processUnifiedSupplier.setSaleId(saleId);
            processUnifiedSupplier.setSaleName(saleName);
            processUnifiedSupplier.setServiceNo(serviceNo);

            processUnifiedSupplier.setCreateTime(DateUtil.date());
            processUnifiedSupplier.setCreateUserId(1L);

            unifiedSupplierMapper.insertSelective(processUnifiedSupplier);

        } else {

            processUnifiedSupplier = unifiedSupplierList.get(0);
            processUnifiedSupplier.setSaleId(saleId);
            processUnifiedSupplier.setSaleName(saleName);
            processUnifiedSupplier.setServiceNo(serviceNo);

            processUnifiedSupplier.setUpdateTime(DateUtil.date());
            processUnifiedSupplier.setUpdateUserId(1L);


            unifiedSupplierMapper.updateByPrimaryKeySelective(processUnifiedSupplier);
        }

    }

    /****************************************** kafka 超级前台 企业采购 企业福利 start ******************************************/

    @Override
    public void kafkaMergeReceiveMerchant4SuperdeskMall(SuperMallSupplierReceiveParam superMallSupplierReceiveParam) {

        try {

            UnifiedSupplier unifiedSupplier = null;

            UnifiedSupplierExample example = new UnifiedSupplierExample();
            example.createCriteria()
                    .andSaleIdEqualTo(superMallSupplierReceiveParam.getSaleId())
                    .andServiceNoEqualTo(superMallSupplierReceiveParam.getServiceNo());

            List<UnifiedSupplier> unifiedSupplierList = unifiedSupplierMapper.selectByExample(example);

            if (CollUtil.isEmpty(unifiedSupplierList)) {

                unifiedSupplier = new UnifiedSupplier();

                convertSlaveSupplierReceiveParam2Entity4SuperdeskMall(superMallSupplierReceiveParam, unifiedSupplier);

                unifiedSupplierMapper.insertSelective(unifiedSupplier);

            } else {

                unifiedSupplier = unifiedSupplierList.get(0);

                convertSlaveSupplierReceiveParam2Entity4SuperdeskMall(superMallSupplierReceiveParam, unifiedSupplier);

                unifiedSupplierMapper.updateByPrimaryKeySelective(unifiedSupplier);
            }

        } catch (Exception e) {

            e.printStackTrace();
            log.error("kafkaMergeReceiveMerchant4SuperdeskMall 失败:{}", ExceptionUtils.getMessage(e));

        }
    }

    @Override
    public void kafkaDeleteReceiveMerchant4SuperdeskMall(SuperMallSupplierReceiveParam superMallSupplierReceiveParam) {


        UnifiedSupplier unifiedSupplier = null;

        UnifiedSupplierExample example = new UnifiedSupplierExample();
        example.createCriteria()
                .andSaleIdEqualTo(superMallSupplierReceiveParam.getSaleId())
                .andServiceNoEqualTo(superMallSupplierReceiveParam.getServiceNo());

        List<UnifiedSupplier> unifiedSupplierList = unifiedSupplierMapper.selectByExample(example);

        if (CollUtil.isNotEmpty(unifiedSupplierList)) {

            unifiedSupplier = unifiedSupplierList.get(0);
            unifiedSupplier.setDeleted(CommonSoftDeleteEnum.删除.getKey());
            unifiedSupplierMapper.updateByPrimaryKeySelective(unifiedSupplier);
        }

    }

    /****************************************** kafka 超级前台 企业采购 企业福利   end ******************************************/

    /****************************************** kafka 侨城汇专用 start ******************************************/

    /**
     * 侨城汇 专用 商户信息同步
     *
     * @param slaveSupplierReceiveParam
     */
    @Override
    public void kafkaMergeReceiveMerchant4SuperdeskMallFLSC_QCH(SlaveSupplierReceiveParam slaveSupplierReceiveParam) {

        try {


            UnifiedSupplier unifiedSupplier = null;

            UnifiedSupplierExample example = new UnifiedSupplierExample();
            example.createCriteria()
                    .andServiceNoEqualTo(slaveSupplierReceiveParam.getServiceNo())
                    .andSaleIdEqualTo(slaveSupplierReceiveParam.getSaleId());

            List<UnifiedSupplier> unifiedSupplierList = unifiedSupplierMapper.selectByExample(example);

            if (CollUtil.isEmpty(unifiedSupplierList)) {

                unifiedSupplier = new UnifiedSupplier();

                convertSlaveSupplierReceiveParam2Entity4SuperdeskMallFLSC_QCH(slaveSupplierReceiveParam, unifiedSupplier);

                unifiedSupplierMapper.insertSelective(unifiedSupplier);

            } else {

                unifiedSupplier = unifiedSupplierList.get(0);

                convertSlaveSupplierReceiveParam2Entity4SuperdeskMallFLSC_QCH(slaveSupplierReceiveParam, unifiedSupplier);

                unifiedSupplierMapper.updateByPrimaryKeySelective(unifiedSupplier);
            }

        } catch (Exception e) {

            e.printStackTrace();
            log.error("kafkaMergeReceiveMerchant4SuperdeskMallFLSC_QCH 失败:{}", ExceptionUtils.getMessage(e));

        }

    }

    /**
     * 20200707 林志鹏说是没删除
     *
     * @param message
     */
    @Override
    @Deprecated
    public void kafkaDeleteReceiveMerchant4SuperdeskMallFLSC_QCH(String message) {

    }

    /****************************************** kafka 侨城汇专用   end ******************************************/

    /**
     * 同步集采供应商数据
     *
     * @param message
     */
    @Override
    public void mergeReceiveSupplier4Qchjc(String message) {
        try {
            log.info("同步集采供应商数据，param ：{}", message);
            QchjcSupplierEntity qchjcSupplierEntity = AnJsonUtils.toObject(message, QchjcSupplierEntity.class);
            handleUser(qchjcSupplierEntity);

            QchjcSupplierEntityExample qchjcSupplierEntityExample = new QchjcSupplierEntityExample();
            qchjcSupplierEntityExample.createCriteria()
                    .andSupplierIdEqualTo(qchjcSupplierEntity.getSupplierId());
            List<QchjcSupplierEntity> res = qchjcSupplierEntityMapper.selectByExample(qchjcSupplierEntityExample);
            UnifiedSupplier           unifiedSupplier;
            if (CollUtil.isEmpty(res)) {
                //未同步数据新增，同步过的数据进行修改
                qchjcSupplierEntityMapper.insertSelective(qchjcSupplierEntity);
                unifiedSupplier = new UnifiedSupplier();
                convertQchjcSupplierParam2Entity(qchjcSupplierEntity, unifiedSupplier);
                unifiedSupplierMapper.insertSelective(unifiedSupplier);
                qchjcSupplierEntity.setUnifiedId(unifiedSupplier.getId());
                qchjcSupplierEntityMapper.updateByPrimaryKeySelective(qchjcSupplierEntity);
            } else {
                QchjcSupplierEntity qchjcEntity = res.get(0);//原有数据
                AnBeanUtils.copy(qchjcSupplierEntity, qchjcEntity);
                qchjcSupplierEntityMapper.updateByPrimaryKeySelective(qchjcEntity);//更新中间表数据
                unifiedSupplier = unifiedSupplierMapper.selectByPrimaryKey(qchjcEntity.getUnifiedId());
                convertQchjcSupplierParam2Entity(qchjcSupplierEntity, unifiedSupplier);
                unifiedSupplierMapper.updateByPrimaryKeySelective(unifiedSupplier);//更新中间表数据
            }
        } catch (Exception e) {
            log.error("同步集采企业数据异常 =====  msg : {}", ExceptionUtils.getMessage(e));
            throw e;
        }
    }

    @Override
    public List<SysSupplierVo> findSupplierList(SysSupplierQueryParam param) {
        return unifiedSupplierMapper.findSupplierList(param);
    }

    @Override
    public List<SysSelectVo> selectList(Map<String, Object> params) {
        return unifiedSupplierMapper.selectList(params);
    }

    private void handleUser(QchjcSupplierEntity qchjcSupplierEntity) {
        String createUserName = qchjcSupplierEntity.getCreateUserName();
        String createUserId   = qchjcSupplierEntity.getCreateUserId();
        String updateUserId   = qchjcSupplierEntity.getUpdateUserId();
        String updateUserName = qchjcSupplierEntity.getUpdateUserName();
        handle(createUserId, createUserName);
        handle(updateUserId, updateUserName);
    }

    private void handle(String userId, String userName) {
        QchjcUserEntityExample example = new QchjcUserEntityExample();
        example.createCriteria()
                .andOutIdEqualTo(userId);
        List<QchjcUserEntity> creator = qchjcUserEntityMapper.selectByExample(example);
        QchjcUserEntity       createUser;
        if (CollectionUtils.isEmpty(creator) || null == creator.get(0)) {
            createUser = QchjcUserEntity.builder()
                    .outId(userId)
                    .userName(userName).build();
            qchjcUserEntityMapper.insertSelective(createUser);
        } else {
            createUser = creator.get(0);
        }
        if (createUser.getUnifiedId() == null) {
            //用户入库统一订单
            SysUser sysUser4Qchjc = sysUserService.createSysUser4Qchjc(userId, userName);
            if (sysUser4Qchjc == null) {
                createUser.setStorageStatus(QchjcStorageStatusEnum.FAIL.getStatus());
                qchjcUserEntityMapper.updateByPrimaryKeySelective(createUser);
                throw new OpenApiException(500, "同步失败,用户入库异常");
            }
            //保存统一订单用户id
            createUser.setUnifiedId(sysUser4Qchjc.getId());
            createUser.setStorageStatus(QchjcStorageStatusEnum.SUCCESS.getStatus());
            createUser.setStorageTime(DateUtil.date());
            qchjcUserEntityMapper.updateByPrimaryKeySelective(createUser);
        }
    }

    /**
     * 集采供应商param转换统一订单供应商entity
     *
     * @param qchjcSupplierEntity
     * @param unifiedSupplier
     */
    private void convertQchjcSupplierParam2Entity(QchjcSupplierEntity qchjcSupplierEntity, UnifiedSupplier unifiedSupplier) {
        unifiedSupplier.setSaleId(qchjcSupplierEntity.getId());//中间id
        unifiedSupplier.setSaleName(qchjcSupplierEntity.getSupplierName());//供应商名称
        unifiedSupplier.setServiceNo(CommonServiceNoEnum.企业商城_侨城汇.getServiceNo());//服务编号统一QYSC_QCH
        unifiedSupplier.setSourceFrom(CommonSourceFromEnum.企业商城_侨城汇.getSourceFromCode());//来源
        unifiedSupplier.setContactsName(qchjcSupplierEntity.getContactsName());//联系人
        unifiedSupplier.setContactsPhone(qchjcSupplierEntity.getContactsPhone());//联系方式
        unifiedSupplier.setContactsAdd(qchjcSupplierEntity.getContactsAddress());//联系地址
        QchjcUserEntity update = qchjcUserEntityMapper.selectByOutId(qchjcSupplierEntity.getUpdateUserId());
        unifiedSupplier.setUpdateUserId(update.getUnifiedId());//修改人id
        QchjcUserEntity create = qchjcUserEntityMapper.selectByOutId(qchjcSupplierEntity.getCreateUserId());
        unifiedSupplier.setCreateUserId(create.getUnifiedId());//创建人id
        unifiedSupplier.setEnabled(qchjcSupplierEntity.getEnabled());//是否可用
        unifiedSupplier.setDeleted(qchjcSupplierEntity.getDeleted());//是否软删除
        //创建时间，修改时间
        Date createTime = qchjcSupplierEntity.getCreateTime();
        if (unifiedSupplier.getCreateTime() == null) {
            unifiedSupplier.setCreateTime(createTime == null ? DateUtil.date() : createTime);
        }
        unifiedSupplier.setUpdateTime(DateUtil.date());
    }
}
