package com.hfkj.service.rebate.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsRebatePartMerMapper;
import com.hfkj.entity.BsMer;
import com.hfkj.entity.BsRebatePartMer;
import com.hfkj.entity.BsRebatePartMerExample;
import com.hfkj.service.BsMerService;
import com.hfkj.service.rebate.BsRebatePartMerService;
import com.hfkj.sysenum.RebatePartMerStatusEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @className: BsRebatePartMerServiceImpl
 * @author: HuRui
 * @date: 2023/10/12
 **/
@Service("rebatePartMerService")
public class BsRebatePartMerServiceImpl implements BsRebatePartMerService {

    @Resource
    private BsRebatePartMerMapper rebatePartMerMapper;

    @Resource
    private BsMerService merService;

    @Override
    public void editData(BsRebatePartMer rebatePartMer) {
        if (rebatePartMer.getId() == null) {
            rebatePartMer.setCreateTime(new Date());
            rebatePartMer.setUpdateTime(new Date());
            rebatePartMerMapper.insert(rebatePartMer);
        } else {
            rebatePartMer.setUpdateTime(new Date());
            rebatePartMerMapper.updateByPrimaryKey(rebatePartMer);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void addPartMer(List<String> merNoList) {
        for (String merNo : merNoList) {
            // 查询商户
            BsMer mer = merService.getMer(merNo);
            if (mer == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到商户号为：" + merNo + " 的数据");
            }
            // 查询是否已经参与
            BsRebatePartMer partMer = getDetailByMerId(mer.getId());
            if (partMer == null) {
                partMer = new BsRebatePartMer();
                partMer.setCompanyId(mer.getCompanyId());
                partMer.setAgentId(mer.getAgentId());
                partMer.setAgentName(mer.getAgentName());
                partMer.setSalesmanId(mer.getSalesmanId());
                partMer.setSalesmanName(mer.getSalesmanName());
                partMer.setMerId(mer.getId());
                partMer.setMerNo(mer.getMerNo());
                partMer.setMerName(mer.getMerName());
                partMer.setMerAbbreviate(mer.getMerAbbreviate());
                partMer.setStatus(RebatePartMerStatusEnum.status1.getNumber());
                editData(partMer);
            }
        }
    }

    @Override
    public void enable(String merNo) {
        // 查询详情
        BsRebatePartMer partMer = getDetailByMerNo(merNo);
        if (partMer == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
        }
        partMer.setStatus(RebatePartMerStatusEnum.status1.getNumber());
        editData(partMer);
    }

    @Override
    public void disable(String merNo) {
        // 查询详情
        BsRebatePartMer partMer = getDetailByMerNo(merNo);
        if (partMer == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
        }
        partMer.setStatus(RebatePartMerStatusEnum.status2.getNumber());
        editData(partMer);
    }

    @Override
    public BsRebatePartMer getDetailByMerId(Long merId) {
        BsRebatePartMerExample example = new BsRebatePartMerExample();
        example.createCriteria().andMerIdEqualTo(merId).andStatusEqualTo(RebatePartMerStatusEnum.status0.getNumber());;
        List<BsRebatePartMer> list = rebatePartMerMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsRebatePartMer getDetailByMerNo(String merNo) {
        BsRebatePartMerExample example = new BsRebatePartMerExample();
        example.createCriteria().andMerNoEqualTo(merNo).andStatusNotEqualTo(RebatePartMerStatusEnum.status0.getNumber());
        List<BsRebatePartMer> list = rebatePartMerMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsRebatePartMer> getMerList(Map<String, Object> param) {
        BsRebatePartMerExample example = new BsRebatePartMerExample();
        BsRebatePartMerExample.Criteria criteria = example.createCriteria()
                .andStatusNotEqualTo(RebatePartMerStatusEnum.status0.getNumber());

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

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

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

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

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

    @Override
    public List<Map<String,Object>> getNotPartMerList(Map<String, Object> param) {
        return rebatePartMerMapper.queryNotPartMer(param);
    }
}
