package com.aucma.dms.service.impl;

import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsRepairRecord;
import com.aucma.dms.mapper.DmsRepairRecordMapper;
import com.aucma.dms.service.IDmsRepairRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 维修记录Service业务层处理
 * 
 * @author Cascade AI
 * @date 2025-01-10
 */
@Service
public class DmsRepairRecordServiceImpl implements IDmsRepairRecordService {
    
    @Autowired
    private DmsRepairRecordMapper dmsRepairRecordMapper;

    /**
     * 查询维修记录
     * 
     * @param recordId 维修记录主键
     * @return 维修记录
     */
    @Override
    public DmsRepairRecord selectDmsRepairRecordByRecordId(Long recordId) {
        return dmsRepairRecordMapper.selectDmsRepairRecordByRecordId(recordId);
    }

    /**
     * 查询维修记录列表
     * 
     * @param dmsRepairRecord 维修记录
     * @return 维修记录
     */
    @Override
    public List<DmsRepairRecord> selectDmsRepairRecordList(DmsRepairRecord dmsRepairRecord) {
        return dmsRepairRecordMapper.selectDmsRepairRecordList(dmsRepairRecord);
    }

    /**
     * 新增维修记录
     * 
     * @param dmsRepairRecord 维修记录
     * @return 结果
     */
    @Override
    public int insertDmsRepairRecord(DmsRepairRecord dmsRepairRecord) {
        // 设置默认值
        if (dmsRepairRecord.getIsArchived() == null) {
            dmsRepairRecord.setIsArchived("0"); // 未存档
        }
        if (dmsRepairRecord.getIsFlag() == null) {
            dmsRepairRecord.setIsFlag("1");
        }
        
        dmsRepairRecord.setCreateTime(DateUtils.getNowDate());
        dmsRepairRecord.setCreateBy(SecurityUtils.getUserId());
        
        return dmsRepairRecordMapper.insertDmsRepairRecord(dmsRepairRecord);
    }

    /**
     * 修改维修记录
     * 
     * @param dmsRepairRecord 维修记录
     * @return 结果
     */
    @Override
    public int updateDmsRepairRecord(DmsRepairRecord dmsRepairRecord) {
        dmsRepairRecord.setUpdateTime(DateUtils.getNowDate());
        dmsRepairRecord.setUpdateBy(SecurityUtils.getUserId());
        return dmsRepairRecordMapper.updateDmsRepairRecord(dmsRepairRecord);
    }

    /**
     * 批量删除维修记录
     * 
     * @param recordIds 需要删除的维修记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsRepairRecordByRecordIds(Long[] recordIds) {
        return dmsRepairRecordMapper.deleteDmsRepairRecordByRecordIds(recordIds);
    }

    /**
     * 删除维修记录信息
     * 
     * @param recordId 维修记录主键
     * @return 结果
     */
    @Override
    public int deleteDmsRepairRecordByRecordId(Long recordId) {
        return dmsRepairRecordMapper.deleteDmsRepairRecordByRecordId(recordId);
    }

    /**
     * 存档维修记录
     * 
     * @param recordIds 需要存档的维修记录主键集合
     * @return 结果
     */
    @Override
    public int archiveRepairRecords(Long[] recordIds) {
        int count = 0;
        for (Long recordId : recordIds) {
            DmsRepairRecord updateEntity = new DmsRepairRecord();
            updateEntity.setRecordId(recordId);
            updateEntity.setIsArchived("1"); // 已存档
            updateEntity.setUpdateTime(DateUtils.getNowDate());
            updateEntity.setUpdateBy(SecurityUtils.getUserId());
            count += dmsRepairRecordMapper.updateDmsRepairRecord(updateEntity);
        }
        return count;
    }

    /**
     * 根据设备ID查询维修历史
     * 
     * @param deviceId 设备ID
     * @return 维修记录集合
     */
    @Override
    public List<DmsRepairRecord> selectRepairHistoryByDeviceId(Long deviceId) {
        DmsRepairRecord query = new DmsRepairRecord();
        query.setDeviceId(deviceId);
        return dmsRepairRecordMapper.selectDmsRepairRecordList(query);
    }
}
