package org.dromara.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.domain.bo.SenFlowDetailBo;
import org.dromara.domain.vo.SenFlowDetailVo;
import org.dromara.domain.SenFlowDetail;
import org.dromara.mapper.SenFlowDetailMapper;
import org.dromara.service.ISenFlowDetailService;

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

/**
 * 流量明细Service业务层处理
 *
 * @author luogang
 * @date 2024-09-19
 */
@RequiredArgsConstructor
@Service
public class SenFlowDetailServiceImpl implements ISenFlowDetailService {

    private final SenFlowDetailMapper baseMapper;

    /**
     * 查询流量明细
     *
     * @param id 主键
     * @return 流量明细
     */
    @Override
    public SenFlowDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询流量明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 流量明细分页列表
     */
    @Override
    public TableDataInfo<SenFlowDetailVo> queryPageList(SenFlowDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SenFlowDetail> lqw = buildQueryWrapper(bo);
        Page<SenFlowDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的流量明细列表
     *
     * @param bo 查询条件
     * @return 流量明细列表
     */
    @Override
    public List<SenFlowDetailVo> queryList(SenFlowDetailBo bo) {
        LambdaQueryWrapper<SenFlowDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SenFlowDetail> buildQueryWrapper(SenFlowDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SenFlowDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTaskId()!= null, SenFlowDetail::getTaskId, bo.getTaskId());
        lqw.eq(bo.getPointId() != null, SenFlowDetail::getPointId, bo.getPointId());
        lqw.like(StringUtils.isNotBlank(bo.getPointName()), SenFlowDetail::getPointName, bo.getPointName());
        lqw.eq(StringUtils.isNotBlank(bo.getLaneNumber()), SenFlowDetail::getLaneNumber, bo.getLaneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleCount()), SenFlowDetail::getVehicleCount, bo.getVehicleCount());
        lqw.eq(StringUtils.isNotBlank(bo.getAverageSpeed()), SenFlowDetail::getAverageSpeed, bo.getAverageSpeed());
        lqw.eq(StringUtils.isNotBlank(bo.getQueueTime()), SenFlowDetail::getQueueTime, bo.getQueueTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSpaceRate()), SenFlowDetail::getSpaceRate, bo.getSpaceRate());
        lqw.eq(StringUtils.isNotBlank(bo.getTimeRate()), SenFlowDetail::getTimeRate, bo.getTimeRate());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadwayTime()), SenFlowDetail::getHeadwayTime, bo.getHeadwayTime());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadwayDistance()), SenFlowDetail::getHeadwayDistance, bo.getHeadwayDistance());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleRemark()), SenFlowDetail::getVehicleRemark, bo.getVehicleRemark());
        lqw.ge(StringUtils.isNotBlank((CharSequence) bo.getParams().get("startDate")),SenFlowDetail::getCreateTime, bo.getParams().get("startDate")).
                le(StringUtils.isNotBlank((CharSequence) bo.getParams().get("endDate")),SenFlowDetail::getCreateTime, bo.getParams().get("endDate"));
        return lqw;
    }

    /**
     * 新增流量明细
     *
     * @param bo 流量明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SenFlowDetailBo bo) {
        SenFlowDetail add = MapstructUtils.convert(bo, SenFlowDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改流量明细
     *
     * @param bo 流量明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SenFlowDetailBo bo) {
        SenFlowDetail update = MapstructUtils.convert(bo, SenFlowDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除流量明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
    /**
     * 获取流量前3的路段流量折线图数据
     */
    @Override
    public List<Map<String, Object>> lineCharts() {
        return baseMapper.lineCharts();
    }
}
