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.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.ExcelExportUtil;
import com.ruoyi.ehsLawEnforce.domain.LawEnforceImportTemplate;
import com.ruoyi.system.service.ISysDictTypeService;
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.ehsLawEnforce.domain.EhsLawEnforcement;
import com.ruoyi.ehsLawEnforce.service.IEhsLawEnforcementService;
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-13
 */
@Api(tags = "行政执法台账管理")
@RestController
@RequestMapping("/ehs/ehsLawEnforce")
public class EhsLawEnforcementController extends BaseController {
    @Autowired
    private IEhsLawEnforcementService ehsLawEnforcementService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询行政执法台账列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsLawEnforce:list')")
    @GetMapping("/list")
    public TableDataInfo list(EhsLawEnforcement ehsLawEnforcement) {
        startPage();
        List<EhsLawEnforcement> list = ehsLawEnforcementService.selectEhsLawEnforcementList(ehsLawEnforcement);
        return getDataTable(list);
    }

    /**
     * 导出行政执法台账列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsLawEnforce:export')")
    @Log(title = "行政执法台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EhsLawEnforcement ehsLawEnforcement) {
        List<EhsLawEnforcement> list = ehsLawEnforcementService.selectEhsLawEnforcementList(ehsLawEnforcement);
        ExcelUtil<EhsLawEnforcement> util = new ExcelUtil<EhsLawEnforcement>(EhsLawEnforcement.class);
        util.exportExcel(response, list, "行政执法台账数据");
    }

    /**
     * 获取行政执法台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsLawEnforce:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ehsLawEnforcementService.selectEhsLawEnforcementById(id));
    }

    /**
     * 新增行政执法台账
     */
    @ApiOperation("新增行政执法台账")
    @PreAuthorize("@ss.hasPermi('ehs:ehsLawEnforce:add')")
    @Log(title = "行政执法台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsLawEnforcement ehsLawEnforcement) {
        return toAjax(ehsLawEnforcementService.save(ehsLawEnforcement));
    }

    /**
     * 修改行政执法台账
     */
    @ApiOperation("修改行政执法台账")
    @PreAuthorize("@ss.hasPermi('ehs:ehsLawEnforce:edit')")
    @Log(title = "行政执法台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsLawEnforcement ehsLawEnforcement) {
        return toAjax(ehsLawEnforcementService.updateById(ehsLawEnforcement));
    }

    /**
     * 删除行政执法台账
     */
    @ApiOperation("删除行政执法台账")
    @PreAuthorize("@ss.hasPermi('ehs:ehsLawEnforce:remove')")
    @Log(title = "行政执法台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ehsLawEnforcementService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 下载批量导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws Exception {
        ExcelUtil<EhsLawEnforcement> util = new ExcelUtil<>(EhsLawEnforcement.class);
        util.importComboTemplateExcel(response, "行政公文台账信息批量导入模板");
    }

    //批量导入 params是classFirst的值
    @PostMapping("/importData")
    public AjaxResult importHubManage(MultipartFile file, Boolean updateSupport, Long params) throws Exception {
        ExcelUtil<EhsLawEnforcement> util = new ExcelUtil<>(EhsLawEnforcement.class);
        List<EhsLawEnforcement> list = util.importExcel(file.getInputStream());
        String message = ehsLawEnforcementService.importData(list, updateSupport, params);
        return AjaxResult.success(message);
    }

}
