package com.hzs.member.account.controller.manage;

import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.account.CuMemberRechargeApplication;
import com.hzs.common.domain.member.account.CuMemberRechargeExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.account.service.ICuMemberRechargeApplicationService;
import com.hzs.member.account.vo.CuMemberRechargeApplicationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 充值申请
 *
 * @Description:
 * @Author: ljc
 * @Time: 2024/2/19 16:16
 * @Classname: CuMemberRechargeApplicationController
 * @Package_name: com.hzs.member.account.controller.manage
 */

@RestController
@RequestMapping("/manager/recharge/application")
public class CuMemberRechargeApplicationController extends BaseController {


    @Autowired
    ICuMemberRechargeApplicationService cuMemberRechargeApplicationService;

    @Autowired
    ITransactionCommonService iTransactionCommonService;

    /**
     * 查询充值申请列表
     *
     * @param rechargeApplicationVO 入参
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.RECHARGE_APPLICATION, business = EOperationBusiness.RECHARGE_APPLICATION, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public TableDataInfo rechargeList(@RequestBody CuMemberRechargeApplicationVO rechargeApplicationVO) {
        rechargeApplicationVO.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        Object[] objects = cuMemberRechargeApplicationService.findRechargeApplicationList(rechargeApplicationVO);
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberRechargeExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberRechargeApplicationVO>) objects[1]);
        return tableDataInfo;
    }

    /**
     * 导出
     *
     * @param response              响应
     * @param rechargeApplicationVO 入参
     */
    @Log(module = EOperationModule.RECHARGE_APPLICATION, business = EOperationBusiness.RECHARGE_APPLICATION, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberRechargeApplicationVO rechargeApplicationVO) {
        rechargeApplicationVO.setPkCountry(SecurityUtils.getPkCountry());
        List<CuMemberRechargeApplicationVO> rechargeApplicationVOList = (List<CuMemberRechargeApplicationVO>) cuMemberRechargeApplicationService.findRechargeApplicationList(rechargeApplicationVO)[1];
        //多语言翻译
        if (CollUtil.isNotEmpty(rechargeApplicationVOList)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EPersonalServiceStatus.values());
            for (CuMemberRechargeApplicationVO applicationVO : rechargeApplicationVOList) {
                applicationVO.setApplicationStatusVal(transactionMap.get(EnumsPrefixConstants.PERSONAL_STATUS + applicationVO.getApplicationStatus()));
            }
        }
        ExcelUtil<CuMemberRechargeApplicationVO> util = new ExcelUtil<>(CuMemberRechargeApplicationVO.class);
        util.exportExcel(response, rechargeApplicationVOList, TransactionUtils.getContent(FinanceMsgConstants.RECHARGE_APPLICATION_EXPORT));
    }


    /**
     * 充值申请驳回
     *
     * @param rechargeApplicationVO 入参
     * @return
     */
    @Log(module = EOperationModule.RECHARGE_APPLICATION, business = EOperationBusiness.RECHARGE_APPLICATION, method = EOperationMethod.REJECT)
    @PostMapping("/application-reject")
    @RepeatSubmitSimple
    public AjaxResult applicationReject(@RequestBody CuMemberRechargeApplicationVO rechargeApplicationVO) {
        rechargeApplicationVO.setPkMember(SecurityUtils.getUserId());
        String rejectionStatement = rechargeApplicationVO.getRejectionStatement();
        //驳回说明
        if (StringUtils.isEmpty(rejectionStatement)) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        CuMemberRechargeApplication cuMemberRechargeApplication = cuMemberRechargeApplicationService.selectApplicationById(rechargeApplicationVO.getPkId());
        if (!cuMemberRechargeApplication.getApplicationStatus().equals(EPersonalServiceStatus.APPLIED_FOR.getValue())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHARGE_NOT_ALLOW_APPROVE));
        }
        return toAjax(cuMemberRechargeApplicationService.updateRejectionStatement(rechargeApplicationVO));
    }


    /**
     * 充值申请驳回
     *
     * @param pkId ID
     * @return
     */
    @Log(module = EOperationModule.RECHARGE_APPLICATION, business = EOperationBusiness.RECHARGE_APPLICATION, method = EOperationMethod.AGREE)
    @GetMapping("/application-agree")
    @RepeatSubmitSimple
    public AjaxResult applicationAgree(Long pkId) {
        CuMemberRechargeApplication application = cuMemberRechargeApplicationService.selectApplicationById(pkId);
        if (!application.getApplicationStatus().equals(EPersonalServiceStatus.APPLIED_FOR.getValue())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHARGE_NOT_ALLOW_APPROVE));
        }
        return toAjax(cuMemberRechargeApplicationService.applicationAgree(pkId, SecurityUtils.getUserId(), application));
    }


}
