package com.fs.company.controller.crm;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.company.service.ICrmStatisticManageService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.statis.dto.ComprehensiveStatisticsDTO;
import com.fs.statis.param.ComprehensiveStatisticsParam;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: Guos
 * @time: 2025/11/13 下午3:19
 */
@RestController
@RequestMapping("/crm/ComprehensiveStatistics")
public class ComprehensiveStatisticsController extends BaseController {

    @Resource
    private ICrmStatisticManageService iCrmStatisticManageService;

    /**
     * 获取综合统计信息
     * @param param
     * @return
     */
    @PostMapping("/statisticMainN")
    public R statisticMainN(@RequestBody ComprehensiveStatisticsParam param) {
        Assert.notNull(param.getDimension(), "请选择统计维度");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long companyId = loginUser.getCompany().getCompanyId();
        param.setCompanyId(companyId);
        param.setTimeGroupFlag(false);
        return R.ok().put("data", iCrmStatisticManageService.statisticMain(param));
    }

    /**
     * 日综合获取统计信息(需要按时间分组)
     * @param param
     * @return
     */
    @PostMapping("/statisticMain")
    public R statisticMain(@RequestBody ComprehensiveStatisticsParam param) {
        Assert.notNull(param.getDimension(), "请选择统计维度");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long companyId = loginUser.getCompany().getCompanyId();
        param.setCompanyId(companyId);
        param.setTimeGroupFlag(true);
        return R.ok().put("data", iCrmStatisticManageService.statisticMain(param));
    }

    /**
     * 获取公司下拉
     * @return
     */
    @GetMapping("/getSearchCompanyInfo")
    public R getSearchCompanyInfo(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long companyId = loginUser.getCompany().getCompanyId();
        return R.ok().put("data", iCrmStatisticManageService.getSearchCompanyInfo(companyId));
    }

    /**
     * 根据公司id获取部门下拉
     * @return
     */
    @GetMapping("/getSearchDeptInfo")
    public R getSearchDeptInfo(@RequestParam("id") Long id){
        return R.ok().put("data", iCrmStatisticManageService.getSearchDeptInfo(id));
    }

    /**
     * 根据部门id获取用户下拉信息
     * @return
     */
    @GetMapping("/getSearchUserInfo")
    public R getSearchUserInfo(@RequestParam("id") Long id){
        return R.ok().put("data", iCrmStatisticManageService.getSearchUserInfo(id));
    }

    /**
     * 导出综合统计列表
     */
    @GetMapping("/export")
    public AjaxResult export(@RequestBody ComprehensiveStatisticsParam param) {
        R r = statisticMain(param);
        List<ComprehensiveStatisticsDTO> data = (List<ComprehensiveStatisticsDTO>)r.get("data");
        ExcelUtil<ComprehensiveStatisticsDTO> util = new ExcelUtil<ComprehensiveStatisticsDTO>(ComprehensiveStatisticsDTO.class);
        return util.exportExcel(data, "综合统计");
    }

}
