package com.fs.provider.controller;

import com.fs.common.annotation.Log;
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.ServiceOrder;
import com.fs.provider.service.IServiceOrderService;
import com.fs.provider.vo.GenerateServiceOrderReqVO;
import com.fs.provider.vo.ServiceOrderAuditReqVO;
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-26
 */
@RestController
@RequestMapping("/service/order")
public class ServiceOrderController extends BaseController
{
    @Autowired
    private IServiceOrderService serviceOrderService;

    /**
     * 查询服务单信息列表
     */
    @PreAuthorize("@ss.hasPermi('service:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServiceOrder serviceOrder)
    {
        startPage();
        List<ServiceOrder> list = serviceOrderService.selectServiceOrderList(serviceOrder);
        return getDataTable(list);
    }

    /**
     * 导出服务单信息列表
     */
    @PreAuthorize("@ss.hasPermi('service:order:export')")
    @Log(title = "服务单信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ServiceOrder serviceOrder)
    {
        List<ServiceOrder> list = serviceOrderService.selectServiceOrderList(serviceOrder);
        ExcelUtil<ServiceOrder> util = new ExcelUtil<ServiceOrder>(ServiceOrder.class);
        return util.exportExcel(list, "服务单信息数据");
    }

    /**
     * 获取服务单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(serviceOrderService.selectServiceOrderById(id));
    }

    /**
     * 新增服务单信息
     */
    @PreAuthorize("@ss.hasPermi('service:order:add')")
    @Log(title = "服务单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceOrder serviceOrder)
    {
        return toAjax(serviceOrderService.insertServiceOrder(serviceOrder));
    }

    /**
     * 修改服务单信息
     */
    @PreAuthorize("@ss.hasPermi('service:order:edit')")
    @Log(title = "服务单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceOrder serviceOrder)
    {
        return toAjax(serviceOrderService.updateServiceOrder(serviceOrder));
    }

    /**
     * 删除服务单信息
     */
    @PreAuthorize("@ss.hasPermi('service:order:remove')")
    @Log(title = "服务单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(serviceOrderService.deleteServiceOrderByIds(ids));
    }


    /**
     * 根据任务ID生成服务单
     */
    @PostMapping("/generateByTask")
    @PreAuthorize("@ss.hasPermi('service:order:add')")
    public AjaxResult generateServiceOrderByTask(@RequestBody GenerateServiceOrderReqVO reqVO) {
        if (reqVO == null || reqVO.getTaskId() == null) {
            return AjaxResult.error("任务ID不能为空");
        }
        return AjaxResult.success(serviceOrderService.generateByTaskId(reqVO.getTaskId()));
    }



    /**
     * 获取服务单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:order:query')")
    @GetMapping(value = "getAuditInfo")
    public AjaxResult getAuditInfo(ServiceOrderAuditReqVO serviceOrderAuditReqVO){
        return AjaxResult.success(serviceOrderService.getAuditInfo(serviceOrderAuditReqVO));
    }


}
