package com.njzscloud.supervisory.sys.stationletter.service.impl;

import com.njzscloud.supervisory.sys.stationletter.constant.WarnCategory;
import com.njzscloud.supervisory.sys.stationletter.mapper.StationLetterMapper;
import com.njzscloud.supervisory.sys.stationletter.pojo.AuditResult;
import com.njzscloud.supervisory.sys.stationletter.pojo.StationLetterEntity;
import com.njzscloud.supervisory.sys.stationletter.pojo.StationLetterResult;
import com.njzscloud.supervisory.sys.stationletter.service.StationLetterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 站内信服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class StationLetterServiceImpl implements StationLetterService {

    private final StationLetterMapper stationLetterMapper;

    @Override
    public StationLetterResult getPendingWarnList() {
        StationLetterResult result = new StationLetterResult();
        List<StationLetterEntity> entities = stationLetterMapper.selectPendingWarnList();
        for (StationLetterEntity entity : entities) {
            // 通过枚举类设置预警类型中文名称
            String warnCategoryName = getWarnCategoryName(entity.getWarnCategory());
            entity.setWarnCategoryName(warnCategoryName);
        }
        result.setWarnList(entities);
        List<AuditResult> auditResults = Lists.newArrayList();
        
        // 1.查询未删除并且待审核的公司数量，表为biz_company
        int companyCount = stationLetterMapper.selectPendingCompanyCount();
        AuditResult companyAudit = new AuditResult();
        companyAudit.setModuleType("company");
        companyAudit.setAuditName("待审核公司");
        companyAudit.setCount(companyCount);
        auditResults.add(companyAudit);

        // 2.查询未删除并且待审核的车辆数量，表为biz_truck
        int truckCount = stationLetterMapper.selectPendingTruckCount();
        AuditResult truckAudit = new AuditResult();
        truckAudit.setModuleType("truck");
        truckAudit.setAuditName("待审核车辆");
        truckAudit.setCount(truckCount);
        auditResults.add(truckAudit);

        // 3.查询未删除并且待审核的项目数量，表为biz_project
        int projectCount = stationLetterMapper.selectPendingProjectCount();
        AuditResult projectAudit = new AuditResult();
        projectAudit.setModuleType("project");
        projectAudit.setAuditName("待审核项目");
        projectAudit.setCount(projectCount);
        auditResults.add(projectAudit);

        // 4.查询未删除并且待审核的司机数量，表为biz_driver
        int driverCount = stationLetterMapper.selectPendingDriverCount();
        AuditResult driverAudit = new AuditResult();
        driverAudit.setModuleType("driver");
        driverAudit.setAuditName("待审核司机");
        driverAudit.setCount(driverCount);
        auditResults.add(driverAudit);

        // 5.查询未删除并且待审核的订单数量，表为order_info
        int orderCount = stationLetterMapper.selectPendingOrderCount();
        AuditResult orderAudit = new AuditResult();
        orderAudit.setModuleType("order");
        orderAudit.setAuditName("待审核订单");
        orderAudit.setCount(orderCount);
        auditResults.add(orderAudit);
        
        result.setAuditResultList(auditResults);
        return result;
    }

    /**
     * 根据预警类型代码获取中文名称
     */
    private String getWarnCategoryName(String warnCategory) {
        if (warnCategory == null) {
            return "未知类型";
        }
        
        for (WarnCategory category : WarnCategory.values()) {
            if (category.getVal().equals(warnCategory)) {
                return category.getTxt();
            }
        }
        return "未知类型";
    }

}
