package com.ruoyi.web.controller.ehs;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ExcelExp;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelExportUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ehsRescueTeam.domain.EhsRescueTeam;
import com.ruoyi.ehsRescueTeam.domain.RescueTeamImportTemplate;
import com.ruoyi.ehsRescueTeam.service.IEhsRescueTeamService;
import com.ruoyi.ehsRescueTeam.vo.TeamResponseVo;
import com.ruoyi.ehsRescueTeamWorker.domain.EhsRescueTeamWorker;
import com.ruoyi.ehsRescueTeamWorker.domain.RescueTeamWorkerImportTemplate;
import com.ruoyi.ehsRescueTeamWorker.service.IEhsRescueTeamWorkerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.Arrays;
import java.util.List;

/**
 * 应急救援队人员信息Controller
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@Api(tags = "应急救援队人员信息管理")
@RestController
@RequestMapping("/ehs/ehsRescueTeamWorker")
public class EhsRescueTeamWorkerController extends BaseController {
    @Autowired
    private IEhsRescueTeamWorkerService ehsRescueTeamWorkerService;
    @Autowired
    private IEhsRescueTeamService ehsRescueTeamService;

    /**
     * 查询应急救援队人员信息列表
     */
    //@PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeamWorker:list')")
    @GetMapping("/list")
    public TableDataInfo list(EhsRescueTeamWorker ehsRescueTeamWorker) {
        startPage();
        List<EhsRescueTeamWorker> list = ehsRescueTeamWorkerService.selectEhsRescueTeamWorkerList(ehsRescueTeamWorker);
        return getDataTable(list);
    }

    /**
     * 导出应急救援队人员信息列表
     */
    //@PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeamWorker:export')")
    @Log(title = "应急救援队人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EhsRescueTeamWorker ehsRescueTeamWorker) {
        List<EhsRescueTeamWorker> list = ehsRescueTeamWorkerService.selectEhsRescueTeamWorkerList(ehsRescueTeamWorker);
        ExcelUtil<EhsRescueTeamWorker> util = new ExcelUtil<EhsRescueTeamWorker>(EhsRescueTeamWorker.class);
        util.exportExcel(response, list, "应急救援队人员信息数据");
    }

    /**
     * 获取应急救援队人员信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeamWorker:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ehsRescueTeamWorkerService.selectEhsRescueTeamWorkerById(id));
    }

    /**
     * 根据队伍id获取队伍信息
     */
    @GetMapping(value = "/getTeamInfo/{rescueTeamId}")
    public AjaxResult getTeamInfo(@PathVariable("rescueTeamId") Long rescueTeamId) {
        TeamResponseVo teamResponseVo = ehsRescueTeamService.getTeamInfoByTeamId(rescueTeamId);
        return success(teamResponseVo);
    }

    /**
     * 新增应急救援队人员信息
     */
    @ApiOperation("新增应急救援队人员信息")
    //@PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeamWorker:add')")
    @Log(title = "应急救援队人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsRescueTeamWorker ehsRescueTeamWorker) {
        return toAjax(ehsRescueTeamWorkerService.save(ehsRescueTeamWorker));
    }

    /**
     * 修改应急救援队人员信息
     */
    @ApiOperation("修改应急救援队人员信息")
    //@PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeamWorker:edit')")
    @Log(title = "应急救援队人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsRescueTeamWorker ehsRescueTeamWorker) {
        return toAjax(ehsRescueTeamWorkerService.updateById(ehsRescueTeamWorker));
    }

    /**
     * 删除应急救援队人员信息
     */
    @ApiOperation("删除应急救援队人员信息")
    //@PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeamWorker:remove')")
    @Log(title = "应急救援队人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ehsRescueTeamWorkerService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 下载批量导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<EhsRescueTeamWorker> teamWorkerList = new ArrayList<>();
        String[] header =
                {"人员姓名#",
                        "年龄#",
                        "性别(0男 1女)#",
                        "身份证号#",
                        "联系方式#",
                        "队内职务#",
                        "工作年限#",
                        "救援技能特长#",
                        "培训次数#",
                        "备注#"};
        ExcelExp e1 = new ExcelExp("应急救援队伍人员信息", header, teamWorkerList, EhsRescueTeamWorker.class);
        List<ExcelExp> mysheet = new ArrayList<>();
        mysheet.add(e1);
        ExcelExportUtil.exportManySheetExcel("应急救援队伍人员信息批量导入模板", mysheet, response);
    }


    /**
     * 批量导入
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Boolean updateSupport, Long params) throws Exception {
        ExcelUtil<RescueTeamWorkerImportTemplate> util = new ExcelUtil<>(RescueTeamWorkerImportTemplate.class);
        List<RescueTeamWorkerImportTemplate> list = util.importExcel(file.getInputStream());
        String message = ehsRescueTeamWorkerService.importRescueTeamWorker(list, updateSupport, params);
        return AjaxResult.success(message);
    }
}
