package com.zehong.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.zehong.common.core.domain.entity.SysUser;
import com.zehong.common.utils.DateUtils;
import com.zehong.system.domain.*;
import com.zehong.system.domain.form.TWorkOrderForm;
import com.zehong.system.domain.vo.WorkOrderVo;
import com.zehong.system.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.service.ITWorkOrderService;

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

    /**
     * 查询工单基础信息
     * 
     * @param orderId 工单基础信息ID
     * @return 工单基础信息
     */
    @Override
    public WorkOrderVo selectTWorkOrderById(String orderId) throws Exception
    {
        WorkOrderVo workOrderVo = new WorkOrderVo();
        TWorkOrder tWorkOrder = tWorkOrderMapper.selectTWorkOrderById(orderId);

        BeanUtils.copyProperties(tWorkOrder, workOrderVo);

        // 获取巡检员姓名
        SysUser appointInspector = sysUserMapper.selectUserById(tWorkOrder.getAppointInspector());
        workOrderVo.setAppointInspectorName(appointInspector.getNickName());

        if(tWorkOrder.getActualInspector() != tWorkOrder.getAppointInspector() && tWorkOrder.getActualInspector() != null){
            SysUser actualInspector = sysUserMapper.selectUserById(tWorkOrder.getActualInspector());
            workOrderVo.setActualInspectorName(actualInspector.getNickName());
        } else {
            workOrderVo.setActualInspectorName(appointInspector.getNickName());
        }

        // 获取源数据(1巡检信息，2隐患信息，3报警信息)
        String orderType = tWorkOrder.getOrderType();
        int resourceId = tWorkOrder.getResourceId();

        List<TDeviceInfo> deviceInfoList = new ArrayList<>();

        if("1".equals(orderType)){

            TInspectionPlan plan = tInspectionPlanMapper.selectTInspectionPlanById(resourceId);
            workOrderVo.setDeviceCodes(plan.getDeviceCodes());

            TInspectionData data = new TInspectionData();
            data.setPlanId(resourceId);

            List<TInspectionData> totalList = tInspectionDataMapper.selectTInspectionDataList(data);
            workOrderVo.setDeviceNum(totalList.size());

            TDeviceInfo deviceInfo = null;
            for(TInspectionData temp : totalList){
                deviceInfo = tDeviceInfoMapper.selectTDeviceInfoByCode(temp.getDeviceCode());
                if(deviceInfo != null){
                    deviceInfoList.add(deviceInfo);
                }
            }

            int finish = tInspectionDataMapper.selectFinishTInspectionData(resourceId);
            workOrderVo.setFinishNum(finish);

        } else if("2".equals(orderType)) {

            THiddenTrouble trouble = tHiddenTroubleMapper.selectTHiddenTroubleById(resourceId);
            workOrderVo.setDeviceCodes((trouble.getDeviceCode() != null ? trouble.getDeviceCode() : null).toString());
            workOrderVo.setLongitude(trouble.getLongitude() != null ? trouble.getLongitude() : null);
            workOrderVo.setLatitude(trouble.getLatitude() != null ? trouble.getLatitude() : null);
            workOrderVo.setCoordinates(trouble.getCoordinates() != null ? trouble.getCoordinates() : null);
            workOrderVo.setDeviceType(trouble.getDeviceType());
            workOrderVo.setAddress(trouble.getAddress());

            String deviceCode = trouble.getDeviceCode();
            TDeviceInfo device = tDeviceInfoMapper.selectTDeviceInfoByCode(deviceCode);
            if(device != null){
                workOrderVo.setDeviceName(device.getDeviceName());
                deviceInfoList.add(device);
            }

        } else if("3".equals(orderType)) {

            TDeviceAlarm alarm = tDeviceAlarmMapper.selectTDeviceAlarmById(resourceId);
            String deviceCode = alarm.getDeviceCode();
            workOrderVo.setDeviceCodes(String.valueOf(deviceCode));

            String isPipe = alarm.getIsPipe();
            if("0".equals(isPipe)){
                TPipe pipe = tPipeMapper.selectTPipeByCode(deviceCode);
                workOrderVo.setCoordinates(pipe.getCoordinates());
                workOrderVo.setDeviceType("0");
                workOrderVo.setAddress(pipe.getPipeAddr());
            } else {
                TDeviceInfo device = tDeviceInfoMapper.selectTDeviceInfoByCode(deviceCode);
                if(device != null){
                    workOrderVo.setLongitude(device.getLongitude());
                    workOrderVo.setLatitude(device.getLatitude());
                    workOrderVo.setDeviceType(device.getDeviceType());
                    workOrderVo.setDeviceName(device.getDeviceName());
                    workOrderVo.setAddress(device.getDeviceAddr());
                    deviceInfoList.add(device);
                }
            }
        }
        workOrderVo.setDeviceInfoList(deviceInfoList);

        List<TOrderFeedback> feedbackList = tOrderFeedbackMapper.selectTOrderFeedbackByOrderId(tWorkOrder.getOrderId());
        if(feedbackList.size() != 0) {
            workOrderVo.setFeedbackList(feedbackList);
        }

        return workOrderVo;
    }

    /**
     * 查询工单基础信息列表
     * 
     * @param tWorkOrderForm 工单基础信息
     * @return 工单基础信息
     */
    @Override
    public List<WorkOrderVo> selectTWorkOrderList(TWorkOrderForm tWorkOrderForm) throws Exception
    {
        List<WorkOrderVo> workOrderVoList = new ArrayList<WorkOrderVo>();
        List<TWorkOrder> workOrderList = tWorkOrderMapper.selectTWorkOrderList(tWorkOrderForm);

        if(workOrderList.size() != 0){

            for(TWorkOrder workOrder : workOrderList){
                WorkOrderVo workOrderVo = new WorkOrderVo();
                BeanUtils.copyProperties(workOrder, workOrderVo);

                // 获取巡检员姓名
                SysUser appointInspector = sysUserMapper.selectUserById(workOrder.getAppointInspector());
                workOrderVo.setAppointInspectorName(appointInspector.getNickName());

                if(workOrder.getActualInspector() != workOrder.getAppointInspector() && workOrder.getActualInspector() != null){
                    SysUser actualInspector = sysUserMapper.selectUserById(workOrder.getActualInspector());
                    workOrderVo.setActualInspectorName(actualInspector.getNickName());
                } else {
                    workOrderVo.setActualInspectorName(appointInspector.getNickName());
                }

                workOrderVoList.add(workOrderVo);
            }
        }
        return workOrderVoList;
    }

    /**
     * 新增工单基础信息
     * 
     * @param tWorkOrder 工单基础信息
     * @return 结果
     */
    @Override
    public int insertTWorkOrder(TWorkOrder tWorkOrder) throws Exception
    {
        String orderId = tWorkOrderMapper.getWorkOrderId();
        int resourceId = tWorkOrder.getResourceId();
        tWorkOrder.setOrderId(orderId);
        tWorkOrder.setOrderStatus("0");
        tWorkOrder.setAllotTime(DateUtils.getNowDate());

        if("1".equals(tWorkOrder.getOrderType())){
            // 修改巡检计划状态为已下发
            TInspectionPlan plan = new TInspectionPlan();
            plan.setPlanId(resourceId);
            plan.setOrderId(orderId);
            plan.setPlanStatus("1");
            plan.setUpdateTime(DateUtils.getNowDate());
            tInspectionPlanMapper.updateTInspectionPlan(plan);

        } else if ("2".equals(tWorkOrder.getOrderType())) {
            THiddenTrouble trouble = new THiddenTrouble();
            trouble.setTroubleId(resourceId);
            trouble.setOrderId(orderId);
            trouble.setUpdateTime(DateUtils.getNowDate());
            tHiddenTroubleMapper.updateTHiddenTrouble(trouble);

        } else if ("3".equals(tWorkOrder.getOrderType())) {
            TDeviceAlarm alarm = new TDeviceAlarm();
            alarm.setAlarmId(resourceId);
            alarm.setOrderId(orderId);
            alarm.setUpdateTime(DateUtils.getNowDate());
            tDeviceAlarmMapper.updateTDeviceAlarm(alarm);
        }

        return tWorkOrderMapper.insertTWorkOrder(tWorkOrder);
    }

    /**
     * 修改工单基础信息
     * 
     * @param tWorkOrder 工单基础信息
     * @return 结果
     */
    @Override
    public int updateTWorkOrder(TWorkOrder tWorkOrder) throws Exception
    {
        if("1".equals(tWorkOrder.getOrderStatus())){

            int planId = tWorkOrder.getResourceId();
            // 修改巡检计划状态为进行中
            TInspectionPlan plan = new TInspectionPlan();
            plan.setPlanId(planId);
            plan.setPlanStatus("2");
            plan.setUpdateTime(DateUtils.getNowDate());
            tInspectionPlanMapper.updateTInspectionPlan(plan);

            tWorkOrder.setActualTime(DateUtils.getNowDate());

        } else if ("3".equals(tWorkOrder.getOrderStatus())){

            int planId = tWorkOrder.getResourceId();
            // 修改巡检计划状态为已完成
            TInspectionPlan plan = new TInspectionPlan();
            plan.setPlanId(planId);
            plan.setPlanStatus("3");
            plan.setUpdateTime(DateUtils.getNowDate());
            tInspectionPlanMapper.updateTInspectionPlan(plan);

        } else if ("0".equals(tWorkOrder.getOrderStatus())){
            tWorkOrder.setUpdateTime(DateUtils.getNowDate());
        }
        return tWorkOrderMapper.updateTWorkOrder(tWorkOrder);
    }

    /**
     * 批量删除工单基础信息
     * 
     * @param orderIds 需要删除的工单基础信息ID
     * @return 结果
     */
    @Override
    public int deleteTWorkOrderByIds(String[] orderIds)
    {
        return tWorkOrderMapper.deleteTWorkOrderByIds(orderIds);
    }

    /**
     * 删除工单基础信息信息
     * 
     * @param orderId 工单基础信息ID
     * @return 结果
     */
    @Override
    public int deleteTWorkOrderById(String orderId)
    {
        return tWorkOrderMapper.deleteTWorkOrderById(orderId);
    }
}
