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.poi.ExcelUtil;
import com.ruoyi.excel.wecom.domain.DepartmentStatisticsData;
import com.ruoyi.excel.wecom.service.IDepartmentStatisticsDataService;
import com.ruoyi.excel.wecom.vo.DepartmentStatisticsDataVO;
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.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 部门统计数据Controller
 */
@RestController
@RequestMapping("/wecom/departmentStatistics")
public class DepartmentStatisticsDataController extends BaseController {

    @Autowired
    private IDepartmentStatisticsDataService departmentStatisticsDataService;

    /**
     * 查询销售看板数据列表
     */
    @PreAuthorize("@ss.hasPermi('wecom:departmentStatistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            @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 = "departmentPath", required = false) String departmentPath) {
        startPage();
        List<DepartmentStatisticsData> list = departmentStatisticsDataService.selectDepartmentStatisticsDataList(startDate, endDate, departmentPath);
        return getDataTable(list);
    }

    /**
     * 导出销售看板数据列表
     */
    @PreAuthorize("@ss.hasPermi('wecom:departmentStatistics:export')")
    @Log(title = "销售看板数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @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 = "departmentPath", required = false) String departmentPath) {
        List<DepartmentStatisticsDataVO> list = departmentStatisticsDataService.selectDepartmentStatisticsDataVOList(startDate, endDate, departmentPath);
        ExcelUtil<DepartmentStatisticsDataVO> util = new ExcelUtil<>(DepartmentStatisticsDataVO.class);
        util.exportExcel(response, list, "销售看板数据");
    }

    /**
     * 获取销售看板数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('wecom:departmentStatistics:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(departmentStatisticsDataService.selectDepartmentStatisticsDataById(id));
    }


    /**
     * 查询销售看板数据列表
     */
    @PreAuthorize("@ss.hasPermi('wecom:departmentStatistics:list')")
    @GetMapping("/summary")
    public AjaxResult summary(
            @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 = "departmentPath", required = false) String departmentPath) {

        Map<String, BigDecimal> map = departmentStatisticsDataService.getSummary(startDate, endDate, departmentPath);
        BigDecimal totalOrders = map.get("totalOrders");
        BigDecimal totalIns = map.get("totalIns");
        if(totalOrders == null) {
            totalOrders = BigDecimal.ZERO;
        }
        if(totalIns == null) {
            totalIns = BigDecimal.ZERO;
        }
        if(totalIns.equals(BigDecimal.ZERO)) {
            map.put("penetrationRate",null);
        } else {
            map.put("penetrationRate",totalOrders.multiply(new BigDecimal(100))
                    .divide(totalIns,2, RoundingMode.HALF_UP)
                    .setScale(2, RoundingMode.HALF_UP));
        }
        return success(map);
    }
}
