package org.jeecg.modules.itdmWenxiangHistory.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.itdmWenxiangHistory.entity.ItdmWenxiangHistory1;
import org.jeecg.modules.itdmWenxiangHistory.service.IItdmWenxiangHistory1Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 1立方温箱历史数据
 * @Author: jeecg-boot
 * @Date:   2023-07-24
 * @Version: V1.0
 */
@Api(tags="1立方温箱历史数据")
@RestController
@RequestMapping("/itdmWenxiangHistory/itdmWenxiangHistory1")
@Slf4j
public class ItdmWenxiangHistory1Controller extends JeecgController<ItdmWenxiangHistory1, IItdmWenxiangHistory1Service> {
	@Autowired
	private IItdmWenxiangHistory1Service itdmWenxiangHistory1Service;

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param itdmWenxiangHistory1
	  */
	 //@RequiresPermissions("org.jeecg.modules:itdm_wenxiang_history1:exportXls")
	 @RequestMapping(value = "/exportXls")
	 @ApiOperation("导出1立方温箱历史数据")
	 public ModelAndView exportXls(HttpServletRequest request, ItdmWenxiangHistory1 itdmWenxiangHistory1) {
		 return super.exportXls(request, itdmWenxiangHistory1, ItdmWenxiangHistory1.class, "1立方温箱历史数据");
//		 //导出前先判断导出时间区间是否小于等于3个月，不是的话提醒最多只能导出3个月的数据
//		 Map<String, String[]> parameterMap = request.getParameterMap();
//		 if(timeJudge(parameterMap)){
//			 return super.exportXls(request, itdmWenxiangHistory1, ItdmWenxiangHistory1.class, "1立方温箱历史数据");
//		 }else return null;
	 }


	 public Boolean timeJudge(Map<String, String[]> parameterMap){

	 	Boolean jieguo = false;

	 	if(parameterMap.containsKey("createTime_begin") && parameterMap.containsKey("createTime_end")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			String[] createTimeBegins = parameterMap.get("createTime_begin");
			String[] createTimeEnds = parameterMap.get("createTime_end");

			Date date1 = null;
			Date date2 = null;
			try {
				date1 = sdf.parse(createTimeBegins[0]);
				date2 = sdf.parse(createTimeEnds[0]);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			Calendar cal1 = Calendar.getInstance();
			Calendar cal2 = Calendar.getInstance();
			cal1.setTime(date1);
			cal2.setTime(date2);
			int intervalYear = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
			int intervalMonth = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
			int intervalDay = cal2.get(Calendar.DAY_OF_MONTH) - cal1.get(Calendar.DAY_OF_MONTH);
			int totalIntervalMonth = intervalYear * 12 + intervalMonth;

			if (totalIntervalMonth > 3) {
				jieguo = false;
			} else if (totalIntervalMonth == 3) {
				if (intervalDay > 0) {
					jieguo = false;
				}
			}
			jieguo = true;
		}
	 	return jieguo;
	 }

	 /**
	 * 分页列表查询
	 *
	 * @param itdmWenxiangHistory1
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "1立方温箱历史数据-分页列表查询")
	@ApiOperation(value="1立方温箱历史数据-分页列表查询", notes="1立方温箱历史数据-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ItdmWenxiangHistory1>> queryPageList(ItdmWenxiangHistory1 itdmWenxiangHistory1,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		QueryWrapper<ItdmWenxiangHistory1> queryWrapper = QueryGenerator.initQueryWrapper(itdmWenxiangHistory1, req.getParameterMap());
		Page<ItdmWenxiangHistory1> page = new Page<ItdmWenxiangHistory1>(pageNo, pageSize);
		IPage<ItdmWenxiangHistory1> pageList = itdmWenxiangHistory1Service.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param itdmWenxiangHistory1
	 * @return
	 */
	@AutoLog(value = "1立方温箱历史数据-添加")
	@ApiOperation(value="1立方温箱历史数据-添加", notes="1立方温箱历史数据-添加")
	//@RequiresPermissions("org.jeecg.modules:itdm_wenxiang_history1:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ItdmWenxiangHistory1 itdmWenxiangHistory1) {
		itdmWenxiangHistory1Service.save(itdmWenxiangHistory1);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param itdmWenxiangHistory1
	 * @return
	 */
	@AutoLog(value = "1立方温箱历史数据-编辑")
	@ApiOperation(value="1立方温箱历史数据-编辑", notes="1立方温箱历史数据-编辑")
	//@RequiresPermissions("org.jeecg.modules:itdm_wenxiang_history1:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ItdmWenxiangHistory1 itdmWenxiangHistory1) {
		itdmWenxiangHistory1Service.updateById(itdmWenxiangHistory1);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "1立方温箱历史数据-通过id删除")
	@ApiOperation(value="1立方温箱历史数据-通过id删除", notes="1立方温箱历史数据-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_wenxiang_history1:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		itdmWenxiangHistory1Service.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "1立方温箱历史数据-批量删除")
	@ApiOperation(value="1立方温箱历史数据-批量删除", notes="1立方温箱历史数据-批量删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_wenxiang_history1:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.itdmWenxiangHistory1Service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "1立方温箱历史数据-通过id查询")
	@ApiOperation(value="1立方温箱历史数据-通过id查询", notes="1立方温箱历史数据-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ItdmWenxiangHistory1> queryById(@RequestParam(name="id",required=true) String id) {
		ItdmWenxiangHistory1 itdmWenxiangHistory1 = itdmWenxiangHistory1Service.getById(id);
		if(itdmWenxiangHistory1==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(itdmWenxiangHistory1);
	}

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("itdm_wenxiang_history1:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ItdmWenxiangHistory1.class);
    }

}
