package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.db.entity.MchAppEntity;
import com.jeequan.jeepay.db.entity.MchPayPassage;
import com.jeequan.jeepay.db.entity.PayInterfaceDefine;
import com.jeequan.jeepay.service.mapper.MchPayPassageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商户支付通道表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2021-04-27
 */
@Service
public class MchPayPassageService extends ServiceImpl<MchPayPassageMapper, MchPayPassage> {

    @Autowired private MchAppService mchAppService;
    @Autowired private PayInterfaceDefineService payInterfaceDefineService;



    /** 根据应用ID 和 支付方式， 查询出商户可用的支付接口 **/
    public MchPayPassage findMchPayPassage(String mchNo, String appId, String wayCode){

        List<MchPayPassage> list = list(MchPayPassage.gw()
                                    .eq(MchPayPassage::getMchNo, mchNo)
                                    .eq(MchPayPassage::getAppId, appId)
                                    .eq(MchPayPassage::getState, CS.YES)
                                    .eq(MchPayPassage::getWayCode, wayCode)
        );

        if (list.isEmpty()) {
            return null;
        }else { // 返回一个可用通道

            HashMap<String, MchPayPassage> mchPayPassageMap = new HashMap<>();

            for (MchPayPassage mchPayPassage:list) {
                mchPayPassageMap.put(mchPayPassage.getIfCode(), mchPayPassage);
            }
            // 查询ifCode所有接口
            PayInterfaceDefine interfaceDefine = payInterfaceDefineService
                    .getOne(PayInterfaceDefine.gw()
                            .select(PayInterfaceDefine::getIfCode, PayInterfaceDefine::getState)
                            .eq(PayInterfaceDefine::getState, CS.YES)
                            .in(PayInterfaceDefine::getIfCode, mchPayPassageMap.keySet()), false);

            if (interfaceDefine != null) {
                return mchPayPassageMap.get(interfaceDefine.getIfCode());
            }
        }
        return null;
    }

    @Transactional
    public void updateMchPassageState(String appId, String wayCode, String ifCode, Byte state){

        MchAppEntity mchAppEntity = mchAppService.getById(appId);

        // 全部关闭
        update(new LambdaUpdateWrapper<MchPayPassage>()
                .eq(MchPayPassage::getAppId, appId).eq(MchPayPassage::getWayCode, wayCode).set(MchPayPassage::getState, CS.NO));


        // 查询当前数据
        MchPayPassage mchPayPassage = getOne(MchPayPassage.gw().eq(MchPayPassage::getAppId, appId).eq(MchPayPassage::getWayCode, wayCode).eq(MchPayPassage::getIfCode, ifCode));

        if(mchPayPassage != null){
            MchPayPassage updateRecord = new MchPayPassage();
            updateRecord.setId(mchPayPassage.getId());
            updateRecord.setState(state);
            updateById(updateRecord);
        }else{

            // 插入
            MchPayPassage insertRecord = new MchPayPassage();
            insertRecord.setMchNo(mchAppEntity.getMchNo());
            insertRecord.setAppId(appId);
            insertRecord.setIfCode(ifCode);
            insertRecord.setWayCode(wayCode);
            insertRecord.setState(state);
            save(insertRecord);
        }
    }


}
