package org.dromara.system.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.GpsData;
import org.dromara.system.domain.TblCarSchedule;
import org.dromara.system.domain.bo.GpsDataBo;
import org.dromara.system.domain.vo.GpsDataVo;
import org.dromara.system.mapper.TblGpsDataMapper;
import org.dromara.system.service.ITblGpsDataService;
import org.springframework.stereotype.Service;

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

/**
 * 丢失货物Service业务层处理
 *
 * @author Lion Li
 * @date 2024-02-22
 */
@RequiredArgsConstructor
@DS("slave")
@Slf4j
@Service
public class TblGpsDataServiceImpl implements ITblGpsDataService {

    private final TblGpsDataMapper baseMapper;

    /**
     * 查询丢失货物
     */
    @Override
    public GpsDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询丢失货物列表
     */
    @Override
    public TableDataInfo<GpsDataVo> queryPageList(GpsDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GpsData> lqw = buildQueryWrapper(bo);
        Page<GpsDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        log.info(JSONUtil.toJsonStr(result.getRecords()));
        return TableDataInfo.build(result);
    }

    /**
     * 查询丢失货物列表
     */
    @Override
    public List<GpsDataVo> queryList(GpsDataBo bo) {
        LambdaQueryWrapper<GpsData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GpsData> buildQueryWrapper(GpsDataBo bo) {
        LambdaQueryWrapper<GpsData> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDevicecode()!=null,GpsData::getDevicecode, bo.getDevicecode());
        if(bo.getParams()!=null && bo.getParams().keySet().size()>0) {
            if (bo.getParams().get("starttime") != null && bo.getParams().get("endtime") != null) {
                lqw.gt(bo.getParams().get("starttime") != null, GpsData::getTs, bo.getParams().get("starttime"));
                lqw.lt(bo.getParams().get("endtime") != null, GpsData::getTs, bo.getParams().get("endtime"));
            }
        }
        return lqw;
    }

    /**
     * 新增丢失货物
     */
    @Override
    public Boolean insertByBo(GpsData bo) {
//        GpsData add = MapstructUtils.convert(bo, GpsData.class);
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {

        }
        return flag;
    }

    @Override
    public Boolean insertData(GpsData bo) {
        baseMapper.insertdata(bo);
        return true;
    }

    /**
     * 修改丢失货物
     */
    @Override
    public Boolean updateByBo(GpsData bo) {
        GpsData update = MapstructUtils.convert(bo, GpsData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GpsData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除丢失货物
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
