package com.muyu.warning.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.muyu.warning.domain.WarnRule;
import com.muyu.warning.domain.WarnRuleDTO;
import com.muyu.warning.domain.WarnStrategy;
import jakarta.servlet.http.HttpServletResponse;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.warning.domain.WarnRule;
import com.muyu.warning.service.IWarnRuleService;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 预警规则Controller
 *
 * @author muyu
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/rule")
public class WarnRuleController extends BaseController {
    @Resource
    private IWarnRuleService warnRuleService;

    /**
     * 查询预警规则列表
     */
    @RequiresPermissions("warning:list")
    @GetMapping("/list")
    public Result<TableDataInfo<WarnRuleDTO>> list(WarnRuleDTO warnRuleDTO) {
        startPage();
        List<WarnRuleDTO> list = warnRuleService.selectWarnRuleList(warnRuleDTO);
//        MPJLambdaQueryWrapper<WarnRule> objectMPJLambdaQueryWrapper = new MPJLambdaQueryWrapper<>()
//                .selectAll(WarnRule.class)
//                .select(WarnStrategy::getStrategyName)
//                .leftJoin(WarnStrategy.class, WarnStrategy::getId, WarnRule::getStrategyId)
//                .stringQuery();
        return getDataTable(list);
    }

    /**
     * 导出预警规则列表
     */
    @RequiresPermissions("warning:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarnRuleDTO warnRuleDTO) {
        List<WarnRuleDTO> list = warnRuleService.selectWarnRuleList(warnRuleDTO);
        ExcelUtil<WarnRuleDTO> util = new ExcelUtil<WarnRuleDTO>(WarnRuleDTO.class);
        util.exportExcel(response, list, "预警规则数据");
    }

    /**
     * 获取预警规则详细信息
     */
    @RequiresPermissions("warning:query")
    @GetMapping(value = "/{id}")
    public Result<List<WarnRule>> getInfo(@PathVariable("id") Long id) {
        return success(warnRuleService.selectWarnRuleById(id));
    }

    /**
     * 新增预警规则
     */
    @RequiresPermissions("warning:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody WarnRuleDTO warnRuleDTO) {
        if (warnRuleService.checkIdUnique(warnRuleDTO)) {
            return error("新增 预警规则 '" + warnRuleDTO + "'失败，预警规则已存在");
        }
        return toAjax(warnRuleService.save(warnRuleDTO));
    }

    /**
     * 修改预警规则
     */
    @RequiresPermissions("warning:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody WarnRuleDTO warnRuleDTO) {
        if (!warnRuleService.checkIdUnique(warnRuleDTO)) {
            return error("修改 预警规则 '" + warnRuleDTO + "'失败，预警规则不存在");
        }
        return toAjax(warnRuleService.updateById(warnRuleDTO));
    }

    /**
     * 删除预警规则
     */
    @RequiresPermissions("warning:remove")
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids) {
        warnRuleService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }

}
