package com.ruoyi.agentrep.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.agentrep.domain.DAgentAuthInfo;
import com.ruoyi.agentrep.service.IDAgentAuthInfoExcel;
import com.ruoyi.agentrep.service.IDAgentAuthInfoService;
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;

/**
 * 代理机构报Controller
 *
 * @author ruoyi
 * @date 2023-04-02
 */
@RestController
@RequestMapping("/agentrep/dAgentAuthInfo")
public class DAgentAuthInfoController extends BaseController {

	private final IDAgentAuthInfoService dAgentAuthInfoService;

	@Autowired
	private IDAgentAuthInfoExcel dAgentAuthInfoExcel;

	public DAgentAuthInfoController(IDAgentAuthInfoService dAgentAuthInfoService) {
		this.dAgentAuthInfoService = dAgentAuthInfoService;
	}

	/**
	 * 查询代理机构报列表
	 *
	 * @param dAgentAuthInfo 代理机构报
	 * @return 代理机构报集合
	 * @date 2023-04-02
	 */
	@PreAuthorize("@ss.hasPermi('agentrep:dAgentAuthInfo:list')")
	@GetMapping("/list")
	public TableDataInfo list(DAgentAuthInfo dAgentAuthInfo) {
//        startPage();
//    	private String beginDate;
//    	@TableField(exist = false)
//    	private String endDate;

		String beginDate = dAgentAuthInfo.getBeginDate();
		String endDate = dAgentAuthInfo.getEndDate();
		
		
		if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
			dAgentAuthInfo.setCountTimeStr(beginDate+"至"+endDate);
			beginDate = beginDate.replaceAll("-", "");
			endDate = endDate.replaceAll("-", "");
		}
		dAgentAuthInfo.setBeginDate(beginDate);
		dAgentAuthInfo.setEndDate(endDate);

		List<DAgentAuthInfo> list = dAgentAuthInfoService.queryAgentRep(dAgentAuthInfo);

//        Collections.sort(list,new Comparator<DAgentAuthInfo>() {
//			@Override
//			public int compare(DAgentAuthInfo o1, DAgentAuthInfo o2) {
//				// TODO Auto-generated method stub
//				return o2.getPubTotal()-o1.getPubTotal();
//			}
//		});

		for (int i = 1; i <= list.size(); i++) {
			list.get(i - 1).setRanking(i);
		}
		
		
//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			dAgentAuthInfoExcel.doExport(out, dAgentAuthInfo, 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 dAgentAuthInfo 代理机构报
	 * @date 2023-04-02
	 */
	@PreAuthorize("@ss.hasPermi('agentrep:dAgentAuthInfo:export')")
	@PostMapping("/export")
	public void export(HttpServletResponse response, DAgentAuthInfo dAgentAuthInfo) {

//      startPage();
//  	private String beginDate;
//  	@TableField(exist = false)
//  	private String endDate;

		String beginDate = dAgentAuthInfo.getBeginDate();
		String endDate = dAgentAuthInfo.getEndDate();
		if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
			dAgentAuthInfo.setCountTimeStr(beginDate+"至"+endDate);
			beginDate = beginDate.replaceAll("-", "");
			endDate = endDate.replaceAll("-", "");
		}
		dAgentAuthInfo.setBeginDate(beginDate);
		dAgentAuthInfo.setEndDate(endDate);

		List<DAgentAuthInfo> list = dAgentAuthInfoService.queryAgentRep(dAgentAuthInfo);

//      Collections.sort(list,new Comparator<DAgentAuthInfo>() {
//			@Override
//			public int compare(DAgentAuthInfo o1, DAgentAuthInfo o2) {
//				// TODO Auto-generated method stub
//				return o2.getPubTotal()-o1.getPubTotal();
//			}
//		});

		for (int i = 1; i <= list.size(); i++) {
			list.get(i - 1).setRanking(i);
		}

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");
		try {
//  		rzgApplicantRankingInfoExcel.doExport(response.getOutputStream(), rzgApplicantRankingInfo,list);
			dAgentAuthInfoExcel.doExport(response.getOutputStream(), dAgentAuthInfo, list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

//      return getDataTable(list);

	}

	/**
	 * 获取代理机构报详细信息
	 *
	 * @param id 主键id
	 * @return 结果
	 * @date 2023-04-02
	 */
	@PreAuthorize("@ss.hasPermi('agentrep:dAgentAuthInfo:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(dAgentAuthInfoService.getById(id));
	}

	/**
	 * 新增代理机构报信息
	 *
	 * @param dAgentAuthInfo 代理机构报
	 * @return 结果
	 * @date 2023-04-02
	 */
	@PreAuthorize("@ss.hasPermi('agentrep:dAgentAuthInfo:add')")
	@Log(title = "代理机构报", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody DAgentAuthInfo dAgentAuthInfo) {
		return toAjax(dAgentAuthInfoService.save(dAgentAuthInfo));
	}

	/**
	 * 修改代理机构报信息
	 *
	 * @param dAgentAuthInfo 代理机构报
	 * @return 结果
	 * @date 2023-04-02
	 */
	@PreAuthorize("@ss.hasPermi('agentrep:dAgentAuthInfo:edit')")
	@Log(title = "代理机构报", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody DAgentAuthInfo dAgentAuthInfo) {
		return toAjax(dAgentAuthInfoService.updateById(dAgentAuthInfo));
	}

	/**
	 * 删除代理机构报信息
	 *
	 * @param ids 主键ids
	 * @return 结果
	 * @date 2023-04-02
	 */
	@PreAuthorize("@ss.hasPermi('agentrep:dAgentAuthInfo:remove')")
	@Log(title = "代理机构报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(dAgentAuthInfoService.removeByIds(Arrays.asList(ids)));
	}

}
