package com.muyu.server.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.domain.CarType;
import com.muyu.domain.WarnStrategy;
import com.muyu.domain.req.WarnStrategyReq;
import com.muyu.server.service.WarnStrategyService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 预警策略控制层
 * @Author：蓬叁
 * @Package：com.muyu.warn.controller
 * @Project：cloud-server-8
 * @name：WarnLogsController
 * @Date：2024/9/20 下午7:29
 */
@RestController
@RequestMapping("/strategy")
@Tag(name = "预警策略")
public class WarnStrategyController extends BaseController
{
    @Resource
    private WarnStrategyService warnStrategyService;

    /**
     * 查询预警策略列表
     */
    @RequiresPermissions("platform:strategy:list")
    @GetMapping("/list")
    public Result<TableDataInfo<WarnStrategy>> list(WarnStrategy warnStrategy)
    {
        startPage();
        List<WarnStrategy> list = warnStrategyService.selectWarnStrategyList(warnStrategy);
        return getDataTable(list);
    }

    /**
     * 导出预警策略列表
     */
    @RequiresPermissions("platform:strategy:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarnStrategy warnStrategy)
    {
        List<WarnStrategy> list = warnStrategyService.selectWarnStrategyList(warnStrategy);
        ExcelUtil<WarnStrategy> util = new ExcelUtil<WarnStrategy>(WarnStrategy.class);
        util.exportExcel(response, list, "预警策略数据");
    }

    /**
     * 获取预警策略详细信息
     */
    @RequiresPermissions("platform:strategy:query")
    @GetMapping(value = "/{id}")
    public Result<List<WarnStrategy>> getInfo(@PathVariable("id") Long id)
    {
        return success(warnStrategyService.selectWarnStrategyById(id));
    }

    /**
     * 新增预警策略
     */
    @RequiresPermissions("platform:strategy:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody WarnStrategy warnStrategy)
    {
        if (warnStrategyService.checkIdUnique(warnStrategy)) {
            return error("新增 预警策略 '" + warnStrategy + "'失败，预警策略已存在");
        }
        return toAjax(warnStrategyService.save(warnStrategy));
    }

    /**
     * 修改预警策略
     */
    @RequiresPermissions("platform:strategy:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody WarnStrategy warnStrategy)
    {
        if (!warnStrategyService.checkIdUnique(warnStrategy)) {
            return error("修改 预警策略 '" + warnStrategy + "'失败，预警策略不存在");
        }
//        warnStrategy.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(warnStrategyService.updateById(warnStrategy));
    }

    /**
     * 删除预警策略
     */
    @RequiresPermissions("platform:strategy:remove")
	@DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        warnStrategyService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }

    /**
     * 查找车辆类型
     * @return
     */
    @RequiresPermissions("platform:strategy:cartype")
    @PostMapping("/findByCarType")
    public Result<List<CarType>> findByCarType(){
        return Result.success(warnStrategyService.findByCarType());
    }

    /**
     * 根据Id查找车辆信息
     * @param ids
     * @return
     */
    @RequiresPermissions("platform:strategy:strategytype")
    @PostMapping("/strategyType/{ids}")
    public Result<List<WarnStrategyReq>> selectStrategyType(@PathVariable("ids") Long ids){
        return Result.success(warnStrategyService.selectStrategyType(ids));
    }

}
