package com.ktg.iscs.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.StringUtils;
import com.ktg.iscs.domain.IsIsolationPoint;
import com.ktg.iscs.domain.IsLotoStation;
import com.ktg.iscs.domain.IsMap;
import com.ktg.iscs.domain.dto.point.BindingPointDTO;
import com.ktg.iscs.domain.vo.loto.IsLotoStationVO;
import com.ktg.iscs.domain.vo.workarea.PointsMapVO;
import com.ktg.iscs.mapper.IsLotoStationMapper;
import com.ktg.iscs.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 电柜Service业务层处理
 *
 * @author cgj
 * @date 2024-12-19
 */
@Service
public class IsLotoStationServiceImpl extends ServiceImpl<IsLotoStationMapper, IsLotoStation> implements IIsLotoStationService {

    @Autowired
    private IsLotoStationMapper isLotoStationMapper;
    @Autowired
    private IIsIsolationPointService iIsIsolationPointService;
    @Autowired
    private IIsSopPointsService iIsSopPointsService;
    @Autowired
    private IIsJobTicketPointsService iIsJobTicketPointsService;
    @Autowired
    private IIsMapService isMapService;

    @Override
    //@MarsDataScope(workstationAlias = "w", userAlias = "u")
    public Page<IsLotoStationVO> getIsLotoStationPage(IsLotoStation isLotoStation, Page<IsLotoStation> page) {
        return isLotoStationMapper.getIsLotoStationPage(page, isLotoStation);
    }

    @Override
    public List<PointsMapVO> selectLotoMapById(Long lotoId, Long sopId, Long ticketId) {
        // 1.断言校验
        Assert.notNull(lotoId, "请传入电柜id");
        // 2.查询区域信息
        IsLotoStation lotoStation = getById(lotoId);
        // 3.解析map
        List<PointsMapVO> points = new ArrayList<>();
        /*if (lotoStation != null && StringUtils.isNotBlank(lotoStation.getMap())) {
            String map = lotoStation.getMap();
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                points = objectMapper.readValue(map, new TypeReference<List<PointsMapVO>>(){});
                // 4.补充点位name,隔离点类型,危险能量类型
                List<Long> pointIds = points.stream().map(PointsMapVO::getPointId).collect(Collectors.toList());
                List<PointDetailVO> pointDetailList = iIsIsolationPointService.getPointDetailList(pointIds);
                // 5.获取sop信息
                List<IsSopPoints> isSopPoints;
                if (sopId != null) {
                    isSopPoints = iIsSopPointsService.list(Wrappers.<IsSopPoints>lambdaQuery()
                            .eq(IsSopPoints::getSopId, sopId));
                } else {
                    isSopPoints = new ArrayList<>();
                }
                // 6.获取作业票点位数据
                List<IsJobTicketPoints> isJobTicketPoints;
                if (ticketId != null) {
                    isJobTicketPoints = iIsJobTicketPointsService.list(Wrappers.<IsJobTicketPoints>lambdaQuery()
                            .eq(IsJobTicketPoints::getTicketId, ticketId));
                } else {
                    isJobTicketPoints = new ArrayList<>();
                }
                // 6.组装数据
                points.stream().filter(o -> {
                    // 初始化选中状态
                    o.setState(false);
                    // 如果点位信息有，则填充
                    if (!pointDetailList.isEmpty()) {
                        for (PointDetailVO pointDetailVO : pointDetailList) {
                            if (o.getPointId().equals(pointDetailVO.getPointId())) {
                                o.setPointName(pointDetailVO.getPointName());
                                o.setPointType(pointDetailVO.getPointType());
                                o.setPointTypeName(pointDetailVO.getPointTypeName());
                                o.setPowerType(pointDetailVO.getPowerType());
                                o.setPowerTypeName(pointDetailVO.getPowerTypeName());
                                o.setPointIcon(pointDetailVO.getPointIcon());
                                o.setPointPicture(pointDetailVO.getPointPicture());
                                o.setRemark(pointDetailVO.getRemark());
                            }
                        }
                    }
                    // sop如果选中信息有则开始标记
                    if (!isSopPoints.isEmpty()) {
                        for (IsSopPoints isSopPoint : isSopPoints) {
                            if (o.getPointId().equals(isSopPoint.getPointId())) {
                                o.setState(true);
                                // 根据sop的前置节点赋值
                                o.setPrePointId(isSopPoint.getPrePointId());
                            }
                        }
                    }
                    // 作业票如果选中信息有则开始标记
                    if (!isJobTicketPoints.isEmpty()) {
                        for (IsJobTicketPoints ticketPoint : isJobTicketPoints) {
                            if (o.getPointId().equals(ticketPoint.getPointId())) {
                                o.setState(true);
                                // 根据作业票的前置节点赋值
                                o.setPrePointId(ticketPoint.getPrePointId());
                            }
                        }
                    }
                    return true;
                }).collect(Collectors.toList());
            } catch (IOException e) {
                Assert.isTrue(false, "map数据解析失败！");
                e.printStackTrace();
            }
        }*/
        return points;
    }

