package org.jeecg.modules.diseaseAnalysis.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.diseaseAnalysis.dto.AnalysisLineBatchDTO;
import org.jeecg.modules.diseaseAnalysis.dto.AnalysisLineBatchInfoDTO;
import org.jeecg.modules.diseaseAnalysis.dto.EkRecordsMasterCheckDTO;
import org.jeecg.modules.diseaseAnalysis.entity.AnalysisLineBatch;
import org.jeecg.modules.diseaseAnalysis.entity.AnalysisLineBatchCheckDataMap;
import org.jeecg.modules.diseaseAnalysis.service.IAnalysisLineBatchService;
import org.jeecg.modules.diseaseAnalysis.vo.*;
import org.jeecg.modules.dynamicStaticAnalysis.vo.AnalysisBatchQueryVO;
import org.jeecg.modules.dynamicStaticAnalysis.vo.CheckBatchVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.jeecg.common.system.base.controller.JeecgController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 线路质量分析	-分析批次表 前端控制器
 * </p>
 *
 * @author hkl
 * @since 2024-07-19
 */
@RestController
@RequestMapping("/analysisLineBatch")
public class AnalysisLineBatchController extends JeecgController<AnalysisLineBatch, IAnalysisLineBatchService> {
    /**
     * 线路质量分析-其他病害分析-检查记录(除去线路巡查)-分页列表查询
     * @param pageNo
     * @param pageSize
     * @param dto
     * @return
     */
    @AutoLog(value = "线路质量分析-其他病害分析-检查记录(除去线路巡查)-分页列表查询")
    @ApiOperation(value = "线路质量分析-其他病害分析-检查记录(除去线路巡查)-分页列表查询", notes = "线路质量分析-其他病害分析-检查记录(除去线路巡查)-分页列表查询")
    @GetMapping(value = "/ekList")
    public Result<IPage<EkRecordsMasterCheckVO>> queryPageEkList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                               EkRecordsMasterCheckDTO dto) {
        Page<EkRecordsMasterCheckVO> pageData = new Page<>(pageNo, pageSize);
        pageData = this.service.queryPageEkList(pageData, dto);
        return Result.OK(pageData);
    }
    /**
     * 线路质量分析-其他病害分析-线路巡查检查记录-分页列表查询
     * @param pageNo
     * @param pageSize
     * @param dto
     * @return
     */
    @AutoLog(value = "线路质量分析-其他病害分析-线路巡查检查记录-分页列表查询")
    @ApiOperation(value = "线路质量分析-其他病害分析-线路巡查检查记录-分页列表查询", notes = "线路质量分析-其他病害分析-线路巡查检查记录-分页列表查询")
    @GetMapping(value = "/ekPatrolList")
    public Result<IPage<EkRecordPatrolMasterVO>> queryPageEkPatrolList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                       EkRecordsMasterCheckDTO dto) {
        Page<EkRecordPatrolMasterVO> pageData = new Page<>(pageNo, pageSize);
        pageData = this.service.queryPageEkPatrolList(pageData, dto);
        return Result.OK(pageData);
    }
    /**
     * 线路质量分析-其他病害分析-分页列表查询
     * @param pageNo
     * @param pageSize
     * @param dto
     * @return
     */
    @AutoLog(value = "线路质量分析-其他病害分析-分页列表查询")
    @ApiOperation(value = "线路质量分析-其他病害分析-分页列表查询", notes = "线路质量分析-其他病害分析-分页列表查询")
    @GetMapping(value = "/getPageList")
    public Result<IPage<AnalysisLineBatchVO>> getPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          AnalysisLineBatchDTO dto) {
        Page<AnalysisLineBatchVO> pageData = new Page<>(pageNo, pageSize);
        pageData = this.service.getPageList(pageData, dto);
        // 查询明细
        List<AnalysisLineBatchVO> records = pageData.getRecords();
        if (ObjectUtil.isNotEmpty(records)) {
            // 新增明细
            List<String> analysisLineBatchIds = records.stream().map(AnalysisLineBatchVO::getId).collect(Collectors.toList());
            List<AnalysisLineBatchCheckDataMapVO> checkBatchList = this.service.listRecordDetail(analysisLineBatchIds);
            if (ObjectUtil.isNotEmpty(checkBatchList)) {
                Map<String, List<AnalysisLineBatchCheckDataMapVO>> analysisBatchMap = checkBatchList.stream().collect(Collectors.groupingBy(AnalysisLineBatchCheckDataMapVO::getAnalysisBatchId));
                records.forEach(record -> {
                    List<AnalysisLineBatchCheckDataMapVO> checkBatchVOS = analysisBatchMap.get(record.getId());
                    record.setVoInfoList(checkBatchVOS);
                });
            }
        }
        return Result.OK(pageData);
    }
    /**
     * 线路质量分析-其他病害分析-详情查询-分页列表查询
     * @param pageNo
     * @param pageSize
     * @param dto
     * @return
     */
    @AutoLog(value = "线路质量分析-其他病害分析-详情查询-分页列表查询")
    @ApiOperation(value = "线路质量分析-其他病害分析-详情查询-分页列表查询", notes = "线路质量分析-其他病害分析-详情查询-分页列表查询")
    @GetMapping(value = "/getInfoPageList")
    public Result<IPage<AnalysisLineBatchUnitDeviceVO>> getInfoPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                        AnalysisLineBatchInfoDTO dto) {
        Page<AnalysisLineBatchUnitDeviceVO> pageData = new Page<>(pageNo, pageSize);
        pageData = this.service.getInfoPageList(pageData, dto);
        return Result.OK(pageData);
    }

    /**
     * 线路质量分析-其他病害分析-详情检查记录查询
     * @param analysisBatchId 分析批次id
     * @param ekId 检查批次id
     * @param deviceId 线路质量分析-其他病害分析-单元表id
     * @return
     */
    @AutoLog(value = "线路质量分析-其他病害分析-详情检查记录查询")
    @ApiOperation(value = "线路质量分析-其他病害分析-详情检查记录查询", notes = "线路质量分析-其他病害分析-详情检查记录查询")
    @GetMapping(value = "/getAnalysisLineBatchUnitEkInfoList")
    public Result<List<AnalysisLineBatchUnitDeviceTurnoutInfoVO>> getAnalysisLineBatchUnitEkInfoList(@RequestParam String analysisBatchId,@RequestParam String ekId,@RequestParam String deviceId) {
        List<AnalysisLineBatchUnitDeviceTurnoutInfoVO> list = this.service.getAnalysisLineBatchUnitEkInfoList(analysisBatchId,ekId,deviceId);
        return Result.OK(list);
    }
}
