package com.hfkj.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.channel.weixin.utils.MD5Util;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.IDGenerator;
import com.hfkj.dao.BsMerMapper;
import com.hfkj.entity.*;
import com.hfkj.model.MerAccountModel;
import com.hfkj.model.MerBasisModel;
import com.hfkj.model.StoreModel;
import com.hfkj.service.*;
import com.hfkj.sysenum.*;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("merService")
public class BsMerServiceImpl implements BsMerService {

    @Resource
    private BsSalesmanService salesmanService;

    @Resource
    private BsMerMapper merMapper;

    @Resource
    private SecUserService userService;

    @Resource
    private BsMerIndustryService merIndustryService;

    @Resource
    private BsMerContractService merContractService;

    @Resource
    private BsMerRegionService merRegionService;

    @Resource
    private BsMerSettleAcctService merSettleAcctService;

    @Resource
    private BsStoreService storeService;

    @Resource
    private BsMerPlatformNoService merPlatformNoService;

    @Resource
    private BsMerPlatformNoRateService merPlatformNoRateService;

    @Resource
    private BsMerContractRecordMsgService merContractRecordMsgService;

    @Resource
    private BsMerRateService merRateService;

    @Resource
    private BsMerAttachService merAttachService;

    @Resource
    private BsMerPlatformTermService merPlatformTermService;

    @Resource
    private CommonService commonService;

