package org.dromara.system.service.impl;

import org.dromara.common.core.utils.DateUtils;
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.dromara.system.domain.TblGoodsOrder;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TblTaxiOrderBo;
import org.dromara.system.domain.vo.TblTaxiOrderVo;
import org.dromara.system.domain.TblTaxiOrder;
import org.dromara.system.mapper.TblTaxiOrderMapper;
import org.dromara.system.service.ITblTaxiOrderService;

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

/**
 * 打车订单Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-01
 */
@RequiredArgsConstructor
@Service
public class TblTaxiOrderServiceImpl implements ITblTaxiOrderService {

    private final TblTaxiOrderMapper baseMapper;

    /**
     * 查询打车订单
     */
    @Override
    public TblTaxiOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询打车订单列表
     */
    @Override
    public TableDataInfo<TblTaxiOrderVo> queryPageList(TblTaxiOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblTaxiOrder> lqw = buildQueryWrapper(bo);
        Page<TblTaxiOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询打车订单列表
     */
    @Override
    public List<TblTaxiOrderVo> queryList(TblTaxiOrderBo bo) {
        LambdaQueryWrapper<TblTaxiOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblTaxiOrder> buildQueryWrapper(TblTaxiOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblTaxiOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), TblTaxiOrder::getOrderId, bo.getOrderId());
        lqw.eq(bo.getFromUserId() != null, TblTaxiOrder::getFromUserId, bo.getFromUserId());
        lqw.eq(bo.getToUserId() != null, TblTaxiOrder::getToUserId, bo.getToUserId());
        lqw.eq(bo.getStatus() != null, TblTaxiOrder::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFromAddr()), TblTaxiOrder::getFromAddr, bo.getFromAddr());
//        lqw.eq(bo.getGotime() != null, TblTaxiOrder::getGotime, bo.getGotime());
        if(bo.getGotime() != null){
            String start= DateUtils.dateTime(bo.getGotime()) +" 00:00:00";
            String end= DateUtils.dateTime(bo.getGotime())+" 23:59:59";
            lqw.gt(bo.getGotime()!=null, TblTaxiOrder::getGotime, start);
            lqw.lt(bo.getGotime()!=null, TblTaxiOrder::getGotime, end);
        }

        lqw.eq(bo.getPeoNum() != null, TblTaxiOrder::getPeoNum, bo.getPeoNum());
        lqw.eq(StringUtils.isNotBlank(bo.getToAddr()), TblTaxiOrder::getToAddr, bo.getToAddr());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblTaxiOrder::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblTaxiOrder::getExt2, bo.getExt2());
        return lqw;
    }

    /**
     * 新增打车订单
     */
    @Override
    public Boolean insertByBo(TblTaxiOrderBo bo) {
        TblTaxiOrder add = MapstructUtils.convert(bo, TblTaxiOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改打车订单
     */
    @Override
    public Boolean updateByBo(TblTaxiOrderBo bo) {
        TblTaxiOrder update = MapstructUtils.convert(bo, TblTaxiOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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