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


import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.model.OgtParam;
import api.common.pojo.po.model.ConfigPO;
import api.common.pojo.po.model.OgtPO;
import api.common.pojo.vo.model.OgtVO;
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.ModelConfigMapper;
import com.css.simulation.resource.server.infra.db.mysql.mapper.OgtMapper;
import org.springframework.stereotype.Service;

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

@Service
public class OgtService {

    @Resource
    private OgtMapper ogtMapper;
    @Resource
    private ModelConfigMapper modelConfigMapper;


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


    public List<OgtVO> getOgtList(OgtParam ogtParam) {
        ogtParam.setCreateUserId(AuthUtil.getCurrentUserId());
        ogtParam.setShare(null);
        return ogtMapper.getOgtList(ogtParam);
    }

    public List<OgtVO> getMyOgtList(OgtParam ogtParam) {
        ogtParam.setCreateUserId(AuthUtil.getCurrentUserId());
//        ogtParam.setShare(DictConstants.NO);
        ogtParam.setShare(null);
        List<OgtVO> list = ogtMapper.getOgtList(ogtParam);
        list.forEach(element -> {
            if ("1".equals(element.getShare())) {
                element.setSensorName(element.getSensorName() + "（公有）");
            }
        });
        return list;
    }
    public List getMyOgtList1(OgtParam ogtParam) {
        ogtParam.setCreateUserId(AuthUtil.getCurrentUserId());
        ogtParam.setShare("0");
        List<OgtVO> listSy = ogtMapper.getOgtList(ogtParam);
        ogtParam.setCreateUserId("");
        ogtParam.setShare("1");
        List<OgtVO> listGy = ogtMapper.getOgtList(ogtParam);
        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 OgtVO getOgtInfo(OgtParam ogtParam) {
        return ogtMapper.getOgtInfo(ogtParam);
    }

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

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

}
