package com.ruoyi.hv.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.apache.commons.lang3.StringUtils;
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.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.hv.domain.DHeightValuePatentDetail;
import com.ruoyi.hv.service.IDHeightValuePatentDetailExcel;
import com.ruoyi.hv.service.IDHeightValuePatentDetailService;

/**
 * 高价值专利详情Controller
 *
 * @author ruoyi
 * @date 2023-04-02
 */
@RestController
@RequestMapping("/highval/dHeightValuePatentDetail")
public class DHeightValuePatentDetailController extends BaseController {
    private final IDHeightValuePatentDetailService dHeightValuePatentDetailService;
    

    @Autowired
    private  IDHeightValuePatentDetailExcel dHeightValuePatentDetailExcel;

    public DHeightValuePatentDetailController(IDHeightValuePatentDetailService dHeightValuePatentDetailService) {
        this.dHeightValuePatentDetailService = dHeightValuePatentDetailService;
    }


    /**
     * 查询高价值专利详情列表
     *
     * @param dHeightValuePatentDetail 高价值专利详情
     * @return 高价值专利详情集合
     * @date 2023-04-02
     */
    @PreAuthorize("@ss.hasPermi('highval:dHeightValuePatentDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(DHeightValuePatentDetail dHeightValuePatentDetail) {
        startPage();
        if(StringUtils.isNotBlank(dHeightValuePatentDetail.getAppliCity())) {
        	dHeightValuePatentDetail.setModifyAppliCity(dHeightValuePatentDetail.getAppliCity());
        	dHeightValuePatentDetail.setAppliCity(null);
        }
        List<DHeightValuePatentDetail> list = dHeightValuePatentDetailService.selectList(dHeightValuePatentDetail);
        
        
//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			dHeightValuePatentDetailExcel.doExport(out, dHeightValuePatentDetail, 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 getDataTable(list);
    }


    /**
     * 导出高价值专利详情列表
     *
     * @param dHeightValuePatentDetail 高价值专利详情
     * @date 2023-04-02
     */
    @PreAuthorize("@ss.hasPermi('highval:dHeightValuePatentDetail:export')")
    @Log(title = "高价值专利详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DHeightValuePatentDetail dHeightValuePatentDetail) {
//        startPage();
        if(StringUtils.isNotBlank(dHeightValuePatentDetail.getAppliCity())) {
        	dHeightValuePatentDetail.setModifyAppliCity(dHeightValuePatentDetail.getAppliCity());
        	dHeightValuePatentDetail.setAppliCity(null);
        }
        List<DHeightValuePatentDetail> list = dHeightValuePatentDetailService.selectList(dHeightValuePatentDetail);
//        return getDataTable(list);
    
        
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			dHeightValuePatentDetailExcel.doExport(response.getOutputStream(), dHeightValuePatentDetail, 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-04-02
     */
    @PreAuthorize("@ss.hasPermi('highval:dHeightValuePatentDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dHeightValuePatentDetailService.getById(id));
    }


    /**
     * 新增高价值专利详情信息
     *
     * @param dHeightValuePatentDetail 高价值专利详情
     * @return 结果
     * @date 2023-04-02
     */
    @PreAuthorize("@ss.hasPermi('highval:dHeightValuePatentDetail:add')")
    @Log(title = "高价值专利详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DHeightValuePatentDetail dHeightValuePatentDetail) {
        return toAjax(dHeightValuePatentDetailService.save(dHeightValuePatentDetail));
    }


    /**
     * 修改高价值专利详情信息
     *
     * @param dHeightValuePatentDetail 高价值专利详情
     * @return 结果
     * @date 2023-04-02
     */
    @PreAuthorize("@ss.hasPermi('highval:dHeightValuePatentDetail:edit')")
    @Log(title = "高价值专利详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DHeightValuePatentDetail dHeightValuePatentDetail) {
        return toAjax(dHeightValuePatentDetailService.updateById(dHeightValuePatentDetail));
    }


    /**
     * 删除高价值专利详情信息
     *
     * @param ids 主键ids
     * @return 结果
     * @date 2023-04-02
     */
    @PreAuthorize("@ss.hasPermi('highval:dHeightValuePatentDetail:remove')")
    @Log(title = "高价值专利详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dHeightValuePatentDetailService.removeByIds(Arrays.asList(ids)));
    }


}
