package com.cmvr.device.service.impl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.cmvr.common.utils.DateUtils;
import com.cmvr.device.domain.DeDeviceRegistration;
import com.cmvr.device.domain.vo.ParameterVo;
import com.cmvr.device.domain.vo.ProfileVo;
import com.cmvr.device.domain.vo.TeMechanicalArmVo;
import com.cmvr.device.mapper.DeDeviceRegistrationMapper;
import org.apache.commons.collections4.list.PredicatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cmvr.device.mapper.DeDeviceProfileMapper;
import com.cmvr.device.domain.DeDeviceProfile;
import com.cmvr.device.service.IDeDeviceProfileService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 设备配置信息Service业务层处理
 * 
 * @author cmvr-iot
 * @date 2025-05-09
 */
@Service
public class DeDeviceProfileServiceImpl implements IDeDeviceProfileService
{
    @Autowired
    private DeDeviceProfileMapper deDeviceProfileMapper;

    @Autowired
    private DeDeviceRegistrationMapper deviceRegistrationMapper;

    /**
     * 根据设备id查询当前设备设备配置信息
     * 
     * @param id 设备id
     * @return 设备配置信息
     */
    @Override
    public List<DeDeviceProfile> selectDeDeviceProfileById(String id)
    {
        //根据设备id查询设备配置信息
        DeDeviceProfile deviceProfile=deDeviceProfileMapper.selectProfileById(id);
        //根据设备id和备注查询设备配置信息
        List<DeDeviceProfile> deviceProfiles=deDeviceProfileMapper
                .selectProfileByIdAndRemark(
                        deviceProfile.getIdDeDeviceRegistration(),
                        deviceProfile.getRemark()
                );
        //打印设备配置信息
        System.out.println(deviceProfiles);
        //返回设备配置信息
        return deviceProfiles;

    }

    @Override
    public List<DeDeviceProfile> selectProfileByIdAndRemark(String id, String remark) {
        return deDeviceProfileMapper.selectProfileByIdAndRemark(id,remark);
    }


    /**
     * 根据设备参数id去修改参数默认值
     * @param mechanicalArmVo 配置参数信息
     * @return 结果
     */
    @Override
    public int updateProfileByIdAndByName(TeMechanicalArmVo mechanicalArmVo) {
        if (mechanicalArmVo == null || StringUtils.isEmpty(mechanicalArmVo.getIdDeDeviceRegistration())) {
            return 0;
        }
        int totalUpdated = 0;
        String deviceId = mechanicalArmVo.getIdDeDeviceRegistration();
        // 使用 Map 收集非空属性
        Map<String, Object> fieldMap = new HashMap<>();
        if (mechanicalArmVo.getWorkModule() != null) fieldMap.put("workModule", mechanicalArmVo.getWorkModule());
        if (mechanicalArmVo.getPosStep() != null) fieldMap.put("posStep", mechanicalArmVo.getPosStep());
        if (mechanicalArmVo.getJointStep() != null) fieldMap.put("jointStep", mechanicalArmVo.getJointStep());
        if (mechanicalArmVo.getJoint1() != null) fieldMap.put("joint1", mechanicalArmVo.getJoint1());
        if (mechanicalArmVo.getJoint2() != null) fieldMap.put("joint2", mechanicalArmVo.getJoint2());
        if (mechanicalArmVo.getJoint3() != null) fieldMap.put("joint3", mechanicalArmVo.getJoint3());
        if (mechanicalArmVo.getJoint4() != null) fieldMap.put("joint4", mechanicalArmVo.getJoint4());
        if (mechanicalArmVo.getJoint5() != null) fieldMap.put("joint5", mechanicalArmVo.getJoint5());
        if (mechanicalArmVo.getJoint6() != null) fieldMap.put("joint6", mechanicalArmVo.getJoint6());
        if (mechanicalArmVo.getX() != null) fieldMap.put("x", mechanicalArmVo.getX());
        if (mechanicalArmVo.getY() != null) fieldMap.put("y", mechanicalArmVo.getY());
        if (mechanicalArmVo.getZ() != null) fieldMap.put("z", mechanicalArmVo.getZ());
        if (mechanicalArmVo.getRx() != null) fieldMap.put("rx", mechanicalArmVo.getRx());
        if (mechanicalArmVo.getRy() != null) fieldMap.put("ry", mechanicalArmVo.getRy());
        if (mechanicalArmVo.getRz() != null) fieldMap.put("rz", mechanicalArmVo.getRz());
        // 遍历 Map 执行更新
        for (Map.Entry<String, Object> entry : fieldMap.entrySet()) {
            totalUpdated += deDeviceProfileMapper.updateProfileByIdAndByName(
                    deviceId, entry.getKey(), (String)entry.getValue());
        }
        return totalUpdated;
    }

    /**
     * 查询设备配置信息列表
     *
     * @param deDeviceProfile 设备配置信息
     * @return 设备配置信息
     */
    @Override
    public List<ProfileVo> selectDeDeviceProfileList(DeDeviceProfile deDeviceProfile)
    {
        List<ProfileVo> profileVoList=deDeviceProfileMapper.selectAllProfilesVo();
        return profileVoList;
    }

    /**
     * 新增设备配置信息
     * 
     * @param deDeviceProfile 设备配置信息
     * @return 结果
     */
    @Override
    public int insertDeDeviceProfile(List<DeDeviceProfile> deDeviceProfile)
    {
        int i=0;
        for (DeDeviceProfile profile : deDeviceProfile) {
            i=deDeviceProfileMapper.insert(profile);
        }
        return i;
    }

    /**
     * 修改设备配置信息
     * 
     * @param deDeviceProfiles 设备配置信息
     * @return 结果
     */
    @Override
    public int updateDeDeviceProfile(List<DeDeviceProfile> deDeviceProfiles)
    {
        int i=0;
        for (DeDeviceProfile profile : deDeviceProfiles) {
            i=deDeviceProfileMapper.updateDeDeviceProfile(profile);
        }
        return i;
    }

    /**
     * 批量删除设备配置信息
     * 
     * @param ids 需要删除的设备配置信息主键
     * @return 结果
     */
    @Override
    public int deleteDeDeviceProfileByIds(String[] ids)
    {
        return deDeviceProfileMapper.deleteDeDeviceProfileByIds(ids);
    }

    /**
     * 删除设备配置信息信息
     * 
     * @param id 设备配置信息主键
     * @return 结果
     */
    @Override
    public int deleteDeDeviceProfileById(String id)
    {
        return deDeviceProfileMapper.deleteDeDeviceProfileById(id);
    }
}
