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


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.C;
import com.hzs.common.core.annotation.Excel;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.MemberFieldConstants;
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.CuMemberAccountDetail;
import com.hzs.common.domain.member.account.CuMemberAccountDetailExt;
import com.hzs.common.domain.system.config.BdAccount;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.service.ICuMemberAccountDetailService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.vo.CuMemberAccountDetailLookExtVO;
import com.hzs.member.account.vo.CuMemberAccountDetailLookVO;
import com.hzs.member.account.vo.CuMemberAccountDetailVO;
import com.hzs.member.account.vo.CuMemberAccountVO;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员账户详情 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-10-31
 */
@RestController
@RequestMapping("/manager/account-detail")
public class CuMemberAccountDetailController extends BaseController {

    @Autowired
    ICuMemberAccountDetailService memberAccountDetailService;

    @Autowired
    ICuMemberBaseService baseService;

    /**
     * 余额汇总
     *
     * @param accountDetailVO 余额信息
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.MEMBER_BALANCE_SUMMARY, business = EOperationBusiness.MEMBER_BALANCE_SUMMARY, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public TableDataInfo findAccountDetailList(@RequestBody CuMemberAccountDetailVO accountDetailVO) {
        TableDataInfo tableDataInfo;
        Integer countryId = SecurityUtils.getPkCountry();
        accountDetailVO.setPkCountry(countryId);
        String startDate = accountDetailVO.getStartDate();
        String endDate = accountDetailVO.getEndDate();
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            startPage();
            Object[] objects = memberAccountDetailService.findAccountDetailList(accountDetailVO);
            //处理分页
            tableDataInfo = getDataTable((List<CuMemberAccountDetail>) objects[0]);
            tableDataInfo.setRows((List<CuMemberAccountDetailVO>) objects[1]);
        } else {
            tableDataInfo = getDataTable(new ArrayList<>());
        }
        return tableDataInfo;
    }


    /**
     * 余额汇总导出
     *
     * @param response
     * @param accountDetailVO 查询入参
     */
    @Log(module = EOperationModule.MEMBER_BALANCE_SUMMARY, business = EOperationBusiness.MEMBER_BALANCE_SUMMARY, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberAccountDetailVO accountDetailVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        accountDetailVO.setPkCountry(pkCountry);
        String startDate = accountDetailVO.getStartDate();
        String endDate = accountDetailVO.getEndDate();
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            List<BdAccount> bdAccounts = baseService.getAccountByCountryId(SecurityUtils.getPkCountry());
            ExcelUtil<CuMemberAccountDetailVO> util = new ExcelUtil<>(CuMemberAccountDetailVO.class, getListShowField(bdAccounts));
            List<CuMemberAccountDetailVO> memberAccountDetailVOList = (List<CuMemberAccountDetailVO>) memberAccountDetailService.findAccountDetailList(accountDetailVO)[1];
            util.exportExcel(response, memberAccountDetailVOList, FinanceMsgConstants.BALANCE_SUMMARY_EXPORT);
        } else {
            ExcelUtil<CuMemberAccountDetailVO> util = new ExcelUtil<>(CuMemberAccountDetailVO.class);
            util.exportExcel(response, new ArrayList<>(), FinanceMsgConstants.BALANCE_SUMMARY_EXPORT);
        }
    }


    /**
     * 根据配置充值账户返回显示字段
     *
     * @param bdAccounts 充值配置显示账户
     * @return List<String>
     */
    private List<String> getListShowField(List<BdAccount> bdAccounts) {
        List<String> fields = new ArrayList<>();
        fields.add("createTime");
        if (CollUtil.isNotEmpty(bdAccounts)) {
            for (BdAccount bdAccount : bdAccounts) {
                fields.add(MemberFieldConstants.ACCOUNT + bdAccount.getFieldValue());
                fields.add(MemberFieldConstants.SETTLE_ACCOUNT + bdAccount.getFieldValue());
            }
        }
        return fields;
    }

    /**
     * 余额汇总
     *
     * @param memberAccountDetailLookVO 余额信息
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.MEMBER_BALANCE_SUMMARY, business = EOperationBusiness.MEMBER_BALANCE_SUMMARY_DETAIL, method = EOperationMethod.SELECT, remark = "余额汇总详情")
    @PostMapping("/detail-list")
    public TableDataInfo findAccountDetailLookList(@RequestBody CuMemberAccountDetailLookVO memberAccountDetailLookVO) {
        List<CuMemberAccountDetailLookVO> memberAccountDetailLookVOList = new ArrayList<>();
        String createTime = memberAccountDetailLookVO.getCreateTime();
        if (StringUtils.isEmpty(createTime)) {
            return getDataTable(memberAccountDetailLookVOList);
        }
        if (CollUtil.isEmpty(memberAccountDetailLookVO.getPkAccounts())) {
            return getDataTable(memberAccountDetailLookVOList);
        }
        Integer countryId = SecurityUtils.getPkCountry();
        memberAccountDetailLookVO.setPkCountry(countryId);
        startPage();
        Object[] objects = memberAccountDetailService.selectAccountDetailLookList(memberAccountDetailLookVO);
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberAccountDetailExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberAccountDetailLookExtVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 余额汇总导出
     *
     * @param response
     * @param memberAccountDetailLookVO 查询入参
     */
    @Log(module = EOperationModule.MEMBER_BALANCE_SUMMARY, business = EOperationBusiness.MEMBER_BALANCE_SUMMARY_DETAIL, method = EOperationMethod.EXPORT, remark = "余额汇总详情导出")
    @PostMapping("/detail-export")
    public void detailExport(HttpServletResponse response, CuMemberAccountDetailLookVO memberAccountDetailLookVO) {
        List<CuMemberAccountDetailLookExtVO> memberAccountDetailLookVOList;
        String createTime = memberAccountDetailLookVO.getCreateTime();

        Integer pkCountry = SecurityUtils.getPkCountry();
        memberAccountDetailLookVO.setPkCountry(pkCountry);
        memberAccountDetailLookVOList = StringUtils.isBlank(createTime) ? new ArrayList<>() : (List<CuMemberAccountDetailLookExtVO>) memberAccountDetailService.selectAccountDetailLookList(memberAccountDetailLookVO)[1];
        ExcelUtil<CuMemberAccountDetailLookExtVO> util = new ExcelUtil<>(CuMemberAccountDetailLookExtVO.class, getDetailShowField(memberAccountDetailLookVO));
        util.exportExcel(response, memberAccountDetailLookVOList, FinanceMsgConstants.BALANCE_SUMMARY_DETAILS_EXPORT);
    }


    /**
     * 根据前台提交的账号id返回显示字段
     *
     * @param memberAccountDetailLookVO 查询入参
     * @return List<String>
     */
    private List<String> getDetailShowField(CuMemberAccountDetailLookVO memberAccountDetailLookVO) {
        List<Integer> selectedPkAccounts = memberAccountDetailLookVO.getPkAccounts();
        List<String> fields = new ArrayList<>();
        fields.add("memberCode");
        fields.add("memberName");
        fields.add("vertexName");
        fields.add("teamName");
        if (CollUtil.isNotEmpty(selectedPkAccounts)) {
            for (Integer accountValue : selectedPkAccounts) {
                fields.add(MemberFieldConstants.ACCOUNT + accountValue);
                fields.add(MemberFieldConstants.AVAILABLE_ACCOUNT + accountValue);

                fields.add(MemberFieldConstants.SETTLE_ACCOUNT + accountValue);
                fields.add(MemberFieldConstants.SETTLE_AVAILABLE_ACCOUNT + accountValue);

                if (accountValue < 11) {
                    fields.add(MemberFieldConstants.LOCK_ACCOUNT + accountValue);
                    fields.add(MemberFieldConstants.LOCK_TRANSFER_ACCOUNT + accountValue);
                }
                if (accountValue < 11) {
                    fields.add(MemberFieldConstants.LOCK_SETTLE_ACCOUNT + accountValue);
                    fields.add(MemberFieldConstants.LOCK_SETTLE_TRANSFER_ACCOUNT + accountValue);
                }
                if (accountValue == 4) {
                    fields.add(MemberFieldConstants.WITHDRAW_ACCOUNT + accountValue);
                    fields.add(MemberFieldConstants.LOCK_WITHDRAW_ACCOUNT + accountValue);
                    fields.add(MemberFieldConstants.SETTLE_AVAILABLE_ACCOUNT + accountValue);
                    fields.add(MemberFieldConstants.LOCK_SETTLE_WITHDRAW_ACCOUNT + accountValue);
                }
            }
        }
        return fields;
    }

    /**
     * 显示的字段
     *
     * @return AjaxResult
     */
    @GetMapping("/list-field")
    public AjaxResult accountListField() {
        List<String> fieldList = new ArrayList<>();
        List<BdAccount> bdAccounts = baseService.getAccountByCountryId(SecurityUtils.getPkCountry());
        fieldList.add("createTime");
        if (CollUtil.isNotEmpty(bdAccounts)) {
            for (BdAccount bdAccount : bdAccounts) {
                fieldList.add(MemberFieldConstants.ACCOUNT + bdAccount.getFieldValue());
                fieldList.add(MemberFieldConstants.SETTLE_ACCOUNT + bdAccount.getFieldValue());
            }
        }
        return AjaxResult.success(fieldList);

    }


    /**
     * 显示的字段
     *
     * @return AjaxResult
     */
    @GetMapping("/detail-field")
    public AjaxResult accountDetailField(@RequestParam(required = false) List<Integer> selectedPkAccounts) {
        List<String> list = new ArrayList<>();
        if (CollUtil.isEmpty(selectedPkAccounts)) {
            Field[] fields = ReflectUtil.getFields(CuMemberAccountDetailLookExtVO.class);
            for (Field field : fields) {
                Excel annotation = field.getAnnotation(Excel.class);
                if (null != annotation) {
                    list.add(field.getName());
                }
            }
        } else {
            CuMemberAccountDetailLookVO detailLookVO = new CuMemberAccountDetailLookVO();
            detailLookVO.setPkAccounts(selectedPkAccounts);
            list = getDetailShowField(detailLookVO);
        }

        //List<String> fieldName = Arrays.stream(fields).map(f -> f.getName()).collect(Collectors.toList());
        return AjaxResult.success(list);

    }

}
