package com.zehong.system.service.impl;

import com.zehong.common.core.domain.entity.SysUser;
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.StringUtils;
import com.zehong.system.domain.TDeviceInfo;
import com.zehong.system.domain.TInspectionData;
import com.zehong.system.domain.TInspectionPlan;
import com.zehong.system.domain.form.InspectionDataFrom;
import com.zehong.system.domain.form.TWorkOrderForm;
import com.zehong.system.domain.vo.InspectionDataVo;
import com.zehong.system.domain.vo.TInspectionDataVo;
import com.zehong.system.domain.vo.WorkOrderVo;
import com.zehong.system.mapper.TDeviceInfoMapper;
import com.zehong.system.mapper.TInspectionDataMapper;
import com.zehong.system.service.ISysUserService;
import com.zehong.system.service.ITInspectionDataService;
import com.zehong.system.service.ITInspectionPlanService;
import com.zehong.system.service.ITWorkOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 巡检记录Service业务层处理
 * 
 * @author zehong
 * @date 2021-07-21
 */
@Service
public class TInspectionDataServiceImpl implements ITInspectionDataService 
{
    @Resource
    private TInspectionDataMapper tInspectionDataMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITWorkOrderService itWorkOrderService;

    @Autowired
    private ITInspectionPlanService itInspectionPlanService;

    @Resource
    private TDeviceInfoMapper tDeviceInfoMapper;

    /**
     * 查询巡检记录
     * 
     * @param dataId 巡检记录ID
     * @return 巡检记录
     */
    @Override
    public TInspectionData selectTInspectionDataById(int dataId)
    {
        return tInspectionDataMapper.selectTInspectionDataById(dataId);
    }

    /**
     * 查询巡检记录列表
     * 
     * @param tInspectionData 巡检记录
     * @return 巡检记录
     */
    @Override
    public List<TInspectionData> selectTInspectionDataList(TInspectionData tInspectionData)
    {
        return tInspectionDataMapper.selectTInspectionDataList(tInspectionData);
    }

    /**
     * 新增巡检记录
     * 
     * @param tInspectionData 巡检记录
     * @return 结果
     */
    @Override
    public int insertTInspectionData(TInspectionData tInspectionData)
    {
        tInspectionData.setCreateTime(DateUtils.getNowDate());
        return tInspectionDataMapper.insertTInspectionData(tInspectionData);
    }

    /**
     * 修改巡检记录
     * 
     * @param tInspectionData 巡检记录
     * @return 结果
     */
    @Override
    public int updateTInspectionData(TInspectionData tInspectionData)
    {
        tInspectionData.setUpdateTime(DateUtils.getNowDate());
        return tInspectionDataMapper.updateTInspectionData(tInspectionData);
    }

    /**
     * 批量删除巡检记录
     * 
     * @param dataIds 需要删除的巡检记录ID
     * @return 结果
     */
    @Override
    public int deleteTInspectionDataByIds(int[] dataIds)
    {
        return tInspectionDataMapper.deleteTInspectionDataByIds(dataIds);
    }

    /**
     * 删除巡检记录信息
     * 
     * @param dataId 巡检记录ID
     * @return 结果
     */
    @Override
    public int deleteTInspectionDataById(int dataId)
    {
        return tInspectionDataMapper.deleteTInspectionDataById(dataId);
    }

    /**
     * 获取值班人员巡检路线
     */
    @Override
    public List<InspectionDataVo> getInspectionDataByInspector(InspectionDataFrom form) throws Exception {
        List<InspectionDataVo> result = new ArrayList<>();
        if(null == form.getUserId()){
            //获取巡检人员
            List<SysUser> inspectorlist = userService.selectInspectorList(new SysUser());
            for(SysUser inspector : inspectorlist){
                InspectionDataVo inspectionDataVo = new InspectionDataVo();
                inspectionDataVo.setSysUser(inspector);
                form.setUserId(inspector.getUserId());
                inspectionDataVo.setInspectionDataVo(getinspectionDataVo(form));
                result.add(inspectionDataVo);
            }
        }else{
            InspectionDataVo inspectionDataVo = new InspectionDataVo();
            SysUser user = userService.selectUserById(form.getUserId());
            inspectionDataVo.setSysUser(user);
            inspectionDataVo.setInspectionDataVo(getinspectionDataVo(form));
            result.add(inspectionDataVo);
        }

        return result;

    }

    private List<TInspectionDataVo> getinspectionDataVo(InspectionDataFrom form) throws Exception {
        //获取巡检人员工单
        TWorkOrderForm workOrderForm = new TWorkOrderForm();
        workOrderForm.setAppointInspector(form.getUserId());
        List<WorkOrderVo> workOrderInfo = itWorkOrderService.selectTWorkOrderList(workOrderForm);
        List<TInspectionDataVo> inspectionDataVos = new ArrayList<>();
        for(WorkOrderVo workOrder : workOrderInfo ){
            //获取巡检人员巡检计划
            TInspectionPlan tInspectionPlan = new TInspectionPlan();
            tInspectionPlan.setOrderId(workOrder.getOrderId());
            List<TInspectionPlan> inspectionPlans = itInspectionPlanService.selectTInspectionPlanList(tInspectionPlan);
            for(TInspectionPlan inspectionPlan : inspectionPlans){
                //获取巡检员巡检记录
                Map<String,Object> map = new HashMap<>();
                map.put("planId",inspectionPlan.getPlanId());
                map.put("beginTime",form.getBeginTime());
                map.put("endTime",form.getEndTime());
                List<TInspectionData> inspectionDataList = tInspectionDataMapper.selectTInspectionDataListByMap(map);
                //查询设备信息
                for(TInspectionData inspectionData : inspectionDataList){
                    TInspectionDataVo tInspectionDataVo = new TInspectionDataVo();
                    TDeviceInfo device = tDeviceInfoMapper.selectTDeviceInfoByCode(inspectionData.getDeviceCode());
                    BeanUtils.copyProperties(inspectionData, tInspectionDataVo);
                    tInspectionDataVo.setLatitude(device.getLatitude());
                    tInspectionDataVo.setLongitude(device.getLongitude());
                    inspectionDataVos.add(tInspectionDataVo);
                }

            }
        }

        return inspectionDataVos;
    }
}
