package com.ruoyi.web.controller.ehs;

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

import com.ruoyi.common.core.domain.ExcelExp;
import com.ruoyi.common.utils.ExcelExportUtil;
import com.ruoyi.ehsRescueTeam.domain.RescueTeamImportTemplate;
import com.ruoyi.ehsRescueTeamWorker.service.IEhsRescueTeamWorkerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ehsRescueTeam.domain.EhsRescueTeam;
import com.ruoyi.ehsRescueTeam.service.IEhsRescueTeamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 应急救援队伍管理Controller
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@Api(tags = "应急救援队伍管理管理")
@RestController
@RequestMapping("/ehs/ehsRescueTeam")
public class EhsRescueTeamController extends BaseController {
    @Autowired
    private IEhsRescueTeamService ehsRescueTeamService;

    @Autowired
    private IEhsRescueTeamWorkerService ehsRescueTeamWorkerService;

    /**
     * 查询应急救援队伍管理列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeam:list')")
    @GetMapping("/list")
    public TableDataInfo list(EhsRescueTeam ehsRescueTeam) {
        startPage();
        List<EhsRescueTeam> list = ehsRescueTeamService.selectEhsRescueTeamList(ehsRescueTeam);
        return getDataTable(list);
    }

    /**
     * 导出应急救援队伍管理列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeam:export')")
    @Log(title = "应急救援队伍管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EhsRescueTeam ehsRescueTeam) {
        List<EhsRescueTeam> list = ehsRescueTeamService.selectEhsRescueTeamList(ehsRescueTeam);
        ExcelUtil<EhsRescueTeam> util = new ExcelUtil<EhsRescueTeam>(EhsRescueTeam.class);
        util.exportExcel(response, list, "应急救援队伍管理数据");
    }

    /**
     * 下载批量导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<EhsRescueTeam> ehsRescueTeamList = new ArrayList<>();
        String[] header =
                {"队伍名称#",
                        "队伍级别#",
                        "负责人#",
                        "联系电话#",
                        "所属单位#",
                        "驻地#",
                        "队伍属性(0-其他,1-政府,2-企业,3-社会)#",
                        "备注#"};
        ExcelExp e1 = new ExcelExp("应急救援队伍信息", header, ehsRescueTeamList, EhsRescueTeam.class);
        List<ExcelExp> mysheet = new ArrayList<>();
        mysheet.add(e1);
        ExcelExportUtil.exportManySheetExcel("应急救援队伍信息批量导入模板", mysheet, response);
    }

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

    /**
     * 获取应急救援队伍管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ehsRescueTeamService.selectEhsRescueTeamById(id));
    }

    /**
     * 新增应急救援队伍管理
     */
    @ApiOperation("新增应急救援队伍管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeam:add')")
    @Log(title = "应急救援队伍管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsRescueTeam ehsRescueTeam) {
        return toAjax(ehsRescueTeamService.save(ehsRescueTeam));
    }

    /**
     * 修改应急救援队伍管理
     */
    @ApiOperation("修改应急救援队伍管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeam:edit')")
    @Transactional
    @Log(title = "应急救援队伍管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsRescueTeam ehsRescueTeam) {
        // 更新应急救援队伍信息
        boolean result = ehsRescueTeamService.updateById(ehsRescueTeam);
        // 更新应急救援队伍人员信息
        ehsRescueTeamWorkerService.updateWorkerByTeamId(ehsRescueTeam);
        return toAjax(result);
    }

    /**
     * 删除应急救援队伍管理
     */
    @ApiOperation("删除应急救援队伍管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsRescueTeam:remove')")
    @Log(title = "应急救援队伍管理", businessType = BusinessType.DELETE)
    @Transactional
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        // 根据队伍ids删除队伍
        boolean result = ehsRescueTeamService.removeByIds(Arrays.asList(ids));
        // 根据队伍ids删除队伍中的队员
        ehsRescueTeamWorkerService.removeWorkerByTeamIds(Arrays.asList(ids));
        return toAjax(result);
    }
}
