package com.zehong.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zehong.common.utils.DateUtils;
import com.zehong.system.domain.*;
import com.zehong.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.service.ITOrderFeedbackService;

/**
 * 工单反馈信息Service业务层处理
 * 
 * @author zehong
 * @date 2021-07-19
 */
@Service
public class TOrderFeedbackServiceImpl implements ITOrderFeedbackService 
{
    @Autowired
    private TOrderFeedbackMapper tOrderFeedbackMapper;
    @Autowired
    private TWorkOrderMapper tWorkOrderMapper;
    @Autowired
    private TInspectionDataMapper tInspectionDataMapper;
    @Autowired
    private TInspectionPlanMapper tInspectionPlanMapper;
    @Autowired
    private THiddenTroubleMapper tHiddenTroubleMapper;
    @Autowired
    private TDeviceAlarmMapper tDeviceAlarmMapper;
    @Autowired
    private TPipeMapper tPipeMapper;
    @Autowired
    private TDeviceInfoMapper tDeviceInfoMapper;

    /**
     * 查询工单反馈信息
     * 
     * @param feedbackId 工单反馈信息ID
     * @return 工单反馈信息
     */
    @Override
    public TOrderFeedback selectTOrderFeedbackById(int feedbackId)
    {
        return tOrderFeedbackMapper.selectTOrderFeedbackById(feedbackId);
    }

    /**
     * 查询工单反馈信息列表
     * 
     * @param tOrderFeedback 工单反馈信息
     * @return 工单反馈信息
     */
    @Override
    public List<TOrderFeedback> selectTOrderFeedbackList(TOrderFeedback tOrderFeedback)
    {
        return tOrderFeedbackMapper.selectTOrderFeedbackList(tOrderFeedback);
    }

    /**
     * 新增工单反馈信息
     * 
     * @param tOrderFeedback 工单反馈信息
     * @return 结果
     */
    @Override
    public int insertTOrderFeedback(TOrderFeedback tOrderFeedback) throws Exception
    {
        if("2".equals(tOrderFeedback.getIsHiddenDanger())){
            tOrderFeedback.setDealStatus("1");
        }
        tOrderFeedback.setFeedbackTime(DateUtils.getNowDate());

        String dealStatus = tOrderFeedback.getDealStatus();
        String isHiddenDanger = tOrderFeedback.getIsHiddenDanger();

        // 根据工单类型和源id修改源数据状态
        TWorkOrder order = tWorkOrderMapper.selectTWorkOrderById(tOrderFeedback.getOrderId());
        String orderType = order.getOrderType();

        if("1".equals(orderType)){
            TInspectionPlan tInspectionPlan = tInspectionPlanMapper.selectTInspectionPlanById(order.getResourceId());
            Integer planId = tInspectionPlan.getPlanId();
            Integer deviceId = tOrderFeedback.getDeviceId();

            TInspectionData data = tInspectionDataMapper.selectTInspectionDataByDeviceId(planId, deviceId);
            data.setDealStatus(dealStatus);
            data.setIsHiddenDanger(isHiddenDanger);
            data.setUpdateTime(DateUtils.getNowDate());

            tOrderFeedback.setDeviceType(data.getDeviceType());

            tInspectionDataMapper.updateTInspectionData(data);

            if("0".equals(data.getDeviceType())){
                TPipe pipe = tPipeMapper.selectTPipeById(deviceId);
                pipe.setInspectionTime(DateUtils.getNowDate());
                tPipeMapper.updateTPipe(pipe);
            } else {
                TDeviceInfo device = tDeviceInfoMapper.selectTDeviceInfoById(deviceId);
                device.setInspectionTime(DateUtils.getNowDate());
                tDeviceInfoMapper.updateTDeviceInfo(device);
            }
        } else if("2".equals(orderType)) {
            THiddenTrouble trouble = tHiddenTroubleMapper.selectTHiddenTroubleById(order.getResourceId());
            trouble.setDealStatus(dealStatus);
            trouble.setUpdateTime(DateUtils.getNowDate());

            tOrderFeedback.setDeviceId(trouble.getDeviceId());
            tOrderFeedback.setDeviceType(trouble.getDeviceType());

            tHiddenTroubleMapper.updateTHiddenTrouble(trouble);

            if("0".equals(trouble.getDeviceType())){
                TPipe pipe = tPipeMapper.selectTPipeById(trouble.getDeviceId());
                pipe.setInspectionTime(DateUtils.getNowDate());
                tPipeMapper.updateTPipe(pipe);
            } else {
                TDeviceInfo device = tDeviceInfoMapper.selectTDeviceInfoById(trouble.getDeviceId());
                device.setInspectionTime(DateUtils.getNowDate());
                tDeviceInfoMapper.updateTDeviceInfo(device);
            }
        } else if("3".equals(orderType)) {
            TDeviceAlarm alarm = tDeviceAlarmMapper.selectTDeviceAlarmById(order.getResourceId());
            alarm.setDealStatus(dealStatus);
            alarm.setUpdateTime(DateUtils.getNowDate());

            tOrderFeedback.setDeviceId(alarm.getDeviceId());
            tOrderFeedback.setDeviceType(alarm.getDeviceType());

            tDeviceAlarmMapper.updateTDeviceAlarm(alarm);

            if("0".equals(alarm.getDeviceType())){
                TPipe pipe = tPipeMapper.selectTPipeById(alarm.getDeviceId());
                pipe.setInspectionTime(DateUtils.getNowDate());
                tPipeMapper.updateTPipe(pipe);
            } else {
                TDeviceInfo device = tDeviceInfoMapper.selectTDeviceInfoById(alarm.getDeviceId());
                device.setInspectionTime(DateUtils.getNowDate());
                tDeviceInfoMapper.updateTDeviceInfo(device);
            }
        }

        // 如果工单状态是已接收，修改状态为已反馈
        if("1".equals(order.getOrderStatus())){
            order.setOrderStatus("2");
            tWorkOrderMapper.updateTWorkOrder(order);
        }

        return tOrderFeedbackMapper.insertTOrderFeedback(tOrderFeedback);
    }

    /**
     * 修改工单反馈信息
     * 
     * @param tOrderFeedback 工单反馈信息
     * @return 结果
     */
    @Override
    public int updateTOrderFeedback(TOrderFeedback tOrderFeedback)
    {
        return tOrderFeedbackMapper.updateTOrderFeedback(tOrderFeedback);
    }

    /**
     * 批量删除工单反馈信息
     * 
     * @param feedbackIds 需要删除的工单反馈信息ID
     * @return 结果
     */
    @Override
    public int deleteTOrderFeedbackByIds(int[] feedbackIds)
    {
        return tOrderFeedbackMapper.deleteTOrderFeedbackByIds(feedbackIds);
    }

    /**
     * 删除工单反馈信息信息
     * 
     * @param feedbackId 工单反馈信息ID
     * @return 结果
     */
    @Override
    public int deleteTOrderFeedbackById(int feedbackId)
    {
        return tOrderFeedbackMapper.deleteTOrderFeedbackById(feedbackId);
    }
}
