package org.jeecg.modules.checkData.face.impl;

import cn.hutool.core.util.ObjectUtil;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.checkData.entity.MovementCourse;
import org.jeecg.modules.checkData.entity.MovementCourseInfo;
import org.jeecg.modules.checkData.face.MovementRecordsMasterFace;
import org.jeecg.modules.checkData.service.IMovementCourseInfoService;
import org.jeecg.modules.checkData.service.IMovementCourseService;
import org.jeecg.modules.utils.RedisLockUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class MovementRecordsMasterFaceImpl implements MovementRecordsMasterFace {

    @Resource
    private IMovementCourseService movementCourseService;

    @Resource
    private IMovementCourseInfoService movementCourseInfoService;


    @Override
    public void saveLabourCheckItem(MovementCourse record) {
        // 保存人工项目检查表
        record.setId(UUIDGenerator.generate());
        record.setDelFlag("0");
        movementCourseService.save(record);


        // 初始化下面的格子模板
        List<MovementCourseInfo> movementCourseInfos = new ArrayList<>();
        int measurePoint = 1;
        String trackCode = "1";
        for (int i = 0; i < 24; i++) {
            MovementCourseInfo movementCourseInfo = new MovementCourseInfo();
            movementCourseInfo.setMovementMasterId(record.getMovementMasterId());
            movementCourseInfo.setMovementCourseId(record.getId());
            movementCourseInfo.setMeasurePoint(measurePoint);
            movementCourseInfo.setInfoSort(i + 1);
            movementCourseInfo.setDelFlag("0");
            movementCourseInfo.setTrackCode(trackCode);
            if (measurePoint % 8 == 0) {
                measurePoint = 1;
                trackCode = String.valueOf(Integer.valueOf(trackCode) + 1);
            } else {
                measurePoint++;
            }
            movementCourseInfos.add(movementCourseInfo);
        }
        movementCourseInfoService.saveBatch(movementCourseInfos);


        /**
         * 人工检查数据排序
         */
        labourCheckItemSort(record.getMovementMasterId());

        /**
         * 人工检查数据单元设备映射
         */
        labourCheckMappingUnitDevice(record.getMovementMasterId());

    }

    @Override
    public void updateLabourCheckItem(MovementCourse record) {
        movementCourseService.lambdaUpdate()
                .set(MovementCourse::getCourseStartingMileage, record.getCourseStartingMileage())
                .set(MovementCourse::getCourseEndMileage, record.getCourseEndMileage())
                .set(MovementCourse::getCurveRadius, record.getCurveRadius())
                .set(MovementCourse::getSuperHigh, record.getSuperHigh())
                .set(MovementCourse::getWiden, record.getWiden())
                .set(MovementCourse::getSlopeRatio, record.getSlopeRatio())
                .set(MovementCourse::getLongExtent, record.getLongExtent())
                .eq(MovementCourse::getId, record.getId())
                .update();
    }

    /**
     * 人工检查数据映射单元设备
     *
     * @param movementMasterId
     */
    private void labourCheckMappingUnitDevice(String movementMasterId) {
    }

    /**
     * 人工检查数据
     *
     * @param movementMasterId
     */
    private void labourCheckItemSort(String movementMasterId) {
        // 检查项目list
        List<MovementCourse> movementCourseList = movementCourseService.lambdaQuery()
                .eq(MovementCourse::getMovementMasterId, movementMasterId)
                .orderByAsc(MovementCourse::getCourseStartingMileage)
                .list();


        // 检查项目明细list
        List<MovementCourseInfo> movementCourseInfoList = movementCourseInfoService.lambdaQuery()
                .eq(MovementCourseInfo::getMovementMasterId, movementMasterId)
                .orderByAsc(MovementCourseInfo::getInfoSort)
                .list();
        Map<String, List<MovementCourseInfo>> movementCourseInfoMap = movementCourseInfoList.stream()
                .collect(Collectors.groupingBy(MovementCourseInfo::getMovementCourseId));


        for (int i = 0; i < movementCourseList.size(); i++) {
            int sort = i + 1;
            MovementCourse movementCourse = movementCourseList.get(i);
            movementCourse.setSort(sort);


            // 修改明细的轨号

            List<MovementCourseInfo> movementCourseInfos = movementCourseInfoMap.get(movementCourse.getId());
            int incr = 2;
            int currentCheckNum = i + 1;
            String trackCode = String.valueOf(currentCheckNum * 3 - incr);
            for (int j = 0; j < movementCourseInfos.size(); j++) {
                if (j != 0 && j % 8 == 0) {
                    incr = incr - 1;
                    trackCode = String.valueOf(currentCheckNum * 3 - incr);
                }
                MovementCourseInfo movementCourseInfo = movementCourseInfos.get(j);
                movementCourseInfo.setTrackCode(trackCode);
            }
        }

        movementCourseService.updateBatchById(movementCourseList);
        movementCourseInfoService.updateBatchById(movementCourseInfoList);
    }
}
