package cn.pluss.platform.controller.jft;

import cn.pluss.platform.annotation.Cert;
import cn.pluss.platform.annotation.ThirdAuth;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.converter.Converter;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.jft.*;
import cn.pluss.platform.merchant.JftMercPaymentChannelService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.vo.JftMercPaymentChannelVO;
import cn.pluss.platform.vo.MercInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author DJH
 */
@Cert
@ThirdAuth
@RestController
@RequestMapping("/jftAudit")
public class JftAuditController {

    @Autowired
    private JftBankCardService jftBankCardService;

    @Autowired
    private JftAuditService jftAuditService;

    @Autowired
    private JftMercBaseInfoService jmbiService;

    @Autowired
    private JftMercPaymentChannelService jpcService;

    @Autowired
    private UserAppService uaService;

    @Autowired
    private JftMerchantRateService jmrService;

    @Autowired
    private JftMerchantRateRecordService jmrrService;

    @RequestMapping(value = "/status", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> auditStatus() {
        final UserApp tokenUa = uaService.queryUserAppByToken();
        JftMercPaymentChannel jpc = jpcService.getByUserId(tokenUa.getUserId());

        if (jpc == null) {
            jpc = new JftMercPaymentChannel();
            jpc.setStatus("0");
        }

        return ResultGenerator.genSuccessResult(jpc);
    }

    @RequestMapping(value = "/auditStatus", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> auditStatus2() {
        final UserApp tokenUa = uaService.queryUserAppByToken();
        JftMercPaymentChannel jpc = jpcService.getByUserId(tokenUa.getUserId());

        jpc = Converter.INSTANCE.toVO(jpc);
        if (jpc == null) {
            jpc = new JftMercPaymentChannelVO();
            jpc.setStatus("0");
        }

        return ResultGenerator.genSuccessResult(jpc);
    }

    @RequestMapping(value = "/mercInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<MercInfoVO> mercInfo() {
        MercInfoVO mercInfoVO = jmbiService.apiGetMercInfo();
        return ResultGenerator.genSuccessResult(mercInfoVO);
    }

    @PostMapping("/saveBankCard")
    public Result<?> saveBankCard(@RequestBody @Validated(JftBankCard.Common.class) JftBankCard jftBankCard) {
        jftBankCardService.apiSaveBankCard(jftBankCard);

        return ResultGenerator.genSuccessResult("操作成功");
    }

    @RequestMapping(value = "/account", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> accountInfo() {
        final Map<String, Object> result = jftBankCardService.apiGetAccount();

        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/changeBankCard")
    public Result<?> changeJftBankCard(@RequestBody @Validated(JftBankCard.Common.class) JftBankCard jftBankCard) {
        jftBankCardService.apiChangeBankCard(jftBankCard);

        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @PostMapping("/audit")
    public Result<?> jftAudit() {
        jftAuditService.apiAudit();

        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @PostMapping("/saveBaseInfo")
    public Result<?> jftBaseInfo(@RequestBody @Validated(JftMercBaseInfo.Common.class) JftMercBaseInfo jftMercBaseInfo) {
        jmbiService.apiSaveMercBaseInfo(jftMercBaseInfo);

        if (!"0".equals(jftMercBaseInfo.getAutoFlag())) {
            jftAuditService.apiAudit();
        }

        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @RequestMapping(value = "/getBaseInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> getBaseInfo() {
        UserApp ua = uaService.queryUserAppByToken();
        final JftMercBaseInfo mercBaseInfo = jmbiService.getMercBaseInfo(ua.getUserId());

        return ResultGenerator.genSuccessResult("操作成功", mercBaseInfo);
    }

    @GetMapping("/mercRate")
    public Result<?> mercRate() {
        UserApp ua = uaService.queryUserAppByToken();
        final JftMerchantRate jftMerchantRate = jmrService.getRateByUserId(ua.getUserId());

        return ResultGenerator.genSuccessResult("操作成功", jftMerchantRate);
    }

    @GetMapping("/mercRateRecord")
    public Result<?> mercRateRecord() {
        final UserApp tokenUa = uaService.queryUserAppByToken();
        JftMercPaymentChannel channel = jpcService.getByUserId(tokenUa.getUserId());
        if(channel == null){
            return ResultGenerator.genFailResult("该商户暂未入网，费率变更记录获取失败");
        }
        LambdaQueryWrapper<JftMerchantRateRecord> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(JftMerchantRateRecord::getMerchantId, channel.getMerchantId());
        qWrapper.orderByDesc(JftMerchantRateRecord::getCreateTime);
        final List<JftMerchantRateRecord> result = jmrrService.list(qWrapper);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/mercRate")
    public Result<?> mercRate(@Validated @RequestBody JftMerchantRate rate) {
        int i = jmrService.apiSaveRate(rate);
        return ResultGenerator.genSuccessResult(i);
    }
}
