package com.fs.provider.controller;

import com.fs.common.annotation.Log;
import com.fs.common.config.LoginContextManager;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.provider.domain.ServiceGenConfig;
import com.fs.provider.service.IServiceGenConfigService;
import com.fs.provider.vo.ServiceGenConfigAuditReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务生成配置Controller
 *
 * @author fs
 * @date 2026-01-27
 */
@RestController
@RequestMapping("/service/config")
public class ServiceGenConfigController extends BaseController {
    @Autowired
    private IServiceGenConfigService serviceGenConfigService;

    /**
     * 查询任务生成配置列表
     */
    @PreAuthorize("@ss.hasPermi('service:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServiceGenConfig serviceGenConfig) {
        startPage();
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        serviceGenConfig.setCompanyId(currentCompanyId);
        List<ServiceGenConfig> list = serviceGenConfigService.selectServiceGenConfigList(serviceGenConfig);
        return getDataTable(list);
    }


    /**
     * 导出任务生成配置列表
     */
    @PreAuthorize("@ss.hasPermi('service:config:export')")
    @Log(title = "任务生成配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ServiceGenConfig serviceGenConfig) {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        serviceGenConfig.setCompanyId(currentCompanyId);
        List<ServiceGenConfig> list = serviceGenConfigService.selectServiceGenConfigList(serviceGenConfig);
        ExcelUtil<ServiceGenConfig> util = new ExcelUtil<ServiceGenConfig>(ServiceGenConfig.class);
        return util.exportExcel(list, "任务生成配置数据");
    }

    /**
     * 获取任务生成配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(serviceGenConfigService.selectServiceGenConfigById(id));
    }


    /**
     * 获取任务生成配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:config:query')")
    @GetMapping("/audit/{id}")
    public AjaxResult getAuditInfoById(@PathVariable Long id) {
        return AjaxResult.success(serviceGenConfigService.getAuditInfoById(id));
    }

    /**
     * 新增任务生成配置
     */
    @PreAuthorize("@ss.hasPermi('service:config:add')")
    @Log(title = "任务生成配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceGenConfig serviceGenConfig) {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        serviceGenConfig.setCompanyId(currentCompanyId);
        return toAjax(serviceGenConfigService.companyInsertServiceGenConfig(serviceGenConfig));
    }

    /**
     * 修改任务生成配置
     */
    @PreAuthorize("@ss.hasPermi('service:config:edit')")
    @Log(title = "任务生成配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceGenConfig serviceGenConfig) {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        serviceGenConfig.setCompanyId(currentCompanyId);
        return toAjax(serviceGenConfigService.updateServiceGenConfig(serviceGenConfig));
    }

    /**
     * 删除任务生成配置
     */
    @PreAuthorize("@ss.hasPermi('service:config:remove')")
    @Log(title = "任务生成配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(serviceGenConfigService.deleteServiceGenConfigByIds(ids));
    }


    /**
     * 审核任务生成配置
     * <p>
     * 前端请求示例：
     * POST /service/config/audit
     * {
     * "id": 1,
     * "auditStatus": 1,        // 1-通过，2-不通过
     * "auditRemark": "xxx",    // 审核描述
     * "auditInstanceId": 123   // 可选
     * }
     */
    @PreAuthorize("@ss.hasPermi('service:config:edit')")
    @Log(title = "任务生成配置审核", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody ServiceGenConfigAuditReqVO serviceGenConfigAuditReqVO) {
        // 这里只做壳子：保证能进来即可
        // 你可以在 Service 里校验 needAudit / auditStatus 状态机 / 幂等 等逻辑
        return toAjax(serviceGenConfigService.companyAudit(serviceGenConfigAuditReqVO));
    }


}
