package org.jeecg.modules.maintenanceWork.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.dynamicStaticAnalysis.entity.AnalysisBatchUnitDevice;
import org.jeecg.modules.dynamicStaticAnalysis.mapper.AnalysisBatchUnitDeviceMapper;
import org.jeecg.modules.dynamicStaticAnalysis.mapper.AnalysisBatchUnitDeviceTransfiniteMapper;
import org.jeecg.modules.homePage.vo.ToDoDetailVO;
import org.jeecg.modules.maintenanceWork.dto.*;
import org.jeecg.modules.maintenanceWork.entity.WorkBatch;
import org.jeecg.modules.maintenanceWork.entity.WorkBatchDetail;
import org.jeecg.modules.maintenanceWork.mapper.WorkBatchDetailMapper;
import org.jeecg.modules.maintenanceWork.mapper.WorkBatchMapper;
import org.jeecg.modules.maintenanceWork.service.IWorkBatchService;
import org.jeecg.modules.maintenanceWork.vo.WorkBatchDetailVO;
import org.jeecg.modules.maintenanceWork.vo.WorkBatchVO;
import org.jeecg.modules.unitDevice.dto.CaseReportWorkBatchDTO;
import org.jeecg.modules.unitDevice.vo.CaseReportWorkBatchVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 维修作业管理-作业计划批次管理表 服务实现类
 * </p>
 *
 * @author hkl
 * @since 2023-08-09
 */
@Service
public class WorkBatchServiceImpl extends ServiceImpl<WorkBatchMapper, WorkBatch> implements IWorkBatchService {
    @Resource
    private WorkBatchDetailMapper workBatchDetailMapper;

    @Resource
    private AnalysisBatchUnitDeviceMapper analysisBatchUnitDeviceMapper;

    @Resource
    private AnalysisBatchUnitDeviceTransfiniteMapper transfiniteMapper;

    @Override
    public Page<WorkBatchVO> listRecord(Page<WorkBatchVO> pageData, WorkBatchDTO dto) {
        Page<WorkBatchVO> pageRecords = this.baseMapper.listRecord(pageData, dto);

        for (WorkBatchVO record : pageRecords.getRecords()) {
            List<WorkBatchDetailVO> detailList = workBatchDetailMapper.queryListByWorkBatchId(record.getId());
//            for (WorkBatchDetailVO workBatchDetailVO : detailList) {
//                List<AnalysisBatchUnitDeviceTransfiniteVO> transfiniteList = transfiniteMapper.getTransfiniteList(record.getId());
//                workBatchDetailVO.setTransfiniteList(transfiniteList);
//            }
            record.setDetailList(detailList);
        }

        return pageRecords;
    }


    @Override
    public void saveRecord(WorkBatchSaveOrUpdateDTO dto) {
        // 保存主信息
        WorkBatch workBatch = BeanUtil.copyProperties(dto, WorkBatch.class);
        if (ObjectUtil.isEmpty(dto.getId())) {
            workBatch.setId(UUIDGenerator.generate());
            workBatch.setWorkStatus(0);
        }

        this.saveOrUpdate(workBatch);


        // 保证明细信息
        // 先删除
        LambdaQueryWrapper<WorkBatchDetail> delWrapper = Wrappers.lambdaQuery();
        delWrapper.eq(WorkBatchDetail::getWorkBatchId, dto.getId());
        workBatchDetailMapper.delete(delWrapper);
        List<WorkBatchSaveOrUpdateDetailDTO> transfiniteList = dto.getDetailList();
        for (WorkBatchSaveOrUpdateDetailDTO detailDTO : transfiniteList) {
            WorkBatchDetail workBatchDetail = new WorkBatchDetail();
            workBatchDetail.setId(UUIDGenerator.generate());
            workBatchDetail.setWorkBatchId(workBatch.getId());
            workBatchDetail.setWorkBatchCode(workBatch.getWorkBatchCode());
            workBatchDetail.setUnitId(detailDTO.getUnitId());
            workBatchDetail.setUnitCode(detailDTO.getUnitCode());
            workBatchDetail.setUnitType(detailDTO.getType());
            workBatchDetail.setUnitTypeName(detailDTO.getUnitTypeName());
            workBatchDetail.setUnitDeviceCode(detailDTO.getUnitDeviceCode());
            workBatchDetail.setUnitStartMileage(detailDTO.getUnitStartMileage());
            workBatchDetail.setUnitEndMileage(detailDTO.getUnitEndMileage());
            workBatchDetail.setUnitCenterMileage(detailDTO.getUnitCenterMileage());
            workBatchDetail.setUnitScore(detailDTO.getUnitScore());
            workBatchDetail.setUnitScoreLevel(detailDTO.getUnitScoreLevel());
            workBatchDetail.setRemark(detailDTO.getRemark());

            workBatchDetailMapper.insert(workBatchDetail);
        }
    }

    @Override
    public void deleteRecord(String workBatchId) {
        this.removeById(workBatchId);


        LambdaQueryWrapper<WorkBatchDetail> delWrapper = Wrappers.lambdaQuery();
        delWrapper.eq(WorkBatchDetail::getWorkBatchId, workBatchId);
        workBatchDetailMapper.delete(delWrapper);
    }

