package com.muyu.enterprise.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.domain.WarnStrategy;
import com.muyu.domain.resp.WarnStrategyAndVinResp;
import com.muyu.enterprise.cache.AllWarnStrategyAndVinCacheService;
import com.muyu.enterprise.cache.AllWarnStrategyCacheService;
import com.muyu.enterprise.cache.WarnStrategyCacheService;
import com.muyu.enterprise.service.WarnStrategyService;
import com.muyu.domain.req.WarnVehicleReq;
import com.muyu.domain.resp.WarnVehicleResp;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/strategy")
public class WarnStrategyController {

    @Autowired
    private WarnStrategyService warnStrategyService;

    //缓存策略
    @Autowired
    private WarnStrategyCacheService warnStrategyCacheService;

    //列表
    @Autowired
    private AllWarnStrategyCacheService allWarnStrategyCacheService;

    //缓存策略和vin
    @Autowired
    private AllWarnStrategyAndVinCacheService allWarnStrategyAndVinCacheService;


    /**
     * 策略列表
     * @return
     */
    @RequestMapping(path = "/strategyList",method = RequestMethod.POST)
    @Operation(summary = "策略列表", description = "获取所有策略列表")
    public Result<List<WarnVehicleResp>> strategyList(@RequestBody @Validated WarnVehicleReq req) {
        List<WarnVehicleResp> list = warnStrategyService.selectList(req);
//        for (WarnVehicleResp warnVehicleResp : list) {
//            //存进Redis
//            allWarnStrategyCacheService.put(String.valueOf(warnVehicleResp.getVehicleVin()), warnVehicleResp);
//        }
        return Result.success(list);
    }

    /**
     * 根据ID查询策略数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询策略数据", description = "根据ID查询策略数据")
    public Result<WarnStrategy> getById(@PathVariable("id") Long id){
        return Result.success(warnStrategyService.getById(id));
    }

    /**
     * 规则策略双表联查
     * @param warnVehicleReq
     * @return
     */
//    @RequestMapping(path = "ruleStrategyList",method = RequestMethod.POST)
//    @Operation(summary = "策略规则双表联查", description = "获取所有策略规则双表联查")
//    public Result<List<WarnVehicleResp>> ruleStrategyList(@RequestBody WarnVehicleReq warnVehicleReq){
//        return Result.success(warnStrategyService.ruleStrategyList(warnVehicleReq));
//    }

    /**
     * 添加策略
     * @param warnStrategy
     * @return
     */
    @RequestMapping(path = "strategyAdd",method = RequestMethod.POST)
    @Operation(summary = "添加策略", description = "添加策略")
    public Result strategyAdd(@Validated @RequestBody WarnStrategy warnStrategy){

        boolean save = warnStrategyService.save(warnStrategy);
        //存进Redis
        //获取用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //获取租户唯一标识
        String databaseName = loginUser.getSysUser().getDatabaseName();
        warnStrategyCacheService.put(databaseName+warnStrategy.getWarnStrategyId(),warnStrategy);
        return Result.success();
    }

    /**
     * 修改策略
     * @param warnStrategy
     * @param id
     * @return
     */
    @RequestMapping(path = "/{id}",method = RequestMethod.POST)
    @Operation(summary = "修改策略", description = "修改策略")
    public Result strategyUpdate(@Validated @RequestBody WarnStrategy warnStrategy, @PathVariable("id")Long id){
        return Result.success(warnStrategyService.updateById(WarnStrategy.updateBuilder(warnStrategy,() -> id)));
    }

    /**
     * 删除策略
     * @param id
     * @return
     */
    @RequestMapping(path = "/{id}",method = RequestMethod.DELETE)
    @Operation(summary = "删除策略", description = "删除策略")
    public Result strategyDelete(@PathVariable("id") Long id){
        return Result.success(warnStrategyService.removeById(id));
    }

    /**
     * 通过vin缓存策略信息
     */
    @RequestMapping(path = "/vinStrategyList/{vehicleId}",method = RequestMethod.POST)
    @Operation(summary = "通过vin缓存策略信息", description = "通过vin缓存策略信息")
    public Result<List<WarnStrategyAndVinResp>> vinStrategyList(@PathVariable("vehicleId") Long vehicleId) {
        List<WarnStrategyAndVinResp> warnStrategyAndVinResp = warnStrategyService.findById(vehicleId);
        for (WarnStrategyAndVinResp strategyAndVinResp : warnStrategyAndVinResp) {
            allWarnStrategyAndVinCacheService.put(strategyAndVinResp.getVehicleVin(), warnStrategyAndVinResp);
        }
        return Result.success(warnStrategyAndVinResp);
    }

}
