package cn.pluss.platform.jft.impl;

import cn.pluss.platform.converter.Converter;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.jft.JftBankCardService;
import cn.pluss.platform.jft.JftMercBaseInfoService;
import cn.pluss.platform.jft.JftMerchantRateService;
import cn.pluss.platform.mapper.JftMercBaseInfoMapper;
import cn.pluss.platform.merchant.JftMercPaymentChannelService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.vo.MercInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 缴费通详情表 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2022-06-22
 */
@Service
public class JftMercBaseInfoServiceImpl extends ServiceImpl<JftMercBaseInfoMapper, JftMercBaseInfo> implements JftMercBaseInfoService {

    @Autowired
    private UserAppService uaService;

    @Autowired
    private JftMercPaymentChannelService jpcService;

    @Autowired
    private JftBankCardService jftBankCardService;

    @Autowired
    private JftMerchantRateService jbrService;

    @Override
    public MercInfoVO apiGetMercInfo() {
        UserApp tokenUa = uaService.queryUserAppByToken();
        Long userId = tokenUa.getUserId();

        JftMercBaseInfo jftMercBaseInfo = getMercBaseInfo(userId);
        jftMercBaseInfo = Converter.INSTANCE.toVO(jftMercBaseInfo);
        JftBankCard jftBankCard = jftBankCardService.getByUserId(userId);

        JftMerchantRate jftMerchantRate = jbrService.getRateByUserId(userId);
        if (jftMerchantRate == null) {
            return new MercInfoVO(jftMercBaseInfo, jftBankCard, tokenUa.getLevelCode(), "暂未进件");
        }

        return new MercInfoVO(jftMercBaseInfo, jftBankCard, tokenUa.getLevelCode(), (jftMerchantRate.getAliRate().intValue() / 100f) + "%");
    }

    @Override
    public void apiSaveMercBaseInfo(JftMercBaseInfo entity) {
        final UserApp tokenUa = uaService.queryUserAppByToken();

        entity.setUserId(tokenUa.getUserId());

        final JftMercPaymentChannel jpc = jpcService.getByUserId(tokenUa.getUserId());

        if (jpc != null) {
            boolean statusFlag = Objects.equals(jpc.getStatus(), MerchantChannelStatus.AUDIT_STATUS_SUCCESS)
                    || Objects.equals(jpc.getStatus(), MerchantChannelStatus.AUDIT_STATUS_EXAMINING);

            if (statusFlag) {
                throw new MsgException("缴费通开通中或已开通，暂不支持该操作");
            }
        }

        entity.setMerchantCode("M800" + System.currentTimeMillis());

        LambdaQueryWrapper<JftMercBaseInfo> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(JftMercBaseInfo::getUserId, tokenUa.getUserId());
        baseMapper.delete(qWrapper);

        // 暂不做校验
        save(entity);
    }
}
