package com.ktg.iscs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.iscs.domain.IsJobTicket;
import com.ktg.iscs.domain.dto.ticket.AddJobTicketDTO;
import com.ktg.iscs.domain.dto.ticket.AddMarsJobDTO;
import com.ktg.iscs.domain.dto.ticket.PageTicketDTO;
import com.ktg.iscs.domain.vo.ticket.*;
import com.ktg.iscs.service.IIsJobTicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 作业票Controller
 *
 * @author cgj
 * @date 2024-10-18
 */
@Api(tags = "作业票")
@RestController
@RequestMapping("/iscs/ticket")
public class IsJobTicketController extends BaseController {
    @Autowired
    private IIsJobTicketService isJobTicketService;


    @ApiOperation("查询作业票-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "dto", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('mes:job:jobm:query')")
    @GetMapping("/getIsJobTicketPage")
    public CommonResult<Page<PageTicketVO>> getIsJobTicketPage(Page<IsJobTicket> page, PageTicketDTO dto) {
        return CommonResult.success(isJobTicketService.getIsJobTicketPage(page, dto));
    }

    @ApiOperation("查询作业票-正在进行中的作业票列表-平台")
    @PreAuthorize("@ss.hasPermi('mes:job:jobm:query')")
    @GetMapping("/getMapWorkstationTicketList")
    public CommonResult<List<IsJobTicket>> getMapWorkstationTicketList() {
        return CommonResult.success(isJobTicketService.getMapWorkstationTicketList());
    }

    @ApiOperation("查询作业票-正在进行中的作业票列表-安卓")
    @PreAuthorize("@ss.hasPermi('mes:job:jobm:query')")
    @GetMapping("/getWorkstationTicketList")
    public CommonResult<List<IsJobTicket>> getWorkstationTicketList() {
        return CommonResult.success(isJobTicketService.getWorkstationTicketList());
    }

    /**
     * 导出作业票列表
     */
    @ApiOperation("导出作业票列表")
    @Parameter(name = "isJobTicket", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:ticket:export')")
    @Log(title = "作业票", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsJobTicket")
    public void exportIsJobTicket(HttpServletResponse response, IsJobTicket isJobTicket) {
        List<IsJobTicket> list = isJobTicketService.selectIsJobTicketList(isJobTicket);
        ExcelUtil<IsJobTicket> util = new ExcelUtil<IsJobTicket>(IsJobTicket.class);
        util.exportExcel(response, list, "作业票数据");
    }

    /**
     * 获取作业票详细信息
     */
    @ApiOperation("获取作业票详细信息")
    @Parameter(name = "ticketId", description = "ticketId")
    // @PreAuthorize("@ss.hasPermi('iscs:ticket:query')")
    @GetMapping(value = "/selectIsJobTicketById")
    public CommonResult<JobTicketDetailVO> selectIsJobTicketById(Long ticketId) {
        return CommonResult.success(isJobTicketService.selectIsJobTicketByTicketId(ticketId));
    }

    /**
     * 新增作业票
     */
    @ApiOperation("新增作业票-sop")
    @PreAuthorize("@ss.hasPermi('mes:job:jobm:add')")
    @Log(title = "作业票", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsJobTicket")
    public CommonResult<Long> insertJobTicketBySop(@RequestBody @Parameter(name = "dto", description = "新增数据类,放到body") AddMarsJobDTO dto) {
        return CommonResult.success(isJobTicketService.insertJobTicketBySop(dto));
    }

    @ApiOperation("新增作业票-工艺")
    @PreAuthorize("@ss.hasPermi('mes:job:jobm:add')")
    @Log(title = "作业票", businessType = BusinessType.INSERT)
    @PostMapping("/insertJobByMachinery")
    public CommonResult<Boolean> insertJobByMachinery(@RequestBody @Parameter(name = "dto", description = "新增数据类,放到body") AddMarsJobDTO dto) {
        return CommonResult.success(isJobTicketService.insertJobByMachinery(dto));
    }

    /**
     * 修改作业票
     */
    @ApiOperation("修改作业票")
    @PreAuthorize("@ss.hasPermi('mes:job:jobm:edit')")
    @Log(title = "作业票", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsJobTicket")
    public CommonResult<Boolean> updateIsJobTicket(@RequestBody @Parameter(name = "dto", description = "修改数据类,放到body") AddJobTicketDTO dto) {
        return CommonResult.success(isJobTicketService.updateIsJobTicket(dto));
    }

    /**
     * 删除作业票
     */
    @ApiOperation("删除作业票")
    // @PreAuthorize("@ss.hasPermi('iscs:ticket:remove')")
    @Log(title = "作业票", businessType = BusinessType.DELETE)
    @PostMapping("/deleteIsJobTicketByTicketIds")
    public CommonResult<Boolean> deleteIsJobTicketByTicketIds(String ticketIds) {
        return CommonResult.success(isJobTicketService.deleteIsJobTicketByTicketIds(ticketIds));
    }

    /**
     * 更新工作票进度
     */
    @ApiOperation("更新作业票进度")
    @PostMapping("/updateProgress")
    public CommonResult<Boolean> updateTicketProgress(@RequestBody @Valid JobTicketUpdateProgressReqVO reqVO) {
        return CommonResult.success(isJobTicketService.updateTicketProgress(reqVO));
    }

    @ApiOperation("监控作业票-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "dto", description = "实体参数")
    })
    @GetMapping("/getMonitorJobTicketPage")
    public CommonResult<Page<MonitorTicketPageVO>> getMonitorJobTicketPage(Page<IsJobTicket> page, PageTicketDTO dto) {
        return CommonResult.success(isJobTicketService.getMonitorJobTicketPage(page, dto));
    }

    @ApiOperation("监控作业票-详情")
    @Parameter(name = "ticketId", description = "ticketId")
    @GetMapping(value = "/selectMonitorJobTicketDetail")
    public CommonResult<MonitorJobTicketDetailVO> selectMonitorJobTicketDetail(Long ticketId) {
        return CommonResult.success(isJobTicketService.selectMonitorJobTicketDetail(ticketId));
    }

    @ApiOperation("mars结束作业票")
    @Log(title = "mars结束作业票", businessType = BusinessType.DELETE)
    @PostMapping("/updateJobToFinish")
    public CommonResult<Boolean> updateJobToFinish(@RequestBody @Valid IsJobTicket isJobTicket) {
        return CommonResult.success(isJobTicketService.updateJobToFinish(isJobTicket));
    }

    @ApiOperation("mars取消作业票")
    @Log(title = "mars取消作业票", businessType = BusinessType.DELETE)
    @PostMapping("/updateJobToCancel")
    public CommonResult<Boolean> updateJobToCancel(@RequestBody @Valid IsJobTicket isJobTicket) {
        return CommonResult.success(isJobTicketService.updateJobToCancel(isJobTicket));
    }


}
