package com.jiayue.ipp.idp.controller.an;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.ipp.common.data.entity.ElectricField;
import com.jiayue.ipp.common.data.entity.an.ReportingRateRecords;
import com.jiayue.ipp.idp.service.ElectricFieldService;
import com.jiayue.ipp.idp.service.an.ReportingRateRecordsService;
import com.jiayue.ipp.idp.util.DateTimeUtil;
import com.jiayue.ipp.idp.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 上报率记录表
 *
 * @author csc code generator
 * @date 2022-12-28 14:49:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/reportingraterecords")
@Api(value = "reportingraterecords", tags = "上报率记录表管理")
public class ReportingRateRecordsController {

    private final ReportingRateRecordsService reportingRateRecordsService;

    private final ElectricFieldService electricFieldService;


    /**
     * 根据上报时间 场站编号  上报文件类型 查询
     *
     * @param stationCode 场站编号
     * @param startTime   上报时间
     * @param endTime     结束时间
     * @param reportType  文件上报类型
     * @return 查询条件
     */
    @ApiOperation(value = "根据场站编号分页查询", notes = "分页查询")
    @PostMapping("/findByStationCodeAndTimeBetweenAndReportType")
    public R findByStationCodeAndTimeBetweenAndReportType(@RequestParam Long currentPage,
                                                          @RequestParam Long pageSize,
                                                          @RequestParam String stationCode,
                                                          @RequestParam Long startTime,
                                                          @RequestParam Long endTime,
                                                          @RequestParam String reportType) {
        Page<ReportingRateRecords> page = new Page<>(currentPage, pageSize);
        page.setMaxLimit((long) -1);
        return R.ok(reportingRateRecordsService.page(page, reportingRateRecordsService.findByStationCodeAndTimeBetweenAndReportType(stationCode, startTime, endTime, reportType)));
    }


    /**
     * 分页查询
     *
     * @param page                 分页对象
     * @param reportingRateRecords 上报率记录表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getReportingRateRecordsPage(Page page, ReportingRateRecords reportingRateRecords) {
        return R.ok(reportingRateRecordsService.page(page, Wrappers.query(reportingRateRecords)));
    }

    /**
     * 通过id查询上报率记录表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(reportingRateRecordsService.getById(id));
    }

    /**
     * 新增上报率记录表
     *
     * @param reportingRateRecords 上报率记录表
     * @return R
     */
    @ApiOperation(value = "新增上报率记录表", notes = "新增上报率记录表")
    // ("新增上报率记录表")
    @PostMapping
    public R save(@RequestBody ReportingRateRecords reportingRateRecords) {
        return R.ok(reportingRateRecordsService.save(reportingRateRecords));
    }

    /**
     * 修改上报率记录表
     *
     * @param reportingRateRecords 上报率记录表
     * @return R
     */
    @ApiOperation(value = "修改上报率记录表", notes = "修改上报率记录表")
    // ("修改上报率记录表")
    @PutMapping
    public R updateById(@RequestBody ReportingRateRecords reportingRateRecords) {
        return R.ok(reportingRateRecordsService.updateById(reportingRateRecords));
    }

    /**
     * 通过id删除上报率记录表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除上报率记录表", notes = "通过id删除上报率记录表")
    // ("通过id删除上报率记录表")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(reportingRateRecordsService.removeById(id));
    }


    /**
     * 首页上报表格
     *
     * @return R
     */
    @ApiOperation(value = "获取当日最新的上报记录", notes = "通过时间获取最新的记录")
    // ("获取当日最新的上报记录")
    @GetMapping("/select")
    public R select() {

        Long now = System.currentTimeMillis();
        Date endTime = DateTimeUtil.getDayLastTime(now);
        Date startTime = DateTimeUtil.getDayStartTime(now);
        List<ElectricField> electricField = electricFieldService.list();
        LambdaQueryWrapper<ReportingRateRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(ReportingRateRecords::getUploadTime, startTime, endTime);
        List<ReportingRateRecords> list = reportingRateRecordsService.list(lambdaQueryWrapper);

        Map<String, List<ReportingRateRecords>> stationCodeMap = list.stream().collect(Collectors.groupingBy(r -> r.getStationCode()));

        List<Map<String, String>> mapList = new ArrayList<>();
        electricField.forEach(e -> {
            Map<String, String> map = new HashMap<>();
            map.put("stationCode", e.getStationCode());
            map.put("stationName", e.getName());
            map.put("dqRate", "0");
            map.put("cdqRate", "0");
            if (stationCodeMap.get(e.getStationCode()) != null && stationCodeMap.get(e.getStationCode()).size() > 0) {
                List<ReportingRateRecords> reportingRateRecordsList = stationCodeMap.get(e.getStationCode());
                Map<String, List<ReportingRateRecords>> fileTypeMap = reportingRateRecordsList.stream().collect(Collectors.groupingBy(r -> r.getFileType()));
                if (fileTypeMap.get("dq") != null && fileTypeMap.get("dq").size() > 0) {
                    List<ReportingRateRecords> reportingRateRecordsListDq = fileTypeMap.get("dq");
                    reportingRateRecordsListDq.stream().sorted(Comparator.comparing(r -> r.getUploadTime()));
                    ;//正常排序
                    ReportingRateRecords reportingRateRecords = reportingRateRecordsListDq.get(0);
                    map.put("dqRate", reportingRateRecords.getReportingRate());
                }
                if (fileTypeMap.get("cdq") != null && fileTypeMap.get("cdq").size() > 0) {
                    List<ReportingRateRecords> reportingRateRecordsListDq = fileTypeMap.get("cdq");
                    reportingRateRecordsListDq.stream().sorted(Comparator.comparing(r -> r.getUploadTime()));
                    ;//正常排序
                    ReportingRateRecords reportingRateRecords = reportingRateRecordsListDq.get(0);
                    map.put("cdqRate", reportingRateRecords.getReportingRate());
                }
            }
            mapList.add(map);

        });

        return R.ok(mapList);
    }

}
