package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.checkPoint.CheckPointRecord;
import com.ruoyi.system.domain.complaintSuggestion.ComplaintSuggestion;
import com.ruoyi.system.domain.illegalParking.IllegalParking;
import com.ruoyi.system.domain.propertyRepair.PropertyRepair;
import com.ruoyi.system.mapper.CheckPointRecordMapper;
import com.ruoyi.system.mapper.ComplaintSuggestionMapper;
import com.ruoyi.system.mapper.IllegalParkingMapper;
import com.ruoyi.system.mapper.PropertyRepairMapper;
import com.ruoyi.system.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 首页统计Service业务层处理
 *
 * @author boman
 * @date 2025-03-06
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService {

    @Autowired
    private IllegalParkingMapper illegalParkingMapper;
    @Autowired
    private CheckPointRecordMapper checkPointRecordMapper;
    @Autowired
    private PropertyRepairMapper propertyRepairMapper;
    @Autowired
    private ComplaintSuggestionMapper complaintSuggestionMapper;


    @Override
    public AjaxResult numApp() {
        String day = DateUtils.getDate();
        String dayTime = DateUtils.getTime();
        Map<String,Object> map = new HashMap<>();
        //空闲车位
        int kxcw = 0;
        //空闲充电桩
        int kxcdz = 0;
        //进入车辆
        int jrcl = 0;
        //离开车辆
        int lkcl = 0;
        //外来车辆
        int wlcl = 0;
        //违停登记
        int wtdj = 0;
        //外来人员
        int wlry = 0;
        //进入人员
        int jrry = 0;
        //已巡更
        int yxg = 0;
        //未巡更
        int wxg = 0;
        //家政订单
        int jzdd = 0;
        //完成订单
        int wcdd = 0;
        //今日报修
        int jrbx = 0;
        //已派报修
        int ypbx = 0;
        //投诉建议
        int tsjy = 0;
        //已回投诉
        int yhjy = 0;
        // todo 空闲车位，空闲充电桩，进入车辆，离开车辆，外来车辆，外来人员，进入人员，家政订单，完成订单


        //查询违停登记
        Map<String, Object> params = new HashMap<>();
        params.put("createTime",day);
        IllegalParking illegalParking = new IllegalParking();
        illegalParking.setParams(params);
        List<IllegalParking> illegalParkings = illegalParkingMapper.selectIllegalParkingList(illegalParking);
        if(illegalParkings!=null && illegalParkings.size()>0){
            wtdj = illegalParkings.size();
        }
        //查询巡更
        CheckPointRecord checkPointRecord = new CheckPointRecord();
        checkPointRecord.setCheckTime(DateUtils.getNowDate());
        List<CheckPointRecord> checkPointRecords = checkPointRecordMapper.selectCheckPointRecordList(checkPointRecord);
        if(checkPointRecords!=null && checkPointRecords.size()>0){
            Map<String, List<CheckPointRecord>> collects = checkPointRecords.stream().collect(Collectors.groupingBy(CheckPointRecord::getCheckStatus));
            if(collects.containsKey("1")){
                wxg = collects.get("1").size();
            }
            if(collects.containsKey("2")){
                yxg = collects.get("2").size();
            }
        }

        //查询报修
        List<PropertyRepair> propertyRepairs = propertyRepairMapper.selectPropertyRepairListByTime(day, day);
        if(propertyRepairs!=null && propertyRepairs.size()>0){
            jrbx = propertyRepairs.size();
            for (PropertyRepair propertyRepair : propertyRepairs) {
                if(StringUtils.isNotEmpty(propertyRepair.getPhoneNumber())){
                    ypbx++;
                }
            }
        }
        //查询投诉建议
        ComplaintSuggestion complaintSuggestion = new ComplaintSuggestion();
        complaintSuggestion.setParams(params);
        List<ComplaintSuggestion> complaintSuggestions = complaintSuggestionMapper.selectComplaintSuggestionList(complaintSuggestion);
        if(complaintSuggestions!=null && complaintSuggestions.size()>0){
            tsjy = complaintSuggestions.size();
            Map<String, List<ComplaintSuggestion>> suggestionCollect = complaintSuggestions.stream().collect(Collectors.groupingBy(ComplaintSuggestion::getStatus));
            if(suggestionCollect.containsKey("2")){
                yhjy = suggestionCollect.get("2").size();
            }
        }
        map.put("wtdj",wtdj);
        map.put("yxg",yxg);
        map.put("wxg",wxg);
        map.put("jrbx",jrbx);
        map.put("ypbx",ypbx);
        map.put("tsjy",tsjy);
        map.put("yhjy",yhjy);
        // todo 空闲车位，空闲充电桩，进入车辆，离开车辆，外来车辆，外来人员，进入人员，家政订单，完成订单
        map.put("kxcw",kxcw);
        map.put("kxcdz",kxcdz);
        map.put("jrcl",jrcl);
        map.put("lkcl",lkcl);
        map.put("wlcl",wlcl);
        map.put("wlry",wlry);
        map.put("jrry",jrry);
        map.put("jzdd",jzdd);
        map.put("wcdd",wcdd);
        return AjaxResult.success(map);
    }
}
