package com.xzzn.web.controller.ems;

import com.xzzn.ems.domain.vo.StrategyTempConfigRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.xzzn.common.annotation.Log;
import com.xzzn.common.core.controller.BaseController;
import com.xzzn.common.core.domain.AjaxResult;
import com.xzzn.common.enums.BusinessType;
import com.xzzn.ems.service.IEmsStrategyTempService;

/**
 * 模板Controller
 *
 * @author xzzn
 * @date 2025-07-11
 */
@RestController
@RequestMapping("/strategy/temp")
public class EmsStrategyTempController extends BaseController
{
    @Autowired
    private IEmsStrategyTempService emsStrategyTempService;

    /**
     * 根据策略id站点id获取所有模板名称
     */
    @GetMapping("/getTempNameList")
    public AjaxResult getTempNameList(Long strategyId, String siteId)
    {
        return success(emsStrategyTempService.getTempNameList(strategyId, siteId));
    }

    /**
     * 获取单个模板时间配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:temp:list')")
    @GetMapping("/list")
    public AjaxResult list(String templateId)
    {
        return success(emsStrategyTempService.selectEmsStrategyTempList(templateId));
    }

    /**
     * 新增模板及时间配置
     */
    @PreAuthorize("@ss.hasPermi('system:temp:add')")
    @Log(title = "模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StrategyTempConfigRequest requestVo)
    {
        boolean result = emsStrategyTempService.addNewTempAndTimeConfig(requestVo);
        if (result) {
            return success();
        } else  {
            return AjaxResult.error("新增失败请重试！");
        }
    }

    /**
     * 修改模板
     */
    @PreAuthorize("@ss.hasPermi('system:temp:edit')")
    @Log(title = "模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StrategyTempConfigRequest requestVo)
    {
        boolean result = emsStrategyTempService.updateEmsStrategyTemp(requestVo);
        if (result) {
            return success();
        } else  {
            return AjaxResult.error("更新失败，请重试！");
        }
    }

    /**
     * 删除模板
     */
    @PreAuthorize("@ss.hasPermi('system:temp:remove')")
    @Log(title = "模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateId}")
    public AjaxResult remove(@PathVariable String templateId)
    {
        return success(emsStrategyTempService.deleteStrategyTempById(templateId));
    }
}
