package com.sptg.device.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.dto.DeviceOptionsDto;
import com.sptg.device.api.dto.DeviceTemplateDto;
import com.sptg.device.api.entity.DeviceOptions;
import com.sptg.device.api.entity.DeviceTemplate;
import com.sptg.device.biz.mapper.DeviceOptionsMapper;
import com.sptg.device.biz.mapper.DeviceTemplateMapper;
import com.sptg.device.biz.service.DeviceOptionsServiceInterface;
import com.sptg.device.biz.service.DeviceTemplateServiceInterface;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class DeviceOptionsServiceImpl extends ServiceImpl<DeviceOptionsMapper, DeviceOptions> implements DeviceOptionsServiceInterface {

    @Override
    public Boolean add(DeviceOptionsDto deviceOptionsDto) {
        deviceOptionsDto.setStatus(OperationConstant.ZERO);
        return this.insert(deviceOptionsDto);
    }

    @Override
    public Boolean edit(DeviceOptionsDto deviceOptionsDto) {
        deviceOptionsDto.setUpdateDate(new Date());
        return this.updateById(deviceOptionsDto);
    }

    @Override
    public Boolean deleteOptions(DeviceOptionsDto deviceOptionsDto) {
        Wrapper<DeviceOptions> wrapper = new EntityWrapper<>();
        wrapper.eq(deviceOptionsDto.getDeviceId() != null,"l_device_id",deviceOptionsDto.getDeviceId());
        wrapper.eq(deviceOptionsDto.getSerialNumber() != null,"s_serial_number",deviceOptionsDto.getSerialNumber());
        if (deviceOptionsDto.getDeviceIds() != null && deviceOptionsDto.getDeviceIds().size() >0){
            wrapper.in("l_device_id",deviceOptionsDto.getDeviceIds());
        }
        return this.delete(wrapper);
    }

    @Override
    public Boolean deleteByOptionsId(Long id) {
        return this.deleteById(id);
    }

    @Override
    public DeviceOptions getBySerialNumber(String serialNumber) {
        Wrapper<DeviceOptions> wrapper = new EntityWrapper<>();
        wrapper.eq("i_status",OperationConstant.ZERO);
        wrapper.eq("s_serial_number",serialNumber);
        return this.selectOne(wrapper);
    }

    @Override
    public DeviceOptions getByDeviceId(Long deviceId) {
        Wrapper<DeviceOptions> wrapper = new EntityWrapper<>();
        wrapper.eq("i_status",OperationConstant.ZERO);
        wrapper.eq("l_device_id",deviceId);
        return this.selectOne(wrapper);
    }

    @Override
    public DeviceOptions getOne(DeviceOptionsDto deviceOptionsDto) {
        Wrapper<DeviceOptions> wrapper = new EntityWrapper<>();
        wrapper.notIn("i_status",OperationConstant.MINUS_ONE);
        wrapper.eq(deviceOptionsDto.getDeviceId() != null,"l_device_id",deviceOptionsDto.getDeviceId());
        wrapper.eq(deviceOptionsDto.getSerialNumber() != null,"s_serial_number",deviceOptionsDto.getSerialNumber());
        return this.selectOne(wrapper);
    }
}
