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.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.CuMemberTradeExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.account.dto.BusinessCommissionDTO;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.service.ICuMemberTradeRecoilService;
import com.hzs.member.account.service.ICuMemberTradeService;
import com.hzs.member.account.vo.CuMemberTradeDetailVO;
import com.hzs.member.account.vo.CuMemberTradeRecoilVO;
import com.hzs.member.account.vo.CuMemberTradeVO;
import com.hzs.system.sys.IMenuColumnServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
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;

/**
 * <p>
 * 消费明细
 * </p>
 *
 * @author hzs
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/manager/trade")
public class CuMemberTradeController extends BaseController {

    @Autowired
    private ICuMemberTradeService cuMemberTradeService;

    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    @Autowired
    private ICuMemberBaseService baseService;

    @Autowired
    private ICuMemberTradeRecoilService cuMemberTradeRecoilService;

    @DubboReference
    private IMenuColumnServiceApi menuColumnServiceApi;

    /**
     * 查询消费明细列表
     *
     * @param memberTradeVO 交易明细信息
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.CONSUMPTION_DETAIL, business = EOperationBusiness.CONSUMPTION_DETAIL, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    @ColumnAuthority(perms = "ConsumptionDetails")
    public TableDataInfo findTradeList(@RequestBody CuMemberTradeVO memberTradeVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberTradeVO.setPkCountry(pkCountry);
        startPage();
        Object[] objects = cuMemberTradeService.selectMemberTradeList(memberTradeVO);
        //处理分页
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberTradeExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberTradeVO>) objects[1]);
        return tableDataInfo;
    }

    /**
     * 充值明细列表导出
     *
     * @param response      入参
     * @param memberTradeVO 查询入参
     */
    @Log(module = EOperationModule.MEMBER_TRADE, business = EOperationBusiness.CONSUMPTION_DETAIL, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberTradeVO memberTradeVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberTradeVO.setPkCountry(pkCountry);
        List<CuMemberTradeVO> cuMemberTradeVOS = (List<CuMemberTradeVO>) cuMemberTradeService.selectMemberTradeList(memberTradeVO)[1];
        if (CollUtil.isNotEmpty(cuMemberTradeVOS)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(ETradeType.values());
            Map<Integer, String> accountMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_ACC);
            for (CuMemberTradeVO memberTrade : cuMemberTradeVOS) {
                Integer pkAccount = memberTrade.getPkAccount();
                memberTrade.setPkAccountVal(accountMap.get(pkAccount));
                memberTrade.setTradeTypeVal(transactionMap.get(EnumsPrefixConstants.TRADE_TYPE + memberTrade.getTradeType()));
            }
        }
        ExcelUtil<CuMemberTradeVO> util = new ExcelUtil<>(CuMemberTradeVO.class, menuColumnServiceApi.queryMenuColumn("ConsumptionDetails", SecurityUtils.getUserId()).getData());
        util.exportExcel(response, cuMemberTradeVOS, FinanceMsgConstants.CONSUMPTION_DETAILS_EXPORT);
    }

    /**
     * 查询列表
     *
     * @param memberTradeDetailVO 钱包明细详情
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.WALLET_DETAIL, business = EOperationBusiness.WALLET_DETAIL_DETAIL, method = EOperationMethod.SELECT, remark = "详情列表")
    @PostMapping("/detail-list")
    public TableDataInfo findTransferList(@RequestBody CuMemberTradeDetailVO memberTradeDetailVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberTradeDetailVO.setPkCountry(pkCountry);
        startPage();
        Object[] objects = cuMemberTradeService.selectMemberTradeDetailList(memberTradeDetailVO);

        //处理分页
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberTradeExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberTradeDetailVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 充值明细列表导出
     *
     * @param response            入参
     * @param memberTradeDetailVO 查询入参
     */
    @Log(module = EOperationModule.WALLET_DETAIL, business = EOperationBusiness.WALLET_DETAIL_DETAIL, method = EOperationMethod.EXPORT, remark = "详情导出")
    @PostMapping("/detail-export")
    public void export(HttpServletResponse response, CuMemberTradeDetailVO memberTradeDetailVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberTradeDetailVO.setPkCountry(pkCountry);
        List<CuMemberTradeDetailVO> cuMemberTradeDetailVOs = (List<CuMemberTradeDetailVO>) cuMemberTradeService.selectMemberTradeDetailList(memberTradeDetailVO)[1];
        if (CollUtil.isNotEmpty(cuMemberTradeDetailVOs)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(ETradeType.values());
            Map<Integer, String> accountMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_ACC);
            for (CuMemberTradeDetailVO memberTradeDetail : cuMemberTradeDetailVOs) {
                Integer pkAccount = memberTradeDetail.getPkAccount();
                memberTradeDetail.setPkAccountVal(accountMap.get(pkAccount));
                memberTradeDetail.setTradeTypeVal(transactionMap.get(EnumsPrefixConstants.TRADE_TYPE + memberTradeDetail.getTradeType()));
            }
        }
        ExcelUtil<CuMemberTradeDetailVO> util = new ExcelUtil<>(CuMemberTradeDetailVO.class);
        util.exportExcel(response, cuMemberTradeDetailVOs, FinanceMsgConstants.RECHARGE_DETAILS_EXPORT);
    }


    @GetMapping("/test")
    public AjaxResult findTransferList() {
        String rechargeCode = "RC1678260681936884";
        return toAjax(cuMemberTradeService.saveMemberTradeAndAccountByRechargeCode(rechargeCode));
    }

    @PostMapping("/refue")
    public AjaxResult findTransferListRefue(@RequestBody BusinessCommissionDTO businessCommissionDTO) {
        return toAjax(cuMemberTradeService.businessCommissionReject(businessCommissionDTO));
    }


    @PostMapping("/balnace")
    public TableDataInfo findTransferListBalance(@RequestBody BusinessCommissionDTO businessCommissionDTO) {
        return getDataTable(cuMemberTradeService.selectBalanceNotEnoughMember(businessCommissionDTO));
    }

    /**
     * 查询复消明细列表
     *
     * @param memberTradeDetailVO 入参
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.WALLET_DETAIL, business = EOperationBusiness.MEMBER_RECOIL_DETAIL, method = EOperationMethod.SELECT, remark = "详情列表")
    @PostMapping("/recoil-list")
    public TableDataInfo findRecoilList(@RequestBody CuMemberTradeDetailVO memberTradeDetailVO) {
        memberTradeDetailVO.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        Object[] objects = cuMemberTradeRecoilService.selectMemberRecoilDetailList(memberTradeDetailVO);
        //处理分页
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberTradeExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberTradeRecoilVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 复消明细列表导出
     *
     * @param response            入参
     * @param memberTradeDetailVO 查询入参
     */
    @Log(module = EOperationModule.WALLET_DETAIL, business = EOperationBusiness.MEMBER_RECOIL_DETAIL, method = EOperationMethod.EXPORT, remark = "详情导出")
    @PostMapping("/recoil-export")
    public void recoilexport(HttpServletResponse response,CuMemberTradeDetailVO memberTradeDetailVO) {
        memberTradeDetailVO.setPkCountry(SecurityUtils.getPkCountry());
        List<CuMemberTradeRecoilVO> memberTradeRecoilVOList = (List<CuMemberTradeRecoilVO>) cuMemberTradeRecoilService.selectMemberRecoilDetailList(memberTradeDetailVO)[1];
        if (CollUtil.isNotEmpty(memberTradeRecoilVOList)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(ETradeType.values());
            Map<Integer, String> accountMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_ACC);
            for (CuMemberTradeRecoilVO cuMemberTradeRecoilVO : memberTradeRecoilVOList) {
                Integer pkAccount = cuMemberTradeRecoilVO.getPkAccount();
                cuMemberTradeRecoilVO.setPkAccountVal(accountMap.get(pkAccount));
                cuMemberTradeRecoilVO.setTradeTypeVal(transactionMap.get(EnumsPrefixConstants.TRADE_TYPE + cuMemberTradeRecoilVO.getTradeType()));
            }
        }
        ExcelUtil<CuMemberTradeRecoilVO> util = new ExcelUtil<>(CuMemberTradeRecoilVO.class);
        util.exportExcel(response, memberTradeRecoilVOList, FinanceMsgConstants.RECOIL_DETAILS_EXPORT);
    }

}
