package org.jeecg.modules.statistics.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.statistics.entity.BuFaultStatistics;
import org.jeecg.modules.statistics.model.OpenStatistics;
import org.jeecg.modules.statistics.service.IBuFaultStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 设备信息统计表
* @Author: jeecg-boot
* @Date:   2021-01-18
* @Version: V1.0
*/
@Api(tags="设备信息统计表")
@RestController
@RequestMapping("/statistics/buFaultStatistics")
@Slf4j
public class BuFaultStatisticsController extends JeecgController<BuFaultStatistics, IBuFaultStatisticsService> {
   @Autowired
   private IBuFaultStatisticsService buFaultStatisticsService;

   /**
    * 分页列表查询
    *
    * @param buFaultStatistics
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "设备信息统计表-分页列表查询")
   @ApiOperation(value="设备信息统计表-分页列表查询", notes="设备信息统计表-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(BuFaultStatistics buFaultStatistics,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
//       BuFaultStatistics buFaultStatistics1 = buFaultStatisticsService.lineSystemStatistics(buFaultStatistics.getYear());
//       buFaultStatisticsService.save(buFaultStatistics1);
       QueryWrapper<BuFaultStatistics> queryWrapper = QueryGenerator.initQueryWrapper(buFaultStatistics, req.getParameterMap());
       Page<BuFaultStatistics> page = new Page<BuFaultStatistics>(pageNo, pageSize);
       IPage<BuFaultStatistics> pageList = buFaultStatisticsService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param buFaultStatistics
    * @return
    */
   @AutoLog(value = "设备信息统计表-添加")
   @ApiOperation(value="设备信息统计表-添加", notes="设备信息统计表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody BuFaultStatistics buFaultStatistics) {
       buFaultStatisticsService.save(buFaultStatistics);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param buFaultStatistics
    * @return
    */
   @AutoLog(value = "设备信息统计表-编辑")
   @ApiOperation(value="设备信息统计表-编辑", notes="设备信息统计表-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody BuFaultStatistics buFaultStatistics) {
       buFaultStatisticsService.updateById(buFaultStatistics);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "设备信息统计表-通过id删除")
   @ApiOperation(value="设备信息统计表-通过id删除", notes="设备信息统计表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       buFaultStatisticsService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "设备信息统计表-批量删除")
   @ApiOperation(value="设备信息统计表-批量删除", notes="设备信息统计表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.buFaultStatisticsService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }
    // 根据年份按故障类型筛选每月故障数量
    @GetMapping(value="/monthTypeStatistics")
    public Result<?> monthTypeStatistics(@RequestParam(name="year",required = true) String year){
//        List<OpenStatistics> monthOneStatistics = buFaultStatisticsService.monthOneStatistics(year);
//        List<OpenStatistics> monthTwoStatistics = buFaultStatisticsService.monthTwoStatistics(year);
//        Map<Integer, OpenStatistics> monthTwoStatisticsMap = monthTwoStatistics.stream().collect(Collectors.toMap(OpenStatistics::getMonth, openStatistics -> openStatistics));
//        monthOneStatistics.forEach(item->{
//            item.setTypetwocount(monthTwoStatisticsMap.get(item.getMonth()).getTypetwocount());
//        });
        List<OpenStatistics> openStatistics = buFaultStatisticsService.monthTypeStatistics(year);
        return Result.OK(openStatistics);
    }
    // 根据年份月份按故障类型筛选每天故障数量
    @GetMapping(value="/dayTypeStatistics")
    public Result<?> dayTypeStatistics(@RequestParam(name="year",required = true) String year,
                                       @RequestParam(name="month",required = true) String month){
        List<OpenStatistics> openStatistics = buFaultStatisticsService.dayTypeStatistics(year,month);
//        Map<Integer, OpenStatistics typeStatisticsMap = typeStatistics.stream().collect(Collectors.toMap(OpenStatistics::getMonth, openStatistics -> openStatistics));
        return Result.OK(openStatistics);
    }
    // 根据年份按故障系统筛选每月故障数量
    @GetMapping(value="/monthSystemStatistics")
    public Result<?> monthSystemStatistics(@RequestParam(name="year",required = true) String year){
        List<OpenStatistics> openStatistics = buFaultStatisticsService.monthSystemStatistics(year);
        return Result.OK(openStatistics);
    }
    // 根据年份月份按故障系统筛选每天故障数量
    @GetMapping(value="/daySystemStatistics")
    public Result<?> daySystemStatistics(@RequestParam(name="year",required = true) String year,
                                         @RequestParam(name="month",required = true) String month){
        List<OpenStatistics> openStatistics = buFaultStatisticsService.daySystemStatistics(year,month);
        return Result.OK(openStatistics);
    }
    // 根据年份按故障等级筛选每月故障数量
    @GetMapping(value="/monthGradeStatistics")
    public Result<?> monthGradeStatistics(@RequestParam(name="year",required = true) String year){
        List<OpenStatistics> openStatistics = buFaultStatisticsService.monthGradeStatistics(year);
        return Result.OK(openStatistics);
    }
    // 根据年份月份按故障等级筛选每天故障数量
    @GetMapping(value="/dayGradeStatistics")
    public Result<?> dayGradeStatistics(@RequestParam(name="year",required = true) String year,
                                        @RequestParam(name="month",required = true) String month){
        List<OpenStatistics> openStatistics = buFaultStatisticsService.dayGradeStatistics(year,month);
        return Result.OK(openStatistics);
    }

//    @GetMapping(value="/devicePowerStatistics")
//    public Result<?> devicePowerStatistics(@RequestParam(name="year",required = true) String year){
//        List<LineDataStatistics> lineDataStatistics = buFaultStatisticsService.lineSystemStatistics(year);
//        return Result.OK(lineDataStatistics);
//    }


   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "设备信息统计表-通过id查询")
   @ApiOperation(value="设备信息统计表-通过id查询", notes="设备信息统计表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       BuFaultStatistics buFaultStatistics = buFaultStatisticsService.getById(id);
       if(buFaultStatistics==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(buFaultStatistics);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param buFaultStatistics
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, BuFaultStatistics buFaultStatistics) {
       return super.exportXls(request, buFaultStatistics, BuFaultStatistics.class, "设备信息统计表");
   }

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

}
