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

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.model.VehiclePageParam;
import api.common.pojo.po.model.VehicleTempPO;
import api.common.pojo.po.system.DictPO;
import api.common.pojo.vo.model.VehicleDefaultValueVO;
import api.common.pojo.vo.model.VehicleTempVO;
import api.common.util.ObjectUtil;
import api.common.util.StringUtil;
import com.css.simulation.resource.server.infra.util.PageUtil;
import com.css.simulation.resource.server.infra.util.PoUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.VehicleMapper;
import com.css.simulation.resource.server.infra.db.mysql.mapper.VehicleTempMapper;
import com.css.simulation.resource.server.infra.db.mysql.mapper.DictMapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class VehicleTempService {


    @Resource
    VehicleTempMapper vehicleTempMapper;
    @Resource
    VehicleMapper vehicleMapper;
    @Resource
    LogService logService;
    @Resource
    DictMapper dictMapper;


    public List getVehicleTempTree() {
        List<VehicleTempVO> list = vehicleTempMapper.getVehicleTempTree();
        List<Map> treeList = new ArrayList<>();
        Map<String, List<VehicleTempVO>> typeCollect = list.stream().collect(Collectors.groupingBy(VehicleTempVO::getVehicleType, LinkedHashMap::new, Collectors.toList()));
        typeCollect.forEach((typeKey, typeValue) -> {
            Map typeMap = new HashMap();
            typeMap.put("code", typeKey);
            typeMap.put("name", typeKey);
            //typeMap.put("name",typeValue.get(0).getVehicleName());
            List<Map> modelList = new LinkedList();
            Map<String, List<VehicleTempVO>> modelCollect = typeValue.stream().collect(Collectors.groupingBy(VehicleTempVO::getVehicleModel, LinkedHashMap::new, Collectors.toList()));
            modelCollect.forEach((modelKey, modelValue) -> {
                Map modelMap = new HashMap();
                modelMap.put("code", modelKey);
                modelMap.put("name", modelKey);
                List<Map> colourList = new LinkedList();
                modelValue.forEach(vo -> {
                    Map colourMap = new HashMap();
                    colourMap.put("code", vo.getVehicleColour());
                    colourMap.put("name", vo.getVehicleColour());
                    VehicleDefaultValueVO defaultValueVO = new VehicleDefaultValueVO();
                    ObjectUtil.voToPo(vo, defaultValueVO);
                    colourMap.put("vo", defaultValueVO);
                    colourList.add(colourMap);
                });
                modelMap.put("children", colourList);
                modelList.add(modelMap);
            });
            typeMap.put("children", modelList);
            treeList.add(typeMap);
        });
        return treeList;
    }

    public PageInfo<VehicleTempVO> getVehicleTempPageList(VehiclePageParam pageParam) {
        PageUtil.setPageInfo(pageParam);
        List<VehicleTempVO> list = vehicleTempMapper.getVehicleTempPageList(pageParam);
        return new PageInfo<>(list);
    }

    public VehicleTempVO getVehicleTempInfo(VehiclePageParam param) {
        return vehicleTempMapper.getVehicleTempInfo(param);
    }

    public ResponseBodyVO<VehicleTempPO> saveVehicleTemplate(VehicleTempPO newVehicle) {
        //名称校验
        // 查询 id 不同但标识相同的其他车辆模板
        //1 查询所有相同标识的车辆模板，包括本身
        List<VehicleTempVO> vehicleTempVOS = vehicleTempMapper.selectByModelLabel(newVehicle);
        VehicleTempVO oldVehicle = null;
        for (VehicleTempVO vehicleTempVO : vehicleTempVOS) {
            if (vehicleTempVO.getId().equals(newVehicle.getId())) {
                oldVehicle = vehicleTempVO;
            } else {
                return new ResponseBodyVO<>(false, 500, "车辆标识重复。", null);
            }
        }
        String id = newVehicle.getId();
        //常规字段赋值
        PoUtil.initAddPo(newVehicle);

        DictPO dictPo=new DictPO();
        dictPo.setDictCode(newVehicle.getModelLabel());
        dictPo.setDictName(newVehicle.getVehicleTopView());//osgb路径
        PoUtil.initAddPo(dictPo);
        if (ObjectUtil.isNull(id)) {//新增
            newVehicle.setId(StringUtil.getRandomUUID());
            newVehicle.setVehicleCode(StringUtil.getRandomCode());
            newVehicle.setValid("1");
            vehicleTempMapper.insert(newVehicle);
            logService.logVehicle(LogConstants.SYS_LOG_VEHICLE_INSERT, newVehicle);
        } else { //修改
            // 如果旧的车辆模板的标识和新的车辆模板标识不相等，则需要同步所有的车辆
            if (!oldVehicle.getModelLabel().equals(newVehicle.getModelLabel())){
                vehicleMapper.updateModelLabel(oldVehicle.getModelLabel(),newVehicle.getModelLabel());
            }
            vehicleTempMapper.update(newVehicle);
            logService.logVehicle(LogConstants.SYS_LOG_VEHICLE_UPDATE, newVehicle);
        }

        //先判断字典表中是否有
       String carId= dictMapper.getDictByDictCode(dictPo);
        if(ObjectUtil.isNull(carId)){
            //字典新增osgb文件
            dictPo.setId(StringUtil.getRandomUUID());
            dictPo.setDictType("carType");
            dictPo.setDictOrder(1);
            dictMapper.saveSystemDict(dictPo);
        }else{
            dictMapper.updateSystemDict(dictPo);
        }

        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, newVehicle);
    }

    public int delVehicleTempById(VehicleTempPO po) {
        PoUtil.initDelPo(po);
        logService.logVehicle(LogConstants.SYS_LOG_VEHICLE_DEL, po);
        //删除字典车辆模型
        VehiclePageParam param=new VehiclePageParam();
        param.setId(po.getId());
        VehicleTempVO vehicleTempVO= vehicleTempMapper.getVehicleTempInfo(param);
        DictPO dictPo=new DictPO();
        dictPo.setDictCode(vehicleTempVO.getModelLabel());
        PoUtil.initDelPo(dictPo);
        dictMapper.deleteSystemDict(dictPo);
        return vehicleTempMapper.delVehicleTempById(po);
    }
}
