package com.ruoyi.web.controller.ehs;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.domain.EhsFloodDroughtTeam;
import com.ruoyi.ehsFloodDroughtTeam.domain.FloodDroughtTeamImportTemplate;
import com.ruoyi.ehsFloodDroughtTeam.service.IEhsFloodDroughtTeamService;
import com.ruoyi.ehsFloodDroughtWorker.domain.EhsFloodDroughtWorker;
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.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
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/ehsFloodDroughtTeam")
public class EhsFloodDroughtTeamController extends BaseController {
    @Autowired
    private IEhsFloodDroughtTeamService ehsFloodDroughtTeamService;
    @Autowired
    private IEhsFloodDroughtWorkerService workerService;

    /**
     * 查询防汛抗旱队伍管理列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtTeam:list')")
    @GetMapping("/list")
    public TableDataInfo list(EhsFloodDroughtTeam ehsFloodDroughtTeam) {
        startPage();
        List<EhsFloodDroughtTeam> list = ehsFloodDroughtTeamService.selectEhsFloodDroughtTeamList(ehsFloodDroughtTeam);
        return getDataTable(list);
    }

    /**
     * 导出防汛抗旱队伍管理列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtTeam:export')")
    @Log(title = "防汛抗旱队伍管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EhsFloodDroughtTeam ehsFloodDroughtTeam) {
        List<EhsFloodDroughtTeam> list = ehsFloodDroughtTeamService.selectEhsFloodDroughtTeamList(ehsFloodDroughtTeam);
        ExcelUtil<EhsFloodDroughtTeam> util = new ExcelUtil<EhsFloodDroughtTeam>(EhsFloodDroughtTeam.class);
        util.exportExcel(response, list, "防汛抗旱队伍管理数据");
    }

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

    /**
     * 新增防汛抗旱队伍管理
     */
    @ApiOperation("新增防汛抗旱队伍管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtTeam:add')")
    @Log(title = "防汛抗旱队伍管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsFloodDroughtTeam ehsFloodDroughtTeam) {
        return toAjax(ehsFloodDroughtTeamService.save(ehsFloodDroughtTeam));
    }

    /**
     * 修改防汛抗旱队伍管理
     */
    @ApiOperation("修改防汛抗旱队伍管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtTeam:edit')")
    @Transactional
    @Log(title = "防汛抗旱队伍管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsFloodDroughtTeam ehsFloodDroughtTeam) {
        // 更新防汛抗旱队伍信息
        boolean result = ehsFloodDroughtTeamService.updateById(ehsFloodDroughtTeam);
        // 更新防汛抗旱队伍中人员信息
        workerService.updateWorkerByTeamId(ehsFloodDroughtTeam);
        return toAjax(result);
    }

    /**
     * 删除防汛抗旱队伍管理
     */
    @ApiOperation("删除防汛抗旱队伍管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsFloodDroughtTeam:remove')")
    @Transactional
    @Log(title = "防汛抗旱队伍管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        boolean result = ehsFloodDroughtTeamService.removeByIds(Arrays.asList(ids));
        workerService.removeWorkerByTeamIds(Arrays.asList(ids));
        return toAjax(result);
    }

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

    //批量导入
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Boolean updateSupport) throws Exception {
        ExcelUtil<FloodDroughtTeamImportTemplate> util = new ExcelUtil<>(FloodDroughtTeamImportTemplate.class);
        List<FloodDroughtTeamImportTemplate> list = util.importExcel(file.getInputStream());
        String message = ehsFloodDroughtTeamService.importData(list, updateSupport);

        return AjaxResult.success(message);
    }
}
