package com.ruoyi.madridmark.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
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.madridmark.domain.CMadridmark;
import com.ruoyi.madridmark.service.ICMadridmarkExcel;
import com.ruoyi.madridmark.service.ICMadridmarkService;
import com.ruoyi.madridmark.vo.CMadridmarkDTO;

/**
 * 马德里商标Controller
 *
 * @author ruoyi
 * @date 2023-04-29
 */
@RestController
@RequestMapping("/madridmark/cMadridmark")
public class CMadridmarkController extends BaseController {
	private final ICMadridmarkService cMadridmarkService;

	@Autowired
	private ICMadridmarkExcel cMadridmarkExcel;

	public CMadridmarkController(ICMadridmarkService cMadridmarkService) {
		this.cMadridmarkService = cMadridmarkService;
	}

	/**
	 * 查询马德里商标列表
	 *
	 * @param cMadridmark 马德里商标
	 * @return 马德里商标集合
	 * @date 2023-04-29
	 */
	@PreAuthorize("@ss.hasPermi('madridmark:cMadridmark:list')")
	@GetMapping("/list")
	public TableDataInfo list(CMadridmark cMadridmark) {
		startPage();
		List<CMadridmark> list = cMadridmarkService.queryList(cMadridmark);

		return getDataTable(list);
	}

	@PreAuthorize("@ss.hasPermi('madridmark:cMadridmark:list')")
	@GetMapping("/listReport")
	public AjaxResult listReport(CMadridmark cMadridmark) {

		if (StringUtils.isBlank(cMadridmark.getEndDate()) && StringUtils.isNotBlank(cMadridmark.getYear())
				&& StringUtils.isNotBlank(cMadridmark.getMonth())) {
			cMadridmark.setEndDate(cMadridmark.getYear() + cMadridmark.getMonth());
		}
		if (StringUtils.isBlank(cMadridmark.getEndDate())) {
			cMadridmark.setEndDate(new SimpleDateFormat("yyyy").format(new Date()) + "12");
		}

		String endDate = cMadridmark.getEndDate().replace("-", "");
		String month = endDate.substring(4, 6);
		int endYear = Integer.parseInt(cMadridmark.getEndDate().substring(0, 4));
		int beginYear = endYear - 6;
		String beginDate = beginYear + "1216";
		endDate = endYear + month + "15";

		cMadridmark.setBeginDate(beginDate);
		cMadridmark.setEndDate(endDate);

		if (StringUtils.isNotBlank(cMadridmark.getAppliCity()) && !"53".equals(cMadridmark.getAppliCity().trim())) {
			cMadridmark.setCityCode(cMadridmark.getAppliCity().trim());
		}

		CMadridmarkDTO dto = cMadridmarkService.listReport(cMadridmark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cMadridmarkExcel.exportReport(out, cMadridmark, dto);
//
//		} 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 AjaxResult.success(dto);
	}

	/**
	 * 导出马德里商标列表
	 *
	 * @param cMadridmark 马德里商标
	 * @date 2023-04-29
	 */
	@PreAuthorize("@ss.hasPermi('madridmark:cMadridmark:export')")
	@Log(title = "马德里商标", businessType = BusinessType.EXPORT)
	@PostMapping("/exportReport")
	public void exportReport(HttpServletResponse response, CMadridmark cMadridmark) {

		if (StringUtils.isBlank(cMadridmark.getEndDate()) && StringUtils.isNotBlank(cMadridmark.getYear())
				&& StringUtils.isNotBlank(cMadridmark.getMonth())) {
			cMadridmark.setEndDate(cMadridmark.getYear() + cMadridmark.getMonth());
		}
		if (StringUtils.isBlank(cMadridmark.getEndDate())) {
			cMadridmark.setEndDate(new SimpleDateFormat("yyyy").format(new Date()) + "12");
		}

		String endDate = cMadridmark.getEndDate().replace("-", "");
		String month = endDate.substring(4, 6);
		int endYear = Integer.parseInt(cMadridmark.getEndDate().substring(0, 4));
		int beginYear = endYear - 6;
		String beginDate = beginYear + "1216";
		endDate = endYear + month + "15";

		cMadridmark.setBeginDate(beginDate);
		cMadridmark.setEndDate(endDate);

		if (StringUtils.isNotBlank(cMadridmark.getAppliCity()) && !"53".equals(cMadridmark.getAppliCity().trim())) {
			cMadridmark.setCityCode(cMadridmark.getAppliCity().trim());
		}

		CMadridmarkDTO dto = cMadridmarkService.listReport(cMadridmark);
//		return AjaxResult.success(dto);

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

		try {

			cMadridmarkExcel.exportReport(response.getOutputStream(), cMadridmark, dto);
		} 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-29
	 */
	@PreAuthorize("@ss.hasPermi('madridmark:cMadridmark:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(cMadridmarkService.getById(id));
	}

	/**
	 * 新增马德里商标信息
	 *
	 * @param cMadridmark 马德里商标
	 * @return 结果
	 * @date 2023-04-29
	 */
	@PreAuthorize("@ss.hasPermi('madridmark:cMadridmark:add')")
	@Log(title = "马德里商标", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody CMadridmark cMadridmark) {
		return toAjax(cMadridmarkService.save(cMadridmark));
	}

	/**
	 * 修改马德里商标信息
	 *
	 * @param cMadridmark 马德里商标
	 * @return 结果
	 * @date 2023-04-29
	 */
	@PreAuthorize("@ss.hasPermi('madridmark:cMadridmark:edit')")
	@Log(title = "马德里商标", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody CMadridmark cMadridmark) {
		return toAjax(cMadridmarkService.updateById(cMadridmark));
	}

	/**
	 * 删除马德里商标信息
	 *
	 * @param ids 主键ids
	 * @return 结果
	 * @date 2023-04-29
	 */
	@PreAuthorize("@ss.hasPermi('madridmark:cMadridmark:remove')")
	@Log(title = "马德里商标", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(cMadridmarkService.removeByIds(Arrays.asList(ids)));
	}

}
