package com.muyu.enterpise.controller;



import com.muyu.common.core.domain.Result;
import com.muyu.domain.FaultCondition;
import com.muyu.domain.req.FaultConditionAddReq;
import com.muyu.domain.req.FaultConditionListReq;
import com.muyu.domain.req.FaultConditionUpdReq;
import com.muyu.enterpise.service.FaultConditionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 故障检测策略
 *  * @className: FaultConditionController  ️✈️
 * * @author: Yang 鹏  🦅
 * * @date: 2024/9/23 22:06 ⏰
 * * @Version: 1.0
 * * @description: 故障检测策略
 */
@RestController
@RequestMapping("/faultcondition")
@Tag(name = "故障规则模块",description = "故障规则模块")
public class FaultConditionController {

    @Autowired
    private FaultConditionService faultConditionService;


    /**
     * 故障规则
     * @param faultConditionListReq
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "故障规则列表展示",description = "故障规则列表展示")
    public Result getfaultrulelist(@RequestBody @Validated FaultConditionListReq faultConditionListReq){
        return Result.success(faultConditionService.getfaultrulelist(faultConditionListReq));
    }


    /**
     * 故障规则添加
     * @param faultConditionAddReq
     * @return
     */
    @PostMapping("/faultconditionadd")
    @Operation(summary = "添加规则",description = "添加故障规则")
    public Result faultconditionadd(@RequestBody FaultConditionAddReq faultConditionAddReq){
        //判断车辆类型是否已存在所对应的故障规则
        List<FaultCondition> faultConditionList = faultConditionService.selectBytypeAndlabel(faultConditionAddReq);
        if (faultConditionList.size()>0){
            return Result.error("此车辆类型已存在所对应的故障规则,无需重新制定，可在原规则上进行修改");
        }
        faultConditionService.save(FaultCondition.faultConditionadd(faultConditionAddReq));
        return Result.success(null,"规则制定成功");
    }

    /**
     * 故障规则修改
     * @param
     * @param faultConditionUpdReq
     * @return
     */
    @PostMapping("/faultconditionupd")
    @Operation(summary = "修改规则",description = "修改故障规则")
    public Result faultconditionupd(
            @RequestBody @Validated FaultConditionUpdReq faultConditionUpdReq){
        faultConditionService.updateById(FaultCondition.faultConditionupd(faultConditionUpdReq,()->faultConditionUpdReq.getCarconditionId()));
        return Result.success(null,"规则修改成功");
    }


    /**
     * 故障规则删除
     * @param carconditionId
     * @return
     */
    @PostMapping("/faultconditiondel/{carconditionId}")
    @Operation(summary = "删除规则",description = "删除故障规则")
    public Result faultconditiondel(@PathVariable("carconditionId") Long carconditionId){
        faultConditionService.removeById(carconditionId);
        return Result.success(null,"规则删除成功");
    }



}
