package com.ktg.iscs.controller;

import cn.hutool.core.lang.Assert;
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.AjaxResult;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.bean.BeanUtils;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.iscs.domain.IsSop;
import com.ktg.iscs.domain.dto.sop.AddMarsSopDTO;
import com.ktg.iscs.domain.dto.sop.PageSopDTO;
import com.ktg.iscs.domain.vo.sop.ExportSopVO;
import com.ktg.iscs.domain.vo.sop.ImportSopVO;
import com.ktg.iscs.domain.vo.sop.MarsSopDetailVO;
import com.ktg.iscs.domain.vo.sop.SopPageVO;
import com.ktg.iscs.service.IIsMarsSopService;
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.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

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

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

    @ApiOperation("导出mars-SOP信息列表")
    @Parameter(name = "isSop", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:sop:export')")
    @Log(title = "SOP信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsMarsSop")
    public void exportIsSop(HttpServletResponse response, PageSopDTO dto) {
        Page<IsSop> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        Page<SopPageVO> isSopPage = isMarsSopService.getIsMarsSopPage(dto, page);
        ExcelUtil<ExportSopVO> util = new ExcelUtil<>(ExportSopVO.class);
        List<ExportSopVO> list = BeanUtils.toBean(isSopPage.getRecords(), ExportSopVO.class);
        util.exportExcel(response, list, "SOP信息数据");
    }

    @ApiOperation("获取mars-SOP信息详细信息")
    @Parameter(name = "sopId", description = "sopId")
    @PreAuthorize("@ss.hasPermi('iscs:mars:sop:query')")
    @GetMapping(value = "/selectIsMarsSopById")
    public CommonResult<MarsSopDetailVO> selectIsMarsSopById(Long sopId) {
        return CommonResult.success(isMarsSopService.selectIsMarsSopBySopId(sopId));
    }

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

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

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

    @ApiOperation("删除mars-SOP信息")
    @Log(title = "SOP信息", businessType = BusinessType.DELETE)
    @PostMapping("/deleteIsMarsSopBySopIds")
    public CommonResult<Boolean> deleteIsMarsSopBySopIds(String sopIds) {
        return CommonResult.success(isMarsSopService.deleteIsMarsSopBySopIds(sopIds));
    }

    @ApiOperation("导入模板-sop")
    @Log(title = "导入模板-sop")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ImportSopVO> util = new ExcelUtil<>(ImportSopVO.class);
        util.importTemplateExcel(response, "sop数据");
    }

    @ApiOperation("导入-sop")
    @Log(title = "导入-sop", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('iscs:sop:import')")
    @PostMapping("/importSop")
    public AjaxResult importSop(MultipartFile file, boolean updateSupport) throws Exception
    {
        String filename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(filename);
        // 文件不是 .xlsx 或 .xls
        Assert.isFalse(!"xls".equalsIgnoreCase(extension) && !"xlsx".equalsIgnoreCase(extension), "文件不是xls或xlsx，请重新上传！");
        ExcelUtil<ImportSopVO> util = new ExcelUtil<>(ImportSopVO.class);
        List<ImportSopVO> itemList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = isMarsSopService.importSop(itemList, updateSupport, operName);
        return AjaxResult.success(message);
    }

}
