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

import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.model.GpsParam;
import api.common.pojo.po.model.ConfigPO;
import api.common.pojo.po.model.GpsPO;
import api.common.pojo.vo.model.GpsVO;
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.util.PoUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.GpsMapper;
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 GpsService {

    @Resource
    private GpsMapper gpsMapper;
    @Resource
    private ModelConfigMapper modelConfigMapper;
    //* -------------------------------- 删除 GPS --------------------------------

    public void delGpsById(GpsPO gpsPO) {
        //1 将传感器相关的车辆配置修改为已禁用
        ConfigPO configPO = new ConfigPO();
        configPO.setIsUnavailable(DictConstants.IS_UNAVAILABLE);
        configPO.setSensorId(gpsPO.getId());
        configPO.setModifyUserId(AuthUtil.getCurrentUserId());
        configPO.setModifyTime(TimeUtil.getNowForMysql());
        modelConfigMapper.updateIsUnavailableBySensorId(configPO);
        //2 删除传感器
        PoUtil.initDelPo(gpsPO);
        gpsMapper.delGpsById(gpsPO);
        LogUtil.delete();
    }
    //* -------------------------------- Comment --------------------------------

    public List<GpsVO> getGpsList(GpsParam gpsParam) {
        gpsParam.setCreateUserId(AuthUtil.getCurrentUserId());
        gpsParam.setShare(null);
        return gpsMapper.getGpsList(gpsParam);
    }
    public List getGpsList1(GpsParam gpsParam) {
        gpsParam.setCreateUserId(AuthUtil.getCurrentUserId());
        gpsParam.setShare("0");
        List<GpsVO> listSy= gpsMapper.getGpsListGSy(gpsParam);
        gpsParam.setCreateUserId("");
        gpsParam.setShare("1");
        List<GpsVO> listGy= gpsMapper.getGpsListGSy(gpsParam);
        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 List<GpsVO> getMyGpsList(GpsParam gpsParam) {
        gpsParam.setCreateUserId(AuthUtil.getCurrentUserId());
        gpsParam.setShare(null);
        List<GpsVO> gpsList = gpsMapper.getGpsList(gpsParam);
        gpsList.forEach(gps -> {
            if ("1".equals(gps.getShare())) {
                gps.setSensorName(gps.getSensorName() + "（公有）");
            }
        });
        return gpsList;
    }

    public GpsVO getGpsInfo(GpsParam gpsParam) {
        return gpsMapper.getGpsInfo(gpsParam);
    }

    public GpsPO saveGps(GpsPO gpsPO) {
        //常规字段赋值
        PoUtil.initAddPo(gpsPO);
        gpsPO.setShare(DictConstants.NO);//私有
        //名称校验
        List<GpsVO> list = gpsMapper.checkGpsName(gpsPO);
        if (ObjectUtil.isNotNull(list)) {
            gpsPO.setId(null);
            return gpsPO;
        }
        String id = gpsPO.getId();
        if (ObjectUtil.isNull(id)) {//新增
            gpsPO.setId(StringUtil.getRandomUUID());
            gpsPO.setSensorCode(StringUtil.getRandomCode());
            gpsMapper.insert(gpsPO);
            LogUtil.insert();
        } else {//修改
            gpsMapper.update(gpsPO);
            LogUtil.update();
        }
        return gpsPO;
    }

    public GpsPO shareGps(GpsPO gpsPO) {
        //调用保存
        saveGps(gpsPO);
        gpsPO.setShare(DictConstants.YES);//转公有
        gpsPO.setCreateUserId(null);
        //名称校验
        List<GpsVO> list = gpsMapper.checkGpsName(gpsPO);
        if (ObjectUtil.isNotNull(list)) {
            String currentUserId = AuthUtil.getCurrentUserId();
            Timestamp currentTime = TimeUtil.getNowForMysql();
            gpsPO.setId(list.get(0).getId());
            gpsPO.setModifyUserId(currentUserId);
            gpsPO.setModifyTime(currentTime);
            gpsMapper.update(gpsPO);
            LogUtil.update();
        } else {
            //常规字段赋值
            PoUtil.initAddPo(gpsPO);
            gpsPO.setId(StringUtil.getRandomUUID());
            gpsPO.setSensorCode(StringUtil.getRandomCode());
            gpsMapper.insert(gpsPO);
        }
        LogUtil.share();
        return gpsPO;
    }

}
