package com.ruoyi.powerdistribution.controller;

import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.fastjson.JSON;
import com.ruoyi.powerdistribution.domain.excel.ConstructTeam;
import com.ruoyi.powerdistribution.domain.vo.PdmConstructTeamVo;
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 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.powerdistribution.domain.PdmConstructTeam;
import com.ruoyi.powerdistribution.service.IPdmConstructTeamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.powerdistribution.util.DynamicEasyExcelConstructTeamListener;

/**
 * 配网工程队伍施工小组信息Controller
 * 
 * @author ruoyi
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/powerdistribution/constructTeam")
public class PdmConstructTeamController extends BaseController
{
    @Autowired
    private IPdmConstructTeamService pdmConstructTeamService;

    /**
     * 查询配网工程队伍施工小组信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PdmConstructTeam pdmConstructTeam)
    {
        startPage();
        List<PdmConstructTeam> list = pdmConstructTeamService.selectPdmConstructTeamList(pdmConstructTeam);
        return getDataTable(list);
    }

    /**
     * 导出配网工程队伍施工小组信息列表
     */

    @Log(title = "配网工程队伍施工小组信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PdmConstructTeam pdmConstructTeam)
    {
        List<PdmConstructTeam> list = pdmConstructTeamService.selectPdmConstructTeamList(pdmConstructTeam);
        ExcelUtil<PdmConstructTeam> util = new ExcelUtil<PdmConstructTeam>(PdmConstructTeam.class);
        util.exportExcel(response, list, "配网工程队伍施工小组信息数据");
    }

    /**
     * 获取配网工程队伍施工小组信息详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pdmConstructTeamService.selectPdmConstructTeamById(id));
    }

    /**
     * 新增配网工程队伍施工小组信息
     */

    @Log(title = "配网工程队伍施工小组信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PdmConstructTeamVo pdmConstructTeam)
    {
        return toAjax(pdmConstructTeamService.insertPdmConstructTeam(pdmConstructTeam));
    }

    /**
     * 修改配网工程队伍施工小组信息
     */

    @Log(title = "配网工程队伍施工小组信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PdmConstructTeamVo pdmConstructTeam)
    {
        return toAjax(pdmConstructTeamService.updatePdmConstructTeam(pdmConstructTeam));
    }

    /**
     * 删除配网工程队伍施工小组信息
     */

    @Log(title = "配网工程队伍施工小组信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pdmConstructTeamService.deletePdmConstructTeamByIds(ids));
    }
    @PostMapping("/import")
    public void importExcel(HttpServletRequest request, MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            /**
             *DynamicEasyExcelConstructTeamListener:导入监听类
             * 1：是正文起始行，用于处理合并单元格
             **/
            DynamicEasyExcelConstructTeamListener listener = new DynamicEasyExcelConstructTeamListener(request,1);

            //读取全部sheet使用extraRead(CellExtraTypeEnum.MERGE).doReadAll()
            /**
             * headRowNumber(1)：1是正文起始行
             * CellExtraTypeEnum.MERGE：读取合并单元格
             * CellExtraTypeEnum.COMMENT：读取批注
             * CellExtraTypeEnum.HYPERLINK：读取超链接
             * extraRead(CellExtraTypeEnum.MERGE).doReadAll()：如果多sheet，直接使用这个方法
             **/
            EasyExcel.read(inputStream, ConstructTeam.class,listener).headRowNumber(1).extraRead(CellExtraTypeEnum.MERGE).sheet(0).doRead();
            inputStream.close();
            pdmConstructTeamService.importExcel(listener.getDataList());
            //System.out.println(JSON.toJSON(listener.getDataList()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
