package cn.pluss.platform.jft.impl;

import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.jft.JftAuditService;
import cn.pluss.platform.jft.JftMerchantRateRecordService;
import cn.pluss.platform.jft.JftMerchantRateService;
import cn.pluss.platform.mapper.JftMerchantRateMapper;
import cn.pluss.platform.merchant.JftMercPaymentChannelService;
import cn.pluss.platform.user.UserLevelService;
import cn.pluss.platform.userApp.UserAppService;
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 org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 * 缴费通商户费率表(新) 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2022-06-21
 */
@Service
public class JftMerchantRateServiceImpl extends ServiceImpl<JftMerchantRateMapper, JftMerchantRate> implements JftMerchantRateService {

    @Autowired
    private UserAppService uaService;

    @Autowired
    private UserLevelService userLevelService;

    @Autowired
    private JftMercPaymentChannelService jpcService;

    @Autowired
    private JftAuditService jftAuditService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int apiSaveRate(JftMerchantRate jftMerchantRate) {
        UserApp tokenUa = uaService.queryUserAppByToken();
        jftMerchantRate.setUserId(tokenUa.getUserId());
        JftMercPaymentChannel jpc = jpcService.getByUserId(tokenUa.getUserId());
        // TODO 此处存在一些特殊情况，比如费率修改时候，发起进件，完成进件，费率将会不一致，暂不考虑
        if (jpc == null || MerchantChannelStatus.AUDIT_STATUS_REJECT.equals(jpc.getStatus())) {
            // 先删除、后保存
            LambdaQueryWrapper<JftMerchantRate> dWrapper = Wrappers.lambdaQuery();
            dWrapper.eq(JftMerchantRate::getUserId, tokenUa.getUserId());
            remove(dWrapper);

            save(jftMerchantRate);

            return 0;
        }

        if (MerchantChannelStatus.AUDIT_STATUS_SUCCESS.equals(jpc.getStatus())) {
            jftAuditService.changeRate(tokenUa.getUserId(), jftMerchantRate.getAliRate());

            return 1;
        }

        throw new MsgException("当前状态不允许设置或变更费率");
    }

    @Override
    public JftMerchantRate getAuditRate(Long userId) {
        final UserApp userApp = uaService.getUserAppByUserId(userId);

        if ("LL".equals(userApp.getRoleCode())) {
            //
            JftMerchantRate jftMerchantRate = new JftMerchantRate();
            jftMerchantRate.setAliRate(new BigDecimal("28"));
            jftMerchantRate.setUserId(userId);
            jftMerchantRate.setChannel(4);

            return jftMerchantRate;
        }

        final UserLevel userLevel = userLevelService.getLevelInfoByRoleLevel(userApp.getRoleCode(), userApp.getLevelCode());

        JftMerchantRate jftMerchantRate = new JftMerchantRate();
        jftMerchantRate.setAliRate(userLevel.getJftRate());
        jftMerchantRate.setUserId(userId);
        jftMerchantRate.setChannel(4);

        return jftMerchantRate;
    }
}
