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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.db.entity.MchAppApplyment;
import com.jeequan.jeepay.db.entity.MchAppEntity;
import com.jeequan.jeepay.db.entity.MchApplyment;
import com.jeequan.jeepay.service.mapper.MchAppApplymentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 *
 */
@Slf4j
@Service
public class MchAppApplymentService extends ServiceImpl<MchAppApplymentMapper, MchAppApplyment> implements IService<MchAppApplyment> {

    @Autowired
    private MchAppService mchAppService;
    @Autowired
    private MchAppApplymentMapper mchAppApplymentMapper;

    public IPage<MchAppApplyment> appApplymentsInquire(Page<MchAppApplyment> page, MchAppApplyment queryObject) {
        return mchAppApplymentMapper.appApplymentInquire(page, queryObject);
    }

    public boolean appApplymentRevamp(MchAppApplyment mchAppApplyment) {
        LambdaUpdateWrapper<MchAppApplyment> condition = Wrappers.lambdaUpdate();
        condition.eq(!ObjectUtils.isEmpty(mchAppApplyment.getMchNo()), MchAppApplyment::getMchNo, mchAppApplyment.getMchNo());
        condition.eq(!ObjectUtils.isEmpty(mchAppApplyment.getMchAppId()), MchAppApplyment::getMchAppId, mchAppApplyment.getMchAppId());
        condition.eq(!ObjectUtils.isEmpty(mchAppApplyment.getMchApplyId()), MchAppApplyment::getMchApplyId, mchAppApplyment.getMchApplyId());

        condition.set(MchAppApplyment::getWxWeight, mchAppApplyment.getWxWeight());
        condition.set(MchAppApplyment::getAliWeight, mchAppApplyment.getAliWeight());
        condition.set(MchAppApplyment::getYsfWeight, mchAppApplyment.getYsfWeight());
        condition.set(MchAppApplyment::getStatus, mchAppApplyment.getStatus());
        condition.set(MchAppApplyment::getRemark, mchAppApplyment.getRemark());
        condition.set(MchAppApplyment::getUpdatedAt, new Date());
        return mchAppApplymentMapper.update(mchAppApplyment, condition) > 0;
    }

    /**
     * 商户完成进件后，将商户关联进自动关联的应用商户池
     *
     * @param tbMchApplyment    商户信息
     */
    public void autoInsertMchIntoPool(MchApplyment tbMchApplyment) {
        String mchNo = tbMchApplyment.getMchNo();
        LambdaQueryWrapper<MchAppEntity> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(MchAppEntity::getAppId, tbMchApplyment.getAutoConfigMchAppId());
        MchAppEntity mchAppEntity = mchAppService.getOne(qWrapper);

        if (mchAppEntity == null) {
            log.info("商户{}关联的应用{}信息未获取到，自动绑定失败", tbMchApplyment.getApplyId(), tbMchApplyment.getAutoConfigMchAppId());
        } else if ( mchAppEntity.getPollingBindState() == 1) {
            // 开通商户池功能并开启同步绑定功能时，保存应用池关联关系
            MchAppApplyment bindItem = new MchAppApplyment();
            bindItem.setMchNo(mchNo);
            bindItem.setMchAppId(mchAppEntity.getAppId());
            bindItem.setMchApplyId(tbMchApplyment.getApplyId());
            bindItem.setStatus(1);
            save(bindItem);
            log.info("商户{}关联的应用{}，已自动绑定商户池", tbMchApplyment.getApplyId(), tbMchApplyment.getAutoConfigMchAppId());
        } else {
            log.info("商户{}关联的应用{}未开通轮询或未开启自动绑定", tbMchApplyment.getApplyId(), tbMchApplyment.getAutoConfigMchAppId());
        }
    }
}
