package com.ruoyi.predict.controller;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.predict.domain.StandardPointManagement;
import com.ruoyi.predict.service.IStandardPointManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author hlf
 * @date 2022/10/21 14:46
 * 文件说明：规范测点管理
 */
@Api(value = "规范测点管理Controller", tags = {"规范测点管理操作接口"})
@Controller
@RequestMapping("/predict/api/smpmc")
public class SpecificationMeasuringPointManagementController extends BaseController {

    @Autowired
    private IStandardPointManagementService standardPointManagementService;

    @ApiOperation("获取规范测点信息列表")
    @PostMapping("/getStandardPointManagementList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemType", value = "测点类型"),
            @ApiImplicitParam(name = "itemNo", value = "测点编号"),
            @ApiImplicitParam(name = "itemName", value = "测点描述"),
            @ApiImplicitParam(name = "itemRef", value = "描述简称"),
            @ApiImplicitParam(name = "note", value = "备注"),
    })
    public TableDataInfo getStandardPointManagementList(StandardPointManagement standardPointManagement) {
        startPage();
        List<StandardPointManagement> list = standardPointManagementService.selectStandardPointManagementList(standardPointManagement);
        return getDataTable(list);
    }

    @ApiOperation("批量删除规范测点信息")
    @PostMapping("/deleteStandardPointManagement")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemNos", value = "批量规范测点编号", required = true),
    })
    public AjaxResult deleteStandardPointManagement(@RequestParam(value = "itemNos") String itemNos) {
        standardPointManagementService.deleteStandardPointManagementByIds(itemNos);
        return AjaxResult.success();
    }

    @ApiOperation("规范测点管理信息导入")
    @PostMapping("/standardPointManagementImport")
    @ResponseBody
    public AjaxResult standardPointManagementImport(MultipartFile file, boolean updateSupport) throws Exception {
        //判断是否为excel文件
        if (!"xlsx".equals(Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1)) && !"xls".equals(Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1))) {
            return error("仅允许导入“xlsx或xls”格式文件!");
        }
        ExcelUtil<StandardPointManagement> util = new ExcelUtil<StandardPointManagement>(StandardPointManagement.class);
        List<StandardPointManagement> standardPointManagementList = util.importExcel(file.getInputStream());
        for (StandardPointManagement obj : standardPointManagementList) {
            standardPointManagementService.insertStandardPointManagement(obj);
        }
        return AjaxResult.success("正在导入中...");
    }

    @ApiOperation("规范测点管理信息导出")
    @GetMapping("/standardPointManagementExport")
    @ResponseBody
    public void standardPointManagementExport(HttpServletResponse response, StandardPointManagement standardPointManagement, String ids) throws IOException {
        List<StandardPointManagement> list = new ArrayList<>();
        if (!"".equals(ids)) {
            String[] itemNos = Convert.toStrArray(ids);
            for (String itemNo : itemNos) {
                list.add(standardPointManagementService.selectStandardPointManagementById(Convert.toLong(itemNo)));
            }
        } else {
            list = standardPointManagementService.selectStandardPointManagementList(standardPointManagement);
        }
        ExcelUtil<StandardPointManagement> util = new ExcelUtil<StandardPointManagement>(StandardPointManagement.class);
        util.exportExcel(response, list, "规范测点管理信息数据");
    }
}
