package com.xzzn.ems.service.impl;

import java.util.List;
import java.util.Random;

import com.xzzn.common.enums.AlarmStatus;
import com.xzzn.common.utils.DateUtils;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.EmsAlarmRecords;
import com.xzzn.ems.domain.EmsTicket;
import com.xzzn.ems.domain.vo.TicketListVo;
import com.xzzn.ems.mapper.EmsAlarmRecordsMapper;
import com.xzzn.ems.mapper.EmsTicketMapper;
import com.xzzn.ems.service.IEmsAlarmRecordsService;
import com.xzzn.ems.service.IEmsTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 工单主Service业务层处理
 * 
 * @author xzzn
 * @date 2025-06-26
 */
@Service
public class EmsTicketServiceImpl implements IEmsTicketService
{
    @Autowired
    private EmsTicketMapper emsTicketMapper;
    @Autowired
    private EmsAlarmRecordsMapper emsAlarmRecordsMapper;
    @Autowired
    private IEmsAlarmRecordsService iEmsAlarmRecordsService;

    /**
     * 查询工单主-工单详情
     * 
     * @param id 工单主主键
     * @return 工单主
     */
    @Override
    public TicketListVo selectEmsTicketById(String id)
    {
        return emsTicketMapper.getTicketDetailInfo(id);
    }

    /**
     * 查询工单主列表
     * 
     * @param emsTicket 工单主
     * @return 工单主
     */
    @Override
    public List<EmsTicket> selectEmsTicketList(EmsTicket emsTicket)
    {
        return emsTicketMapper.selectEmsTicketList(emsTicket);
    }

    /**
     * 新增工单主
     * 
     * @param emsTicket 工单主
     * @return 结果
     */
    @Override
    public int insertEmsTicket(EmsTicket emsTicket)
    {
        // 工单号随机生产
        String ticketNo = "";
        String nowDate = DateUtils.dateTime();
        ticketNo = "T" + nowDate + String.format("%06d", new Random().nextInt(1000000));
        emsTicket.setTicketNo(ticketNo);
        emsTicket.setStatus(1L);//默认待处理
        emsTicket.setCreateTime(DateUtils.getNowDate());
        return emsTicketMapper.insertEmsTicket(emsTicket);
    }

    /**
     * 修改工单主
     * 
     * @param emsTicket 工单主
     * @return 结果
     */
    @Override
    public int updateEmsTicket(EmsTicket emsTicket)
    {
        String status = emsTicket.getStatus() == null ? "" : emsTicket.getStatus().toString();
        String ticketNo = emsTicket.getTicketNo();
        // 判断工单状态为：3-已完成
        if (!StringUtils.isEmpty(status) && "3".equals(status) && !StringUtils.isEmpty(ticketNo)) {
            // 处理告警数据状态-已处理
            EmsAlarmRecords emsAlarmRecords = emsAlarmRecordsMapper.getAlarmByTicketNo(ticketNo);
            if (emsAlarmRecords != null) {
                emsAlarmRecords.setStatus(AlarmStatus.DONE.getCode());
                emsAlarmRecords.setUpdateTime(DateUtils.getNowDate());
                emsAlarmRecords.setAlarmEndTime(DateUtils.getNowDate());
                emsAlarmRecordsMapper.updateEmsAlarmRecords(emsAlarmRecords);
                // 更新告警redis
                iEmsAlarmRecordsService.updateRedisAlarmRecords(emsAlarmRecords.getSiteId(),emsAlarmRecords.getDeviceId());
            }
        }
        emsTicket.setUpdateTime(DateUtils.getNowDate());
        return emsTicketMapper.updateEmsTicket(emsTicket);
    }

    /**
     * 批量删除工单主
     * 
     * @param ids 需要删除的工单主主键
     * @return 结果
     */
    @Override
    public int deleteEmsTicketByIds(String[] ids)
    {
        return emsTicketMapper.deleteEmsTicketByIds(ids);
    }

    /**
     * 删除工单主信息
     * 
     * @param id 工单主主键
     * @return 结果
     */
    @Override
    public int deleteEmsTicketById(String id)
    {
        return emsTicketMapper.deleteEmsTicketById(id);
    }

    /**
     * 删除工单主信息-物理删除
     *
     * @param id 工单主主键
     * @return 结果
     */
    @Override
    public int dropEmsTicketById(String id)
    {
        return emsTicketMapper.dropEmsTicketById(id);
    }

    /**
     * 根据工单状态数组获取-工单列表
     * @param statusList
     * @return
     */
    @Override
    public List<TicketListVo> getAllTicketList(Long[] statusList) {
        return emsTicketMapper.getAllTicketList(statusList);
    }
}
