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

import cn.hutool.core.collection.CollUtil;
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.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.ETradeType;
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.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.report.member.service.MemberReportService;
import com.hzs.report.member.vo.CuMemberWalletVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 伞下钱包明细
 *
 * @Description:
 * @Author: ljc
 * @Time: 2024/3/22 15:09
 * @Classname: CuMemberUnderUmbrellaWalletController
 * @Package_name: com.hzs.report.member.controller.manage
 */
@RequestMapping("/manager/wallet")
@RestController
public class CuMemberUnderUmbrellaWalletController extends BaseController {

    @Autowired
    MemberReportService memberReportService;


    @Autowired
    ITransactionCommonService iTransactionCommonService;

    /**
     * 查询列表
     *
     * @param memberWalletVO 入参
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.WALLET_DETAIL_DETAIL, method = EOperationMethod.SELECT, remark = "伞下钱包明细")
    @PostMapping("/detail-list")
    public TableDataInfo findWalletList(@RequestBody CuMemberWalletVO memberWalletVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberWalletVO.setPkCountry(pkCountry);
        if (StringUtils.isEmpty(memberWalletVO.getMemberCode())) {
            return getDataTable(new ArrayList<>());
        } else {
            startPage();
            List<CuMemberWalletVO> cuMemberWalletVOList = memberReportService.selectMemberUnderWalletList(memberWalletVO);
            return getDataTable(cuMemberWalletVOList);
        }
    }


    /**
     * 伞下钱包明细导出
     *
     * @param response       入参
     * @param memberWalletVO 查询入参
     */
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.WALLET_DETAIL_DETAIL, method = EOperationMethod.EXPORT, remark = "伞下钱包明细导出")
    @PostMapping("/detail-export")
    public void export(HttpServletResponse response, CuMemberWalletVO memberWalletVO) {
        List<CuMemberWalletVO> cuMemberWalletVOList;
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberWalletVO.setPkCountry(pkCountry);
        if (StringUtils.isEmpty(memberWalletVO.getMemberCode())) {
            cuMemberWalletVOList = new ArrayList<>();
        } else {
            cuMemberWalletVOList = memberReportService.selectMemberUnderWalletList(memberWalletVO);
        }
        if (CollUtil.isNotEmpty(cuMemberWalletVOList)) {
            //枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(ETradeType.values());
            Map<Integer, String> accountMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_ACC);
            for (CuMemberWalletVO cuMemberWalletVO : cuMemberWalletVOList) {
                Integer pkAccount = cuMemberWalletVO.getPkAccount();
                cuMemberWalletVO.setPkAccountVal(accountMap.get(pkAccount));
                cuMemberWalletVO.setTradeTypeVal(transactionMap.get(EnumsPrefixConstants.TRADE_TYPE + cuMemberWalletVO.getTradeType()));
            }
        }
        ExcelUtil<CuMemberWalletVO> util = new ExcelUtil<>(CuMemberWalletVO.class);
        util.exportExcel(response, cuMemberWalletVOList, FinanceMsgConstants.RECHARGE_DETAILS_EXPORT);
    }
}
