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.ehsFloodDroughtTeam.service.IEhsFloodDroughtTeamService;
import com.ruoyi.ehsFloodDroughtTeam.vo.FloodDroughtTeamVo;
import com.ruoyi.ehsFloodDroughtWorker.domain.EhsFloodDroughtWorker;
import com.ruoyi.ehsFloodDroughtWorker.domain.FloodDroughtWorkerImportTemplate;
import com.ruoyi.ehsFloodDroughtWorker.service.IEhsFloodDroughtWorkerService;
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-14
 */
@Api(tags = "防汛抗旱队伍人员信息管理")
@RestController
@RequestMapping("/ehs/ehsFloodDroughtWorker")
public class EhsFloodDroughtWorkerController extends BaseController {
    @Autowired
    private IEhsFloodDroughtWorkerService ehsFloodDroughtWorkerService;
    @Autowired
    private IEhsFloodDroughtTeamService ehsFloodDroughtTeamService;

    /**
     * 查询防汛抗旱队伍人员信息列表
     */
    //@PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtWorker:list')")
    @GetMapping("/list")
    public TableDataInfo list(EhsFloodDroughtWorker ehsFloodDroughtWorker) {
        startPage();
        List<EhsFloodDroughtWorker> list = ehsFloodDroughtWorkerService.selectEhsFloodDroughtWorkerList(ehsFloodDroughtWorker);
        return getDataTable(list);
    }

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

    /**
     * 导出防汛抗旱队伍人员信息列表
     */
    //@PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtWorker:export')")
    @Log(title = "防汛抗旱队伍人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EhsFloodDroughtWorker ehsFloodDroughtWorker) {
        List<EhsFloodDroughtWorker> list = ehsFloodDroughtWorkerService.selectEhsFloodDroughtWorkerList(ehsFloodDroughtWorker);
        ExcelUtil<EhsFloodDroughtWorker> util = new ExcelUtil<EhsFloodDroughtWorker>(EhsFloodDroughtWorker.class);
        util.exportExcel(response, list, "防汛抗旱队伍人员信息数据");
    }

    /**
     * 获取防汛抗旱队伍人员信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtWorker:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ehsFloodDroughtWorkerService.selectEhsFloodDroughtWorkerById(id));
    }

    /**
     * 新增防汛抗旱队伍人员信息
     */
    @ApiOperation("新增防汛抗旱队伍人员信息")
    //@PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtWorker:add')")
    @Log(title = "防汛抗旱队伍人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsFloodDroughtWorker ehsFloodDroughtWorker) {
        return toAjax(ehsFloodDroughtWorkerService.save(ehsFloodDroughtWorker));
    }

    /**
     * 修改防汛抗旱队伍人员信息
     */
    @ApiOperation("修改防汛抗旱队伍人员信息")
    //@PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtWorker:edit')")
    @Log(title = "防汛抗旱队伍人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsFloodDroughtWorker ehsFloodDroughtWorker) {
        return toAjax(ehsFloodDroughtWorkerService.updateById(ehsFloodDroughtWorker));
    }

    /**
     * 删除防汛抗旱队伍人员信息
     */
    @ApiOperation("删除防汛抗旱队伍人员信息")
    //@PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtWorker:remove')")
    @Log(title = "防汛抗旱队伍人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ehsFloodDroughtWorkerService.removeByIds(Arrays.asList(ids)));
    }

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

    //批量导入 params是防汛抗旱队伍id
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Boolean updateSupport, Long params) throws Exception {
        ExcelUtil<FloodDroughtWorkerImportTemplate> util = new ExcelUtil<>(FloodDroughtWorkerImportTemplate.class);
        List<FloodDroughtWorkerImportTemplate> list = util.importExcel(file.getInputStream());
        String message = ehsFloodDroughtWorkerService.importData(list, updateSupport, params);

        return AjaxResult.success(message);
    }
}
