package com.repair.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.mapper.RepairRecordMapper;
import com.repair.model.pojo.RepairRecord;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.*;
import com.repair.services.RepairRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-07-24
 */
@Service
public class RepairRecordServiceImpl extends ServiceImpl<RepairRecordMapper, RepairRecord> implements RepairRecordService {

    @Autowired
    private RepairRecordMapper repairRecordMapper;

    @Override
    public int insertRepairRecord(RepairRecord rr) {
        int result = repairRecordMapper.insert(rr);
        return rr.getId();
    }

    @Override
    public int updateRepairRecord(RepairRecord rr) {
        int result = repairRecordMapper.updateById(rr);
        return result;
    }

    @Override
    public PageUtils<RepairRecord> queryPageRepairRecords(int currentPage, int pageCount, String name) {
        Page<RepairRecord> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairRecord> queryWrapper = new QueryWrapper<>();
        IPage<RepairRecord> result = repairRecordMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public int deleteRepairRecordById(int id) {
        int result = repairRecordMapper.deleteById(id);
        return result;
    }

    @Override
    public RepairRecord getRepairById(int id) {
        RepairRecord result = repairRecordMapper.selectById(id);
        return result;
    }

    @Override
    public List<RepairRecord> getRepairs() {
        List<RepairRecord> result = repairRecordMapper.selectList(null);
        return result;
    }

    @Override
    public RepairRecordCountVo getRepairRecordCount(Integer userId) {
        RepairRecordCountVo data = repairRecordMapper.queryRepairRecordCount(userId);
        return data;
    }

    @Override
    public PageUtils<MyRepairRecordVo> queryMyRepairRecordList(int currentPage, int pageCount, Integer state, Integer userId) {
        String stateStr = null;
        if (state == 1) {
            stateStr = "< 7 or rr.maintenance_state = 11)";
        } else if (state == 2) {
            stateStr = "= 4)";
        } else {
            stateStr = ">= 7 and rr.maintenance_state <= 10)";
        }

        Page<MyRepairRecordVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<MyRepairRecordVo> result = repairRecordMapper.queryMyRepairRecordList(page, stateStr, userId);
        return new PageUtils(result);
    }

    @Override
    public MaintenanceTechnicianVo queryRepairTechnicianCount(Integer userId, String dateNow, Integer schoolId) {
        MaintenanceTechnicianVo result = repairRecordMapper.queryRepairTechnicianCount(userId, dateNow, schoolId);
        return result;
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryPendingOrderList(int currentPage, int pageCount, Integer userId, String dateNow, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryPendingOrderList(page, userId, dateNow, schoolId);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryRepairOrderList(int currentPage, int pageCount, Integer userId, String dateNow, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryRepairOrderList(page, userId, dateNow, schoolId);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTimeoutOrderList(int currentPage, int pageCount, Integer userId, String dateNow, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTimeoutOrderList(page, userId, dateNow, schoolId);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryExamineOrderList(int currentPage, int pageCount, Integer userId, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryExamineOrderList(page, userId, schoolId);
        return new PageUtils(result);
    }

    @Override
    public RepairManageCountVo queryManageCount(Integer schoolId, String keyWord, String startTime, String endTime,String maintenanceName) {
        RepairManageCountVo result = repairRecordMapper.queryManageCount(schoolId, keyWord, startTime, endTime,maintenanceName);
        return result;
    }

    @Override
    public PageUtils<RepairRecordPageVo> queryNotTimeout(int currentPage, int pageCount, String stateStr, String keyWord, String startTime, String endTime, Integer schoolId, String maintenanceName) {
        Page<RepairRecordPageVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairRecordPageVo> result = repairRecordMapper.queryNotTimeout(page, stateStr, keyWord, startTime, endTime, schoolId, maintenanceName);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairRecordTimeoutPageVo> queryTimeout(int currentPage, int pageCount, String keyWord, String startTime, String endTime, Integer schoolId, String maintenanceName) {
        Page<RepairRecordTimeoutPageVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairRecordTimeoutPageVo> result = repairRecordMapper.queryTimeout(page, keyWord, startTime, endTime, schoolId, maintenanceName);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<pendingOrderVo> queryPendingOrder(int currentPage, int pageCount, int schoolId) {
        Page<pendingOrderVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<pendingOrderVo> result = repairRecordMapper.queryPendingOrder(page, schoolId);
        return new PageUtils(result);
    }

    @Override
    public TeamWorkCountVo queryTeamWorkCount(Integer schoolId, String dateNow) {
        TeamWorkCountVo result = repairRecordMapper.queryTeamWorkCount(schoolId, dateNow);
        return result;
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTeamWorkExamine(int currentPage, int pageCount, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTeamWorkExamine(page, schoolId);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTeamWorkPending(int currentPage, int pageCount, Integer schoolId, String dateNow) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTeamWorkPending(page, schoolId, dateNow);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTeamWorkMaintenance(int currentPage, int pageCount, Integer schoolId, String dateNow) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTeamWorkMaintenance(page, schoolId, dateNow);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTeamWorkFinished(int currentPage, int pageCount, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTeamWorkFinished(page, schoolId);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTeamWorkLogistics(int currentPage, int pageCount, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTeamWorkLogistics(page, schoolId);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTeamWorkTimeout(int currentPage, int pageCount, Integer schoolId, String dateNow) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTeamWorkTimeout(page, schoolId, dateNow);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTeamWorkClosed(int currentPage, int pageCount, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTeamWorkClosed(page, schoolId);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairTechnicianVo> queryTeamWorkOffline(int currentPage, int pageCount, Integer schoolId) {
        Page<RepairTechnicianVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairTechnicianVo> result = repairRecordMapper.queryTeamWorkOffline(page, schoolId);
        return new PageUtils(result);
    }

    @Override
    public RepairRecord queryRepairByRecordno(String recordNo) {
        QueryWrapper<RepairRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_no", recordNo);
        RepairRecord result = repairRecordMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public List<ReportExportVo> queryNotTimeoutReportExport(String stateStr, String keyWord, String startTime, String endTime, Integer schoolId) {
        List<ReportExportVo> reportExportVos = repairRecordMapper.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
        return reportExportVos;
    }

    @Override
    public List<ReportExportVo> queryTimeoutReportExport(String keyWord, String startTime, String endTime, Integer schoolId) {
        List<ReportExportVo> reportExportVos = repairRecordMapper.queryTimeoutReportExport(keyWord, startTime, endTime, schoolId);
        return reportExportVos;
    }

    @Override
    public MasterHomepageVo queryMasterHomeCount(Integer userId, Integer schoolId) {
        MasterHomepageVo result = repairRecordMapper.queryMasterHomeCount(userId, schoolId);
        return result;
    }

    @Override
    public MasterHomepageVo queryLogisticHomeCount(Integer schoolId) {
        MasterHomepageVo result = repairRecordMapper.queryLogisticHomeCount(schoolId);
        return result;
    }

@Override
    public List<RepairCampusRecordVo> repairCampusTotal() {
        List<RepairCampusRecordVo> campusVoList = repairRecordMapper.repairCampusTotal();
        return campusVoList;
    }

    @Override
    public List<RepairCampusRecordVo> repairCampus(String startTime, String endTime) {
        List<RepairCampusRecordVo> campusVoList = repairRecordMapper.repairCampus(startTime, endTime);
        return campusVoList;
    }

    @Override
    public IPage<RepairAreaVo> repairAreaTotal(int currentPage, int pageCount) {
        Page<RepairAreaVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairAreaVo> vos = repairRecordMapper.repairAreaTotal(page);
        return vos;
    }

    @Override
    public IPage<RepairAreaVo> repairArea(String startTime, String endTime,int currentPage, int pageCount) {
        Page<RepairAreaVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairAreaVo> vos = repairRecordMapper.repairArea(page,startTime, endTime );
        return vos;
    }

    @Override
    public IPage<MaintenanceGoodsVo> maintenanceGoodsTotal(int currentPage, int pageCount) {
        Page<MaintenanceGoodsVo> page=new Page<>(currentPage, pageCount);
        IPage<MaintenanceGoodsVo> vos = repairRecordMapper.maintenanceGoodsTotal(page);
        return vos;
    }

    @Override
    public IPage<MaintenanceGoodsVo> maintenanceGoods(String startTime, String endTime,int currentPage, int pageCount) {
        Page<MaintenanceGoodsVo> page=new Page<>(currentPage,pageCount);
        IPage<MaintenanceGoodsVo> vos = repairRecordMapper.maintenanceGoods(page,startTime,endTime);
        return vos;
    }

    @Override
    public List<RepairAreaVo> repairAreaExport(String startTime, String endTime) {
        List<RepairAreaVo> vos=repairRecordMapper.repairAreaExport(startTime,endTime);
        return vos;
    }

    @Override
    public List<MaintenanceGoodsVo> maintenanceGoodsExport(String startTime, String endTime) {
        List<MaintenanceGoodsVo> vos =repairRecordMapper.maintenanceGoodsExport(startTime,endTime);
        return vos;
    }

    @Override
    public RepairAnalysisVo repairAnalysis(String startTime, String endTime) {
        RepairAnalysisVo vo=repairRecordMapper.repairAnalysis(startTime,endTime);
        return vo;
    }

    @Override
    public Integer queryTimeOut(String startTime, String endTime) {
        Integer timeOutCount=repairRecordMapper.queryTimeOut(startTime,endTime);
        return timeOutCount;
    }

    @Override
    public RepairSummarizingVo repairSummarizing(String startTime, String endTime) {
        RepairSummarizingVo vo=repairRecordMapper.repairSummarizing(startTime,endTime);
        return vo;
    }

    @Override
    public DetailsVo queryDetailData(Integer recordId) {
        DetailsVo result = repairRecordMapper.queryDetailData(recordId);
        return result;
    }

    @Override
    public RepairTotalDataVo repairTotalData() {
        RepairTotalDataVo vo=repairRecordMapper.repairTotalData();
        return vo;
    }

    @Override
    public int queryRepairRepeat (Integer userId,Integer buildId,Integer articleId,String dateStr) {
        QueryWrapper<RepairRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("build_id", buildId);
        queryWrapper.eq("article_id", articleId);
        queryWrapper.ge("report_time", dateStr);
        queryWrapper.ne("maintenance_state", 8);
        queryWrapper.ne("maintenance_state", 9);
        int result = repairRecordMapper.selectCount(queryWrapper);
        return result;
    }

    @Override
    public PrintVo queryPrintData(Integer recordId) {
        PrintVo result=repairRecordMapper.queryPrintData(recordId);
        return result;
    }
}
