package com.ygtx.gxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
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.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.GxtWorkOrderPerson;
import com.ygtx.gxt.service.IGxtWorkOrderPersonService;
import com.ygtx.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 工单工作组员Controller
 *
 * @author ygtx
 * @date 2025-10-29
 */
@RestController
@RequestMapping("/gxt/workOrderPerson")
@Api(tags = "工单工作组员")
public class GxtWorkOrderPersonController extends BaseController
{
    @Autowired
    private IGxtWorkOrderPersonService gxtWorkOrderPersonService;

    /**
     * 查询工单工作组员列表
     */
    @ApiOperation("查询工单工作组员列表")
    @GetMapping("/list")
    public AjaxResult list(GxtWorkOrderPerson gxtWorkOrderPerson)
    {
        List<GxtWorkOrderPerson> list = gxtWorkOrderPersonService.selectGxtWorkOrderPersonList(gxtWorkOrderPerson);
        return success(list);
    }

    /**
     * 根据工单ID查询工作组员列表
     */
    @ApiOperation("根据工单ID查询工作组员列表")
    @ApiImplicitParam(name = "orderId", value = "工单ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping("/listByOrderId/{orderId}")
    public AjaxResult listByOrderId(@PathVariable Long orderId)
    {
        List<GxtWorkOrderPerson> list = gxtWorkOrderPersonService.selectGxtWorkOrderPersonByOrderId(orderId);
        return success(list);
    }

    /**
     * 导出工单工作组员列表
     */
    @Log(title = "工单工作组员", businessType = BusinessType.EXPORT)
    @ApiOperation("导出工单工作组员列表")
    @GetMapping("/export")
    public AjaxResult export(GxtWorkOrderPerson gxtWorkOrderPerson)
    {
        List<GxtWorkOrderPerson> list = gxtWorkOrderPersonService.selectGxtWorkOrderPersonList(gxtWorkOrderPerson);
        ExcelUtil<GxtWorkOrderPerson> util = new ExcelUtil<GxtWorkOrderPerson>(GxtWorkOrderPerson.class);
        return util.exportExcel(list, "工单工作组员");
    }

    /**
     * 获取工单工作组员详细信息
     */
    @ApiOperation("获取工单工作组员详细信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(gxtWorkOrderPersonService.selectGxtWorkOrderPersonById(id));
    }

    /**
     * 新增工单工作组员
     */
    @Log(title = "工单工作组员", businessType = BusinessType.INSERT)
    @ApiOperation("新增工单工作组员")
    @PostMapping
    public AjaxResult add(@RequestBody GxtWorkOrderPerson gxtWorkOrderPerson)
    {
        return toAjax(gxtWorkOrderPersonService.insertGxtWorkOrderPerson(gxtWorkOrderPerson));
    }

    /**
     * 批量新增工单工作组员
     */
    @Log(title = "工单工作组员", businessType = BusinessType.INSERT)
    @ApiOperation("批量新增工单工作组员")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderId", value = "工单ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "orderCode", value = "工单编码", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    })
    @PostMapping("/insertBatch/{orderId}/{orderCode}")
    public AjaxResult insertBatch(@PathVariable Long orderId, @PathVariable String orderCode, @RequestBody List<GxtWorkOrderPerson> personList)
    {
        return toAjax(gxtWorkOrderPersonService.insertBatchGxtWorkOrderPerson(orderId, orderCode, personList));
    }

    /**
     * 修改工单工作组员
     */
    @Log(title = "工单工作组员", businessType = BusinessType.UPDATE)
    @ApiOperation("修改工单工作组员")
    @PutMapping
    public AjaxResult edit(@RequestBody GxtWorkOrderPerson gxtWorkOrderPerson)
    {
        return toAjax(gxtWorkOrderPersonService.updateGxtWorkOrderPerson(gxtWorkOrderPerson));
    }

    /**
     * 删除工单工作组员
     */
    @Log(title = "工单工作组员", businessType = BusinessType.DELETE)
    @ApiOperation("删除工单工作组员")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(gxtWorkOrderPersonService.deleteGxtWorkOrderPersonById(id));
    }

    /**
     * 批量删除工单工作组员
     */
    @Log(title = "工单工作组员", businessType = BusinessType.DELETE)
    @ApiOperation("批量删除工单工作组员")
    @ApiImplicitParam(name = "ids", value = "主键ID数组", required = true, dataType = "Long[]", paramType = "path", dataTypeClass = Long[].class)
    @DeleteMapping("/{ids}")
    public AjaxResult removeByIds(@PathVariable Long[] ids)
    {
        return toAjax(gxtWorkOrderPersonService.deleteGxtWorkOrderPersonByIds(ids));
    }

    /**
     * 下载人员导入模板
     */
    @ApiOperation("下载人员导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<GxtWorkOrderPerson> util = new ExcelUtil<GxtWorkOrderPerson>(GxtWorkOrderPerson.class);
        util.importTemplateExcel(response, "工单人员数据");
    }

    /**
     * 导入工单人员数据
     */
    @PreAuthorize("@ss.hasPermi('gxt:maintenance:order:import')")
    @Log(title = "工单人员", businessType = BusinessType.IMPORT)
    @ApiOperation("导入工单人员数据")
    @ApiImplicitParam(name = "file", value = "上传文件", required = true, dataType = "MultipartFile", paramType = "form", dataTypeClass = MultipartFile.class)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<GxtWorkOrderPerson> util = new ExcelUtil<GxtWorkOrderPerson>(GxtWorkOrderPerson.class);
        List<GxtWorkOrderPerson> personList = util.importExcel(file.getInputStream());
        String message = gxtWorkOrderPersonService.importGxtWorkOrderPerson(personList);
        return success(message);
    }


    /**
     * 根据工单ID删除工作组员
     */
    @Log(title = "工单工作组员", businessType = BusinessType.DELETE)
    @ApiOperation("根据工单ID删除工作组员")
    @ApiImplicitParam(name = "orderId", value = "工单ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/removeByOrderId/{orderId}")
    public AjaxResult removeByOrderId(@PathVariable Long orderId)
    {
        return toAjax(gxtWorkOrderPersonService.deleteGxtWorkOrderPersonByOrderId(orderId));
    }
}