package com.dcs.hnyz.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcs.common.enums.GeneralStatus;
import com.dcs.common.exception.CustomException;
import com.dcs.hnyz.domain.Equipment;
import com.dcs.hnyz.domain.vo.EquipmentExtensionFormVO;
import com.dcs.hnyz.domain.vo.EquipmentLayoutVO;
import com.dcs.hnyz.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dcs.hnyz.mapper.EquipmentExtensionMapper;
import com.dcs.hnyz.domain.EquipmentExtension;
import com.dcs.hnyz.service.IEquipmentExtensionService;

import javax.annotation.Resource;

/**
 * 设备拓展管理Service业务层处理
 * 
 * @author hmy
 * @date 2025-04-23
 */
@Service
public class EquipmentExtensionServiceImpl implements IEquipmentExtensionService 
{
    @Resource
    private EquipmentExtensionMapper equipmentExtensionMapper;
    @Autowired
    private IEquipmentService equipmentService;
    /**
     * 查询设备拓展管理
     * 
     * @param id 设备拓展管理主键
     * @return 设备拓展管理
     */
    @Override
    public EquipmentExtension selectEquipmentExtensionById(Long id)
    {
        return equipmentExtensionMapper.selectById(id);
    }

    /**
     * 查询设备拓展管理列表
     * 
     * @param equipmentExtension 设备拓展管理
     * @return 设备拓展管理
     */
    @Override
    public List<EquipmentExtension> selectEquipmentExtensionList(EquipmentExtension equipmentExtension)
    {
        return equipmentExtensionMapper.selectEquipmentExtensionList(equipmentExtension);
    }

    /**
     * 新增设备拓展管理
     * 
     * @param equipmentExtension 设备拓展管理
     * @return 结果
     */
    @Override
    public int insertEquipmentExtension(EquipmentExtension equipmentExtension)
    {
        //新增数据时的校验
        Equipment e=new Equipment();
        e.setEquipmentId(equipmentExtension.getEquipmentId());
        e.setFlowIds(String.valueOf(equipmentExtension.getFlowId()));
        List<Equipment> equipmentList = equipmentService.selectEquipmentList(e);
        if(equipmentList.size()==0){
            throw new CustomException("设备与该流程不匹配");
        }
        LambdaQueryWrapper<EquipmentExtension> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(EquipmentExtension::getEquipmentId,equipmentExtension.getEquipmentId());
        wrapper.eq(EquipmentExtension::getFlowId,equipmentExtension.getFlowId());
        EquipmentExtension ee=equipmentExtensionMapper.selectOne(wrapper);
        if(ee!=null) {
            throw new CustomException("该设备对应流程已配置");
        }
        return equipmentExtensionMapper.insert(equipmentExtension);
    }

    /**
     * 修改设备拓展管理
     * 
     * @param equipmentExtension 设备拓展管理
     * @return 结果
     */
    @Override
    public int updateEquipmentExtension(EquipmentExtension equipmentExtension)
    {
        //修改数据时的校验
        Equipment e=new Equipment();
        e.setEquipmentId(equipmentExtension.getEquipmentId());
        e.setFlowIds(String.valueOf(equipmentExtension.getFlowId()));
        List<Equipment> equipmentList = equipmentService.selectEquipmentList(e);
        if(equipmentList.size()==0){
            throw new CustomException("设备与该流程不匹配");
        }
        LambdaQueryWrapper<EquipmentExtension> wrapper=new LambdaQueryWrapper<>();
        wrapper.ne(EquipmentExtension::getId,equipmentExtension.getId());
        wrapper.eq(EquipmentExtension::getEquipmentId,equipmentExtension.getEquipmentId());
        wrapper.eq(EquipmentExtension::getFlowId,equipmentExtension.getFlowId());
        EquipmentExtension ee=equipmentExtensionMapper.selectOne(wrapper);
        if(ee!=null) {
            throw new CustomException("该设备对应流程已配置");
        }
        return equipmentExtensionMapper.updateById(equipmentExtension);
    }

    /**
     * 批量删除设备拓展管理
     * 
     * @param ids 需要删除的设备拓展管理主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentExtensionByIds(Long[] ids)
    {
        return equipmentExtensionMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除设备拓展管理信息
     * 
     * @param id 设备拓展管理主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentExtensionById(Long id)
    {
        return equipmentExtensionMapper.deleteById(id);
    }

    @Override
    public List<EquipmentExtensionFormVO> getEquipmentExtensionFormList(EquipmentExtensionFormVO equipmentExtensionFormVo) {
        return equipmentExtensionMapper.getEquipmentExtensionFormList(equipmentExtensionFormVo);
    }

    @Override
    public int changeStatus(EquipmentExtension equipmentExtension) {
        return equipmentExtensionMapper.updateById(equipmentExtension);
    }

    /**
     * 获取该组态流程页面相关设备的布局参数
     * @param flowId
     * @return
     */
    @Override
    public List<EquipmentLayoutVO> getLayout(Integer flowId) {
        LambdaQueryWrapper<EquipmentExtension> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(EquipmentExtension::getFlowId,flowId);
        wrapper.eq(EquipmentExtension::getStatus, GeneralStatus.ENABLE.getCode());
        List<EquipmentExtension> equipmentExtensions = equipmentExtensionMapper.selectList(wrapper);
        return equipmentExtensions.stream().map(e->{
            String code=equipmentService.selectEquipmentByEquipmentId(e.getEquipmentId()).getCode();
            EquipmentLayoutVO vo=new EquipmentLayoutVO();
            vo.setCode(code);
            vo.setLeft(e.getX());
            vo.setTop(e.getY());
            vo.setSize(e.getSize());
            return vo;
        }).collect(Collectors.toList());
    }
}
