package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.db.entity.MchDivisionReceiver;
import com.jeequan.jeepay.db.entity.MchDivisionReceiverGroup;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.service.mapper.MchDivisionReceiverGroupMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 分账账号组 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2021-08-23
 */
@Service
public class MchDivisionReceiverGroupService extends ServiceImpl<MchDivisionReceiverGroupMapper, MchDivisionReceiverGroup> {

    @Autowired private MchDivisionReceiverService mchDivisionReceiverService;
    @Autowired private MchInfoService mchInfoService;

    /** 根据ID和商户号查询 **/
    public MchDivisionReceiverGroup findByIdAndMchNo(Long groupId, String mchNo){
        return getOne(MchDivisionReceiverGroup.gw().eq(MchDivisionReceiverGroup::getReceiverGroupId, groupId).eq(MchDivisionReceiverGroup::getMchNo, mchNo));
    }

    public IPage<MchDivisionReceiverGroup> listByPage(IPage iPage, MchDivisionReceiverGroup queryObject) {

        LambdaQueryWrapper<MchDivisionReceiverGroup> condition = MchDivisionReceiverGroup.gw();
        if (StringUtils.isNotEmpty(queryObject.getMchNo())) {
            condition.eq(MchDivisionReceiverGroup::getMchNo, queryObject.getMchNo());
        }

        if(StringUtils.isNotEmpty(queryObject.getReceiverGroupName())){
            condition.like(MchDivisionReceiverGroup::getReceiverGroupName, queryObject.getReceiverGroupName());
        }

        if(queryObject.getReceiverGroupId() != null){
            condition.eq(MchDivisionReceiverGroup::getReceiverGroupId, queryObject.getReceiverGroupId());
        }

        condition.eq(queryObject.getAutoDivisionFlag() != null, MchDivisionReceiverGroup::getAutoDivisionFlag, queryObject.getAutoDivisionFlag());

        //时间倒序
        condition.orderByDesc(MchDivisionReceiverGroup::getCreatedAt);

		return page(iPage, condition);
    }

    public void add(MchDivisionReceiverGroup record) {
        if (StringUtils.isEmpty(record.getMchNo())) {
            throw new BizException("商户号不能为空");
        }
        MchInfo mchInfo = mchInfoService.getById(record.getMchNo());
        if (mchInfo == null) {
            throw new BizException("商户不存在");
        }
        if(save(record)){
            //更新其他组为非默认分账组
            if(record.getAutoDivisionFlag() == CS.YES){
                update(new LambdaUpdateWrapper<MchDivisionReceiverGroup>()
                        .set(MchDivisionReceiverGroup::getAutoDivisionFlag, CS.NO)
                        .eq(MchDivisionReceiverGroup::getMchNo, record.getMchNo())
                        .ne(MchDivisionReceiverGroup::getReceiverGroupId, record.getReceiverGroupId())
                );
            }
        }
    }

    public void updateGroup(MchDivisionReceiverGroup reqRecord) {
        if (StringUtils.isEmpty(reqRecord.getMchNo())) {
            throw new BizException("商户号不能为空");
        }
        MchDivisionReceiverGroup record = new MchDivisionReceiverGroup();
        record.setReceiverGroupName(reqRecord.getReceiverGroupName());
        record.setAutoDivisionFlag(reqRecord.getAutoDivisionFlag());

        LambdaUpdateWrapper<MchDivisionReceiverGroup> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MchDivisionReceiverGroup::getReceiverGroupId, reqRecord.getReceiverGroupId());
        updateWrapper.eq(MchDivisionReceiverGroup::getMchNo, reqRecord.getMchNo());

        if(update(record, updateWrapper)){
            //更新其他组为非默认分账组
            if(record.getAutoDivisionFlag() == CS.YES){
                update(new LambdaUpdateWrapper<MchDivisionReceiverGroup>()
                        .set(MchDivisionReceiverGroup::getAutoDivisionFlag, CS.NO)
                        .eq(MchDivisionReceiverGroup::getMchNo, reqRecord.getMchNo())
                        .ne(MchDivisionReceiverGroup::getReceiverGroupId, reqRecord.getReceiverGroupId())
                );
            }
        }
    }

    public void removeGroup(Long recordId, String mchNo) {
        LambdaQueryWrapper<MchDivisionReceiverGroup> condition = MchDivisionReceiverGroup.gw();
        if (recordId != null) {
            condition.eq(MchDivisionReceiverGroup::getReceiverGroupId, recordId);
        }

        if (StringUtils.isNotEmpty(mchNo)) {
            condition.eq(MchDivisionReceiverGroup::getMchNo, mchNo);
        }

        MchDivisionReceiverGroup record = getOne(condition);
        if (record == null) {
            throw new BizException(ApiCodeEnum.SYS_OPERATION_FAIL_SEARCH);
        }


        if(mchDivisionReceiverService.count(MchDivisionReceiver.gw().eq(MchDivisionReceiver::getReceiverGroupId, recordId)) > 0){
            throw new BizException("该组存在账号，无法删除");
        }
        removeById(recordId);

    }
}
