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.common.exception.JeecgBootException;
import org.jeecg.modules.diseaseAnalysis.dto.AnalysisLineBatchDTO;
import org.jeecg.modules.diseaseAnalysis.dto.AnalysisLineBatchInfoDTO;
import org.jeecg.modules.diseaseAnalysis.dto.AnalysisLineBatchSaveOrUpdateDTO;
import org.jeecg.modules.diseaseAnalysis.dto.EkRecordsMasterCheckDTO;
import org.jeecg.modules.diseaseAnalysis.entity.AnalysisLineBatch;
import org.jeecg.modules.diseaseAnalysis.service.IAnalysisLineBatchService;
import org.jeecg.modules.diseaseAnalysis.vo.*;
import org.springframework.web.bind.annotation.*;

import org.jeecg.common.system.base.controller.JeecgController;

import java.util.ArrayList;
import java.util.Arrays;
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.setCheckBatchList(checkBatchVOS);
                });
            }
            // 获取是否可编辑列表
            List<Map<String, Object>> isEditList = this.service.getIsEditList(analysisLineBatchIds);
            if (ObjectUtil.isNotEmpty(isEditList)) {
                Map<String, Boolean> isEditMap = isEditList.stream().collect(
                        Collectors.toMap(
                                key -> key.get("id").toString(),
                                value -> !Convert.toBool(value.get("isEdit"))
                        )
                );
                records.forEach(record -> {
                    record.setEdit(isEditMap.get(record.getId()));
                });
            }
        }
        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);
    }
    /**
     * 线路质量分析-其他病害分析-钢轨探伤单元详情检查记录查询
     *
     * @param analysisBatchId 分析批次id
     * @param ekId            检查批次id
     * @param deviceId        线路质量分析-其他病害分析-单元表id
     * @return
     */
    @AutoLog(value = "线路质量分析-其他病害分析-钢轨探伤单元详情检查记录查询")
    @ApiOperation(value = "线路质量分析-其他病害分析-钢轨探伤单元详情检查记录查询", notes = "线路质量分析-其他病害分析-钢轨探伤单元详情检查记录查询")
    @GetMapping(value = "/getAnalysisLineBatchUnitDeviceRailWearInfoList")
    public Result<List<AnalysisLineBatchUnitDeviceRailWearInfoVO>> getAnalysisLineBatchUnitDeviceRailWearInfoList(@RequestParam String analysisBatchId, @RequestParam String ekId, @RequestParam String deviceId) {
        List<AnalysisLineBatchUnitDeviceRailWearInfoVO> list = this.service.getAnalysisLineBatchUnitDeviceRailWearInfoList(analysisBatchId, ekId, deviceId);
        return Result.OK(list);
    }

    //***********************************分析新增编辑接口*************************************************************************************************************
    @AutoLog(value = "其他病害分析-分析批次-新增分析批次")
    @ApiOperation(value = "其他病害分析-分析批次-新增分析批次", notes = "其他病害分析-分析批次-新增分析批次")
    @PostMapping(value = "/saveRecord")
    public Result<String> saveRecord(@RequestBody AnalysisLineBatchSaveOrUpdateDTO dto) {

        if (dto.getStartingMileage().compareTo(dto.getEndMileage()) > 0) {
            throw new JeecgBootException("起始里程不能大于结束里程");
        }
        // 去重
        AnalysisLineBatch exist = this.service.lambdaQuery().eq(AnalysisLineBatch::getAnalysisBatchCode, dto.getAnalysisBatchCode())
                .last("limit 1")
                .one();
        if (exist != null) {
            throw new JeecgBootException("【" + dto.getAnalysisBatchCode() + "】分析批次已存在");
        }

        this.service.saveRecord(dto);

        return Result.OK("操作成功");
    }

    @AutoLog(value = "其他病害分析-分析批次-更新分析批次")
    @ApiOperation(value = "其他病害分析-分析批次-更新分析批次", notes = "其他病害分析-分析批次-更新分析批次")
    @PostMapping(value = "/updateRecord")
    public Result<String> updateRecord(@RequestBody AnalysisLineBatchSaveOrUpdateDTO dto) {
        if (dto.getStartingMileage().compareTo(dto.getEndMileage()) > 0) {
            throw new JeecgBootException("起始里程不能大于结束里程");
        }
        // 去重
        AnalysisLineBatch exist = this.service.lambdaQuery()
                .eq(AnalysisLineBatch::getAnalysisBatchCode, dto.getAnalysisBatchCode())
                .ne(AnalysisLineBatch::getId, dto.getId())
                .last("limit 1")
                .one();
        if (exist != null) {
            throw new JeecgBootException("【" + dto.getAnalysisBatchCode() + "】分析批次已存在");
        }

        this.service.updateRecord(dto);

        return Result.OK("操作成功");
    }

    /**
     * 其他病害分析-分析批次-删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "其他病害分析-分析批次-删除")
    @ApiOperation(value = "其他病害分析-分析批次-删除", notes = "其他病害分析-分析批次-删除")
    @GetMapping(value = "/deleteBatchRecord")
    public Result<String> deleteBatchRecord(@RequestParam(name = "ids", required = true) String ids,@RequestParam(name = "type", required = true) Integer type) {
        List<String> idList = new ArrayList<>();
        if (ids.contains(",")) {
            idList = Arrays.asList(ids.split(","));
        } else {
            idList.add(ids);
        }
        this.service.deleteBatchRecord(idList,type);

        return Result.OK("批量删除成功!");
    }
}
