package com.ruoyi.web.controller.wocom;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CorpContextHolder;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.excel.wecom.service.ICustomerStatisticsDataService;
import com.ruoyi.excel.wecom.vo.CustomerStatisticsDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 客户统计数据Controller
 */
@RestController
@RequestMapping("/wecom/customerStatistics")
public class CustomerStatisticsDataController extends BaseController {

    @Autowired
    private ICustomerStatisticsDataService customerStatisticsDataService;

    /**
     * 查询客户统计数据列表
     */
    @PreAuthorize("@ss.hasPermi('wecom:customerStatistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(value = "dataType", required = false) String dataType,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "week", required = false) Integer week,
            @RequestParam(value = "yearMonth", required = false) String yearMonth,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(value = "indicatorName", required = false) String indicatorName) {
        String corpId = CorpContextHolder.getCurrentCorpId();

        List<CustomerStatisticsDataVO> list;
        if ("week".equals(dataType)) {
            list = customerStatisticsDataService.selectByWeekAggregation(corpId, year, week, indicatorName);
            return getDataTable(list);
        } else if ("month".equals(dataType)) {
            list = customerStatisticsDataService.selectByMonthAggregation(corpId, yearMonth, indicatorName);
            return getDataTable(list);
        } else if ("all".equals(dataType)) {
            list = customerStatisticsDataService.selectAllAggregation(corpId, indicatorName);
            return getDataTable(list);
        } else {
            startPage();
            list = customerStatisticsDataService.selectCustomerStatisticsDataVOList(corpId, startDate, endDate, indicatorName);
            return getDataTable(list);
        }
    }

    /**
     * 导出客户统计数据列表
     */
    @PreAuthorize("@ss.hasPermi('wecom:customerStatistics:export')")
    @Log(title = "客户统计数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "dataType", required = false) String dataType,
                       @RequestParam(value = "year", required = false) Integer year,
                       @RequestParam(value = "week", required = false) Integer week,
                       @RequestParam(value = "yearMonth", required = false) String yearMonth,
                       @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                       @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                       @RequestParam(value = "indicatorName", required = false) String indicatorName) {
        String corpId = CorpContextHolder.getCurrentCorpId();

        List<CustomerStatisticsDataVO> dataList;
        if ("week".equals(dataType)) {
            dataList = customerStatisticsDataService.selectByWeekAggregation(corpId, year, week, indicatorName);
        } else if ("month".equals(dataType)) {
            dataList = customerStatisticsDataService.selectByMonthAggregation(corpId, yearMonth, indicatorName);
        } else if ("all".equals(dataType)) {
            dataList = customerStatisticsDataService.selectAllAggregation(corpId, indicatorName);
        } else {
            dataList = customerStatisticsDataService.selectCustomerStatisticsDataVOList(corpId, startDate, endDate, indicatorName);
        }

        ExcelUtil<CustomerStatisticsDataVO> util = new ExcelUtil<>(CustomerStatisticsDataVO.class);
        util.exportExcel(response, dataList, "流量看板数据");
    }

    /**
     * 获取客户统计数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('wecom:customerStatistics:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(customerStatisticsDataService.selectCustomerStatisticsDataById(id));
    }


    /**
     * 修改客户成本数据
     */
    @PreAuthorize("@ss.hasPermi('wecom:customerStatistics:edit')")
    @Log(title = "客户统计数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestParam(value = "curDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date curDate,
                           @RequestParam(value = "cost")BigDecimal cost,
                           @RequestParam(value = "type")String type,
                           @RequestParam(value = "attr")String attr) {
        String corpId = CorpContextHolder.getCurrentCorpId();
        return toAjax(customerStatisticsDataService.updateCost(corpId,curDate,cost,type,attr));
    }

}
