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

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EStatisticsScope;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberAccountExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.account.param.AccountSummaryStatisticsParam;
import com.hzs.member.account.service.ICuMemberAccountService;
import com.hzs.member.account.vo.AccountSummaryStatisticsVO;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.system.config.IVertexServiceApi;
import com.hzs.system.config.dto.VertexDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Description: 会员余额汇总统计
 * @Author: jiang chao
 * @Time: 2023/12/11 14:50
 * @Classname: CuMemberAccountSummaryController
 * @PackageName: com.hzs.member.account.controller.manage
 */
@RestController
@RequestMapping("/manager/account-summary")
public class CuMemberAccountSummaryController {

    @Autowired
    private ICuMemberAccountService iCuMemberAccountService;
    @Autowired
    private ICuMemberService iCuMemberService;

    @DubboReference
    IVertexServiceApi iVertexServiceApi;

    /**
     * 统计数据
     *
     * @param param
     * @return
     */
    @AccessPermissions("BalanceSummaryTotal")
    @GetMapping("/statistics")
    public AjaxResult statistics(AccountSummaryStatisticsParam param) {
        if (null == param.getType()) {
            // 缺少参数
            return AjaxResult.error("缺少查询类型");
        }
        if (EStatisticsScope.TOP.getValue() == param.getType() && null == param.getPkMember()
                || EStatisticsScope.VERTEX.getValue() == param.getType() && null == param.getPkVertex()
                || EStatisticsScope.MEMBER.getValue() == param.getType() && null == param.getMemberCode()) {
            // 缺少查询参数
            return AjaxResult.error("缺少查询条件");
        }

        // 编号
        String code = null;
        // 名称
        String name = null;
        // 所属国家
        Integer pkCountry = SecurityUtils.getPkCountry();
        // 统计余额
        CuMemberAccountExt cuMemberAccountExt = new CuMemberAccountExt();

        if (EStatisticsScope.TOP.getValue() == param.getType()) {
            // 按顶点统计
            CuMember cuMember = iCuMemberService.queryMember(param.getPkMember());
            if (cuMember != null) {
                cuMemberAccountExt = iCuMemberAccountService.queryAccountSummaryByMember(cuMember.getMemberCode(), pkCountry);
                code = cuMember.getMemberCode();
                name = cuMember.getMemberName();
            } else {
                // 数据不存在
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
            }
        } else if (EStatisticsScope.VERTEX.getValue() == param.getType()) {
            // 按体系统计
            R<VertexDTO> rVertex = iVertexServiceApi.findById(param.getPkVertex());
            if (rVertex.isSuccess() && null != rVertex.getData()) {
                cuMemberAccountExt = iCuMemberAccountService.queryAccountSummaryByVertex(param.getPkVertex(), pkCountry);
                code = rVertex.getData().getMemberCode();
                name = rVertex.getData().getVertexName();
            } else {
                // 数据不存在
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
            }
        } else if (EStatisticsScope.MEMBER.getValue() == param.getType()) {
            // 按会员统计
            CuMember cuMember = iCuMemberService.queryMember(param.getMemberCode());
            if (cuMember != null) {
                cuMemberAccountExt = iCuMemberAccountService.queryAccountSummaryByMember(param.getMemberCode(), pkCountry);
                code = cuMember.getMemberCode();
                name = cuMember.getMemberName();
            } else {
                // 数据不存在
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
            }
        }

        AccountSummaryStatisticsVO vo = BeanUtil.copyProperties(cuMemberAccountExt, AccountSummaryStatisticsVO.class);
        vo.setCode(code);
        vo.setName(name);
        return AjaxResult.success(vo);
    }


    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountSummaryStatisticsParam param) {
        if (null == param.getType()) {
            // 缺少参数
            throw new ServiceException(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        if (EStatisticsScope.TOP.getValue() == param.getType() && null == param.getPkMember()
                || EStatisticsScope.VERTEX.getValue() == param.getType() && null == param.getPkVertex()
                || EStatisticsScope.MEMBER.getValue() == param.getType() && null == param.getMemberCode()) {
            // 缺少参数
            throw new ServiceException(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        // 编号
        String code = null;
        // 名称
        String name = null;
        // 所属国家
        Integer pkCountry = SecurityUtils.getPkCountry();
        // 统计余额
        CuMemberAccountExt cuMemberAccountExt = new CuMemberAccountExt();

        if (EStatisticsScope.TOP.getValue() == param.getType()) {
            // 按顶点统计
            CuMember cuMember = iCuMemberService.queryMember(param.getPkMember());
            if (cuMember != null) {
                cuMemberAccountExt = iCuMemberAccountService.queryAccountSummaryByMember(cuMember.getMemberCode(), pkCountry);
                code = cuMember.getMemberCode();
                name = cuMember.getMemberName();
            }
        } else if (EStatisticsScope.VERTEX.getValue() == param.getType()) {
            // 按体系统计
            R<VertexDTO> rVertex = iVertexServiceApi.findById(param.getPkVertex());
            if (rVertex.isSuccess() && null != rVertex.getData()) {
                cuMemberAccountExt = iCuMemberAccountService.queryAccountSummaryByVertex(param.getPkVertex(), pkCountry);
                code = rVertex.getData().getMemberCode();
                name = rVertex.getData().getVertexName();
            }
        } else if (EStatisticsScope.MEMBER.getValue() == param.getType()) {
            // 按会员统计
            CuMember cuMember = iCuMemberService.queryMember(param.getMemberCode());
            if (cuMember != null) {
                cuMemberAccountExt = iCuMemberAccountService.queryAccountSummaryByMember(param.getMemberCode(), pkCountry);
                code = cuMember.getMemberCode();
                name = cuMember.getMemberName();
            }
        }

        AccountSummaryStatisticsVO vo = BeanUtil.copyProperties(cuMemberAccountExt, AccountSummaryStatisticsVO.class);
        vo.setCode(code);
        vo.setName(name);


        ExcelUtil<AccountSummaryStatisticsVO> util = new ExcelUtil<>(AccountSummaryStatisticsVO.class);
        util.exportExcel(response, Collections.singletonList(vo), "余额汇总统计导出");
    }

}
