package com.foreverwin.mesnac.common.service.impl;

import com.foreverwin.mesnac.common.mapper.ReportDataMapper;
import com.foreverwin.mesnac.common.service.ReportDataService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ReportDataServiceImpl implements ReportDataService{

    @Autowired
    private ReportDataMapper reportDataMapper;

    @Override
    public List<Map<String, Object>> findInspectionTask(Map paramMap) {
        return reportDataMapper.findInspectionTask(paramMap);
    }

    @Override
    public List<Map<String, Object>> findInspectionTaskDetail(Map paramMap) {
        return reportDataMapper.findInspectionTaskDetail(paramMap);
    }

    @Override
    public List<Map<String, Object>> findResourceOrResourceTypeLoadRateInfo(Map paramMap) {
        String site = CommonMethods.getSite();
        paramMap.put("site",site);
        String startFromDate = (String) paramMap.get("startFromDate");
        String startToDate = (String) paramMap.get("startToDate");
        try{
            LocalDate.parse(startToDate);
            LocalDate.parse(startFromDate);
        }catch (Exception e){
            throw BusinessException.build("时间格式错误！");
        }
        String resourceOrResourceType = (String) paramMap.get("resourceOrResourceType");
        List<Map<String, Object>> resourceLoadRateInfo;
        switch (resourceOrResourceType){
            case "RESOURCE_TYPE":
                resourceLoadRateInfo = reportDataMapper.findResourceTypeLoadRateInfo(paramMap);
                break;
            case "RESOURCE":
                resourceLoadRateInfo = reportDataMapper.findResourceLoadRateInfo(paramMap);
                break;
            default:
                throw  BusinessException.build("未找到查询路径！");
        }
        if (resourceLoadRateInfo.size() < 1){
            throw BusinessException.build("按条件查询未找到数据！");
        }
        return resourceLoadRateInfo;
    }

    @Override
    public List<Map<String, Object>> findResourceRepairDataStatInfo(Map paramMap) {
        return reportDataMapper.findResourceRepairDataStatInfo(paramMap);
    }

    @Override
    public List<Map<String, Object>> findProductStateInfo(Map paramMap) {
        paramMap.put("site", CommonMethods.getSite());
        return reportDataMapper.findProductStateInfo(paramMap);
    }

    @Override
    public List<Map<String, Object>> findWorkShopPassRate(Map paramMap) {
        return reportDataMapper.findWorkShopPassRate(paramMap);
    }

    @Override
    public List<Map<String, Object>> findCheckUserPassRate(Map paramMap) {
        return reportDataMapper.findCheckUserPassRate(paramMap);
    }

    @Override
    public List<Map<String, Object>> findUserPassRate(Map paramMap) {
        return reportDataMapper.findUserPassRate(paramMap);
    }

    @Override
    public List<Map<String, Object>> findInspectionDetailPassRate(Map paramMap) {
        return reportDataMapper.findInspectionDetailPassRate(paramMap);
    }

    @Override
    public List<Map<String, Object>> findSectionPassRate(Map paramMap) {
        return reportDataMapper.findSectionPassRate(paramMap);
    }
}