    @Override
    public Boolean updatePointsBindingLoto(BindingPointDTO dto) {
        Assert.notNull(dto.getLotoId(), "电柜ID不能为空!");
        if (!dto.getBindingPointIds().isEmpty()) {
            // 开始绑定
            iIsIsolationPointService.update(Wrappers.<IsIsolationPoint>lambdaUpdate()
                    .in(IsIsolationPoint::getPointId, dto.getBindingPointIds())
                    .set(IsIsolationPoint::getLotoId, dto.getLotoId()));
        }
        if (!dto.getUnbindPointIds().isEmpty()) {
            // 开始解绑
            iIsIsolationPointService.update(Wrappers.<IsIsolationPoint>lambdaUpdate()
                    .in(IsIsolationPoint::getPointId, dto.getUnbindPointIds())
                    .set(IsIsolationPoint::getLotoId, null));
        }
        return true;
    }

    @Override
    public Boolean updateIsLotoStation(IsLotoStation isLotoStation) {
        Assert.notNull(isLotoStation.getLotoId(), "主键id不能为空！");
        // 判断名称重复
        List<IsLotoStation> list = list(Wrappers.<IsLotoStation>lambdaQuery()
                .eq(IsLotoStation::getLotoName, isLotoStation.getLotoName())
                .ne(IsLotoStation::getLotoId, isLotoStation.getLotoId()));
        Assert.isTrue(list.isEmpty(), "该名称已被使用，请重新填写！");
        // 判断序列号重复
        List<IsLotoStation> list1 = list(Wrappers.<IsLotoStation>lambdaQuery()
                .eq(IsLotoStation::getLotoSerialNumber, isLotoStation.getLotoSerialNumber())
                .ne(IsLotoStation::getLotoId, isLotoStation.getLotoId()));
        Assert.isTrue(list1.isEmpty(), "该序列号已被使用，请重新填写！");
        update(Wrappers.<IsLotoStation>lambdaUpdate()
                .eq(IsLotoStation::getLotoId, isLotoStation.getLotoId())
                .set(IsLotoStation::getLotoName, isLotoStation.getLotoName())
                .set(IsLotoStation::getLotoSerialNumber, isLotoStation.getLotoSerialNumber())
                .set(IsLotoStation::getOrderNum, isLotoStation.getOrderNum())
                .set(IsLotoStation::getMapId, isLotoStation.getMapId() != null ? isLotoStation.getMapId() : null)
                .set(IsLotoStation::getWorkstationId, isLotoStation.getWorkstationId() != null ? isLotoStation.getWorkstationId() : null)
                .set(IsLotoStation::getMap, StringUtils.isNotBlank(isLotoStation.getMap()) ? isLotoStation.getMap() : null)
                .set(IsLotoStation::getMotorMapId, isLotoStation.getMotorMapId() != null ? isLotoStation.getMotorMapId() : null));
        return true;
    }

    @Override
    public Boolean insertIsLotoStation(IsLotoStation isLotoStation) {
        Assert.notBlank(isLotoStation.getLotoName(), "名称不可为空！");
        // 判断名称重复
        List<IsLotoStation> list = list(Wrappers.<IsLotoStation>lambdaQuery()
                .eq(IsLotoStation::getLotoName, isLotoStation.getLotoName()));
        Assert.isTrue(list.isEmpty(), "该名称已被使用，请重新填写！");
        // 判断序列号重复
        List<IsLotoStation> list1 = list(Wrappers.<IsLotoStation>lambdaQuery()
                .eq(IsLotoStation::getLotoSerialNumber, isLotoStation.getLotoSerialNumber()));
        Assert.isTrue(list1.isEmpty(), "该序列号已被使用，请重新填写！");
        return save(isLotoStation);
    }

    @Override
    public IsLotoStation selectIsLotoStationById(Long lotoId) {
        IsLotoStation byId = getById(lotoId);
        if (byId != null && byId.getMapId() != null) {
            IsMap map = isMapService.getById(byId.getMapId());
            if (map != null) {
                byId.setMapName(map.getName());
            }
        }
        return byId;
    }

    @Override
    public IsLotoStation selectLotoBySerialNumber(String lotoSerialNumber) {
        Assert.notBlank(lotoSerialNumber, "请提供loto柜序列号！");
        IsLotoStation one = getOne(Wrappers.<IsLotoStation>lambdaQuery()
                .eq(IsLotoStation::getLotoSerialNumber, lotoSerialNumber));
        return one;
    }
}
