package com.hfkj.service.impl;

import com.hfkj.channel.saas.SaasMerService;
import com.hfkj.common.security.UserCenter;
import com.hfkj.dao.BsMerRateApplyDetailMapper;
import com.hfkj.dao.BsMerRateApplyMapper;
import com.hfkj.entity.*;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.BsAuditService;
import com.hfkj.service.BsMerRateApplyService;
import com.hfkj.sysenum.AuditStatusEnum;
import com.hfkj.sysenum.AuditVocationalWorkType;
import com.hfkj.sysenum.MerRateApplyStatusEnum;
import org.apache.commons.collections4.MapUtils;
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: BsMerRateApplyServiceImpl
 * @author: HuRui
 * @date: 2023/7/25
 **/
@Service("merRateApplyService")
public class BsMerRateApplyServiceImpl implements BsMerRateApplyService {

    @Resource
    private BsMerRateApplyMapper merRateApplyMapper;
    @Resource
    private BsMerRateApplyDetailMapper merRateApplyDetailMapper;
    @Resource
    private BsAuditService auditService;
    @Resource
    private SaasMerService saasMerService;
    @Resource
    private UserCenter userCenter;

    @Override
    public void editData(BsMerRateApply merRateApply) {
        if (merRateApply.getId() == null) {
            merRateApply.setCreateTime(new Date());
            merRateApply.setUpdateTime(new Date());
            merRateApplyMapper.insert(merRateApply);
        } else {
            merRateApply.setUpdateTime(new Date());
            merRateApplyMapper.updateByPrimaryKey(merRateApply);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void rateApply(BsMerRateApply merRateApply, List<BsMerRateApplyDetail> rateApplyDetailList) {
        editData(merRateApply);

        for (BsMerRateApplyDetail merRateApplyDetail : rateApplyDetailList) {
            merRateApplyDetail.setMerRateApplyId(merRateApply.getId());
            merRateApplyDetail.setStatus(1);
            merRateApplyDetail.setCreateTime(new Date());
            merRateApplyDetail.setUpdateTime(new Date());
            merRateApplyDetailMapper.insert(merRateApplyDetail);
        }

        UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);

        BsAudit audit = new BsAudit();
        audit.setAuditNo(System.currentTimeMillis()+"");
        audit.setVocationalWorkType(AuditVocationalWorkType.type2.getNumber());
        audit.setVocationalWorkTypeName(AuditVocationalWorkType.type2.getName());
        audit.setAuditObjectId(merRateApply.getId());
        audit.setAuditObjectName(merRateApply.getMerName());
        audit.setOpUserId(userInfoModel.getSecUser().getId());
        audit.setOpUserName(userInfoModel.getSecUser().getUserName());
        audit.setReviewedUserId(userInfoModel.getSalesman().getAgentId());
        audit.setReviewedUserName(userInfoModel.getSalesman().getAgentName());
        audit.setStatus(AuditStatusEnum.status1.getNumber());
        auditService.createAudit(audit);
    }

    @Override
    public BsMerRateApply getRateApplyById(Long id) {
        return merRateApplyMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsMerRateApplyDetail getRateApplyDetail(Long rateApplyId, Integer rateTypeCode) {
        BsMerRateApplyDetailExample example = new BsMerRateApplyDetailExample();
        example.createCriteria().andMerRateApplyIdEqualTo(rateApplyId).andRateTypeCodeEqualTo(rateTypeCode);
        example.setOrderByClause("create_time desc");
        List<BsMerRateApplyDetail> list = merRateApplyDetailMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsMerRateApply> getRateApplyList(Map<String, Object> param) {
        BsMerRateApplyExample example = new BsMerRateApplyExample();
        BsMerRateApplyExample.Criteria criteria = example.createCriteria()
                .andStatusNotEqualTo(MerRateApplyStatusEnum.status0.getNumber());

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

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

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

    @Override
    public List<BsMerRateApplyDetail> getRateApplyDetailListByApply(Long rateApplyId) {
        BsMerRateApplyDetailExample example = new BsMerRateApplyDetailExample();
        example.createCriteria().andMerRateApplyIdEqualTo(rateApplyId);
        return merRateApplyDetailMapper.selectByExample(example);
    }

    @Override
    public void approveCallback(BsMerRateApply merRateApply) {
        editData(merRateApply);
        saasMerService.updateFeeApply(merRateApply.getId());
    }
}
