package com.zehong.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.zehong.common.utils.DateUtils;
import com.zehong.system.domain.TPipeTaskInspect;
import com.zehong.system.mapper.TPipeTaskInspectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.TPipeInfoMapper;
import com.zehong.system.domain.TPipeInfo;
import com.zehong.system.service.ITPipeInfoService;

/**
 * 管道信息Service业务层处理
 * 
 * @author zehong
 * @date 2023-11-02
 */
@Service
public class TPipeInfoServiceImpl implements ITPipeInfoService 
{
    @Autowired
    private TPipeInfoMapper tPipeInfoMapper;
    @Autowired
    private TPipeTaskInspectMapper tPipeTaskInspectMapper;

    /**
     * 查询管道信息
     * 
     * @param pipeId 管道信息ID
     * @return 管道信息
     */
    @Override
    public TPipeInfo selectTPipeInfoById(Long pipeId)
    {
        return tPipeInfoMapper.selectTPipeInfoById(pipeId);
    }

    /**
     * 查询管道信息列表
     * 
     * @param tPipeInfo 管道信息
     * @return 管道信息
     */
    @Override
    public List<TPipeInfo> selectTPipeInfoList(TPipeInfo tPipeInfo)
    {
        return tPipeInfoMapper.selectTPipeInfoList(tPipeInfo);
    }

    /**
     * 新增管道信息
     * 
     * @param tPipeInfo 管道信息
     * @return 结果
     */
    @Override
    public int insertTPipeInfo(TPipeInfo tPipeInfo)
    {
        tPipeInfo.setCreateTime(DateUtils.getNowDate());
        return tPipeInfoMapper.insertTPipeInfo(tPipeInfo);
    }

    /**
     * 修改管道信息
     * 
     * @param tPipeInfo 管道信息
     * @return 结果
     */
    @Override
    public int updateTPipeInfo(TPipeInfo tPipeInfo)
    {
        tPipeInfo.setUpdateTime(DateUtils.getNowDate());
        return tPipeInfoMapper.updateTPipeInfo(tPipeInfo);
    }

    /**
     * 批量删除管道信息
     * 
     * @param pipeIds 需要删除的管道信息ID
     * @return 结果
     */
    @Override
    public int deleteTPipeInfoByIds(Long[] pipeIds)
    {
        return tPipeInfoMapper.deleteTPipeInfoByIds(pipeIds);
    }

    /**
     * 删除管道信息信息
     * 
     * @param pipeId 管道信息ID
     * @return 结果
     */
    @Override
    public int deleteTPipeInfoById(Long pipeId)
    {
        return tPipeInfoMapper.deleteTPipeInfoById(pipeId);
    }

    /**
     * 根据巡线查询管道信息
     * @param tPipeInfo 管道信息
     * @return
     */
    @Override
    public List<TPipeInfo> selectPipeInfoByUser(TPipeInfo tPipeInfo){
        return tPipeInfoMapper.selectPipeInfoByUser(tPipeInfo);
    }

    /**
     * 查询已巡线段
     * @return
     */
    @Override
    public List<TPipeTaskInspect> selectTPipeInspectList() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String datestr = dateFormat.format(new Date());
        List<TPipeTaskInspect> allPoint = tPipeTaskInspectMapper.selectTPipeInspectList(datestr);
        //返回多个线段数组
        List<TPipeTaskInspect> resplist = new ArrayList<>();
        //单个线段
        TPipeTaskInspect onelist = new TPipeTaskInspect();
        for(TPipeTaskInspect inspect : allPoint){
            if(inspect.getStatus()==0){
                if(onelist.getStatus()!=null&&onelist.getCoordinates().size()>1){
                    resplist.add(onelist);
                }
                onelist= new TPipeTaskInspect();
                continue;
            }
            if(onelist.getStatus()==null){
                inspect.setCoordinates(tolist(inspect));
                onelist = inspect;
                continue;
            }
            if(onelist.getTaskId()==inspect.getTaskId()&&inspect.getPipeId()==onelist.getPipeId()){
               List<List<String>> list = onelist.getCoordinates();
               list.add(tolist(inspect).get(0));
                onelist.setCoordinates(list );
            }else {
                if(onelist!=null){
                    resplist.add(onelist);
                }
                onelist = new TPipeTaskInspect();
                inspect.setCoordinates(tolist(inspect));
                onelist= inspect;
            }
        }
        if(onelist.getStatus()!=null&&onelist.getCoordinates().size()>1){
            resplist.add(onelist);
        }
        return resplist;
    }

    public List<List<String>> tolist(TPipeTaskInspect inspect){
        List<List<String>> list = new ArrayList<>();
        list.add(new ArrayList<>(Arrays.asList(inspect.getLongitude().toString(), inspect.getLatitude().toString())));
        return list;
    }
}
