package org.jeecg.modules.gijcj.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
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.gijcj.entity.BuZyGijcj_6Online;
import org.jeecg.modules.gijcj.service.IBuZyGijcj_6OnlineService;

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;

import java.sql.SQLSyntaxErrorException;

 /**
 * @Description: bu_zy_gijcj_6_online
 * @Author: jeecg-boot
 * @Date:   2026-01-23
 * @Version: V1.0
 */
@Api(tags="bu_zy_gijcj_6_online")
@RestController
@RequestMapping("/gijcj/buZyGijcj_6Online")
@Slf4j
public class BuZyGijcj_6OnlineController extends JeecgController<BuZyGijcj_6Online, IBuZyGijcj_6OnlineService> {
	@Autowired
	private IBuZyGijcj_6OnlineService buZyGijcj_6OnlineService;

	/**
	 * 分页列表查询
	 *
	 * @param buZyGijcj_6Online
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "bu_zy_gijcj_6_online-分页列表查询")
	@ApiOperation(value="bu_zy_gijcj_6_online-分页列表查询", notes="bu_zy_gijcj_6_online-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BuZyGijcj_6Online>> queryPageList(BuZyGijcj_6Online buZyGijcj_6Online,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		try {
			QueryWrapper<BuZyGijcj_6Online> queryWrapper = QueryGenerator.initQueryWrapper(buZyGijcj_6Online, req.getParameterMap());
			Page<BuZyGijcj_6Online> page = new Page<BuZyGijcj_6Online>(pageNo, pageSize);
			IPage<BuZyGijcj_6Online> pageList = buZyGijcj_6OnlineService.pageListWithCustomFields(page, queryWrapper);
			return Result.OK(pageList);
		} catch (Exception e) {
			log.error("分页查询异常:", e);
			return Result.error("分页查询异常: " + e.getMessage());
		}
	}

	/**
	 * 按月份查询历史数据
	 *
	 * @param month 月份(格式: yyyyMM)
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@AutoLog(value = "bu_zy_gijcj_6_online-按月份查询历史数据")
	@ApiOperation(value="bu_zy_gijcj_6_online-按月份查询历史数据", notes="bu_zy_gijcj_6_online-按月份查询历史数据")
	@GetMapping(value = "/historyList")
	public Result<IPage<BuZyGijcj_6Online>> queryHistoryList(@RequestParam(name="month") String month,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		Page<BuZyGijcj_6Online> page = new Page<BuZyGijcj_6Online>(pageNo, pageSize);
		try
		{
			IPage<BuZyGijcj_6Online> pageList = buZyGijcj_6OnlineService.queryHistoryDataByMonth(page, month);

			// 判断是否有数据
			if (pageList == null || pageList.getRecords().isEmpty()) {
				return Result.error("暂无数据");
			}
			return Result.OK(pageList);
		} catch (Exception e) {
			log.error("查询历史数据异常: ", e);
			// 如果表不存在或其他数据库错误，返回友好提示
			// 检查异常及其原因链中是否包含SQLSyntaxErrorException
			Throwable cause = e;
			while (cause != null) {
				if (cause instanceof SQLSyntaxErrorException && cause.getMessage().contains("doesn't exist")) {
					// 提取表名并加入错误消息中
					String message = cause.getMessage();
					String tableName = "";
					if (message.contains("'")) {
						int start = message.indexOf("'");
						int end = message.indexOf("'", start + 1);
						if (end > start) {
							tableName = message.substring(start + 1, end);
							return Result.error("暂无数据，数据库中不存在表: " + tableName);
						}
					}
					return Result.error("暂无数据");
				}
				cause = cause.getCause();
			}
			return Result.error("数据查询异常，请稍后重试");
		}
	}

	/**
	 * 按时间范围查询历史数据
	 *
	 * @param startTime 开始时间(格式: yyyy-MM-dd HH:mm:ss)
	 * @param endTime 结束时间(格式: yyyy-MM-dd HH:mm:ss)
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@AutoLog(value = "bu_zy_gijcj_6_online-按时间范围查询历史数据")
	@ApiOperation(value="bu_zy_gijcj_6_online-按时间范围查询历史数据", notes="bu_zy_gijcj_6_online-按时间范围查询历史数据")
	@GetMapping(value = "/historyListByTimeRange")
	public Result<IPage<BuZyGijcj_6Online>> queryHistoryListByTimeRange(@RequestParam(name="startTime") String startTime,
								   @RequestParam(name="endTime") String endTime,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		Page<BuZyGijcj_6Online> page = new Page<BuZyGijcj_6Online>(pageNo, pageSize);
		try
		{
			IPage<BuZyGijcj_6Online> pageList = buZyGijcj_6OnlineService.queryHistoryDataByTimeRange(page, startTime, endTime);

			// 判断是否有数据
			if (pageList == null || pageList.getRecords().isEmpty()) {
				return Result.error("暂无数据");
			}
			return Result.OK(pageList);
		} catch (Exception e) {
			log.error("查询历史数据异常: ", e);
			// 如果表不存在或其他数据库错误，返回友好提示
			// 检查异常及其原因链中是否包含SQLSyntaxErrorException
			Throwable cause = e;
			while (cause != null) {
				if (cause instanceof SQLSyntaxErrorException && cause.getMessage().contains("doesn't exist")) {
					// 提取表名并加入错误消息中
					String message = cause.getMessage();
					String tableName = "";
					if (message.contains("'")) {
						int start = message.indexOf("'");
						int end = message.indexOf("'", start + 1);
						if (end > start) {
							tableName = message.substring(start + 1, end);
							return Result.error("暂无数据，数据库中不存在表: " + tableName);
						}
					}
					return Result.error("暂无数据");
				}
				cause = cause.getCause();
			}
			return Result.error("数据查询异常，请稍后重试");
		}
	}

	/**
	 *   添加
	 *
	 * @param buZyGijcj_6Online
	 * @return
	 */
	@AutoLog(value = "bu_zy_gijcj_6_online-添加")
	@ApiOperation(value="bu_zy_gijcj_6_online-添加", notes="bu_zy_gijcj_6_online-添加")
	//@RequiresPermissions("org.jeecg.modules:bu_zy_gijcj_6_online:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BuZyGijcj_6Online buZyGijcj_6Online) {
		buZyGijcj_6OnlineService.save(buZyGijcj_6Online);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param buZyGijcj_6Online
	 * @return
	 */
	@AutoLog(value = "bu_zy_gijcj_6_online-编辑")
	@ApiOperation(value="bu_zy_gijcj_6_online-编辑", notes="bu_zy_gijcj_6_online-编辑")
	//@RequiresPermissions("org.jeecg.modules:bu_zy_gijcj_6_online:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BuZyGijcj_6Online buZyGijcj_6Online) {
		buZyGijcj_6OnlineService.updateById(buZyGijcj_6Online);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "bu_zy_gijcj_6_online-通过id删除")
	@ApiOperation(value="bu_zy_gijcj_6_online-通过id删除", notes="bu_zy_gijcj_6_online-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:bu_zy_gijcj_6_online:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		buZyGijcj_6OnlineService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "bu_zy_gijcj_6_online-批量删除")
	@ApiOperation(value="bu_zy_gijcj_6_online-批量删除", notes="bu_zy_gijcj_6_online-批量删除")
	//@RequiresPermissions("org.jeecg.modules:bu_zy_gijcj_6_online:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.buZyGijcj_6OnlineService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "bu_zy_gijcj_6_online-通过id查询")
	@ApiOperation(value="bu_zy_gijcj_6_online-通过id查询", notes="bu_zy_gijcj_6_online-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BuZyGijcj_6Online> queryById(@RequestParam(name="id",required=true) String id) {
		BuZyGijcj_6Online buZyGijcj_6Online = buZyGijcj_6OnlineService.getById(id);
		if(buZyGijcj_6Online==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(buZyGijcj_6Online);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param buZyGijcj_6Online
    */
    //@RequiresPermissions("org.jeecg.modules:bu_zy_gijcj_6_online:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BuZyGijcj_6Online buZyGijcj_6Online) {
        return super.exportXls(request, buZyGijcj_6Online, BuZyGijcj_6Online.class, "bu_zy_gijcj_6_online");
    }

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

}
