package com.aucma.dms.service.impl;

import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.common.utils.uuid.Seq;
import com.aucma.dms.domain.DmsRepairMaterial;
import com.aucma.dms.domain.DmsRepairProject;
import com.aucma.dms.domain.DmsRepairWorkOrder;
import com.aucma.dms.mapper.DmsRepairMaterialMapper;
import com.aucma.dms.mapper.DmsRepairProjectMapper;
import com.aucma.dms.mapper.DmsRepairWorkOrderMapper;
import com.aucma.dms.service.IDmsRepairWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 维修工单Service业务层处理
 * 
 * @author Cascade AI
 * @date 2025-01-10
 */
@Service
public class DmsRepairWorkOrderServiceImpl implements IDmsRepairWorkOrderService {
    
    @Autowired
    private DmsRepairWorkOrderMapper dmsRepairWorkOrderMapper;
    
    @Autowired
    private DmsRepairProjectMapper dmsRepairProjectMapper;
    
    @Autowired
    private DmsRepairMaterialMapper dmsRepairMaterialMapper;

    /**
     * 查询维修工单
     * 
     * @param workOrderId 维修工单主键
     * @return 维修工单
     */
    @Override
    public DmsRepairWorkOrder selectDmsRepairWorkOrderByWorkOrderId(Long workOrderId) {
        DmsRepairWorkOrder workOrder = dmsRepairWorkOrderMapper.selectDmsRepairWorkOrderByWorkOrderId(workOrderId);
        if (workOrder != null) {
            // 查询维修工程明细
            DmsRepairProject projectQuery = new DmsRepairProject();
            projectQuery.setWorkOrderId(workOrderId);
            List<DmsRepairProject> projects = dmsRepairProjectMapper.selectDmsRepairProjectList(projectQuery);
            workOrder.setRepairProjects(projects);
            workOrder.setProjectCount(projects != null ? projects.size() : 0);
            
            // 查询维修材料明细
            DmsRepairMaterial materialQuery = new DmsRepairMaterial();
            materialQuery.setWorkOrderId(workOrderId);
            List<DmsRepairMaterial> materials = dmsRepairMaterialMapper.selectDmsRepairMaterialList(materialQuery);
            workOrder.setRepairMaterials(materials);
            workOrder.setMaterialCount(materials != null ? materials.size() : 0);
        }
        return workOrder;
    }

    /**
     * 查询维修工单列表
     * 
     * @param dmsRepairWorkOrder 维修工单
     * @return 维修工单
     */
    @Override
    public List<DmsRepairWorkOrder> selectDmsRepairWorkOrderList(DmsRepairWorkOrder dmsRepairWorkOrder) {
        List<DmsRepairWorkOrder> list = dmsRepairWorkOrderMapper.selectDmsRepairWorkOrderList(dmsRepairWorkOrder);
        // 填充工程数和材料数
        if (!CollectionUtils.isEmpty(list)) {
            for (DmsRepairWorkOrder workOrder : list) {
                DmsRepairProject projectQuery = new DmsRepairProject();
                projectQuery.setWorkOrderId(workOrder.getWorkOrderId());
                int projectCount = dmsRepairProjectMapper.selectDmsRepairProjectList(projectQuery).size();
                workOrder.setProjectCount(projectCount);
                
                DmsRepairMaterial materialQuery = new DmsRepairMaterial();
                materialQuery.setWorkOrderId(workOrder.getWorkOrderId());
                int materialCount = dmsRepairMaterialMapper.selectDmsRepairMaterialList(materialQuery).size();
                workOrder.setMaterialCount(materialCount);
            }
        }
        return list;
    }

