package com.ygtx.gxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ygtx.common.annotation.Log;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.enums.BusinessType;
import com.ygtx.gxt.domain.GxtWorkOrder;
import com.ygtx.gxt.domain.GxtWorkOrderFlow;
import com.ygtx.gxt.service.IGxtWorkOrderService;
import com.ygtx.common.utils.poi.ExcelUtil;
import com.ygtx.common.core.page.TableDataInfo;

/**
 * 维保工单Controller
 *
 * @author ygtx
 * @date 2025-10-25
 */
@RestController
@RequestMapping("/gxt/order")
public class GxtWorkOrderController extends BaseController
{
    @Autowired
    private IGxtWorkOrderService gxtWorkOrderService;

    /**
     * 查询维保工单列表
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxtWorkOrder gxtWorkOrder)
    {
        startPage();
        List<GxtWorkOrder> list = gxtWorkOrderService.selectGxtWorkOrderList(gxtWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出维保工单列表
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:export')")
    @Log(title = "维保工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxtWorkOrder gxtWorkOrder)
    {
        List<GxtWorkOrder> list = gxtWorkOrderService.selectGxtWorkOrderList(gxtWorkOrder);
        ExcelUtil<GxtWorkOrder> util = new ExcelUtil<GxtWorkOrder>(GxtWorkOrder.class);
        util.exportExcel(response, list, "维保工单数据");
    }

    /**
     * 获取维保工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gxtWorkOrderService.selectGxtWorkOrderById(id));
    }

    /**
     * 新增维保工单
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:add')")
    @Log(title = "维保工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxtWorkOrder gxtWorkOrder)
    {
        return toAjax(gxtWorkOrderService.insertGxtWorkOrder(gxtWorkOrder));
    }

    /**
     * 修改维保工单
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:edit')")
    @Log(title = "维保工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxtWorkOrder gxtWorkOrder)
    {
        return toAjax(gxtWorkOrderService.updateGxtWorkOrder(gxtWorkOrder));
    }

    /**
     * 删除维保工单
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:remove')")
    @Log(title = "维保工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gxtWorkOrderService.deleteGxtWorkOrderByIds(ids));
    }

    /**
     * 派单给班组组长
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:assign')")
    @Log(title = "派单给班组组长", businessType = BusinessType.UPDATE)
    @PutMapping("/assignToTeamLeader/{id}")
    public AjaxResult assignToTeamLeader(
            @PathVariable("id") Long id,
            @RequestParam("teamLeaderId") Long teamLeaderId,
            @RequestParam("teamLeaderName") String teamLeaderName)
    {
        return toAjax(gxtWorkOrderService.assignToTeamLeader(id, teamLeaderId, teamLeaderName));
    }

    /**
     * 班组组长接单
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:accept')")
    @Log(title = "班组组长接单", businessType = BusinessType.UPDATE)
    @PutMapping("/acceptByTeamLeader/{id}")
    public AjaxResult acceptByTeamLeader(@PathVariable("id") Long id)
    {
        return toAjax(gxtWorkOrderService.acceptByTeamLeader(id));
    }

    /**
     * 班组组长派单给工作组成员
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:assign')")
    @Log(title = "派单给工作组成员", businessType = BusinessType.UPDATE)
    @PutMapping("/assignToWorkGroupMember/{id}")
    public AjaxResult assignToWorkGroupMember(
            @PathVariable("id") Long id,
            @RequestParam("workGroupMemberId") Long workGroupMemberId,
            @RequestParam("workGroupMemberName") String workGroupMemberName)
    {
        return toAjax(gxtWorkOrderService.assignToWorkGroupMember(id, workGroupMemberId, workGroupMemberName));
    }

    /**
     * 工作组成员接单
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:accept')")
    @Log(title = "工作组成员接单", businessType = BusinessType.UPDATE)
    @PutMapping("/acceptByWorkGroupMember/{id}")
    public AjaxResult acceptByWorkGroupMember(@PathVariable("id") Long id)
    {
        return toAjax(gxtWorkOrderService.acceptByWorkGroupMember(id));
    }

    /**
     * 挂起工单
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:suspend')")
    @Log(title = "挂起工单", businessType = BusinessType.UPDATE)
    @PutMapping("/suspend/{id}")
    public AjaxResult suspend(
            @PathVariable("id") Long id,
            @RequestParam("suspendReason") String suspendReason)
    {
        return toAjax(gxtWorkOrderService.suspendWorkOrder(id, suspendReason));
    }

    /**
     * 重启工单
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:restart')")
    @Log(title = "重启工单", businessType = BusinessType.UPDATE)
    @PutMapping("/restart/{id}")
    public AjaxResult restart(@PathVariable("id") Long id)
    {
        return toAjax(gxtWorkOrderService.restartWorkOrder(id));
    }

    /**
     * 完成工单
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:complete')")
    @Log(title = "完成工单", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{id}")
    public AjaxResult complete(@PathVariable("id") Long id)
    {
        return toAjax(gxtWorkOrderService.completeWorkOrder(id));
    }

    /**
     * 查询工单流转记录
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:query')")
    @GetMapping("/flow/{orderId}")
    public AjaxResult getFlowList(@PathVariable("orderId") Long orderId)
    {
        List<GxtWorkOrderFlow> flowList = gxtWorkOrderService.selectWorkOrderFlowByOrderId(orderId);
        return success(flowList);
    }

    /**
     * 接单（支持一次性选择处理人员和检修人员）
     */
    @PreAuthorize("@ss.hasPermi('worklog:maintenance:order:accept')")
    @Log(title = "接单", businessType = BusinessType.UPDATE)
    @PostMapping("/accept/{id}")
    public AjaxResult accept(@PathVariable("id") Long id, @RequestBody GxtWorkOrder gxtWorkOrder)
    {
        gxtWorkOrder.setId(id);
        return toAjax(gxtWorkOrderService.acceptOrder(gxtWorkOrder));
    }
}