    @Override
    public void editMer(BsMer mer) {
        if (mer.getId() == null) {
            mer.setCreateTime(new Date());
            mer.setUpdateTime(new Date());
            merMapper.insert(mer);
        } else {
            mer.setUpdateTime(new Date());
            merMapper.updateByPrimaryKey(mer);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public Long configMerAccount(MerAccountModel merAccount) {
        if (userService.findByLoginName(merAccount.getLoginName()) != null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "账户已存在，请更换");
        }

        // 查询业务员信息
        BsSalesman salesman = salesmanService.getSalesman(merAccount.getSalesmanId());
        if (salesman == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的业务员");
        }

        // 生成商户号
        Long merNo = Long.parseLong(DateUtil.format(new Date(), DateUtil.YMDHMS) + IDGenerator.nextId(5));

        BsMer mer = new BsMer();
        mer.setCompanyId(salesman.getCompanyId());
        mer.setCompanyName(salesman.getCompanyName());
        mer.setAgentId(salesman.getAgentId());
        mer.setAgentName(salesman.getAgentName());
        mer.setSalesmanId(salesman.getId());
        mer.setSalesmanName(salesman.getName());
        mer.setCreateSalesmanId(salesman.getId());
        mer.setCreateSalesmanName(salesman.getName());
        mer.setRegName(merAccount.getName());
        mer.setRegPhone(merAccount.getPhone());
        mer.setRegEmail(merAccount.getEmail());
        mer.setMerNo(String.valueOf(merNo));
        mer.setCompleteStatus(MerCompleteStatusEnum.status2.getNumber());
        mer.setMerStatus(MerStatusEnum.status2.getNumber());
        editMer(mer);

        SecUser user = new SecUser();
        user.setUserName(merAccount.getName());
        user.setTelephone(merAccount.getPhone());
        user.setLoginName(merAccount.getLoginName());
        user.setPassword(MD5Util.MD5Encode("123456", "utf-8").toUpperCase());
        user.setAdminFlag(1);
        user.setRoleId(SecUserRoleIdEnum.roleId5.getRoleId());
        user.setObjectType(SecUserTypeEnum.type4.getNumber());
        user.setObjectId(mer.getId());
        user.setStatus(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userService.addUser(user);

        return mer.getId();
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void configMerBasis(BsMer mer, BsMerIndustry merIndustry, BsMerRegion merRegion) throws IOException {
        editMer(mer);

        // 行业
        BsMerIndustry industry = merIndustryService.getMerIndustryByMerId(mer.getId());
        if (industry != null) {
            merIndustry.setId(industry.getId());
            merIndustry.setCreateTime(industry.getCreateTime());
            merIndustry.setStatus(industry.getStatus());
        }
        merIndustry.setMerId(mer.getId());
        merIndustryService.editMerIndustry(merIndustry);

        // 区域
        BsMerRegion region = merRegionService.getMerRegionByMerId(mer.getId());
        if (region != null) {
            merRegion.setId(region.getId());
            merRegion.setCreateTime(region.getCreateTime());
            merRegion.setStatus(region.getStatus());
        }
        merRegion.setMerId(mer.getId());
        merRegionService.editMerRegion(merRegion);

        BsMerAttach attach;
        /*******  营业执照 *******/
        List<BsMerAttach> blisUrl = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type1.getNumber());
        if (blisUrl.size() > 0) {
            for (BsMerAttach merAttach : blisUrl) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        if (StringUtils.isNotBlank(mer.getBlisUrl())) {
            attach = new BsMerAttach();
            attach.setMerId(mer.getId());
            attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
            attach.setType(MerAttachType.type1.getNumber());
            attach.setFileUrl(mer.getBlisUrl());
            merAttachService.editMerAttach(attach);
        }
        /*******  营业执照 *******/

        /*******  法人身份证正面 *******/
        // 处理法人身份证正面
        List<BsMerAttach> larIdCardPortraitImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type2.getNumber());
        if (larIdCardPortraitImg.size() > 0) {
            for (BsMerAttach merAttach : larIdCardPortraitImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        attach = new BsMerAttach();
        attach.setMerId(mer.getId());
        attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
        attach.setType(MerAttachType.type2.getNumber());
        attach.setFileUrl(mer.getLarIdCardPortraitImg());
        merAttachService.editMerAttach(attach);
        /*******  法人身份证正面  *******/

        /*******  法人身份证反面  *******/
        // 处理法人身份证反面
        List<BsMerAttach> larIdCardNationalEmblemImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type3.getNumber());
        if (larIdCardNationalEmblemImg.size() > 0) {
            for (BsMerAttach merAttach : larIdCardNationalEmblemImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        attach = new BsMerAttach();
        attach.setMerId(mer.getId());
        attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
        attach.setType(MerAttachType.type3.getNumber());
        attach.setFileUrl(mer.getLarIdCardNationalEmblemImg());
        merAttachService.editMerAttach(attach);
        /*******  法人身份证反面  *******/
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void configMerSettlement(BsMerSettleAcct merSettleAcct) {
        // 查询商户
        BsMer mer = getMer(merSettleAcct.getMerId());
        if (mer == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到商户");
        }
        mer.setCompleteStatus(MerCompleteStatusEnum.status4.getNumber());
        editMer(mer);

        for (BsMerRate merRate : merSettleAcct.getMerRateList()) {
            if (merRate.getRateTypeCode() == null || merRate.getRatePct() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询费率
            BsMerRate rate = merRateService.getDetail(mer.getId(), merRate.getRateTypeCode());
            if (rate != null) {
                rate.setRatePct(merRate.getRatePct());
                merRateService.editMerRate(rate);
            } else {
                merRate.setMerId(mer.getId());
                merRate.setMerNo(mer.getMerNo());
                SecDictionary platformNoRate = commonService.mappingSysCode("MER_PLATFORM_NO_RATE", merRate.getRateTypeCode().toString());
                merRate.setRateTypeName(platformNoRate.getCodeName());
                merRate.setRateSaasTypeCode(platformNoRate.getExt2());
                merRate.setExt1(platformNoRate.getExt3());
                merRateService.editMerRate(merRate);
            }
        }

        // 查询商户结算信息
        BsMerSettleAcct settle = merSettleAcctService.getMerSettleByMerId(merSettleAcct.getMerId());
        if (settle != null) {
            merSettleAcct.setId(settle.getId());
            merSettleAcct.setStatus(settle.getStatus());
            merSettleAcct.setCreateTime(settle.getCreateTime());
        }

        BsMerAttach attach;


        /*******  银行卡照片 *******/
        // 处理法人身份证正面
        List<BsMerAttach> bankCardImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type7.getNumber());
        if (bankCardImg.size() > 0) {
            for (BsMerAttach merAttach : bankCardImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }

        if (StringUtils.isNotBlank(merSettleAcct.getBankCardImg())){
            attach = new BsMerAttach();
            attach.setMerId(mer.getId());
            attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
            attach.setType(MerAttachType.type7.getNumber());
            attach.setFileUrl(merSettleAcct.getBankCardImg());
            merAttachService.editMerAttach(attach);
        }
        /*******  银行卡照片  *******/

        /*******  开户许可证照片 *******/
        // 处理法人身份证正面
        List<BsMerAttach> openningBankLicense = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type8.getNumber());
        if (openningBankLicense.size() > 0) {
            for (BsMerAttach merAttach : openningBankLicense) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        if (StringUtils.isNotBlank(merSettleAcct.getOpenningBankLicenseUrl())){
            attach = new BsMerAttach();
            attach.setMerId(mer.getId());
            attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
            attach.setType(MerAttachType.type8.getNumber());
            attach.setFileUrl(merSettleAcct.getOpenningBankLicenseUrl());
            merAttachService.editMerAttach(attach);
        }
        /*******  开户许可证照片  *******/

        /*******  结算委托书 *******/
        // 处理法人身份证正面
        List<BsMerAttach> commissionImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type99.getNumber());
        if (commissionImg.size() > 0) {
            for (BsMerAttach merAttach : commissionImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        if (merSettleAcct.getSettleType().equals(MerSettleType.status3.getNumber()) && StringUtils.isNotBlank(merSettleAcct.getCommissionImg())){
            attach = new BsMerAttach();
            attach.setMerId(mer.getId());
            attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
            attach.setType(MerAttachType.type99.getNumber());
            attach.setFileUrl(merSettleAcct.getCommissionImg());
            merAttachService.editMerAttach(attach);
        }
        /*******  结算委托书  *******/

        /*******  结算人身份证人像面 *******/
        // 处理法人身份证正面
        List<BsMerAttach> larIdCardPortraitImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type9.getNumber());
        if (larIdCardPortraitImg.size() > 0) {
            for (BsMerAttach merAttach : larIdCardPortraitImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        attach = new BsMerAttach();
        attach.setMerId(mer.getId());
        attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
        attach.setType(MerAttachType.type9.getNumber());
        attach.setFileUrl(merSettleAcct.getSettleIdCardPortraitImg());
        merAttachService.editMerAttach(attach);
        /*******  结算人身份证人像面  *******/

        /*******  结算人身份证国徽面  *******/
        // 处理法人身份证反面
        List<BsMerAttach> larIdCardNationalEmblemImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type10.getNumber());
        if (larIdCardNationalEmblemImg.size() > 0) {
            for (BsMerAttach merAttach : larIdCardNationalEmblemImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        attach = new BsMerAttach();
        attach.setMerId(mer.getId());
        attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
        attach.setType(MerAttachType.type10.getNumber());
        attach.setFileUrl(merSettleAcct.getSettleIdCardNationalEmblemImg());
        merAttachService.editMerAttach(attach);
        /*******  结算人身份证国徽面  *******/

        merSettleAcctService.editMerSettleAcct(merSettleAcct);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void configMerStore(StoreModel storeModel) throws Exception {
        BsMer mer = getMer(storeModel.getMerId());
        if (mer == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到商户信息");
        }
        mer.setCompleteStatus(MerCompleteStatusEnum.status1.getNumber());
        mer.setMerStatus(MerStatusEnum.status3.getNumber());
        editMer(mer);

        storeModel.setAloneSettle(false);
        storeModel.setStatus(StoreStatusEnum.status3.getNumber());
        storeService.configStore(storeModel);


        BsMerAttach attach;
        /**** 场所门头照片 ****/
        List<BsMerAttach> doorHeadImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type4.getNumber());
        if (doorHeadImg.size() > 0) {
            for (BsMerAttach merAttach : doorHeadImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        attach = new BsMerAttach();
        attach.setMerId(mer.getId());
        attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
        attach.setType(MerAttachType.type4.getNumber());
        attach.setFileUrl(storeModel.getDoorHeadImg());
        merAttachService.editMerAttach(attach);
        /**** 场所门头照片 ****/

        /**** 场所内部照片 ****/
        List<BsMerAttach> internalImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type5.getNumber());
        if (internalImg.size() > 0) {
            for (BsMerAttach merAttach : internalImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        attach = new BsMerAttach();
        attach.setMerId(mer.getId());
        attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
        attach.setType(MerAttachType.type5.getNumber());
        attach.setFileUrl(storeModel.getInternalImg());
        merAttachService.editMerAttach(attach);
        /**** 场所内部照片 ****/

        /**** 场所收银台照片 ****/
        List<BsMerAttach> cashRegisterImg = merAttachService.getAttachListByMer(mer.getId(), PlatformTypeEnum.type0, MerAttachType.type6.getNumber());
        if (cashRegisterImg.size() > 0) {
            for (BsMerAttach merAttach : cashRegisterImg) {
                merAttach.setStatus(0);
                merAttachService.editMerAttach(merAttach);
            }
        }
        attach = new BsMerAttach();
        attach.setMerId(mer.getId());
        attach.setPlatformType(PlatformTypeEnum.type0.getNumber());
        attach.setType(MerAttachType.type6.getNumber());
        attach.setFileUrl(storeModel.getCashRegisterImg());
        merAttachService.editMerAttach(attach);
        /**** 场所收银台照片 ****/

        // 进件
        PlatformTypeEnum platformType = null;
        if (storeModel.getMerPlatformType() != null) {
            platformType = PlatformTypeEnum.getDataByNumber(storeModel.getMerPlatformType());
        }
        merContractService.addMerContract(mer.getId(), platformType);
    }

    @Override
    @Cached(cacheType = CacheType.REMOTE, name="MER:", key = "#merId")
    public BsMer getMer(Long merId) {
        return merMapper.selectByPrimaryKey(merId);
    }

    @Override
    public BsMer getMer(String merNo) {
        BsMerExample example = new BsMerExample();
        example.createCriteria().andMerNoEqualTo(merNo).andMerStatusEqualTo(MerStatusEnum.status1.getNumber());
        List<BsMer> merList = merMapper.selectByExample(example);
        if (merList.size() > 0) {
            return merList.get(0);
        }
        return null;
    }

    @Override
    public void reconsider(Long merId) throws Exception {
        merContractService.reconsider(merId);
    }

    @Override
    public List<BsMer> getMerList(Map<String, Object> param) {
        BsMerExample example = new BsMerExample();
        BsMerExample.Criteria criteria = example.createCriteria().andMerStatusNotEqualTo(MerStatusEnum.status0.getNumber());

        if (MapUtils.getLong(param, "agentId") != null) {
            criteria.andAgentIdEqualTo(MapUtils.getLong(param, "agentId"));
        }

        if (MapUtils.getLong(param, "salesmanId") != null) {
            criteria.andSalesmanIdEqualTo(MapUtils.getLong(param, "salesmanId"));
        }

        if (MapUtils.getInteger(param, "merStatus") != null) {
            criteria.andMerStatusEqualTo(MapUtils.getInteger(param, "merStatus"));
        }

        if (MapUtils.getInteger(param, "merType") != null) {
            criteria.andMerTypeEqualTo(MapUtils.getInteger(param, "merType"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merName"))) {
            criteria.andMerNameLike("%"+MapUtils.getString(param, "merName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merNo"))) {
            criteria.andMerNoEqualTo(MapUtils.getString(param, "merNo"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "regName"))) {
            criteria.andRegNameLike("%"+MapUtils.getString(param, "regName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "regPhone"))) {
            criteria.andRegPhoneEqualTo(MapUtils.getString(param, "regPhone"));
        }

        example.setOrderByClause("create_time desc");
        return merMapper.selectByExample(example);
    }

    @Override
    public MerBasisModel getMerDetail(Long merId) {
        BsMer bsMer = getMer(merId);
        if (bsMer == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到商户");
        }
        MerBasisModel basisModel = new MerBasisModel();
        BeanUtils.copyProperties(bsMer, basisModel);

        // 查询账户
        SecUser account = userService.getMainAccount(SecUserTypeEnum.type4.getNumber(), bsMer.getId());
        if (account != null) {
            account.setPassword(null);
            basisModel.setAccount(account);
        }
        basisModel.setMerRegion(merRegionService.getMerRegionByMerId(merId));
        basisModel.setMerIndustry(merIndustryService.getMerIndustryByMerId(merId));

        // 结算信息
        BsMerSettleAcct settle = merSettleAcctService.getMerSettleByMerId(merId);
        if (settle != null) {
            settle.setMerRateList(merRateService.getRateListByMerId(merId));
            basisModel.setMerSettleAcct(settle);
        }

        // 查询门店
        List<BsStore> store = storeService.getStoreByMerId(merId, StoreCreateTypeEnum.type1);
        if (store.size() > 0) {
            basisModel.setStoreModel(storeService.getStoreDetail(store.get(0).getId()));
        }

        return basisModel;
    }

    @Override
    public void laKaLaMerContractNotify(Long merId, MerStatusEnum merStatus, JSONObject data) {
        // 查询商户
        BsMer mer = getMer(merId);
        if (mer != null) {
            mer.setMerStatus(merStatus.getNumber());
            mer.setCompleteStatus(MerCompleteStatusEnum.status1.getNumber());

            List<BsStore> storeList = storeService.getStoreByMerId(mer.getId(), StoreCreateTypeEnum.type1);


            // 审核通过
            if (merStatus.getNumber().equals(MerStatusEnum.status1.getNumber())) {

                // 查询平台
                BsMerPlatformNo merPlatformNo = merPlatformNoService.getPlatformNo(merId, PlatformTypeEnum.type1);
                if (merPlatformNo != null) {
                    merPlatformNo.setPlatformNo(data.getString("merInnerNo"));
                    merPlatformNo.setCupNo(data.getString("merCupNo"));
                    merPlatformNoService.editMerPlatformNo(merPlatformNo);

                    // 平台费率
                    List<BsMerPlatformNoRate> rateList = merPlatformNoRateService.getMerPlatformNoRateList(merId, PlatformTypeEnum.type1.getNumber());
                    for (BsMerPlatformNoRate rate : rateList) {
                        rate.setPlatformNo(merPlatformNo.getPlatformNo());
                        rate.setCupNo(merPlatformNo.getCupNo());
                        merPlatformNoRateService.editRate(rate);
                    }
                }

                // 终端号
                JSONArray termDatas = data.getJSONArray("termDatas");
                if (termDatas != null && termDatas.size() > 0) {
                    for (Object termObject : termDatas) {
                        JSONObject term = (JSONObject) termObject;
                        BsMerPlatformTerm merPlatformTerm = new BsMerPlatformTerm();
                        merPlatformTerm.setMerPlatformNoId(merPlatformNo.getId());
                        merPlatformTerm.setMerId(merPlatformNo.getMerId());
                        merPlatformTerm.setActiveNo(term.getString("activeNo"));
                        merPlatformTerm.setBusiTypeCode(term.getString("busiTypeCode"));
                        merPlatformTerm.setBusiTypeName(term.getString("busiTypeName"));
                        merPlatformTerm.setProductCode(term.getString("productCode"));
                        merPlatformTerm.setProductName(term.getString("productName"));
                        merPlatformTerm.setShopId(term.getString("shopId"));
                        merPlatformTerm.setTermId(term.getString("termId"));
                        merPlatformTerm.setTermNo(term.getString("termNo"));
                        merPlatformTermService.editMerPlatformTerm(merPlatformTerm);
                    }
                }
            }

            if (merStatus.getNumber().equals(MerStatusEnum.status4.getNumber())) {
                mer.setCompleteStatus(MerCompleteStatusEnum.status2.getNumber());
            }
            editMer(mer);
        }

    }

    @Override
    public long countMerSum(Map<String, Object> param) {
        BsMerExample example = new BsMerExample();
        BsMerExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(param, "salesmanId") != null) {
            criteria.andSalesmanIdEqualTo(MapUtils.getLong(param, "salesmanId"));
        }

        if (MapUtils.getInteger(param, "completeStatus") != null) {
            criteria.andCompleteStatusEqualTo(MapUtils.getInteger(param, "completeStatus"));
        }

        if (MapUtils.getInteger(param, "merStatus") != null) {
            criteria.andMerStatusEqualTo(MapUtils.getInteger(param, "merStatus"));
        }

        return merMapper.countByExample(example);
    }
}
