package com.zcxk.charge.web.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.charge.core.dao.DeviceAttributeSpecsMapper;
import com.zcxk.charge.core.entity.DeviceAttributeSpecsEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 设备属性规则Service接口
 *
 * @author: WYY
 * @date 2020-11-20 20:29
 */
@Service
public class DeviceAttributeSpecsService extends ServiceImpl<DeviceAttributeSpecsMapper, DeviceAttributeSpecsEntity> {

    @Resource
    private DeviceAttributeSpecsMapper deviceAttributeSpecsMapper;


    /**
     * 查列表
     */
    public List<DeviceAttributeSpecsEntity> findList(DeviceAttributeSpecsEntity deviceAttributeSpecsEntity) {
        return deviceAttributeSpecsMapper.findList(deviceAttributeSpecsEntity);
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delDeviceAttributeSpecsByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));

    }

    /**
     * 单个删除
     */
    public boolean delDeviceAttributeSpecsById(Long id) {
        return this.removeById(id);
    }

    /**
     * 保存
     */
    public boolean addDeviceAttributeSpecs(DeviceAttributeSpecsEntity deviceAttributeSpecs) {
        if (this.save(deviceAttributeSpecs)) {
            return true;
        }
        return false;
    }

    /**
     * 修改根居ID
     */
    public boolean updateDeviceAttributeSpecsById(DeviceAttributeSpecsEntity deviceAttributeSpecs) {
        if (this.updateById(deviceAttributeSpecs)) {
            return true;
        }
        return false;
    }

    /**
     * 根居ID获取对象
     */
    public DeviceAttributeSpecsEntity findDeviceAttributeSpecsById(Long id) {
        return deviceAttributeSpecsMapper.findDeviceAttributeSpecsById(id);
    }
    /**
     * 根据属性id和值获取规格名称
    */
    public String  selectSpecsName(Long attributeId,String specsValue){
        DeviceAttributeSpecsEntity res= this.getOne(new QueryWrapper<DeviceAttributeSpecsEntity>().eq("ATTRIBUTE_ID", attributeId).
                eq("SPECS_VALUE", specsValue));
        if(res==null) {
            res= this.getOne(new QueryWrapper<DeviceAttributeSpecsEntity>().eq("ATTRIBUTE_ID", attributeId).
                    eq("SPECS_VALUE", '0'+specsValue));
        }
        return res==null?null:res.getSpecsName();
    }
}
