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.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.member.base.IMemberServiceApi;
import com.hzs.report.member.service.IMemberService;
import com.hzs.report.member.vo.UmbrellaBelowPushVO;
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;
import java.util.stream.Collectors;

/**
 * 伞下直推
 */
@RestController
@RequestMapping("/manage/umbrella-below-push")
public class CuUmbrellaBelowPushController extends BaseController {

    @Autowired
    private IMemberService iMemberService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    /**
     * 伞下直推
     **/
    @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 = iMemberServiceApi.getMember(uvo.getMemberCode());
        if (null != member.getData()) {
            uvo.setPkMember(member.getData().getPkId());
            //查询伞下推荐人
            startPage();
            uvoExtList = iMemberService.queryUmbrellaBelowPush(uvo);
            if (CollectionUtil.isNotEmpty(uvoExtList)) {
                uvo.setPkMemberList(uvoExtList.stream().map(UmbrellaBelowPushVO::getPkMember).collect(Collectors.toList()));
                List<UmbrellaBelowPushVO> ordeList = iMemberService.queryOrderExpend(uvo);
                Map<Long, BigDecimal> orderMap = new HashMap<>();
                if (CollectionUtil.isNotEmpty(ordeList)) {
                    ordeList.forEach(itms -> {
                        orderMap.merge(itms.getPkMember(), itms.getOrderAchieve(), (a, b) -> b.add(a));
                    });
                }
                uvoExtList.forEach(itm -> {
                    itm.setOrderAchieve(orderMap.get(itm.getPkMember()));
                });
            }
        }
        TableDataInfo tableDataInfo = getDataTable(uvoExtList);
        tableDataInfo.setRows(uvoExtList);
        return tableDataInfo;
    }

    /**
     * 导出 伞下直推
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.UMBRELLA_DIRECT_PUSH, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UmbrellaBelowPushVO uvo) {
        List<UmbrellaBelowPushVO> uvoExtList = new ArrayList<>();
        if (StringUtils.isEmpty(uvo.getMemberCode())) {
            ExcelUtil<UmbrellaBelowPushVO> util = new ExcelUtil<>(UmbrellaBelowPushVO.class);
            util.exportExcel(response, uvoExtList, "伞下直推统计");
        }
        R<CuMember> member = iMemberServiceApi.getMember(uvo.getMemberCode());
        if (null != member.getData()) {
            uvo.setPkMember(member.getData().getPkId());
            //查询伞下推荐人
            startPage();
            uvoExtList = iMemberService.queryUmbrellaBelowPush(uvo);
            if (CollectionUtil.isNotEmpty(uvoExtList)) {
                uvo.setPkMemberList(uvoExtList.stream().map(UmbrellaBelowPushVO::getPkMember).collect(Collectors.toList()));
                List<UmbrellaBelowPushVO> ordeList = iMemberService.queryOrderExpend(uvo);
                Map<Long, BigDecimal> orderMap = new HashMap<>();
                if (CollectionUtil.isNotEmpty(ordeList)) {
                    ordeList.forEach(itms -> {
                        orderMap.merge(itms.getPkMember(), itms.getOrderAchieve(), (a, b) -> b.add(a));
                    });
                }
                uvoExtList.forEach(itm -> {
                    itm.setOrderAchieve(orderMap.get(itm.getPkMember()));
                });
            }
        }
        ExcelUtil<UmbrellaBelowPushVO> util = new ExcelUtil<>(UmbrellaBelowPushVO.class);
        util.exportExcel(response, uvoExtList, "伞下直推统计");
    }

}
