package com.ruoyi.project.business.controller.board;

import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TcProject;
import com.ruoyi.project.business.domain.vo.board.*;
import com.ruoyi.project.business.service.ITcAccountService;
import com.ruoyi.project.business.service.ITcLeadService;
import com.ruoyi.project.business.service.ITcProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
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.time.LocalDate;
import java.util.List;


/**
 * @author fangk
 * @date 2024/4/25
 * @desc BoardController
 */
@RequestMapping("/board")
@RestController
@RequiredArgsConstructor
public class BoardController extends BaseController {

    private final ITcLeadService tcLeadService;

    private final ITcAccountService tcAccountService;
    
    private final ITcProjectService tcProjectService;

    /**
     * 近半年内线索数量
     */
    @GetMapping("/leadCountStat")
    public AjaxResult getLeadCountStat(
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            String statMode) {
        BarChartVo vo = tcLeadService.getLeadCountStat(startDate, endDate, statMode);
        return AjaxResult.success(vo);
    }

    /**
     * 近半年内客户数量
     */
    //@GetMapping("/customerCountStat")
    //public AjaxResult getCustomerCountStat(List<LocalDate> leadDateRange) {
    //    BarChartVo vo = tcAccountService.getCustomerCountStat();
    //    return AjaxResult.success(vo);
    //}
    @GetMapping("/customerCountStat")
    public AjaxResult getCustomerCountStat( @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
                                            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
                                            String statMode) {
        BarChartVo vo = tcAccountService.getCustomerCountStat(startDate, endDate, statMode);
        return AjaxResult.success(vo);
    }

    /**
     * 项目跟进记录统计
     */
    @GetMapping("/getCountAndPieData")
    public AjaxResult getCountAndPieData(ProjectFollowReq req) {
        //return AjaxResult.success(tcProjectService.getCountAndPieData(req));
        return null;
    }

    ////getUnFollowedList
    //@GetMapping("/getUnFollowedList")
    //public AjaxResult getUnFollowedList() {
    //    return AjaxResult.success(tcProjectService.getUnFollowedList());
    //}

    /**
     *  查询 未跟进 项目列表 分页
     * @param unfollowedMode    ：查询模式：1：最近一周未跟进；2：最近一月未跟进；3：最近半年未跟进
     * @return
     */
    @GetMapping("/getUnFollowedList")
    public TableDataInfo getUnFollowedList(String unfollowedMode) {
        startPage();
        List<TcProject> list = tcProjectService.getUnFollowedList(unfollowedMode);
        return getDataTable(list);
    }

    /**
     * @Author renzx
     * @Date 2024/8/27
     * @Description 代理商成单率和丢单率统计
     **/
    @GetMapping("/getAgentProjectStatisticsList")
    public TableDataInfo getAgentProjectStatisticsList(AgentProjectStatistics agentProjectStatistics) {
        startPage();
        return getDataTable(tcProjectService.getAgentProjectStatisticsList(agentProjectStatistics));
    }

    /**
     * @Author renzx
     * @Date 2024/8/27
     * @Description 代理商成单率和丢单率统计 导出
     **/
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/exportgetAgentProjectStatistics")
    public void exportAgentAddProject(HttpServletResponse response, AgentProjectStatistics agentProjectStatistics) {
        List<AgentProjectStatisticsVo> list = tcProjectService.getAgentProjectStatisticsList(agentProjectStatistics);
        list.stream()
                .filter(item -> item.getOwnerAgent() == 0)
                .forEach(item -> item.setOwnerAgentName("总部"));
        ExcelUtil<AgentProjectStatisticsVo> util = new ExcelUtil<AgentProjectStatisticsVo>(AgentProjectStatisticsVo.class);
        String[] finalColumns = {"areaName"};
        if (!agentProjectStatistics.isShowColumn()){
            util.hideColumn(finalColumns);
        }
        util.exportExcel(response, list, MessageUtils.message("account.exportSheetName"));
    }

    /**
     * @Author renzx
     * @Date 2024/8/29
     * @Description 新代理商成单统计
     **/
    @GetMapping("/getAgentCompleteOrderList")
    public TableDataInfo getAgentCompleteOrderList(AgentCompleteOrder agentCompleteOrder) {
        startPage();
        return getDataTable(tcProjectService.getAgentCompleteOrderList(agentCompleteOrder));
    }

    /**
     * @Author renzx
     * @Date 2024/8/29
     * @Description 新代理商成单统计 导出
     **/
    @Log(title = "新代理商成单统计", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAgentCompleteOrder")
    public void exportCustomerCompleteOrder(HttpServletResponse response, AgentCompleteOrder agentCompleteOrder ) {
        List<AgentCompleteOrderVo> list = tcProjectService.getAgentCompleteOrderList(agentCompleteOrder);
        list.stream()
                .filter(item -> item.getAgentId() == 0)
                .forEach(item -> item.setAgentName("总部"));
        ExcelUtil<AgentCompleteOrderVo> util = new ExcelUtil<AgentCompleteOrderVo>(AgentCompleteOrderVo.class);
        String[] finalColumns = {"areaName"};
        if (!agentCompleteOrder.isShowColumn()){
            util.hideColumn(finalColumns);
        }
        util.exportExcel(response, list, MessageUtils.message("account.exportSheetName"));
    }

    /**
     * @Author renzx
     * @Date 2024/8/29
     * @Description 客户成单统计
     **/
    @GetMapping("/getAccountCompleteOrderList")
    public TableDataInfo getAccountCompleteOrderList(AgentCompleteOrder agentCompleteOrder) {
        startPage();
        return getDataTable(tcProjectService.getAccountCompleteOrderList(agentCompleteOrder));
    }

    /**
     * @Author renzx
     * @Date 2024/8/29
     * @Description 客户成单统计 导出
     **/
    @Log(title = "新代理商成单统计", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAccountCompleteOrder")
    public void exportAccountCompleteOrder(HttpServletResponse response, AgentCompleteOrder agentCompleteOrder ) {
        List<AccountCompleteOrderVo> list = tcProjectService.getAccountCompleteOrderList(agentCompleteOrder);
        ExcelUtil<AccountCompleteOrderVo> util = new ExcelUtil<AccountCompleteOrderVo>(AccountCompleteOrderVo.class);
        String[] finalColumns = {"areaName"};
        if (!agentCompleteOrder.isShowColumn()){
            util.hideColumn(finalColumns);
        }
        util.exportExcel(response, list, MessageUtils.message("account.exportSheetName"));
    }

    /**
     * @Author renzx
     * @Date 2024/8/30
     * @Description 新代理商成单统计
     **/
    @GetMapping("/getAgentTargetProgressList")
    public TableDataInfo getAgentTargetProgressList(AgentCompleteOrder agentCompleteOrder) {
        startPage();
        return getDataTable(tcProjectService.getAgentTargetProgressList(agentCompleteOrder));
    }
}