package com.ruoyi.web.controller.ehs;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.ehsRescueTeam.domain.RescueTeamImportTemplate;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.ehsEnterprise.domain.EhsEnterprise;
import com.ruoyi.ehsEnterprise.service.IEhsEnterpriseService;
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-08
 */
@Api(tags="企业基本信息管理")
@RestController
@RequestMapping("/ehsEnterprise/ehsEnterprise")
public class EhsEnterpriseController extends BaseController
{
    @Autowired
    private IEhsEnterpriseService ehsEnterpriseService;
	@Autowired
	private ISysDictDataService dictDataService;
/**
 * 查询企业基本信息列表
 */
@PreAuthorize("@ss.hasPermi('ehsEnterprise:ehsEnterprise:list')")
@GetMapping("/list")
	public TableDataInfo list(EhsEnterprise ehsEnterprise)
	{
		startPage();
		List<EhsEnterprise> list = ehsEnterpriseService.selectEhsEnterpriseList(ehsEnterprise);
		return getDataTable(list);
	}

	/**
	 * 导出企业基本信息列表
	 */
	@PreAuthorize("@ss.hasPermi('ehsEnterprise:ehsEnterprise:export')")
	@Log(title = "企业基本信息", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, EhsEnterprise ehsEnterprise)
	{
		List<EhsEnterprise> list = ehsEnterpriseService.selectEhsEnterpriseList(ehsEnterprise);
		ExcelUtil<EhsEnterprise> util = new ExcelUtil<EhsEnterprise>(EhsEnterprise.class);
		util.exportExcel(response, list, "企业基本信息数据");
	}

	/**
	 * 获取企业基本信息详细信息
	 */
	@PreAuthorize("@ss.hasPermi('ehsEnterprise:ehsEnterprise:query')")
	@GetMapping(value = "/{enterpriseId}")
	public AjaxResult getInfo(@PathVariable("enterpriseId") Long enterpriseId)
	{
		return success(ehsEnterpriseService.selectEhsEnterpriseByEnterpriseId(enterpriseId));
	}

    /**
     * 新增企业基本信息
     */
    @ApiOperation("新增企业基本信息")
    @PreAuthorize("@ss.hasPermi('ehsEnterprise:ehsEnterprise:add')")
    @Log(title = "企业基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsEnterprise ehsEnterprise)
    {
        return toAjax(ehsEnterpriseService.save(ehsEnterprise));
    }

    /**
     * 修改企业基本信息
     */
    @ApiOperation("修改企业基本信息")
    @PreAuthorize("@ss.hasPermi('ehsEnterprise:ehsEnterprise:edit')")
    @Log(title = "企业基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsEnterprise ehsEnterprise)
    {
        return toAjax(ehsEnterpriseService.updateById(ehsEnterprise));
    }

    /**
     * 删除企业基本信息
     */
    @ApiOperation("删除企业基本信息")
    @PreAuthorize("@ss.hasPermi('ehsEnterprise:ehsEnterprise:remove')")
    @Log(title = "企业基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{enterpriseIds}")
    public AjaxResult remove(@PathVariable Long[] enterpriseIds)
    {
        return toAjax(ehsEnterpriseService.removeByIds(Arrays.asList(enterpriseIds)));
    }
	@PostMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response) throws Exception {
		ExcelUtil<EhsEnterprise> util = new ExcelUtil<EhsEnterprise>(EhsEnterprise.class);
		util.importComboTemplateExcel(response, "企业信息信息数据");
	}
	/**
	 * 批量导入
	 */
	@PostMapping("/importData")
	public AjaxResult importHubManage(MultipartFile file, Boolean updateSupport) throws Exception {
		ExcelUtil<EhsEnterprise> util = new ExcelUtil<>(EhsEnterprise.class);
		List<EhsEnterprise> list = util.importExcel(file.getInputStream());
		String message = ehsEnterpriseService.importEnterprise(list, updateSupport);
		return AjaxResult.success(message);
	}
}
