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

import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.model.CameraParam;
import api.common.pojo.po.model.CameraPO;
import api.common.pojo.po.model.ConfigPO;
import api.common.pojo.vo.model.CameraVO;
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.CameraMapper;
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 CameraService {

    @Resource
    private CameraMapper cameraMapper;
    @Resource
    private ModelConfigMapper modelConfigMapper;

    public List<CameraVO> getCameraList(CameraParam cameraParam) {
        cameraParam.setCreateUserId(AuthUtil.getCurrentUserId());
        cameraParam.setShare(null);
        return cameraMapper.getCameraList(cameraParam);
    }

    public List<CameraVO> getMyCameraList(CameraParam cameraParam) {
        cameraParam.setCreateUserId(AuthUtil.getCurrentUserId());
//        cameraParam.setShare(DictConstants.NO);
        cameraParam.setShare(null);
        List<CameraVO> cameraList = cameraMapper.getCameraList(cameraParam);
        cameraList.forEach(camera -> {
            if ("1".equals(camera.getShare())) {
                camera.setSensorName(camera.getSensorName() + "（公有）");
            }
        });
        return cameraList;
    }
    public List getMyCameraList1(CameraParam cameraParam) {

        cameraParam.setCreateUserId(AuthUtil.getCurrentUserId());
        List<CameraVO> cameraListSy = cameraMapper.getCameraListSy(cameraParam);
        List<CameraVO> cameraListGy = cameraMapper.getCameraListGy();
        List<Map> treeList = new ArrayList<>();
        Map typeMap = new HashMap();
        typeMap.put("code", "公有");
        typeMap.put("name", "公有");
        List<Map> colourList = new LinkedList();
        cameraListGy.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();
        cameraListSy.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 CameraVO getCameraInfo(CameraParam cameraParam) {
        return cameraMapper.getCameraInfo(cameraParam);
    }


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

    public CameraPO shareCamera(CameraPO cameraPO) {
        //调用保存
        saveCamera(cameraPO);
        cameraPO.setShare(DictConstants.YES);//转公有
        cameraPO.setCreateUserId(null);
        // 同名覆盖，不同名新增
        List<CameraVO> list = cameraMapper.checkCameraName(cameraPO);
        if (ObjectUtil.isNotNull(list)) {
            String currentUserId = AuthUtil.getCurrentUserId();
            Timestamp currentTime = TimeUtil.getNowForMysql();
            cameraPO.setId(list.get(0).getId());
            cameraPO.setModifyUserId(currentUserId);
            cameraPO.setModifyTime(currentTime);
            cameraMapper.update(cameraPO);
            LogUtil.update();
        } else {
            //常规字段赋值
            String currentUserId = AuthUtil.getCurrentUserId();
            Timestamp currentTime = TimeUtil.getNowForMysql();
            cameraPO.setCreateUserId(currentUserId);
            cameraPO.setCreateTime(currentTime);
            cameraPO.setModifyUserId(currentUserId);
            cameraPO.setModifyTime(currentTime);
            cameraPO.setIsDeleted(DictConstants.NO);
            cameraPO.setId(StringUtil.getRandomUUID());
            cameraPO.setSensorCode(StringUtil.getRandomCode());
            cameraMapper.insert(cameraPO);
        }
        LogUtil.share();
        return cameraPO;
    }

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