package com.hzs.bonus.detail.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.bonus.detail.service.ICuRegionAssessService;
import com.hzs.bonus.detail.vo.CuRegionAssessExcelVO;
import com.hzs.bonus.detail.vo.CuRegionAssessVO;
import com.hzs.bonus.param.CuRegionAssessParam;
import com.hzs.common.core.constant.TableNameConstants;
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.detail.CuRegionAssessDetail;
import com.hzs.common.domain.member.detail.ext.CuRegionAssessExt;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 新零售会员区域考核 前端控制器
 */
@RestController
@RequestMapping("/manage/region")
public class CuRegionAssessController extends BaseController {

    @Autowired
    private ICuRegionAssessService cuRegionAssessService;

    @Autowired
    private ICuMemberSettlePeriodService cuMemberSettlePeriodService;

    @Autowired
    private ICuRegionAssessService regionAssessService;

    /**
     * 考核
     * 考核默认考核当月
     *
     * @return AjaxResult
     */
    @GetMapping("/examine")
    public AjaxResult calculationCuRegionAssess(String settleDate) {
        Integer period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(settleDate).getPkId();
        String rangeTableName = TableNameConstants.CU_MEMBER_RETAIL_RANGE + period;
        cuRegionAssessService.assessRegionByEveryMonth(settleDate, rangeTableName);
        return AjaxResult.success();
    }

    /**
     * 查询会员昨天奖金的值
     */
    @PostMapping("/query")
    public TableDataInfo listRegionAssess(@RequestBody CuRegionAssessParam cuRegionAssessParam) {
        startPage();
        cuRegionAssessParam.setPkCountry(SecurityUtils.getPkCountry());
        List<CuRegionAssessExt> cuRegionAssessExtList = regionAssessService.queryRegionAssessByCondition(cuRegionAssessParam);
        List<CuRegionAssessVO> cuRegionAssessVoList = regionAssessService.getCuRegionAssessVoS(cuRegionAssessExtList);
        TableDataInfo dataTable = getDataTable(cuRegionAssessExtList);
        dataTable.setRows(cuRegionAssessVoList);
        return dataTable;
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @PostMapping("/export")
    public void exportRegionAssess(HttpServletResponse response, CuRegionAssessParam cuRegionAssessParam) {
        List<CuRegionAssessExt> cuRegionAssessExtList = regionAssessService.queryRegionAssessByCondition(cuRegionAssessParam);
        List<CuRegionAssessExcelVO> cuRegionAssessVoList = new ArrayList<>();
        for (CuRegionAssessExt cuRegionAssessExt : cuRegionAssessExtList) {
            for (CuRegionAssessDetail cuRegionAssessDetail : cuRegionAssessExt.getCuRegionAssessDetailList()) {
                CuRegionAssessExcelVO regionAssessExcelVO = BeanUtil.copyProperties(cuRegionAssessExt, CuRegionAssessExcelVO.class);
                regionAssessExcelVO.setAssessDate(cuRegionAssessDetail.getAssessDate());
                regionAssessExcelVO.setTeamMonthPv(cuRegionAssessDetail.getTeamMonthPv());
                regionAssessExcelVO.setSmallAreaPv(cuRegionAssessDetail.getSmallAreaPv());
                regionAssessExcelVO.setAssessStatus(cuRegionAssessDetail.getAssessStatus());
                cuRegionAssessVoList.add(regionAssessExcelVO);
            }
        }
        ExcelUtil<CuRegionAssessExcelVO> util = new ExcelUtil<>(CuRegionAssessExcelVO.class);
        util.exportExcel(response, cuRegionAssessVoList, "区域考核导出");
    }
}
