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


import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.domain.R;
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.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.domain.member.base.CuMember;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.report.member.service.IMemberService;
import com.hzs.report.member.vo.MallPVStatVO;
import com.hzs.report.member.vo.UmbrellaBelowPushVO;
import com.hzs.sale.order.ISaOrderServiceApi;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 伞下直推
 * </p>
 *
 * @author zhangjing
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/manage/umbrella-below-push")
public class CuUmbrellaBelowPushController extends BaseController {

    @DubboReference
    private IMemberServiceApi memberServiceApi;
    @Autowired
    private IMemberService memberService;
    @DubboReference
    private ISaOrderServiceApi orderServiceApi;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * @description: 伞下直推
     * @author: zhang jing
     * @date: 2023/5/18 9:57
     * @param: []
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.UMBRELLA_DIRECT_PUSH, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(UmbrellaBelowPushVO uvo) {
        List<UmbrellaBelowPushVO> uvoExtList = new ArrayList<>();
        if (StringUtils.isEmpty(uvo.getMemberCode())) {
            return getDataTable(uvoExtList);
        }
        R<CuMember> member = memberServiceApi.getMember(uvo.getMemberCode());
        if (null != member.getData()) {
            //查询伞下推荐人
            startPage();
            uvo.setPkMember(member.getData().getPkId());
            uvoExtList = memberService.queryUmbrellaBelowPush(uvo);
            List<Long> pkMemberList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(uvoExtList)) {
                uvoExtList.forEach(itm -> {
                    pkMemberList.add(itm.getPkMember());
                });
            }
            if (CollectionUtil.isNotEmpty(pkMemberList)) {
                uvo.setPkMemberList(pkMemberList);
                List<UmbrellaBelowPushVO> ordeList = memberService.queryOrderExpend(uvo);
                Map<Long, BigDecimal> orderMap = new HashMap<>();
                if (CollectionUtil.isNotEmpty(ordeList)) {
                    ordeList.forEach(itms -> {
                        if (null != orderMap.get(itms.getPkMember())) {
                            orderMap.put(itms.getPkMember(), itms.getOrderAchieve().add(orderMap.get(itms.getPkMember())));
                        } else {
                            orderMap.put(itms.getPkMember(), itms.getOrderAchieve());
                        }
                    });
                }
                if (orderMap != null) {
                    uvoExtList.forEach(itm -> {
                        itm.setOrderAchieve(orderMap.get(itm.getPkMember()));
                    });
                }
            }
        }
        TableDataInfo tableDataInfo = getDataTable(uvoExtList);
        tableDataInfo.setRows(uvoExtList);
        return tableDataInfo;
    }

    /**
     * @description: 导出 伞下直推
     * @author: zhang jing
     * @date: 2023/8/31 9:15
     * @param: [response, uvo]
     * @return: void
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.UMBRELLA_DIRECT_PUSH, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UmbrellaBelowPushVO uvo) {
        List<UmbrellaBelowPushVO> list = new ArrayList<>();
        if (StringUtils.isEmpty(uvo.getMemberCode())) {
            ExcelUtil<UmbrellaBelowPushVO> util = new ExcelUtil<>(UmbrellaBelowPushVO.class);
            util.exportExcel(response, list, "伞下直推统计");
        }
        Map<Integer, String> transactionMap1 = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_GRADE,
                EnumsPrefixConstants.KEY_AWARD);
        R<CuMember> member = memberServiceApi.getMember(uvo.getMemberCode());
        if (null != member.getData()) {
            //查询伞下推荐人
            startPage();
            uvo.setPkMember(member.getData().getPkId());
            list = memberService.queryUmbrellaBelowPush(uvo);
            List<Long> pkMemberList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(list)) {
                list.forEach(itm -> {
                    pkMemberList.add(itm.getPkMember());
                });
            }
            if (CollectionUtil.isNotEmpty(pkMemberList)) {
                uvo.setPkMemberList(pkMemberList);
                List<UmbrellaBelowPushVO> ordeList = memberService.queryOrderExpend(uvo);
                Map<Long, BigDecimal> orderMap = new HashMap<>();
                if (CollectionUtil.isNotEmpty(ordeList)) {
                    ordeList.forEach(itms -> {
                        if (null != orderMap.get(itms.getPkMember())) {
                            orderMap.put(itms.getPkMember(), itms.getOrderAchieve().add(orderMap.get(itms.getPkMember())));
                        } else {
                            orderMap.put(itms.getPkMember(), itms.getOrderAchieve());
                        }
                    });
                }
                if (orderMap != null) {
                    list.forEach(itm -> {
                        itm.setOrderAchieve(orderMap.get(itm.getPkMember()));
                        itm.setPkAwardsVal(transactionMap1.get(itm.getPkAwards()));
                        itm.setPkRegisterGradeVal(transactionMap1.get(itm.getPkRegisterGrade()));
                    });
                }
            }
        }
        ExcelUtil<UmbrellaBelowPushVO> util = new ExcelUtil<>(UmbrellaBelowPushVO.class);
        util.exportExcel(response, list, "伞下直推统计");
    }


}
