package com.hfkj.service.impl;

import com.hfkj.channel.saas.SaasMerService;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.dao.BsMerSettleAcctApplyMapper;
import com.hfkj.entity.*;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.BsAuditService;
import com.hfkj.service.BsMerSettleAcctApplyService;
import com.hfkj.sysenum.AuditStatusEnum;
import com.hfkj.sysenum.AuditVocationalWorkType;
import com.hfkj.sysenum.MerSettleAcctApplyStatus;
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: BsMerSettleAcctApplyServiceImpl
 * @author: HuRui
 * @date: 2023/5/6
 **/
@Service("merSettleAcctApplyService")
public class BsMerSettleAcctApplyServiceImpl implements BsMerSettleAcctApplyService {

    @Resource
    private BsMerSettleAcctApplyMapper merSettleAcctApplyMapper;
    @Resource
    private BsAuditService auditService;
    @Resource
    private SaasMerService saasMerService;

    @Resource
    private UserCenter userCenter;

    @Override
    public void editData(BsMerSettleAcctApply merSettleAcctApply) {
        if (merSettleAcctApply.getId() == null) {
            merSettleAcctApply.setCreateTime(new Date());
            merSettleAcctApply.setUpdateTime(new Date());
            merSettleAcctApplyMapper.insert(merSettleAcctApply);
        } else {
            merSettleAcctApply.setUpdateTime(new Date());
            merSettleAcctApplyMapper.updateByPrimaryKey(merSettleAcctApply);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void createApply(BsMerSettleAcctApply merSettleAcctApply) {
        merSettleAcctApply.setStatus(MerSettleAcctApplyStatus.status2.getNumber());
        editData(merSettleAcctApply);

        UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
        if (userInfoModel == null || userInfoModel.getSalesman() == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "该角色暂无权限");
        }

        BsAudit audit = new BsAudit();
        audit.setAuditNo(System.currentTimeMillis()+"");
        audit.setVocationalWorkType(AuditVocationalWorkType.type1.getNumber());
        audit.setVocationalWorkTypeName(AuditVocationalWorkType.type1.getName());
        audit.setAuditObjectId(merSettleAcctApply.getId());
        audit.setAuditObjectName(merSettleAcctApply.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 BsMerSettleAcctApply applyUpdateSettleAcct(BsMerSettleAcctApply merSettleAcctApply) {
        editData(merSettleAcctApply);

        saasMerService.updateSettleApply(merSettleAcctApply.getId());
        return null;
    }

    @Override
    public BsMerSettleAcctApply getLastApplyByMer(Long merId) {
        BsMerSettleAcctApplyExample example = new BsMerSettleAcctApplyExample();
        example.createCriteria().andMerIdEqualTo(merId).andStatusNotEqualTo(0);
        example.setOrderByClause("create_time desc");
        List<BsMerSettleAcctApply> list = merSettleAcctApplyMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsMerSettleAcctApply getApplyById(Long id) {
        return merSettleAcctApplyMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsMerSettleAcctApply> getApplyListByMer(Long merId) {
        BsMerSettleAcctApplyExample example = new BsMerSettleAcctApplyExample();
        example.createCriteria().andMerIdEqualTo(merId);
        example.setOrderByClause("create_time desc");
        return merSettleAcctApplyMapper.selectByExample(example);
    }

    @Override
    public List<BsMerSettleAcctApply> getApplyList(Map<String, Object> param) {
        BsMerSettleAcctApplyExample example = new BsMerSettleAcctApplyExample();
        BsMerSettleAcctApplyExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

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

        if (MapUtils.getBoolean(param, "applyNoIsNotNull") != null) {
            criteria.andApplyNoIsNotNull();
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "applyNo"))) {
            criteria.andApplyNoEqualTo(MapUtils.getString(param, "applyNo"));
        }

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

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

}
