package org.jeecg.modules.statistics.controller;

import java.text.DecimalFormat;
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 com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.device.entity.BuDevice;
import org.jeecg.modules.device.service.IBuDeviceRunTimeService;
import org.jeecg.modules.device.service.IBuDeviceService;
import org.jeecg.modules.statistics.entity.BuDeviceEnergyStatistics;
import org.jeecg.modules.statistics.entity.BuDeviceStatistics;
import org.jeecg.modules.statistics.service.IBuDeviceEnergyStatisticsService;

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.jeecg.modules.statistics.service.IBuDeviceStatisticsService;
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: bu_device_energy_statistics
 * @Author: jeecg-boot
 * @Date:   2024-05-09
 * @Version: V1.0
 */
@Api(tags="bu_device_energy_statistics")
@RestController
@RequestMapping("/statistics/buDeviceEnergyStatistics")
@Slf4j
public class BuDeviceEnergyStatisticsController extends JeecgController<BuDeviceEnergyStatistics, IBuDeviceEnergyStatisticsService> {
	@Autowired
	private IBuDeviceEnergyStatisticsService buDeviceEnergyStatisticsService;
	@Autowired
	private IBuDeviceStatisticsService buDeviceStatisticsService;
	@Autowired
	private IBuDeviceService buDeviceService;
	@Autowired
	private IBuDeviceRunTimeService buDeviceRunTimeService;

	/**
	 * 分页列表查询
	 *
	 * @param buDeviceEnergyStatistics
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "bu_device_energy_statistics-分页列表查询")
	@ApiOperation(value="bu_device_energy_statistics-分页列表查询", notes="bu_device_energy_statistics-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BuDeviceEnergyStatistics>> queryPageList(BuDeviceEnergyStatistics buDeviceEnergyStatistics,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BuDeviceEnergyStatistics> queryWrapper = QueryGenerator.initQueryWrapper(buDeviceEnergyStatistics, req.getParameterMap());
		Page<BuDeviceEnergyStatistics> page = new Page<BuDeviceEnergyStatistics>(pageNo, pageSize);
		IPage<BuDeviceEnergyStatistics> pageList = buDeviceEnergyStatisticsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/lists")
	 public Result<?> queryPageLists(BuDeviceStatistics buDeviceStatistics,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 @RequestParam(name="year", required = false) String year,
									 @RequestParam(name="month", required = false) String month,
									 @RequestParam(name="deviceId", required = false) String deviceId,
									 HttpServletRequest req) {
		 QueryWrapper<BuDeviceStatistics> queryWrapper = QueryGenerator.initQueryWrapper(buDeviceStatistics, req.getParameterMap());
		 if (year != null){
			 queryWrapper.eq("year(open_time)",year);
		 }
		 if (month != null){
			 queryWrapper.eq("month(open_time)",month);
		 }
		 if (deviceId != null){
			 queryWrapper.eq("device_id",deviceId);
		 }
		 Page<BuDeviceStatistics> page = new Page<BuDeviceStatistics>(pageNo, pageSize);
		 IPage<BuDeviceStatistics> pageList = buDeviceStatisticsService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 @GetMapping(value = "/selectList")
	 public Result<?> selectList(BuDeviceEnergyStatistics buDeviceEnergyStatistics,
								 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								 @RequestParam(name="year", required = true) String year,
								 @RequestParam(name="month", required = true) String month,
								 HttpServletRequest req) {
		 QueryWrapper<BuDeviceEnergyStatistics> queryWrapper = QueryGenerator.initQueryWrapper(buDeviceEnergyStatistics, req.getParameterMap());
//			 queryWrapper.eq("year(open_time)",year);
		 Page<BuDeviceEnergyStatistics> page = new Page<BuDeviceEnergyStatistics>(pageNo, pageSize);
		 IPage<BuDeviceEnergyStatistics> pageList = buDeviceEnergyStatisticsService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  * 根据设备类型查询当前类型下所有设备
	  */
	 @GetMapping(value = "/selectDevices")
	 public Result<?> selectDevices(String type){
		QueryWrapper<BuDevice> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("device_type_id",type);
		List<BuDevice> list = buDeviceService.list(queryWrapper);
		return Result.OK(list);
	 }


