package com.bonus.material.devConfig.service.impl;

import com.bonus.material.devConfig.domain.EquipmentProperty;
import com.bonus.material.devConfig.domain.EquipmentPropertyDTO;
import com.bonus.material.devConfig.mapper.EquipmentPropertyMapper;
import com.bonus.material.devConfig.service.EquipmentPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author 30791
 * @version 1.0
 * Create by 2025/10/14 16:29
 */

@Service
public class EquipmentPropertyServiceImpl implements EquipmentPropertyService {
    @Autowired
    private EquipmentPropertyMapper equipmentPropertyMapper;


    @Override
    public List<EquipmentProperty> list() {
        return equipmentPropertyMapper.selectAll();
    }

    @Override
    public EquipmentProperty getById(Long id) {
        return equipmentPropertyMapper.selectById(id);
    }

    @Override
    public List<EquipmentProperty> getByTypeId(Long typeId) {
        return equipmentPropertyMapper.selectByTypeId(typeId);
    }


    @Override
    public List<EquipmentProperty> getByType(Long typeId) {
        return equipmentPropertyMapper.selectByType(typeId);
    }

    @Override
    public void add(EquipmentProperty equipmentProperty) {
//        equipmentProperty.setCreateTime(new Date());
//        equipmentPropertyMapper.insert(equipmentProperty);


        // 1. 根据 property_name 查询是否已存在记录
        EquipmentProperty existingProperty = equipmentPropertyMapper.getByPropertyName(equipmentProperty);

        if (existingProperty != null) {
            // 2. 若存在，合并 property_value（用逗号分隔）
            String mergedValue = existingProperty.getPropertyValue() + "," + equipmentProperty.getPropertyValue();
            existingProperty.setPropertyValue(mergedValue);
            // 3. 更新已有记录
            equipmentPropertyMapper.updateById(existingProperty);
        } else {
            // 4. 若不存在，直接新增

            equipmentPropertyMapper.insert(equipmentProperty);
        }
    }

    @Override
    public void update(EquipmentProperty equipmentProperty) {
        equipmentPropertyMapper.update(equipmentProperty);
    }

    @Override
    public void delete(Long id) {
        equipmentPropertyMapper.deleteById(id);
    }



    @Override
    public List<EquipmentPropertyDTO> getTypeWithProperties(Long typeId) {
        return equipmentPropertyMapper.selectTypeWithProperties(typeId);
    }

    @Override
    public List<EquipmentPropertyDTO> getAllTypesWithProperties() {
        return equipmentPropertyMapper.selectAllTypesWithProperties();
    }

    @Override
    public EquipmentPropertyDTO getTypeDetailWithProperties(Long typeId) {
        return equipmentPropertyMapper.selectTypeDetailWithProperties(typeId);
    }

    @Override
    public List<EquipmentProperty> getNameByTypeId(Long typeId) {
        return equipmentPropertyMapper.selectNameByTypeId(typeId);
    }


}
