package com.css.simulation.resource.server.app.service;

import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.model.LidarParam;
import api.common.pojo.po.model.ConfigPO;
import api.common.pojo.po.model.LidarPO;
import api.common.pojo.vo.model.LidarVO;
import api.common.util.LogUtil;
import api.common.util.ObjectUtil;
import api.common.util.StringUtil;
import api.common.util.TimeUtil;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.LidarMapper;
import com.css.simulation.resource.server.infra.db.mysql.mapper.ModelConfigMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

@Service
public class LidarService {

    @Resource
    private LidarMapper lidarMapper;
    @Resource
    private ModelConfigMapper modelConfigMapper;

    public int delLidarById(LidarPO lidarPO) {
        //1 将传感器相关的车辆配置修改为已禁用
        ConfigPO configPO = new ConfigPO();
        configPO.setIsUnavailable(DictConstants.IS_UNAVAILABLE);
        configPO.setSensorId(lidarPO.getId());
        configPO.setModifyUserId(AuthUtil.getCurrentUserId());
        configPO.setModifyTime(TimeUtil.getNowForMysql());
        modelConfigMapper.updateIsUnavailableBySensorId(configPO);
        //2 删除传感器
        lidarPO.setIsDeleted(DictConstants.YES);
        lidarPO.setModifyUserId(AuthUtil.getCurrentUserId());
        lidarPO.setModifyTime(TimeUtil.getNowForMysql());
        int i = lidarMapper.delLidarById(lidarPO);
        LogUtil.delete();
        return i;
    }

    public List<LidarVO> getLidarList(LidarParam lidarParam) {
        lidarParam.setCreateUserId(AuthUtil.getCurrentUserId());
        lidarParam.setShare(null);
        List<LidarVO> list = lidarMapper.getLidarList(lidarParam);
        return list;
    }

    public List<LidarVO> getMyLidarList(LidarParam lidarParam) {
        lidarParam.setCreateUserId(AuthUtil.getCurrentUserId());
//        lidarParam.setShare(DictConstants.NO);

        lidarParam.setShare(null);
        List<LidarVO> list = lidarMapper.getLidarList(lidarParam);
        list.forEach(element -> {
            if ("1".equals(element.getShare())) {
                element.setSensorName(element.getSensorName() + "（公有）");
            }
        });
        return list;
    }
    public List getMyLidarList1(LidarParam lidarParam) {
        lidarParam.setCreateUserId(AuthUtil.getCurrentUserId());
        lidarParam.setShare("0");
        List<LidarVO> listSy = lidarMapper.getLidarListGSy(lidarParam);
        lidarParam.setCreateUserId("");
        lidarParam.setShare("1");
        List<LidarVO> listGy= lidarMapper.getLidarListGSy(lidarParam);
        List<Map> treeList = new ArrayList<>();
        Map typeMap = new HashMap();
        typeMap.put("code", "公有");
        typeMap.put("name", "公有");
        List<Map> colourList = new LinkedList();
        listGy.forEach(vo -> {
            Map colourMap = new HashMap();
            colourMap.put("code", vo.getId());
            colourMap.put("name", vo.getSensorName());
            colourMap.put("vo", vo);
            colourList.add(colourMap);
        });
        typeMap.put("children", colourList);
        Map typeMap1 = new HashMap();
        typeMap1.put("code", "私有");
        typeMap1.put("name", "私有");
        List<Map> colourList1 = new LinkedList();
        listSy.forEach(vo -> {
            Map colourMap = new HashMap();
            colourMap.put("code", vo.getId());
            colourMap.put("name", vo.getSensorName());
            colourMap.put("vo", vo);
            colourList1.add(colourMap);
        });
        typeMap1.put("children", colourList1);
        treeList.add(typeMap);
        treeList.add(typeMap1);
        return treeList;
    }

    public LidarVO getLidarInfo(LidarParam lidarParam) {
        return lidarMapper.getLidarInfo(lidarParam);
    }

    public LidarPO saveLidar(LidarPO lidarPO) {
        //常规字段赋值
        String currentUserId = AuthUtil.getCurrentUserId();
        Timestamp currentTime = TimeUtil.getNowForMysql();
        lidarPO.setCreateUserId(currentUserId);
        lidarPO.setCreateTime(currentTime);
        lidarPO.setModifyUserId(currentUserId);
        lidarPO.setModifyTime(currentTime);
        lidarPO.setIsDeleted(DictConstants.NO);
        lidarPO.setShare(DictConstants.NO);//私有
        //名称校验
        List<LidarVO> list = lidarMapper.checkLidarName(lidarPO);
        if (ObjectUtil.isNotNull(list)) {
            lidarPO.setId(null);
            return lidarPO;
        }
        String id = lidarPO.getId();
        if (ObjectUtil.isNull(id)) {//新增
            lidarPO.setId(StringUtil.getRandomUUID());
            lidarPO.setSensorCode(StringUtil.getRandomCode());
            lidarMapper.insert(lidarPO);
            LogUtil.insert();
        } else {//修改
            lidarMapper.update(lidarPO);
            LogUtil.update();
        }
        return lidarPO;
    }

    public LidarPO shareLidar(LidarPO lidarPO) {
        //调用保存
        saveLidar(lidarPO);
        lidarPO.setShare(DictConstants.YES);//转公有
        lidarPO.setCreateUserId(null);
        //名称校验
        List<LidarVO> list = lidarMapper.checkLidarName(lidarPO);
        if (ObjectUtil.isNotNull(list)) {
            String currentUserId = AuthUtil.getCurrentUserId();
            Timestamp currentTime = TimeUtil.getNowForMysql();
            lidarPO.setId(list.get(0).getId());
            lidarPO.setModifyUserId(currentUserId);
            lidarPO.setModifyTime(currentTime);
            lidarMapper.update(lidarPO);
            LogUtil.update();
        } else {
            //常规字段赋值
            String currentUserId = AuthUtil.getCurrentUserId();
            Timestamp currentTime = TimeUtil.getNowForMysql();
            lidarPO.setCreateUserId(currentUserId);
            lidarPO.setCreateTime(currentTime);
            lidarPO.setModifyUserId(currentUserId);
            lidarPO.setModifyTime(currentTime);
            lidarPO.setIsDeleted(DictConstants.NO);
            lidarPO.setId(StringUtil.getRandomUUID());
            lidarPO.setSensorCode(StringUtil.getRandomCode());
            lidarMapper.insert(lidarPO);
        }
        LogUtil.share();
        return lidarPO;
    }


}
