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

import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.utils.DateUtils;
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.security.utils.SecurityUtils;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.census.param.CensusSummaryParam;
import com.hzs.member.census.vo.CensusSummaryVo;
import com.hzs.system.sys.IMenuColumnServiceApi;
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.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/2/6 11:50
 * @Classname: censusSummaryController
 * @PackageName: com.hzs.member.censusSummary
 */
@RestController
@RequestMapping("/manage/member-structure")
public class CensusSummaryController extends BaseController {

    @Autowired
    private ICuMemberSettlePeriodService memberSettlePeriodService;
    @Autowired
    private ICuMemberService memberService;

    @DubboReference
    IMenuColumnServiceApi iMenuColumnServiceApi;

    /**
     * 业绩汇总
     *
     * @param censusSummaryParam
     * @return
     */
    @AccessPermissions("performanceTotal")
    @GetMapping("/list")
    public AjaxResult list(CensusSummaryParam censusSummaryParam) {
        if (censusSummaryParam.getStartDate() == null || censusSummaryParam.getEndDate() == null) {
            return AjaxResult.success(new ArrayList<>());
        }
        List<Long> pkVertex = new ArrayList<>();
        if (StringUtils.isNotEmpty(censusSummaryParam.getPkBdVertexStr())) {
            pkVertex = Arrays.stream(censusSummaryParam.getPkBdVertexStr().split(","))
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
        }
        censusSummaryParam.setPkVertex(pkVertex);
        // 当天查询秒结
        Date date = new Date();
        // 查询当天
        String thisTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);

        // 查询当前时间范围
        List<String> days = new ArrayList();
        if (censusSummaryParam.getStartDate() != null && censusSummaryParam.getEndDate() != null) {
            days = DateUtils.findDaysStr(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, censusSummaryParam.getStartDate()), DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, censusSummaryParam.getEndDate()));
        } else {
            // 默认查询当天
            days.add(thisTime);
        }

        List<CensusSummaryVo> resultList = memberService.selectCensusSummary(censusSummaryParam, days);

        return AjaxResult.success(resultList);
    }

    /**
     * 导出（导出）
     */
    @PostMapping("/export")
    public void export(CensusSummaryParam censusSummaryParam, HttpServletResponse response) {
        if (censusSummaryParam.getStartDate() == null || censusSummaryParam.getEndDate() == null) {
            ExcelUtil<CensusSummaryVo> util = new ExcelUtil<>(CensusSummaryVo.class);
            util.exportExcel(response, new ArrayList<>(), "业绩汇总信息");
        }

        // 当天查询秒结
        Date date = new Date();
        // 查询当天
        String thisTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);
        if (censusSummaryParam.getMemberSettlePeriodId() != null) {
            memberSettlePeriodService.getById(censusSummaryParam.getMemberSettlePeriodId());
        } else {
            memberSettlePeriodService.getCuMemberSettlePeriodByDate(thisTime);
        }

        // 查询当前时间范围
        List<String> days = new ArrayList();
        if (censusSummaryParam.getStartDate() != null && censusSummaryParam.getEndDate() != null) {
            days = DateUtils.findDaysStr(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, censusSummaryParam.getStartDate()), DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, censusSummaryParam.getEndDate()));
        } else {
            // 默认查询当天
            days.add(thisTime);
        }

        List<CensusSummaryVo> resultList = memberService.selectCensusSummary(censusSummaryParam, days);
        ExcelUtil<CensusSummaryVo> util = new ExcelUtil<>(CensusSummaryVo.class, iMenuColumnServiceApi.queryMenuColumn("performanceTotal", SecurityUtils.getUserId()).getData());
        util.exportExcel(response, resultList, "业绩汇总信息");
    }

}
