package com.ruoyi.dimp.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.adjust.service.IDHighValuePubService;
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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dimp.domain.DHighValueInfo;
import com.ruoyi.dimp.service.IDHighValueInfoExcel;
import com.ruoyi.dimp.service.IDHighValueInfoService;

/**
 * 高价值数据导入Controller
 *
 * @author ruoyi
 * @date 2023-03-19
 */
@RestController
@RequestMapping("/dimp/dHighValueInfo")
public class DHighValueInfoController extends BaseController {
    private final IDHighValueInfoService dHighValueInfoService;
    
    @Autowired
    private IDHighValuePubService dHighValuePubService;
    @Autowired
    private IDHighValueInfoExcel dHighValueInfoExcel;
    public DHighValueInfoController(IDHighValueInfoService dHighValueInfoService) {
        this.dHighValueInfoService = dHighValueInfoService;
    }


    /**
     * 查询高价值数据导入列表
     *
     * @param dHighValueInfo 高价值数据导入
     * @return 高价值数据导入集合
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dHighValueInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(DHighValueInfo dHighValueInfo) {
        startPage();
        List<DHighValueInfo>list=dHighValueInfoService.doList(dHighValueInfo);
        TableDataInfo ts=getDataTable(list);
        
        
//        OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			dHighValueInfoExcel.doExport(out, dHighValueInfo, list);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		
        return ts;
    }
    


    /**
     * 导出高价值数据导出列表
     *
     * @param dHighValueInfo 高价值数据导入
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dHighValueInfo:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DHighValueInfo dHighValueInfo) {
//    	 startPage();
         List<DHighValueInfo>list=dHighValueInfoService.doList(dHighValueInfo);

         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
 		response.setCharacterEncoding("utf-8");

 		try {

 			dHighValueInfoExcel.doExport(response.getOutputStream(), dHighValueInfo, list);
 		} catch (Exception e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 			logger.error("导出失败," + e.getMessage(), e);
 			throw new RuntimeException("导出失败，请联系管理员", e);
 		}
 		         
    }

    /**
     * 获取高价值数据导入详细信息
     *
     * @param id 主键id
     * @return 结果
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dHighValueInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dHighValueInfoService.getById(id));
    }


    /**
     * 新增高价值数据导入信息
     *
     * @param dHighValueInfo 高价值数据导入
     * @return 结果
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dHighValueInfo:add')")
    @Log(title = "高价值数据导入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DHighValueInfo dHighValueInfo) {
        return toAjax(dHighValueInfoService.save(dHighValueInfo));
    }


    /**
     * 修改高价值数据导入信息
     *
     * @param dHighValueInfo 高价值数据导入
     * @return 结果
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dHighValueInfo:edit')")
    @Log(title = "高价值数据导入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DHighValueInfo dHighValueInfo) {
        return toAjax(dHighValueInfoService.updateById(dHighValueInfo));
    }


    /**
     * 删除高价值数据导入信息
     *
     * @param ids 主键ids
     * @return 结果
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dHighValueInfo:remove')")
    @Log(title = "高价值数据导入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dHighValueInfoService.removeByIds(Arrays.asList(ids)));
    }


}