    @Override
    public void transfiniteSaveRecord(WorkBatchTransfiniteSaveDTO dto) {
        WorkBatch workBatch = BeanUtil.copyProperties(dto, WorkBatch.class);
        workBatch.setId(UUIDGenerator.generate());
        workBatch.setWorkStatus(0);
        workBatch.setSource(2); // 超限页面
        this.save(workBatch);

        List<DispatchDTO> transfiniteList = dto.getDetailList();
        for (DispatchDTO transfiniteDTO : transfiniteList) {
            WorkBatchDetail workBatchDetail = new WorkBatchDetail();
            workBatchDetail.setId(UUIDGenerator.generate());
            workBatchDetail.setWorkBatchId(workBatch.getId());
            workBatchDetail.setWorkBatchCode(workBatch.getWorkBatchCode());
            workBatchDetail.setAnalysisBatchUnitDeviceId(transfiniteDTO.getId());
            workBatchDetail.setUnitId(transfiniteDTO.getUnitId());
            workBatchDetail.setUnitCode(transfiniteDTO.getUnitCode());
            workBatchDetail.setUnitType(transfiniteDTO.getUnitType());
            workBatchDetail.setUnitTypeName(transfiniteDTO.getUnitTypeName());
            workBatchDetail.setUnitDeviceCode(transfiniteDTO.getUnitDeviceCode());
            workBatchDetail.setUnitEndMileage(transfiniteDTO.getUnitStartMileage());
            workBatchDetail.setUnitEndMileage(transfiniteDTO.getUnitEndMileage());
            workBatchDetail.setUnitCenterMileage(transfiniteDTO.getUnitCenterMileage());
            workBatchDetail.setUnitScore(transfiniteDTO.getUnitScore());
            workBatchDetail.setUnitScoreLevel(transfiniteDTO.getUnitScoreLevel());
            workBatchDetail.setUnitScoreLevelStr(transfiniteDTO.getUnitScoreLevelStr());
            workBatchDetailMapper.insert(workBatchDetail);
        }
    }


    @Override
    public Page<Map<String, Object>> getUnitDevicePage(Page<Map<String, Object>> pageData, UnitDeviceDTO dto) {

        return this.baseMapper.getUnitDevicePage(pageData, dto);

    }

    @Override
    public void batchSaveRecord(AnalysisBatchSaveDTO dto) {
        WorkBatch workBatch = BeanUtil.copyProperties(dto, WorkBatch.class);
        workBatch.setId(UUIDGenerator.generate());
        workBatch.setWorkStatus(0); // 9-已作业
        workBatch.setSource(2); // 超限页面
        this.save(workBatch);


        // 2.查询
        LambdaQueryWrapper<AnalysisBatchUnitDevice> unitDeviceWrapper = Wrappers.lambdaQuery();
        if (dto.getSaveType() == 2) {
            unitDeviceWrapper.eq(AnalysisBatchUnitDevice::getUnitScoreLevel, 3);
        }
        List<AnalysisBatchUnitDevice> unitDevices = analysisBatchUnitDeviceMapper.selectList(unitDeviceWrapper);
        for (AnalysisBatchUnitDevice unitDevice : unitDevices) {
            WorkBatchDetail workBatchDetail = new WorkBatchDetail();
            workBatchDetail.setId(UUIDGenerator.generate());
            workBatchDetail.setWorkBatchId(workBatch.getId());
            workBatchDetail.setWorkBatchCode(workBatch.getWorkBatchCode());
            workBatchDetail.setUnitId(unitDevice.getUnitId());
            workBatchDetail.setUnitCode(unitDevice.getUnitCode());
            workBatchDetail.setUnitType(unitDevice.getUnitType());
            workBatchDetail.setUnitTypeName(unitDevice.getUnitTypeName());
            workBatchDetail.setUnitDeviceCode(unitDevice.getUnitDeviceCode());
            workBatchDetail.setUnitStartMileage(unitDevice.getUnitStartMileage());
            workBatchDetail.setUnitEndMileage(unitDevice.getUnitEndMileage());
            workBatchDetail.setUnitCenterMileage(unitDevice.getUnitCenterMileage());
            workBatchDetail.setUnitScore(unitDevice.getUnitScore());
            workBatchDetail.setUnitScoreLevel(unitDevice.getUnitScoreLevel());
            workBatchDetail.setUnitScoreLevelStr(unitDevice.getUnitScoreLevelStr());
            workBatchDetailMapper.insert(workBatchDetail);
        }
    }

    @Override
    public Page<CaseReportWorkBatchVO> caseReportWorkBatch(Page<CaseReportWorkBatchVO> pageData, CaseReportWorkBatchDTO dto) {
        return this.baseMapper.caseReportWorkBatch(pageData, dto);
    }

    @Override
    public List<ToDoDetailVO> getTodoList(String currentUserId) {
        return this.baseMapper.getTodoList(currentUserId);
    }


}
