package com.muyu.enterprise.controller;

import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.domain.MessageValue;
import com.muyu.domain.VehicleType;
import com.muyu.domain.req.MessageValueAddReq;
import com.muyu.domain.req.VehicleAddReq;
import com.muyu.enterprise.cache.AllVehicleTypeCacheService;
import com.muyu.enterprise.cache.VehicleCacheService;
import com.muyu.enterprise.cache.VehicleTypeCacheService;
import com.muyu.enterprise.service.VehicleTypeService;
import com.muyu.common.core.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: LiDongJia
 * @Package: com.muyu.car.controller
 * @Project: 2112-car-cloud-server
 * @name: VehicleTypeController
 * @Date: 2024/9/17  17:08
 * @Description: 车辆类型实体类
 */
@Log4j2
@RestController
@RequestMapping("/vehicleType")
@Tag(name = "车辆类型控制层", description = "车辆类型控制层")
public class VehicleTypeController {

    @Autowired
    private VehicleTypeService vehicleTypeService;

    @Autowired
    private VehicleTypeCacheService vehicleTypeCacheService;

    //存缓存
    @Autowired
    private AllVehicleTypeCacheService allVehicleTypeCacheService;


    /**
     * 查询所有车辆类型
     * @return
     */
    @RequestMapping(path = "/findAll", method = RequestMethod.POST)
    @Operation(summary = "车辆类型列表",description = "车辆类型列表")
    public Result<List<VehicleType>> findAll(){
        List<VehicleType> list = vehicleTypeService.list();
//        for (VehicleType vehicleType : list) {
//            allVehicleTypeCacheService.put(String.valueOf(vehicleType.getVehicleTypeId()),vehicleType);
//        }
        list.forEach(vehicleType -> {
            vehicleTypeCacheService.put(String.valueOf(vehicleType.getVehicleTypeId()), vehicleType);
        });
        return Result.success(list);
    }

    /**
     * 添加车辆类型
     * @param vehicleType
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "添加报文数据", description = "新增报文数据")
    public Result<String> save(@RequestBody VehicleType vehicleType) {
        boolean save = vehicleTypeService.save(vehicleType);
//        //获取用户信息
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        //获取租户唯一标识
//        String databaseName = loginUser.getSysUser().getDatabaseName();
        //存到redis
//        vehicleTypeCacheService.put(databaseName+vehicleType.getVehicleTypeId(),vehicleType);
        vehicleTypeCacheService.put(String.valueOf(vehicleType.getVehicleTypeId()),vehicleType);
        return  Result.success(save? "添加成功" : "添加失败");
    }
}
