package com.ktg.iscs.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.IsMachinery;
import com.ktg.iscs.domain.dto.machinery.IsMachineryDTO;
import com.ktg.iscs.domain.vo.machinery.IsMachineryVO;
import com.ktg.iscs.service.IIsMachineryService;
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;

/**
 * 设备工艺Controller
 *
 * @author cgj
 * @date 2024-12-19
 */
@Api(tags = "设备工艺")
@RestController
@RequestMapping("/iscs/machinery")
public class IsMachineryController extends BaseController
{
    @Autowired
    private IIsMachineryService isMachineryService;

    /**
     * 查询设备工艺分页
     */
    @ApiOperation("查询设备工艺-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isMachinery", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:machinery:list')")
    @GetMapping("/getIsMachineryPage")
    public CommonResult<Page<IsMachineryVO>> getIsMachineryPage(Page<IsMachinery> page, IsMachinery isMachinery)
    {
        Page<IsMachineryVO> result = isMachineryService.getIsMachineryPage(page, isMachinery);
        return CommonResult.success(result);
    }

    /**
     * 导出设备工艺列表
     */
    @ApiOperation("导出设备工艺列表")
    @Parameter(name = "isMachinery", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:machinery:export')")
    @Log(title = "设备工艺", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsMachinery")
    public void exportIsMachinery(HttpServletResponse response, IsMachinery isMachinery)
    {
        Page<IsMachinery> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsMachinery> list = isMachineryService.page(page, Wrappers.<IsMachinery>lambdaQuery()
                .orderByDesc(IsMachinery::getMachineryId)).getRecords();
        ExcelUtil<IsMachinery> util = new ExcelUtil<IsMachinery>(IsMachinery.class);
        util.exportExcel(response, list, "设备工艺数据");
    }

    /**
     * 获取设备工艺详细信息
     */
    @ApiOperation("获取设备工艺详细信息")
    @Parameter(name = "technologyId", description = "technologyId")
    @PreAuthorize("@ss.hasPermi('iscs:machinery:query')")
    @GetMapping(value = "/selectIsMachineryById")
    public CommonResult<IsMachineryVO> selectIsMachineryById(Long machineryId)
    {
        return CommonResult.success(isMachineryService.selectIsMachineryById(machineryId));
    }

    /**
     * 新增设备工艺
     */
    @ApiOperation("新增设备工艺")
    @PreAuthorize("@ss.hasPermi('iscs:machinery:add')")
    @Log(title = "设备工艺", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsMachinery")
    public CommonResult<Boolean> insertIsMachinery(@RequestBody @Parameter(name = "isMachinery", description = "新增数据类,放到body") IsMachinery isMachinery)
    {
        return CommonResult.success(isMachineryService.insertIsMachinery(isMachinery));
    }

    /**
     * 修改设备工艺
     */
    @ApiOperation("修改设备工艺")
    @PreAuthorize("@ss.hasPermi('iscs:machinery:edit')")
    @Log(title = "设备工艺", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsMachinery")
    public CommonResult<Boolean> updateIsMachinery(@RequestBody @Parameter(name = "isMachinery", description = "修改数据类,放到body") IsMachinery isMachinery)
    {
        return CommonResult.success(isMachineryService.updateIsMachinery(isMachinery));
    }

    @ApiOperation("设备工艺隔离点建立关联关系")
    @PreAuthorize("@ss.hasPermi('iscs:machinery:edit')")
    @Log(title = "设备工艺", businessType = BusinessType.UPDATE)
    @PostMapping("/saveMachineryPoints")
    public CommonResult<Boolean> saveMachineryPoints(@RequestBody @Parameter(name = "dto", description = "修改数据类,放到body") IsMachineryDTO dto)
    {
        return CommonResult.success(isMachineryService.saveMachineryPoints(dto));
    }

    /**
     * 删除设备工艺
     */
    @ApiOperation("删除设备工艺")
    @PreAuthorize("@ss.hasPermi('iscs:machinery:remove')")
    @Log(title = "设备工艺", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsMachineryByTechnologyIds")
    public CommonResult<Boolean> deleteIsMachineryByMachineryIds(String machineryIds)
    {
        return CommonResult.success(isMachineryService.deleteIsMachineryByMachineryIds(machineryIds));
    }
}
