package cn.skcks.docking.gb28181.wvp.service.docking;

import cn.skcks.docking.gb28181.common.json.JsonException;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.mapper.WvpProxyDockingDynamicSqlSupport;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.mapper.WvpProxyDockingMapper;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDocking;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

@Slf4j
@Service
@RequiredArgsConstructor
public class DockingService {
    private final WvpProxyDockingMapper wvpProxyDockingMapper;

    public Optional<WvpProxyDocking> getDeviceById(Long id){
        return wvpProxyDockingMapper.selectOne(s->
                s.where(WvpProxyDockingDynamicSqlSupport.id, isEqualTo(id)));
    }

    public Optional<WvpProxyDocking> getDeviceByDeviceCode(String deviceCode){
        return wvpProxyDockingMapper.selectOne(s->
                s.where(WvpProxyDockingDynamicSqlSupport.gbDeviceId,isEqualTo(deviceCode)));
    }

    public Boolean hasDeviceByDeviceCode(String deviceCode){
        return getDeviceByDeviceCode(deviceCode).orElse(null) != null;
    }

    public Optional<WvpProxyDocking> getDeviceByGbDeviceId(String gbDeviceId){
        return wvpProxyDockingMapper.selectOne(s->
                s.where(WvpProxyDockingDynamicSqlSupport.gbDeviceId, isEqualTo(gbDeviceId)));
    }

    public Boolean hasDeviceByGbDeviceId(String deviceCode){
        return getDeviceByGbDeviceId(deviceCode).orElse(null) != null;
    }

    /**
     * 添加设备
     * @param device 设备
     * @return 是否成功
     */
    @SneakyThrows
    @Transactional
    public boolean addDevice(WvpProxyDocking device) {
        if(device == null){
            return false;
        }

        String deviceCode = device.getGbDeviceId();
        if(StringUtils.isBlank(deviceCode)){
            throw new JsonException("设备编码不能为空");
        }
        if(getDeviceByDeviceCode(deviceCode).isPresent()){
            wvpProxyDockingMapper.delete(d->d.where(WvpProxyDockingDynamicSqlSupport.gbDeviceId,isEqualTo(deviceCode)));
        }

        return wvpProxyDockingMapper.insert(device) > 0;
    }

    /**
     * 分页查询对接设备/平台
     * @param page 页数
     * @param size 数量
     * @return 分页设备/平台
     */
    public PageInfo<WvpProxyDocking> getDockingWithPage(int page, int size){
        ISelect select = () -> wvpProxyDockingMapper.select(s -> s.orderBy(WvpProxyDockingDynamicSqlSupport.id.descending()));
        return getDockingWithPage(page,size, select);
    }

    /**
     * 分页查询对接设备/平台
     * @param page 页数
     * @param size 数量
     * @param select 查询语句
     * @return 分页设备/平台
     */
    public PageInfo<WvpProxyDocking> getDockingWithPage(int page, int size, ISelect select){
        PageInfo<WvpProxyDocking> pageInfo;
        try (Page<WvpProxyDocking> startPage = PageHelper.startPage(page, size)) {
            pageInfo = startPage.doSelectPageInfo(select);
        }
        return pageInfo;
    }
}
