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

import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.MemberFieldConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.enums.EAccount;
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.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.ext.CuMemberAccountExt;
import com.hzs.common.domain.system.config.BdAccount;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.service.ICuMemberAccountService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.vo.CuMemberAccountShowVO;
import com.hzs.member.account.vo.CuMemberWalletDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 会员账户信息 后端控制器
 *
 * @author hzs
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/manager/account")
public class CuMemberAccountController extends BaseController {

    @Autowired
    ICuMemberAccountService cuMemberAccountService;

    @Autowired
    ICuMemberBaseService baseService;

    /**
     * 查询账户详情
     *
     * @param pkMember 用户ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.WALLET_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/detail")
    public AjaxResult findAccountDetail(Long pkMember) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuMemberAccountShowVO> memberAccountShowVOS = cuMemberAccountService.selectAccountDetail(pkMember, pkCountry);
        return AjaxResult.success(memberAccountShowVOS);
    }


    /**
     * 查询钱包明细列表
     *
     * @param memberWalletDetailVO 会员账户信息
     * @return TableDataInfo
     */
    @AccessPermissions("WalletDetails")
    @Log(module = EOperationModule.WALLET_DETAIL, business = EOperationBusiness.WALLET_DETAIL, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public TableDataInfo findTransferList(@RequestBody CuMemberWalletDetailVO memberWalletDetailVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberWalletDetailVO.setPkCountry(pkCountry);
        memberWalletDetailVO.setSystemType(SecurityUtils.getSystemType());
        startPage();
        Object[] objects = cuMemberAccountService.selectMemberAccountList(memberWalletDetailVO);
        //处理分页
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberAccountExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberWalletDetailVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 钱包明细列表导出
     *
     * @param response        入参
     * @param memberAccountVO 查询入参
     */
    @Log(module = EOperationModule.WALLET_DETAIL, business = EOperationBusiness.WALLET_DETAIL, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberWalletDetailVO memberAccountVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberAccountVO.setPkCountry(pkCountry);
        memberAccountVO.setSystemType(SecurityUtils.getSystemType());
        List<CuMemberWalletDetailVO> cuMemberAccountVOS = (List<CuMemberWalletDetailVO>) cuMemberAccountService.selectMemberAccountList(memberAccountVO)[1];
        ExcelUtil<CuMemberWalletDetailVO> util = new ExcelUtil<>(CuMemberWalletDetailVO.class, getExportField(memberAccountVO));
        util.exportExcel(response, cuMemberAccountVOS, FinanceMsgConstants.WALLET_DETAILS_EXPORT);
    }

    /**
     * 根据前台提交的账号id返回显示字段
     *
     * @param memberAccountVO 查询入参
     * @return List<String>
     */
    private List<String> getExportField(CuMemberWalletDetailVO memberAccountVO) {
        List<String> fields = queryAccountByConfig(memberAccountVO.getPkCountry());
        fields.add("memberCode");
        fields.add("memberName");
        fields.add("vertexName");
        fields.add("teamName");

        fields.add("plevel");
        fields.add("slevel");
        return fields;
    }


    /**
     * 显示的字段
     *
     * @return AjaxResult
     */
    @GetMapping("/list-field")
    public AjaxResult accountListField() {
        return AjaxResult.success(queryAccountByConfig(SecurityUtils.getPkCountry()));
    }

    /**
     * 根据配置查询币种显示信息
     *
     * @return List<String>
     */
    private List<String> queryAccountByConfig(Integer pkCountry) {
        List<String> fieldList = new ArrayList<>();
        List<BdAccount> bdAccounts = baseService.getAccountByCountryId(pkCountry);
        if (CollUtil.isNotEmpty(bdAccounts)) {
            for (BdAccount bdAccount : bdAccounts) {
                fieldList.add(MemberFieldConstants.ACCOUNT + bdAccount.getFieldValue());
                if (bdAccount.getFieldValue() <= MagicNumberConstants.ACC0UNT_LOCK_NUM) {
                    fieldList.add(MemberFieldConstants.AVAILABLE_ACCOUNT + bdAccount.getFieldValue());
                    fieldList.add(MemberFieldConstants.LOCK_ACCOUNT + bdAccount.getFieldValue());
                    fieldList.add(MemberFieldConstants.LOCK_TRANSFER_ACCOUNT + bdAccount.getFieldValue());
                } else if (bdAccount.getFieldValue() == 13) {
                    fieldList.add(MemberFieldConstants.AVAILABLE_ACCOUNT + bdAccount.getFieldValue());
                    fieldList.add(MemberFieldConstants.LOCK_ACCOUNT + bdAccount.getFieldValue());
                }
                if (bdAccount.getFieldValue().equals(EAccount.BONUS.getValue())) {
                    fieldList.add(MemberFieldConstants.LOCK_WITHDRAW_ACCOUNT + bdAccount.getFieldValue());
                    fieldList.add(MemberFieldConstants.SUBMIT_ACCOUNT + bdAccount.getFieldValue());
                }

            }
        }
        return fieldList;
    }
}
