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

import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.ColumnAuthority;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
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.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.CuMemberWithdraw;
import com.hzs.common.domain.member.account.CuMemberWithdrawExt;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.service.ICuMemberWithdrawService;
import com.hzs.member.account.vo.CuMemberPetitionVO;
import com.hzs.member.account.vo.CuMemberWithdrawVO;
import com.hzs.system.sys.IMenuColumnServiceApi;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 * 会员提现表 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/manager/withdraw")
public class CuMemberWithdrawController extends BaseController {


    @Autowired
    private UserTokenService userTokenService;

    @Autowired
    private ICuMemberBaseService baseService;

    @Autowired
    ICuMemberWithdrawService cuMemberWithdrawService;

    @Autowired
    ITransactionCommonService iTransactionCommonService;

    @DubboReference
    IMenuColumnServiceApi menuColumnServiceApi;


    /**
     * 发起签呈显示
     *
     * @param pkIds 充值参数
     * @return AjaxResult
     */
    @Log(module = EOperationModule.WITHDRAWAL_DETAIL, business = EOperationBusiness.WITHDRAWAL_DETAIL, method = EOperationMethod.SELECT, remark = "发起签呈")
    @PostMapping("/show-withdraw-petition")
    public AjaxResult showWithdrawPetition(@RequestBody List<Long> pkIds) {
        if (CollUtil.isEmpty(pkIds)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_INITIATING_SIGNATURE_RECORD));
        }
        List<CuMemberWithdrawVO> cuMemberWithdrawList = cuMemberWithdrawService.checkWithdrawPetitionByState(pkIds, EApproveStatus.WAIT_SUBMIT.getValue());
        if (CollUtil.isNotEmpty(cuMemberWithdrawList)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECTED_RECORD_DUPLICATE_SIGNATURE_RECORD));
        }
        return AjaxResult.success(cuMemberWithdrawService.showMemberWithdrawPetition(pkIds, null));
    }

    /**
     * 确认签呈
     *
     * @param cuMemberPetitionVO 签呈信息
     * @return AjaxResult
     */
    @Log(module = EOperationModule.WITHDRAWAL_DETAIL, business = EOperationBusiness.WITHDRAWAL_DETAIL, method = EOperationMethod.SUBMIT, remark = "确认签呈")
    @PostMapping("/confirm-withdraw-petition")
    public AjaxResult confirmWithdrawPetition(@RequestBody CuMemberPetitionVO cuMemberPetitionVO) {
        LoginUser loginUser = userTokenService.getLoginUser();
        cuMemberPetitionVO.setLoginUser(loginUser);
        return AjaxResult.success(cuMemberWithdrawService.confirmWithdrawPetition(cuMemberPetitionVO));
    }


    /**
     * 查询列表
     *
     * @param cuMemberWithdrawVO 会员提现信息
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.WITHDRAWAL_DETAIL, business = EOperationBusiness.WITHDRAWAL_DETAIL, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    @ColumnAuthority(perms = "CashDetails")
    public TableDataInfo findRechargeList(@RequestBody CuMemberWithdrawVO cuMemberWithdrawVO) {
        Integer countryId = SecurityUtils.getPkCountry();
        cuMemberWithdrawVO.setPkCountry(countryId);
        String memberCode = cuMemberWithdrawVO.getMemberCode();
        if (StringUtils.isNotEmpty(memberCode)) {
            String[] memberCodes = memberCode.split("\\s+");
            if (memberCodes.length > 1) {
                cuMemberWithdrawVO.setMemberCodeList(Arrays.asList(memberCodes));
            }
        }
        startPage();
        Object[] objects = cuMemberWithdrawService.selectMemberWithdrawList(cuMemberWithdrawVO);
        //处理分页
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberWithdrawExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberWithdrawVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 充值明细列表导出
     *
     * @param response           入参
     * @param cuMemberWithdrawVO 查询入参
     */
    @Log(module = EOperationModule.WITHDRAWAL_DETAIL, business = EOperationBusiness.WITHDRAWAL_DETAIL, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberWithdrawVO cuMemberWithdrawVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        cuMemberWithdrawVO.setPkCountry(pkCountry);
        List<CuMemberWithdrawVO> cuMemberWithdrawList = (List<CuMemberWithdrawVO>) cuMemberWithdrawService.selectMemberWithdrawList(cuMemberWithdrawVO)[1];
        //枚举翻译
        Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EApproveStatus.values());
        Map<String, String> transactionStatusMap = iTransactionCommonService.exportEnumTransaction(EWithdrawStatus.values());
        if (CollUtil.isNotEmpty(cuMemberWithdrawList)) {
            for (CuMemberWithdrawVO memberWithdraw : cuMemberWithdrawList) {
                Integer pkAccount = memberWithdraw.getPkAccount();
                memberWithdraw.setPkAccountVal(baseService.getAccountTranslateFromDataBase(pkAccount));
                memberWithdraw.setApproveStateVal(transactionMap.get(EnumsPrefixConstants.APPROVE_STATUS + memberWithdraw.getApproveState()));
                memberWithdraw.setStatusVal(transactionStatusMap.get(EnumsPrefixConstants.ENU_WITHDRAW_STATUS + memberWithdraw.getStatus()));
            }
        }
        ExcelUtil<CuMemberWithdrawVO> util = new ExcelUtil<>(CuMemberWithdrawVO.class, menuColumnServiceApi.queryMenuColumn("CashDetails", SecurityUtils.getUserId()).getData());
        util.exportExcel(response, cuMemberWithdrawList, TransactionUtils.getContent(FinanceMsgConstants.WITHDRAWAL_DETAILS_EXPORT));
    }

    /**
     * 查询详情
     *
     * @param pkId 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.WITHDRAWAL_DETAIL, business = EOperationBusiness.WITHDRAWAL_DETAIL, method = EOperationMethod.SELECT, remark = "查看详情")
    @GetMapping("/details")
    public AjaxResult findWithdrawDetails(Long pkId) {
        Assert.notNull(pkId, TransactionUtils.getContent(FinanceMsgConstants.SELECT_WITHDRAWAL_RECORD));
        CuMemberWithdrawVO cuMemberRechargeDetailVO = cuMemberWithdrawService.queryCuMemberWithdrawDetail(pkId);
        return AjaxResult.success(cuMemberRechargeDetailVO);
    }


    /**
     * 打款
     *
     * @param memberWithdrawVO 打款信息
     * @return AjaxResult
     */
    @Log(module = EOperationModule.WITHDRAWAL_DETAIL, business = EOperationBusiness.WITHDRAWAL_DETAIL, method = EOperationMethod.PAYMENT)
    @PostMapping("/payment")
    public AjaxResult withdrawPayment(@RequestBody CuMemberWithdrawVO memberWithdrawVO) {
        List<Long> pkIds = memberWithdrawVO.getPkIds();
        Assert.notNull(pkIds, TransactionUtils.getContent(FinanceMsgConstants.SELECT_PAID_RECORD));
        List<CuMemberWithdraw> withdrawApproveStateByIds = cuMemberWithdrawService.getWithdrawApproveStateByIds(pkIds, EApproveStatus.FINISH.getValue());
        if (CollUtil.isNotEmpty(withdrawApproveStateByIds)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.DATA_UNAPPROVED_DATA));
        }

        Long pkMember = SecurityUtils.getUserId();
        memberWithdrawVO.setStatus(EWithdrawStatus.PAYMENT.getValue());
        memberWithdrawVO.setPayer(pkMember);
        memberWithdrawVO.setPaymentTime(new Date());
        memberWithdrawVO.setPkModified(pkMember);
        return toAjax(cuMemberWithdrawService.withdrawPaymentByPkId(memberWithdrawVO));
    }


    /**
     * 打款充正
     *
     * @param memberWithdrawVO 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.WITHDRAWAL_DETAIL, business = EOperationBusiness.WITHDRAWAL_DETAIL, method = EOperationMethod.POSITIVE)
    @PostMapping("/positive")
    public AjaxResult withdrawPaymentPositive(@RequestBody CuMemberWithdrawVO memberWithdrawVO) {
        List<Long> pkIds = memberWithdrawVO.getPkIds();
        Assert.notNull(pkIds, TransactionUtils.getContent(FinanceMsgConstants.SELECT_CHARGED_RECORD));
        memberWithdrawVO.setStatus(EWithdrawStatus.CHARGING.getValue());
        memberWithdrawVO.setPayer(SecurityUtils.getUserId());
        memberWithdrawVO.setPayerRemarks(memberWithdrawVO.getPayerRemarks());
        memberWithdrawVO.setPaymentTime(new Date());
        return toAjax(cuMemberWithdrawService.withdrawPaymentPositiveByPkId(memberWithdrawVO));
    }

}
