package com.gree.mall.manager.controller.settle.repair;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.settle.repair.DailyBankAccountVO;
import com.gree.mall.manager.bean.settle.repair.SettleDailyBankTransferRecordVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.settle.repair.DailyTransferLogic;
import com.gree.mall.manager.logic.settle.repair.RepairSettleAccountLogic;
import com.gree.mall.manager.plus.entity.SettleDailyBankAccount;
import com.gree.mall.manager.plus.entity.SettleDailyBankCardBalance;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "银行卡API", tags = {"银行卡API"})
@RequestMapping(value = "/daily/bank", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
public class BankAccountController {

    private final RepairSettleAccountLogic repairSettleAccountLogic;
    private final RedisLockRegistry redisLockRegistry;
    private final DailyTransferLogic dailyTransferLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<DailyBankAccountVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<DailyBankAccountVO> page = repairSettleAccountLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<DailyBankAccountVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation("导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<DailyBankAccountVO> page = repairSettleAccountLogic.page(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<SettleDailyBankAccount> detail(
            @ApiParam(required = true) @RequestParam String id
    ) {
        SettleDailyBankAccount detail = repairSettleAccountLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public ResponseHelper add(
            @RequestBody SettleDailyBankAccount dailyBankAccount
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_DAILY_BANKACCOUNT + dailyBankAccount.getIdcard() + dailyBankAccount.getBankAccount());
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("请勿频繁操作，稍后再试");
        }
        try {
            repairSettleAccountLogic.add(dailyBankAccount);
            return ResponseHelper.success();
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "银行卡信息-修改")
    public ResponseHelper update(
            @RequestBody SettleDailyBankAccount dailyBankAccount
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_DAILY_BANKACCOUNT + dailyBankAccount.getIdcard() + dailyBankAccount.getBankAccount());
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("请勿频繁操作，稍后再试");
        }
        try {
            repairSettleAccountLogic.update(dailyBankAccount);
            return ResponseHelper.success();
        } finally {
            obtain.unlock();
        }
    }


    @PostMapping("/delete")
    @ApiOperation(value = "银行卡信息-删除")
    public ResponseHelper getAccountBalance(
            @ApiParam(required = true, value = "id") @RequestParam String id
    ) {
        repairSettleAccountLogic.delete(id);
        return ResponseHelper.success();
    }

    @ZfireList
    @PostMapping("/transfer/list")
    @ApiOperation(value = "转帐记录列表")
    public ResponseHelper<IPage<SettleDailyBankTransferRecordVO>> transferList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<SettleDailyBankTransferRecordVO> page = repairSettleAccountLogic.transferList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<SettleDailyBankTransferRecordVO>() {
        });
    }

    @PostMapping("/company/account/list")
    @ApiOperation(value = "公司卡号余额列表")
    public ResponseHelper<IPage<SettleDailyBankCardBalance>> companyAccountList(
            @ApiParam(required = true, value = "页号") @RequestParam Integer pageNo,
            @ApiParam(required = true, value = "页大小") @RequestParam Integer pageSize
    ) {
        IPage<SettleDailyBankCardBalance> list = repairSettleAccountLogic.companyAccountList(pageNo, pageSize);
        return ResponseHelper.success(list);
    }

    @GetMapping("/company/account/balance")
    @ApiOperation(value = "查询公司卡余额")
    public ResponseHelper<SettleDailyBankCardBalance> selBankCardBalance(
            @ApiParam(required = true, value = "id") @RequestParam Integer id,
            @ApiParam(required = true, value = "手机号") @RequestParam String mobile,
            @ApiParam(required = true, value = "验证码") @RequestParam String code
    ) throws Exception {
        SettleDailyBankCardBalance bean = repairSettleAccountLogic.selBankCardBalance(id, mobile, code);
        return ResponseHelper.success(bean);
    }

    @GetMapping("/send/msg")
    @ApiOperation(value = "查询余额/(发放工资)发送验证码")
    public ResponseHelper sendMsg(
            @ApiParam(required = true, value = "手机号") @RequestParam String mobile,
            @ApiParam(value = "类型 发放：issue , 余额:balance") @RequestParam(required = false) String type
    ) throws Exception {
        repairSettleAccountLogic.sendMsg(mobile, type);
        return ResponseHelper.success();
    }

    @PostMapping("/template/import")
    @ApiOperation("导入银行账户")
    public ResponseHelper importData(MultipartFile file) throws Exception {
        repairSettleAccountLogic.importData(file);
        return ResponseHelper.success();
    }

    @GetMapping("/excute")
    @ApiOperation("查询转账结果")
    public ResponseHelper excute() {
        dailyTransferLogic.excute();
        return ResponseHelper.success();
    }

}
