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.IsWorkarea;
import com.ktg.iscs.domain.vo.workarea.PageWorkareaVO;
import com.ktg.iscs.domain.vo.workarea.PointsMapVO;
import com.ktg.iscs.service.IIsWorkareaService;
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-10-18
 */
@Api(tags = "工作区域")
@RestController
@RequestMapping("/iscs/workarea")
public class IsWorkareaController extends BaseController {
    @Autowired
    private IIsWorkareaService isWorkareaService;

    /**
     * 查询工作区域分页
     */
    @ApiOperation("查询工作区域-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "dto", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('mes:md:waa:query')")
    @GetMapping("/getIsWorkareaPage")
    public CommonResult<Page<PageWorkareaVO>> getIsWorkareaPage(Page page, IsWorkarea dto) {
        return CommonResult.success(isWorkareaService.getIsWorkareaPage(page, dto));
    }

    @ApiOperation("查询工作区域-列表")
    @Parameters({
            @Parameter(name = "workshopId", description = "所属车间ID")
    })
    // @PreAuthorize("@ss.hasPermi('iscs:workarea:list')")
    @GetMapping("/getIsWorkareaList")
    public CommonResult<List<IsWorkarea>> getIsWorkareaList(Long workshopId) {
        List<IsWorkarea> result = isWorkareaService.list(Wrappers.<IsWorkarea>lambdaQuery()
                .eq(workshopId != null, IsWorkarea::getWorkshopId, workshopId)
                .orderByDesc(IsWorkarea::getWorkareaId));
        return CommonResult.success(result);
    }

    /**
     * 导出工作区域列表
     */
    @ApiOperation("导出工作区域列表")
    @Parameter(name = "isWorkarea", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:workarea:export')")
    @Log(title = "工作区域", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsWorkarea")
    public void exportIsWorkarea(HttpServletResponse response, IsWorkarea isWorkarea) {
        List<IsWorkarea> list = isWorkareaService.selectIsWorkareaList(isWorkarea);
        ExcelUtil<IsWorkarea> util = new ExcelUtil<IsWorkarea>(IsWorkarea.class);
        util.exportExcel(response, list, "工作区域数据");
    }

    /**
     * 获取工作区域详细信息
     */
    @ApiOperation("获取工作区域详细信息")
    @Parameter(name = "workareaId", description = "workareaId")
    // @PreAuthorize("@ss.hasPermi('iscs:workarea:query')")
    @GetMapping(value = "/selectIsWorkareaById")
    public CommonResult<IsWorkarea> selectIsWorkareaById(Long workareaId) {
        return CommonResult.success(isWorkareaService.selectIsWorkareaByWorkareaId(workareaId));
    }

    /**
     * 新增工作区域
     */
    @ApiOperation("新增工作区域")
    @PreAuthorize("@ss.hasPermi('iscs:workarea:add')")
    @Log(title = "工作区域", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsWorkarea")
    public CommonResult<Boolean> insertIsWorkarea(@RequestBody @Parameter(name = "isWorkarea", description = "新增数据类,放到body") IsWorkarea isWorkarea) {
        return CommonResult.success(isWorkareaService.insertIsWorkarea(isWorkarea) == 1);
    }

    /**
     * 修改工作区域
     */
    @ApiOperation("修改工作区域")
    @PreAuthorize("@ss.hasPermi('mes:md:waa:edit')")
    @Log(title = "工作区域", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsWorkarea")
    public CommonResult<Boolean> updateIsWorkarea(@RequestBody @Parameter(name = "isWorkarea", description = "修改数据类,放到body") IsWorkarea isWorkarea) {
        return CommonResult.success(isWorkareaService.updateIsWorkarea(isWorkarea) == 1);
    }

    /**
     * 删除工作区域
     */
    @ApiOperation("删除工作区域")
    // @PreAuthorize("@ss.hasPermi('mes:md:waa:batchremove')")
    @Log(title = "工作区域", businessType = BusinessType.DELETE)
    @PostMapping("/deleteIsWorkareaByWorkareaIds")
    public CommonResult<Boolean> deleteIsWorkareaByWorkareaIds(String workareaIds) {
        return CommonResult.success(isWorkareaService.deleteIsWorkareaByWorkareaIds(workareaIds) != 0);
    }

    @ApiOperation("获取区域map解析数据")
    @Parameter(name = "workareaId", description = "workareaId")
    @Parameters({
            @Parameter(name = "workareaId", description = "区域id"),
            @Parameter(name = "sopId", description = "sopId"),
            @Parameter(name = "ticketId", description = "ticketId")
    })
    // @PreAuthorize("@ss.hasPermi('iscs:workarea:query')")
    @GetMapping(value = "/selectIsWorkareaMapById")
    public CommonResult<List<PointsMapVO>> selectIsWorkareaMapById(Long workareaId, Long sopId, Long ticketId) {
        return CommonResult.success(isWorkareaService.selectIsWorkareaMapById(workareaId, sopId, ticketId));
    }
}