	 // 设备能耗统计按年、月柱形图数据分析
//	 @GetMapping(value="/devicePowerStatistics")
//	 public Result<?> devicePowerStatistics(
//			 @RequestParam(name="year",required = true) String year,
//			 @RequestParam(name = "deviceId",required = false) String deviceId){
//		 JSONObject obj = new JSONObject();
//		 List<String> x = new ArrayList<>();
//		 List<String> y = new ArrayList<>();
//		 for (int i=1; i<13; i++){
//		 	String a = null;
//		 	if (deviceId == "" || deviceId == null){
//				a = buDeviceEnergyStatisticsService.lineSystemStatisticsNoId(year,i);
//			}else {
//				a = buDeviceEnergyStatisticsService.lineSystemStatistics(year,deviceId,i);
//			}
//		 	if (a == null){
//		 		y.add("0");
//		 	}else {
//		 		y.add(a);
//		 	}
//		 	x.add(i + "月");
//		 }
//		 obj.put("x",x);
//		 obj.put("y",y);
//		 return Result.OK(obj);
//	 }
//	 @GetMapping(value="/monthPowerStatistics")
//	 public Result<?> monthPowerStatistics(
//			 @RequestParam(name="month",required = true) String month,
//			 @RequestParam(name="year",required = true) String year,
//			 @RequestParam(name = "deviceId",required = true) String deviceId){
//		 JSONObject obj = new JSONObject();
//		 List<String> x = new ArrayList<>();
//		 List<String> y = new ArrayList<>();
//		 int length = 0;
//		 if (month.equals("02")){
//			 if ((Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0) || (Integer.parseInt(year) % 400 == 0)){
//				 length = 29;
//			 }else {
//				 length = 28;
//			 }
//		 }else if (month.equals("04") || month.equals("06") || month.equals("09") || month.equals("11")){
//			 length = 30;
//		 }else {
//			 length = 31;
//		 }
//		 for (int i=1; i<length+1; i++){
//			 String day  = buDeviceEnergyStatisticsService.lineSystemStatistics1(year,month,deviceId,i);
//			 if (day == null){
//				 y.add("0");
//			 }else {
//				 y.add(day);
//			 }
//			 x.add(String.valueOf(i) + "日");
//		 }
//		 obj.put("x",x);
//		 obj.put("y",y);
//		 return Result.OK(obj);
//	 }

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

	/**
	 *  编辑
	 *
	 * @param buDeviceEnergyStatistics
	 * @return
	 */
	@AutoLog(value = "bu_device_energy_statistics-编辑")
	@ApiOperation(value="bu_device_energy_statistics-编辑", notes="bu_device_energy_statistics-编辑")
	//@RequiresPermissions("org.jeecg.modules:bu_device_energy_statistics:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BuDeviceEnergyStatistics buDeviceEnergyStatistics) {
		int u = 380;
		int i1 = Integer.parseInt(buDeviceEnergyStatistics.getInvalidElectric());
		int i2 = Integer.parseInt(buDeviceEnergyStatistics.getUsefulElectric());
		int time1 = Integer.parseInt(buDeviceEnergyStatistics.getInvalidTime());
		int time2 = Integer.parseInt(buDeviceEnergyStatistics.getUsefulTime());
		long invalidPower = u * i1 * time1;
		long usePower = u * i2 * time2;
		buDeviceEnergyStatistics.setTotalInvalidPower(String.valueOf(invalidPower /60 /60));
		buDeviceEnergyStatistics.setTotalUsefulPower(String.valueOf(usePower /60 /60));
		buDeviceEnergyStatistics.setTotalPower(String.valueOf((invalidPower + usePower) /60 /60));
		buDeviceEnergyStatisticsService.updateById(buDeviceEnergyStatistics);
		return Result.OK("编辑成功!");
	}

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

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

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

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

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

}
