package cn.iocoder.yudao.module.base.service.impl;

import cn.iocoder.yudao.module.base.mapper.RepairStatisticsMapper;
import cn.iocoder.yudao.module.base.service.RepairStatisticsService;
import cn.iocoder.yudao.module.base.vo.RepairStatisticsVO;
import cn.iocoder.yudao.module.base.vo.RepairTicketStatisticsVO;
import cn.iocoder.yudao.module.tickets.vo.DlRepairTicketsRespVO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 维修数据统计
 *
 * @author pqz
 */
@Service
@Validated
public class RepairStatisticsServiceImpl implements RepairStatisticsService {

    @Resource
    private RepairStatisticsMapper statisticsMapper;

    /**
     * 客户来源分析饼图
     *
     * @return java.util.List<cn.iocoder.yudao.module.base.vo.RepairStatisticsVO>
     * @author PQZ
     * @date 14:01 2024/10/21
     **/
    @Override
    public List<RepairStatisticsVO> customerSource() {
        return statisticsMapper.customerSource();
    }

    /**
     * 月工单数量、金额走势、支付渠道统计
     *
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author PQZ
     * @date 13:20 2024/10/21
     **/
    @Override
    public Map<String, Object> ticketMain(String type) {
        Map<String, Object> result = new HashMap<>();
        List<RepairStatisticsVO> tickets;
        if ("count".equals(type)) {
            //月工单数量走势折线图
            tickets = statisticsMapper.ticketCountTrend();
        } else if ("amount".equals(type)) {
            //月工单金额走势折线图
            tickets = statisticsMapper.ticketAmountTrend();
        } else {
            //支付渠道柱状图
            tickets = statisticsMapper.paymentWay();
        }
        //取出数值
        List<Integer> data = tickets.stream().map(RepairStatisticsVO::getValue).collect(Collectors.toList());
        //x轴值
        List<String> xData = tickets.stream().map(RepairStatisticsVO::getName).collect(Collectors.toList());
        result.put("data", data);
        result.put("xData", xData);
        return result;
    }

    /**
     * 维修类型相关统计分析饼图
     *
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author PQZ
     * @date 13:25 2024/10/21
     **/
    @Override
    public List<RepairStatisticsVO> repairTypeStatistics() {
        return statisticsMapper.repairTypeStatistics();
    }


    /**
     * 工单总数、今日工单、工单总金额、今日工单金额
     *
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author PQZ
     * @date 13:23 2024/10/21
     **/
    @Override
    public RepairTicketStatisticsVO ticketStatistics() {
        return statisticsMapper.ticketStatistics();
    }

    /**
     * 工单状态数量统计
     *
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author PQZ
     * @date 13:28 2024/10/21
     **/
    @Override
    public List<RepairStatisticsVO> ticketStatusCount() {
        return statisticsMapper.ticketStatusCount();
    }

    /**
     * 今日工单
     *
     * @return java.util.List<com.alibaba.fastjson.JSONObject>
     * @author PQZ
     * @date 13:25 2024/10/21
     **/
    @Override
    public List<DlRepairTicketsRespVO> listTicketToday() {
        return statisticsMapper.listTicketToday();
    }

    /**
     * 维修工人排行
     *
     * @return java.util.List<com.alibaba.fastjson.JSONObject>
     * @author PQZ
     * @date 13:26 2024/10/21
     **/
    @Override
    public List<RepairStatisticsVO> listWorks() {
        return statisticsMapper.listWorks();
    }
}
