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

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.DeviceRemoteInfoDto;
import com.sptg.device.api.entity.DevicePath;
import com.sptg.device.api.entity.DeviceRemoteInfo;
import com.sptg.device.biz.mapper.DeviceRemoteInfoMapper;
import com.sptg.device.biz.service.DeviceRemoteInfoServiceInterface;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author zwx
 * @create 2020/9/29 17:55
 */
@Service
public class DeviceRemoteInfoServiceImpl extends ServiceImpl<DeviceRemoteInfoMapper, DeviceRemoteInfo> implements DeviceRemoteInfoServiceInterface {
    @Override
    public Page<DeviceRemoteInfo> pagingQuery(DeviceRemoteInfoDto dto) {
        Page<DeviceRemoteInfo> page = dto.getPage();
        EntityWrapper wrapper=new EntityWrapper<DevicePath>();
        wrapper.eq("i_status", OperationConstant.ZERO);
        if(StringUtils.hasText(dto.getSerialNumber())){
            wrapper.like("s_serialNumber",dto.getSerialNumber());
        }
        //排序
        if (dto.getSortField() != null && dto.getSortOrder() != null){
            wrapper.orderBy(dto.getSortField(),dto.getSortOrder().equals("asc"));
        }else {
            //  排序字段 时间降序
            wrapper.orderBy("t_update_date",false);
        }
        Page<DeviceRemoteInfo> deviceRemoteInfoPage = this.selectPage(page,wrapper);
        return deviceRemoteInfoPage;
    }

    @Override
    public ResultBean<Boolean> add(DeviceRemoteInfoDto dto) {
        boolean insert = this.insert(dto);
        if(insert){
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }else{
            return new ResultBean<>(ResultEnum.ERROR, false);
        }
    }

    @Override
    public ResultBean<Boolean> edit(DeviceRemoteInfo deviceRemoteInfo) {
        boolean b = this.updateById(deviceRemoteInfo);
        if(b){
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }else{
            return new ResultBean<>(ResultEnum.ERROR, false);
        }
    }

    @Override
    public DeviceRemoteInfo findDeviceRemoteInfo(DeviceRemoteInfoDto dto) {
        Wrapper<DeviceRemoteInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("i_status", OperationConstant.ZERO);
        return this.selectOne(wrapper);
    }

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

    @Override
    public DeviceRemoteInfo getById(Long id) {
        return this.selectById(id);
    }

    @Override
    public ResultBean<Boolean> delete(DeviceRemoteInfoDto dto) {
        Wrapper<DeviceRemoteInfo> wrapper = new EntityWrapper<>();
        if(dto.getId() != null){
            wrapper.eq("l_id", dto.getId());
        }
        if(StringUtils.hasText(dto.getSerialNumber())){
            wrapper.eq("s_serialNumber", dto.getSerialNumber());
        }
        Integer delete = this.baseMapper.delete(wrapper);

        if(delete > 0){
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }else{
            return new ResultBean<>(ResultEnum.ERROR, false);
        }
    }
}
