package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.utils.SeqKit;
import com.jeequan.jeepay.db.entity.*;
import com.jeequan.jeepay.service.mapper.DivisionSubjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2024-04-03
 */
@Service
public class DivisionSubjectService extends ServiceImpl<DivisionSubjectMapper, DivisionSubject> {

    @Autowired
    private MchStoreService mchStoreService;

    @Autowired
    private MchApplymentService mchApplymentService;

    @Autowired
    private IsvInfoService isvInfoService;

    @Autowired
    private PayInterfaceConfigService payInterfaceConfigService;
    
    /**
     * 分页查询
     * @param page
     * @param entity
     * @return
     */
    public IPage<DivisionSubject> pageList(Page<DivisionSubject> page, DivisionSubject entity) {
        Date[] dateRange = entity.buildQueryDateRange();
        return baseMapper.pageList(page,entity,dateRange);
    }

    /**
     * 新增分账分出方
     * @param subject
     */
    public void addDivisionSave(DivisionSubject subject) {
        subject.preDivCheck();
        checkInfo(subject);
        save(subject);
    }

    /**
     * 新增分账接收方
     * @param subject
     */
    public void addReceiverSave(DivisionSubject subject) {
        subject.preRecCheck();
        checkInfo(subject);
        save(subject);
    }

    /**
     * 校验商户类型的分账方信息
     * @param subject
     */
    public void checkInfo(DivisionSubject subject) {
        IsvInfoEntity isvInfo = isvInfoService.getById(subject.getIsvNo());
        Assert.notNull(isvInfo,"支付渠道信息异常");
        PayInterfaceConfig config = payInterfaceConfigService.getByInfoIdAndIfCode(CS.SYS_ROLE_TYPE.ISV,subject.getIsvNo(),subject.getIfCode());
        Assert.notNull(config,"渠道支付参数配置异常");
        config.preCheckDivision();
        if(DivisionSubject.USER_TYPE_MERCHANT == subject.getUserType()){
            MchStore store = mchStoreService.getById(subject.getDivisionNo());
            Assert.notNull(store,"门店信息异常");
            MchApplyment applyment = mchApplymentService.getById(store.getMchApplyId());
            Assert.notNull(applyment,"商户信息异常");
            Assert.notNull(applyment.isPay(),"商户还未入网成功");
            Assert.isTrue(subject.getIfCode().equals(applyment.getIfCode()),"当前商户所属通道和设置的通道不匹配");
            Assert.isTrue(subject.getIsvNo().equals(applyment.getIsvNo()),"当前商户所属渠道和设置的渠道不匹配");
            subject.setChannelDivisionNo(applyment.getChannelMchNo());
            subject.setDivisionNo(store.getStoreId());
        }
    }

    /**
     * 根据任务id和分账方商户号获取分账接收方信息
     * @param taskId
     * @param divisionNo
     * @return
     */
    public List<DivisionSubject> listByTaskAndDivisionNo(String taskId, String divisionNo) {
        return baseMapper.listByTaskAndDivisionNo(taskId,divisionNo);
    }
}
