package com.ruoyi.zhdd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zhdd.domain.SingleDevice;
import com.ruoyi.zhdd.domain.bo.SingleDeviceBo;
import com.ruoyi.zhdd.domain.vo.SingleDeviceVo;
import com.ruoyi.zhdd.mapper.SingleDeviceMapper;
import com.ruoyi.zhdd.service.ISingleDeviceService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 单兵数据信息Service业务层处理
 *
 * @author xintong
 * @date 2021-12-17
 */
@Service
public class SingleDeviceServiceImpl extends ServicePlusImpl<SingleDeviceMapper, SingleDevice, SingleDeviceVo> implements ISingleDeviceService {

    @Override
    public SingleDeviceVo queryById(String userId) {
        return getVoById(userId);
    }

    @Override
    public TableDataInfo<SingleDeviceVo> queryPageList(SingleDeviceBo bo) {
        PagePlus<SingleDevice, SingleDeviceVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SingleDeviceVo> queryList(SingleDeviceBo bo) {
        // 单兵设备查询一分钟之内的
//        bo.getParams().put("beginCreateTime", DateUtil.formatDateTime(DateUtil.offsetMinute(new Date(), -1)));
//        bo.getParams().put("endCreateTime", DateUtil.now());
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<SingleDevice> buildQueryWrapper(SingleDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SingleDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), SingleDevice::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), SingleDevice::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), SingleDevice::getUserType, bo.getUserType());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), SingleDevice::getDeviceCode, bo.getDeviceCode());
        lqw.eq(bo.getGpsX() != null, SingleDevice::getGpsX, bo.getGpsX());
        lqw.eq(bo.getGpsY() != null, SingleDevice::getGpsY, bo.getGpsY());
        lqw.eq(bo.getHeight() != null, SingleDevice::getHeight, bo.getHeight());
        lqw.eq(bo.getAngle() != null, SingleDevice::getAngle, bo.getAngle());
        lqw.eq(bo.getSpeed() != null, SingleDevice::getSpeed, bo.getSpeed());
        lqw.eq(bo.getStartCount() != null, SingleDevice::getStartCount, bo.getStartCount());
        lqw.eq(bo.getDataSource() != null, SingleDevice::getDataSource, bo.getDataSource());
        lqw.eq(StringUtils.isNotBlank(bo.getTime()), SingleDevice::getTime, bo.getTime());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            SingleDevice::getTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    @Override
    public Boolean insertByBo(SingleDeviceBo bo) {
        SingleDevice add = BeanUtil.toBean(bo, SingleDevice.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(SingleDeviceBo bo) {
        SingleDevice update = BeanUtil.toBean(bo, SingleDevice.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SingleDevice entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
