package com.jeequan.jeepay.service.impl;

import cn.hutool.core.collection.CollUtil;
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.exception.BizException;
import com.jeequan.jeepay.db.entity.MchAppEntity;
import com.jeequan.jeepay.db.entity.MchDivisionReceiver;
import com.jeequan.jeepay.db.entity.MchDivisionReceiverGroup;
import com.jeequan.jeepay.db.entity.RefundOrder;
import com.jeequan.jeepay.service.mapper.MchDivisionReceiverMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户分账接收者账号绑定关系表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2021-08-19
 */
@Service
public class MchDivisionReceiverService extends ServiceImpl<MchDivisionReceiverMapper, MchDivisionReceiver> {

    @Autowired private MchDivisionReceiverGroupService mchDivisionReceiverGroupService;
    @Autowired private MchAppService mchAppService;

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

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

        if(queryObject.getReceiverId() != null){
            condition.eq(MchDivisionReceiver::getReceiverId, queryObject.getReceiverId());
        }

        if(StringUtils.isNotEmpty(queryObject.getReceiverAlias())){
            condition.like(MchDivisionReceiver::getReceiverAlias, queryObject.getReceiverAlias());
        }

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

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

        if(queryObject.getState() != null){
            condition.eq(MchDivisionReceiver::getState, queryObject.getState());
        }

        condition.eq(StringUtils.isNotEmpty(queryObject.getIfCode()), MchDivisionReceiver::getIfCode, queryObject.getIfCode());

        //应用名称和id检索二合一
        if (StringUtils.isNotEmpty(queryObject.getAppId())){
            //传入值作为应用名称进行匹配如果匹配不上则为应用id
            List<MchAppEntity> mchAppEntityList = mchAppService.list(MchAppEntity.gw().eq(MchAppEntity::getAppName, queryObject.getAppId()));
            List<String> collect = mchAppEntityList.stream().map(MchAppEntity::getAppId).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(collect)){
                condition.in(MchDivisionReceiver::getAppId,collect);
            }else {
                condition.eq(MchDivisionReceiver::getAppId,queryObject.getAppId());
            }
        }

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

        return page(iPage, condition);
    }

    public void updateReceiver(MchDivisionReceiver reqReceiver) {
        if (StringUtils.isEmpty(reqReceiver.getMchNo())) {
            throw new BizException("商户号不能为空");
        }
        MchDivisionReceiver record = new MchDivisionReceiver();
        record.setReceiverAlias(reqReceiver.getReceiverAlias());
        record.setReceiverGroupId(reqReceiver.getReceiverGroupId());
        record.setState(reqReceiver.getState());

        // 改为真实比例
        if(reqReceiver.getDivisionProfit() != null){
            record.setDivisionProfit(reqReceiver.getDivisionProfit().divide(new BigDecimal(100)));
        }

        if(record.getReceiverGroupId() != null){
            MchDivisionReceiverGroup groupRecord = mchDivisionReceiverGroupService.findByIdAndMchNo(record.getReceiverGroupId(), reqReceiver.getMchNo());
            if (groupRecord == null) {
                throw new BizException("账号组不存在或当前组不在该商户下");
            }
            record.setReceiverGroupId(groupRecord.getReceiverGroupId());
            record.setReceiverGroupName(groupRecord.getReceiverGroupName());
        }

        LambdaUpdateWrapper<MchDivisionReceiver> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MchDivisionReceiver::getReceiverId, reqReceiver.getReceiverId());
        updateWrapper.eq(MchDivisionReceiver::getMchNo, reqReceiver.getMchNo());
        update(record, updateWrapper);
    }
}
