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.core.domain.entity.SysDictData;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.iscs.domain.IsSop;
import com.ktg.iscs.domain.dto.sop.AddSopDTO;
import com.ktg.iscs.domain.dto.sop.PageSopDTO;
import com.ktg.iscs.domain.vo.sop.SopDetailVO;
import com.ktg.iscs.domain.vo.sop.SopPageVO;
import com.ktg.iscs.service.IIsSopService;
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 java.util.List;

/**
 * SOP信息Controller
 *
 * @author cgj
 * @date 2024-10-18
 */
@Api(tags = "SOP信息")
@RestController
@RequestMapping("/iscs/sop")
public class IsSopController extends BaseController {
    @Autowired
    private IIsSopService isSopService;

    /**
     * 查询SOP信息分页
     */
    @ApiOperation("查询SOP信息-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "dto", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('mes:sop:sopm:query')")
    @GetMapping("/getIsSopPage")
    public CommonResult<Page<SopPageVO>> getIsSopPage(Page<IsSop> page, PageSopDTO dto) {
        return CommonResult.success(isSopService.getIsSopPage(page, dto));
    }

    /**
     * 导出SOP信息列表
     */
    @ApiOperation("导出SOP信息列表")
    @Parameter(name = "isSop", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:sop:export')")
    @Log(title = "SOP信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsSop")
    public void exportIsSop(HttpServletResponse response, IsSop isSop) {
        List<IsSop> list = isSopService.selectIsSopList(isSop);
        ExcelUtil<IsSop> util = new ExcelUtil<IsSop>(IsSop.class);
        util.exportExcel(response, list, "SOP信息数据");
    }

    /**
     * 获取SOP信息详细信息
     */
    @ApiOperation("获取SOP信息详细信息")
    @Parameter(name = "sopId", description = "sopId")
    // @PreAuthorize("@ss.hasPermi('iscs:sop:query')")
    @GetMapping(value = "/selectIsSopById")
    public CommonResult<SopDetailVO> selectIsSopById(Long sopId) {
        return CommonResult.success(isSopService.selectIsSopBySopId(sopId));
    }

    /**
     * 新增SOP信息
     */
    @ApiOperation("新增SOP信息")
    @PreAuthorize("@ss.hasPermi('mes:sop:sopm:add')")
    @Log(title = "SOP信息", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsSop")
    public CommonResult<Boolean> insertIsSop(@RequestBody @Parameter(name = "dto", description = "新增数据类,放到body") AddSopDTO dto) {
        return CommonResult.success(isSopService.insertIsSop(dto));
    }

    /**
     * 修改SOP信息
     */
    @ApiOperation("修改SOP信息")
    @PreAuthorize("@ss.hasPermi('mes:sop:sopm:edit')")
    @Log(title = "SOP信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsSop")
    public CommonResult<Boolean> updateIsSop(@RequestBody @Parameter(name = "dto", description = "修改数据类,放到body") AddSopDTO dto) {
        return CommonResult.success(isSopService.updateIsSop(dto));
    }

    @ApiOperation("修改SOP信息排序")
    @Log(title = "SOP信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsSopIndex")
    public CommonResult<Boolean> updateIsSopIndex(@RequestBody @Parameter(name = "isSop", description = "修改数据类,放到body") IsSop isSop) {
        return CommonResult.success(isSopService.updateIsSopIndex(isSop));
    }

    /**
     * 删除SOP信息
     */
    @ApiOperation("删除SOP信息")
    // @PreAuthorize("@ss.hasPermi('iscs:sop:remove')")
    @Log(title = "SOP信息", businessType = BusinessType.DELETE)
    @PostMapping("/deleteIsSopBySopIds")
    public CommonResult<Boolean> deleteIsSopBySopIds(String sopIds) {
        return CommonResult.success(isSopService.deleteIsSopBySopIds(sopIds));
    }

    @ApiOperation("获取工艺下的sop类型集合")
    @Parameter(name = "sopId", description = "sopId")
    // @PreAuthorize("@ss.hasPermi('iscs:sop:query')")
    @GetMapping(value = "/selectSopTypeByMachineryId")
    public CommonResult<List<SysDictData>> selectSopTypeByMachineryId(Long machineryId) {

        List<SysDictData> sysDictDatas = isSopService.selectSopTypeByMachineryId(machineryId);

        return CommonResult.success(sysDictDatas);
    }

}