    /**
     * 新增维修工单
     * 
     * @param dmsRepairWorkOrder 维修工单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDmsRepairWorkOrder(DmsRepairWorkOrder dmsRepairWorkOrder) {
        // 生成工单编号
        if (dmsRepairWorkOrder.getWorkOrderCode() == null || dmsRepairWorkOrder.getWorkOrderCode().isEmpty()) {
            dmsRepairWorkOrder.setWorkOrderCode(Seq.getId(Seq.dmsBillsFaultInstanceSeqType, "WO"));
        }
        
        // 设置默认值
        if (dmsRepairWorkOrder.getOrderStatus() == null) {
            dmsRepairWorkOrder.setOrderStatus("0"); // 待派工
        }
        if (dmsRepairWorkOrder.getApproveStatus() == null) {
            dmsRepairWorkOrder.setApproveStatus("0"); // 待审批
        }
        if (dmsRepairWorkOrder.getIsFlag() == null) {
            dmsRepairWorkOrder.setIsFlag("1");
        }
        
        dmsRepairWorkOrder.setCreateTime(DateUtils.getNowDate());
        dmsRepairWorkOrder.setCreateBy(SecurityUtils.getUserId());
        
        // 插入主表
        int rows = dmsRepairWorkOrderMapper.insertDmsRepairWorkOrder(dmsRepairWorkOrder);
        
        // 插入维修工程明细
        insertRepairProjects(dmsRepairWorkOrder);
        
        // 插入维修材料明细
        insertRepairMaterials(dmsRepairWorkOrder);
        
        return rows;
    }

    /**
     * 修改维修工单
     * 
     * @param dmsRepairWorkOrder 维修工单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDmsRepairWorkOrder(DmsRepairWorkOrder dmsRepairWorkOrder) {
        dmsRepairWorkOrder.setUpdateTime(DateUtils.getNowDate());
        dmsRepairWorkOrder.setUpdateBy(SecurityUtils.getUserId());
        
        // 删除原有子表数据
        dmsRepairProjectMapper.deleteDmsRepairProjectByWorkOrderId(dmsRepairWorkOrder.getWorkOrderId());
        dmsRepairMaterialMapper.deleteDmsRepairMaterialByWorkOrderId(dmsRepairWorkOrder.getWorkOrderId());
        
        // 重新插入子表数据
        insertRepairProjects(dmsRepairWorkOrder);
        insertRepairMaterials(dmsRepairWorkOrder);
        
        // 更新主表
        return dmsRepairWorkOrderMapper.updateDmsRepairWorkOrder(dmsRepairWorkOrder);
    }

    /**
     * 批量删除维修工单
     * 
     * @param workOrderIds 需要删除的维修工单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsRepairWorkOrderByWorkOrderIds(Long[] workOrderIds) {
        // 删除子表数据
        for (Long workOrderId : workOrderIds) {
            dmsRepairProjectMapper.deleteDmsRepairProjectByWorkOrderId(workOrderId);
            dmsRepairMaterialMapper.deleteDmsRepairMaterialByWorkOrderId(workOrderId);
        }
        return dmsRepairWorkOrderMapper.deleteDmsRepairWorkOrderByWorkOrderIds(workOrderIds);
    }

    /**
     * 删除维修工单信息
     * 
     * @param workOrderId 维修工单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsRepairWorkOrderByWorkOrderId(Long workOrderId) {
        // 删除子表数据
        dmsRepairProjectMapper.deleteDmsRepairProjectByWorkOrderId(workOrderId);
        dmsRepairMaterialMapper.deleteDmsRepairMaterialByWorkOrderId(workOrderId);
        return dmsRepairWorkOrderMapper.deleteDmsRepairWorkOrderByWorkOrderId(workOrderId);
    }

    /**
     * 派工
     * 
     * @param dmsRepairWorkOrder 维修工单
     * @return 结果
     */
    @Override
    public int dispatchWorkOrder(DmsRepairWorkOrder dmsRepairWorkOrder) {
        DmsRepairWorkOrder updateEntity = new DmsRepairWorkOrder();
        updateEntity.setWorkOrderId(dmsRepairWorkOrder.getWorkOrderId());
        updateEntity.setExecutorId(dmsRepairWorkOrder.getExecutorId());
        updateEntity.setExecutorName(dmsRepairWorkOrder.getExecutorName());
        updateEntity.setOrderStatus("1"); // 已派工
        updateEntity.setUpdateTime(DateUtils.getNowDate());
        updateEntity.setUpdateBy(SecurityUtils.getUserId());
        
        return dmsRepairWorkOrderMapper.updateDmsRepairWorkOrder(updateEntity);
    }

