package com.xzzn.web.controller.ems;

import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.EmsStrategyRunning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xzzn.common.core.controller.BaseController;
import com.xzzn.common.core.domain.AjaxResult;
import com.xzzn.ems.service.IEmsStrategyService;

/**
 * 策略Controller
 *
 * @author xzzn
 * @date 2025-07-10
 */
@RestController
@RequestMapping("/system/strategyRunning")
public class EmsStrategyController extends BaseController
{
    @Autowired
    private IEmsStrategyService emsStrategyService;

    /**
     * 获取站点策略配置
     */
    @GetMapping("/list")
    public AjaxResult list(String siteId) {
        return success(emsStrategyService.selectEmsStrategyRunningList(siteId));
    }
    /**
     * 停止策略
     */
    @GetMapping(value = "/stop")
    public AjaxResult stop(Long id)
    {
        return toAjax(emsStrategyService.stopRunningStrategy(id));
    }

    /**
     * 获取主策略列表
     */
    @GetMapping(value = "/getMainStrategyList")
    public AjaxResult getMainStrategyList()
    {
        return success(emsStrategyService.getMainStrategyList());
    }

    /**
     * 获取辅助策略列表
     */
    @GetMapping(value = "/getAuxStrategyList")
    public AjaxResult getAuxStrategyList()
    {
        return success(emsStrategyService.getAuxStrategyList());
    }

    /**
     * 配置策略
     */
    @PostMapping(value = "/configStrategy")
    public AjaxResult configStrategy(@RequestBody EmsStrategyRunning emsStrategyRunning)
    {
        if (emsStrategyRunning.getMainStrategyId() == null
                || StringUtils.isEmpty(emsStrategyRunning.getSiteId())){
            return error("缺少必填字段");
        }
        emsStrategyRunning.setCreateBy(getUsername());
        emsStrategyRunning.setUpdateBy(getUsername());
        int result = emsStrategyService.configStrategy(emsStrategyRunning);
        if (result == -1){
            return error("不支持重复添加");
        }
        return success(result);
    }
}
