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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.device.entity.BuDeviceType;
import org.jeecg.modules.device.mapper.BuDeviceTypeMapper;
import org.jeecg.modules.regular.entity.Regular;
import org.jeecg.modules.regular.mapper.RegularMapper;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.workOrder.entity.WorkOrderModel;
import org.jeecg.modules.workOrder.mapper.WorkOrderModelMapper;
import org.jeecg.modules.workOrder.service.IWorkOrderModelService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 工单模板
 * @Author: jeecg-boot
 * @Date:   2025-02-08
 * @Version: V1.0
 */
@Service
@AllArgsConstructor
public class WorkOrderModelServiceImpl extends ServiceImpl<WorkOrderModelMapper, WorkOrderModel> implements IWorkOrderModelService {

    private RegularMapper regularMapper;
    private final BuDeviceTypeMapper deviceTypeMapper;
    private ISysDictItemService dictItemService;

    @Override
    public IPage<WorkOrderModel> queryPageList(Page<WorkOrderModel> page, QueryWrapper<WorkOrderModel> queryWrapper){
        IPage<WorkOrderModel> pageList = super.page(page, queryWrapper);
        // 获取检修规程类型字典
        Map<String, String> regulerDictMap = dictItemService.selectItemsByDictCode("repair_range");

        pageList.getRecords().forEach(workOrderModel -> {
            Regular regular = regularMapper.selectById(workOrderModel.getRegularId());
            if (regular != null) {
                BuDeviceType deviceType = deviceTypeMapper.selectById(regular.getDeviceTypeId());
                workOrderModel.setRegularOrFault(deviceType.getTypeName()+"("+deviceType.getTypeCode()+")"+"/"+regulerDictMap.get(regular.getRapairRange().toString()));
            }
        });
        return pageList;
    }

    @Override
    public List<Map<String,String>> queryModelMap(){
        List<WorkOrderModel> workOrderModelList = baseMapper.selectList(null);
        if (workOrderModelList.isEmpty()) {
            return Collections.emptyList(); // 提前返回空结果
        }
        // 获取检修规程类型字典
        Map<String, String> regulerDictMap = dictItemService.selectItemsByDictCode("repair_range");

        return workOrderModelList.stream().map(workOrderModel -> {
            Map<String,String> map = new HashMap<>();
            map.put("value", workOrderModel.getId());
            Regular regular = regularMapper.selectById(workOrderModel.getRegularId());
            if (regular != null) {
                BuDeviceType deviceType = deviceTypeMapper.selectById(regular.getDeviceTypeId());
                map.put("label", deviceType.getTypeName()+"("+deviceType.getTypeCode()+")"+"/"+regulerDictMap.get(regular.getRapairRange().toString()));
            }
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 通过设备类型id和修程查询修程id和工单模板id
     * @param regular
     * @return
     */
    @Override
    public WorkOrderModel queryOne(Regular regular){
        //根据设备id查询设备类型id，再联合修程查询regularId
        String regularId = regularMapper.findRegularIdByDeviceId(regular.getDeviceId(), String.valueOf(regular.getRapairRange()));
        if (StringUtils.isBlank(regularId)) {
            return null;
        }

        return baseMapper.selectOne(new LambdaQueryWrapper<WorkOrderModel>()
                .eq(WorkOrderModel::getRegularId,regularId));
    }
}
