package com.xzzn.web.controller.ems;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.xzzn.common.utils.poi.ExcelUtil;
import com.xzzn.ems.domain.EmsTicket;
import com.xzzn.ems.domain.vo.TicketListVo;
import com.xzzn.ems.service.IEmsTicketService;
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.common.core.page.TableDataInfo;

/**
 * 工单主Controller
 * 
 * @author xzzn
 * @date 2025-06-26
 */
@RestController
@RequestMapping("/ticket")
public class EmsTicketController extends BaseController
{
    @Autowired
    private IEmsTicketService emsTicketService;

    /**
     * 查询工单主列表
     */
    @PreAuthorize("@ss.hasPermi('system:ticket:list')")
    @GetMapping("/list")
    public TableDataInfo list(Long[] status)
    {
        startPage();
        List<TicketListVo> list = emsTicketService.getAllTicketList(status);
        return getDataTable(list);
    }

    /**
     * 导出工单主列表
     */
    @PreAuthorize("@ss.hasPermi('system:ticket:export')")
    @Log(title = "工单主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsTicket emsTicket)
    {
        List<EmsTicket> list = emsTicketService.selectEmsTicketList(emsTicket);
        ExcelUtil<EmsTicket> util = new ExcelUtil<EmsTicket>(EmsTicket.class);
        util.exportExcel(response, list, "工单主数据");
    }

    /**
     * 获取工单主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ticket:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(emsTicketService.selectEmsTicketById(id));
    }

    /**
     * 新增工单主
     */
    @PreAuthorize("@ss.hasPermi('system:ticket:add')")
    @Log(title = "工单主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsTicket emsTicket)
    {
        return toAjax(emsTicketService.insertEmsTicket(emsTicket));
    }

    /**
     * 修改工单主
     */
    @PreAuthorize("@ss.hasPermi('system:ticket:edit')")
    @Log(title = "工单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsTicket emsTicket)
    {
        return toAjax(emsTicketService.updateEmsTicket(emsTicket));
    }

    /**
     * 删除工单主
     */
    @PreAuthorize("@ss.hasPermi('system:ticket:remove')")
    @Log(title = "工单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(emsTicketService.deleteEmsTicketByIds(ids));
    }

    /**
     * 废弃工单
     */
    @PreAuthorize("@ss.hasPermi('system:ticket:delete')")
    @Log(title = "工单主", businessType = BusinessType.DELETE)
    @PostMapping("/drop")
    public AjaxResult drop(@RequestBody EmsTicket emsTicket)
    {
        return toAjax(emsTicketService.dropEmsTicketById(emsTicket.getId()));
    }
}
