package cn.pluss.platform.controller;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.dto.AccountDTO;
import cn.pluss.platform.dto.BankCardDTO;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.AccountService;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.vo.StoreSummaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @author Djh
 * 结算信息接口控制
 */
@Validated
@RestController
@RequestMapping("account")
public class AccountController {

    @Autowired
    private UserAppService userAppService;

    @Autowired
    @Qualifier("accountService")
    private AccountService accountService;

    @Autowired
    private MerchantChannelStatusService mcsService;

    @Autowired
    private MerchantBaseInfoService mbiService;

    /**
     * 保存商户结算信息, 最新接口
     *
     * @return
     */
    @PostMapping("/v3/save")
    public Result<Object> saveV3(@Validated @RequestBody AccountDTO accountDTO) {
        accountService.saveOrUpdate(accountDTO);
        return ResultGenerator.genSuccessResult("结算人信息保存成功", "");
    }

    /**
     * 保存商户结算信息, 最新接口 最最最新
     *
     * @return
     */
    @PostMapping("/save/v4")
    public Result<Object> saveV4(@Validated @RequestBody AccountDTO accountDTO) {
        UserApp userApp = userAppService.queryUserAppByToken();
        MsgException.checkUnequals(userApp.getUserId() + "", accountDTO.getUserId(), "非法参数, 拒绝访问");
        accountService.saveOrUpdate(accountDTO);
        return ResultGenerator.genSuccessResult("结算人信息保存成功", "");
    }


    @GetMapping("/v3/{userId}")
    public Result<Object> getAccountV3(@PathVariable("userId") String userId) {
        AccountDTO accountDTO = accountService.getAccount(userId);
        return ResultGenerator.genSuccessResult(accountDTO);
    }

    @GetMapping("/v4")
    public Result<Object> getAccountV4(Account account) {
        UserApp tokenUa = userAppService.queryUserAppByToken();

        if (account.getChannelType() == null) {

            MerchantChannelStatus mcs = mcsService.getValidData(tokenUa.getMerchantCode());
            if (mcs == null || !Objects.equals(mcs.getChannel(), 4)) {
                account.setChannelType(Account.CHANNEL_TYPE_D1);
            } else {
                account.setChannelType(Account.CHANNEL_TYPE_D0);
            }
        }

        AccountDTO accountDTO = accountService.getRealAccount(account);
        if (accountDTO == null) {
            accountDTO = new AccountDTO();
        }

        if (accountDTO.getIdcard() == null && accountDTO.getBankCard() == null) {
            accountDTO.setIdcard(new IdCard());
            accountDTO.setBankCard(new BankCard());
        }

        return ResultGenerator.genSuccessResult(accountDTO);
    }

    @GetMapping("/all")
    public Result<Object> getAccounts(@RequestParam String userId) {
        Map<String, AccountDTO> result = accountService.getAccounts(userId);
        return ResultGenerator.genSuccessResult(result);
    }

    @GetMapping("/reckoner/v2")
    public Result<Object> reckonerV2(String userId, String merchantType) {
        StoreSummaryVO result = accountService.getReckonerV2(userId, merchantType);
        return ResultGenerator.genSuccessResult("请求成功", result);
    }

    @GetMapping("/reckoner")
    public Result<Object> reckoner(String userId, String merchantType) {
        StoreSummaryVO result = accountService.getReckoner(userId, merchantType);
        return ResultGenerator.genSuccessResult("请求成功", result);
    }

    @PostMapping("/editAccountBankCard")
    public Result<Object> editAccountBankCard(@RequestBody BankCard bankCard){
//        throw new MsgException("系统维护中，暂不支持修改结算卡");
         accountService.editAccountBankCard(bankCard);
         return ResultGenerator.genSuccessResult("结算账户修改已申请", null);
    }

    @PostMapping("/editAccountBankCardV2")
    public Result<Object> editAccountBankCardV2(@RequestBody BankCardDTO bankCard){
        accountService.editAccountBankCardV2(bankCard);
        return ResultGenerator.genSuccessResult("结算账户修改已申请", null);
    }

    @GetMapping("/canEditBankCard")
    public Result<Object> canEditBankCard() {
        UserApp userApp = userAppService.queryUserAppByToken();
        boolean flag = accountService.canEditBankCard(userApp.getMerchantCode());
        return ResultGenerator.genSuccessResult("请求成功", flag);
    }
}