    /**
     * 开始维修
     * 
     * @param workOrderId 维修工单ID
     * @return 结果
     */
    @Override
    public int startWorkOrder(Long workOrderId) {
        DmsRepairWorkOrder updateEntity = new DmsRepairWorkOrder();
        updateEntity.setWorkOrderId(workOrderId);
        updateEntity.setOrderStatus("2"); // 维修中
        updateEntity.setActualStartTime(new Date());
        updateEntity.setUpdateTime(DateUtils.getNowDate());
        updateEntity.setUpdateBy(SecurityUtils.getUserId());
        
        return dmsRepairWorkOrderMapper.updateDmsRepairWorkOrder(updateEntity);
    }

    /**
     * 完成维修
     * 
     * @param dmsRepairWorkOrder 维修工单
     * @return 结果
     */
    @Override
    public int completeWorkOrder(DmsRepairWorkOrder dmsRepairWorkOrder) {
        DmsRepairWorkOrder updateEntity = new DmsRepairWorkOrder();
        updateEntity.setWorkOrderId(dmsRepairWorkOrder.getWorkOrderId());
        updateEntity.setOrderStatus("3"); // 已完成
        updateEntity.setActualEndTime(new Date());
        updateEntity.setRepairResult(dmsRepairWorkOrder.getRepairResult());
        updateEntity.setActualCost(dmsRepairWorkOrder.getActualCost());
        updateEntity.setRepairHours(dmsRepairWorkOrder.getRepairHours());
        updateEntity.setUpdateTime(DateUtils.getNowDate());
        updateEntity.setUpdateBy(SecurityUtils.getUserId());
        
        return dmsRepairWorkOrderMapper.updateDmsRepairWorkOrder(updateEntity);
    }

    /**
     * 审批
     * 
     * @param dmsRepairWorkOrder 维修工单
     * @return 结果
     */
    @Override
    public int approveWorkOrder(DmsRepairWorkOrder dmsRepairWorkOrder) {
        DmsRepairWorkOrder updateEntity = new DmsRepairWorkOrder();
        updateEntity.setWorkOrderId(dmsRepairWorkOrder.getWorkOrderId());
        updateEntity.setApproveStatus(dmsRepairWorkOrder.getApproveStatus());
        updateEntity.setApprover(SecurityUtils.getUsername());
        updateEntity.setApproveTime(new Date());
        updateEntity.setApproveRemark(dmsRepairWorkOrder.getApproveRemark());
        updateEntity.setUpdateTime(DateUtils.getNowDate());
        updateEntity.setUpdateBy(SecurityUtils.getUserId());
        
        return dmsRepairWorkOrderMapper.updateDmsRepairWorkOrder(updateEntity);
    }

    /**
     * 插入维修工程明细
     */
    private void insertRepairProjects(DmsRepairWorkOrder workOrder) {
        List<DmsRepairProject> projects = workOrder.getRepairProjects();
        if (!CollectionUtils.isEmpty(projects)) {
            for (DmsRepairProject project : projects) {
                project.setWorkOrderId(workOrder.getWorkOrderId());
                project.setCreateTime(DateUtils.getNowDate());
                project.setCreateBy(SecurityUtils.getUserId());
                dmsRepairProjectMapper.insertDmsRepairProject(project);
            }
        }
    }

    /**
     * 插入维修材料明细
     */
    private void insertRepairMaterials(DmsRepairWorkOrder workOrder) {
        List<DmsRepairMaterial> materials = workOrder.getRepairMaterials();
        if (!CollectionUtils.isEmpty(materials)) {
            for (DmsRepairMaterial material : materials) {
                material.setWorkOrderId(workOrder.getWorkOrderId());
                material.setCreateTime(DateUtils.getNowDate());
                material.setCreateBy(SecurityUtils.getUserId());
                dmsRepairMaterialMapper.insertDmsRepairMaterial(material);
            }
        }
    }
}
