package com.project.zcustom.service.unit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.project.zcustom.controller.core.PageParamVo;
import com.project.zcustom.controller.core.StringUtils;
import com.project.zcustom.domain.addional.LargeCarRank;
import com.project.zcustom.mapper.unit.LargeCarRankMapper;
import com.project.zcustom.service.unit.ILargeCarRankService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LargeCarRankServiceImpl extends ServiceImpl<LargeCarRankMapper, LargeCarRank> implements ILargeCarRankService {

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeCarRank> getLambdaQueryWrapper(LargeCarRank largeCarRank) {
        LambdaQueryWrapper<LargeCarRank> lqw = new LambdaQueryWrapper<LargeCarRank>();
        if (StringUtils.isNotNull(largeCarRank.getPcdCount())){
            lqw.eq(LargeCarRank::getPcdCount ,largeCarRank.getPcdCount());
        }
        if (StringUtils.isNotBlank(largeCarRank.getVehicleNum())){
            lqw.eq(LargeCarRank::getVehicleNum ,largeCarRank.getVehicleNum());
        }
        if (StringUtils.isNotBlank(largeCarRank.getAppEncode())){
            lqw.eq(LargeCarRank::getAppEncode ,largeCarRank.getAppEncode());
        }
        return lqw;
    }

    /**
     * 分页查询公务用车列表
     *
     * @param largeCarRank 公务用车
     * @return 公务用车集合
     */
    @Override
    public IPage<LargeCarRank> pagingLargeCarRankList(LargeCarRank largeCarRank) {
        IPage<LargeCarRank> page = new Page<>(largeCarRank.getPageNum(), largeCarRank.getPageSize());
        LambdaQueryWrapper<LargeCarRank> lqw = getLambdaQueryWrapper(largeCarRank);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCarRank::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询公务用车列表
     *
     * @param lqw
     * @return 公务用车集合
     */
    @Override
    public IPage<LargeCarRank> pagingLargeCarRankList(PageParamVo pageParamVo, LambdaQueryWrapper<LargeCarRank> lqw) {
        IPage<LargeCarRank> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCarRank::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询公务用车列表
     *
     * @param largeCarRank 公务用车
     * @return 公务用车
     */
    @Override
    public List<LargeCarRank> selectLargeCarRankList(LargeCarRank largeCarRank) {
        LambdaQueryWrapper<LargeCarRank> lqw = getLambdaQueryWrapper(largeCarRank);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCarRank::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询公务用车列表
     *
     * @param largeCarRank 公务用车
     * @return 公务用车
     */
    @Override
    public List<LargeCarRank> selectLargeCarRankList(LargeCarRank largeCarRank,Long limitNum) {
        LambdaQueryWrapper<LargeCarRank> lqw = getLambdaQueryWrapper(largeCarRank);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeCarRank::getCreateTime);
        lqw.last("limit "+limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车
     */
    @Override
    public LargeCarRank getLargeCarRankByField(String field, String filedValue) {
        QueryWrapper<LargeCarRank> lqw = new QueryWrapper<LargeCarRank>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车
     */
    @Override
    public List<LargeCarRank> selectLargeCarRankListByField(String field, String filedValue,Long limitNum) {
        QueryWrapper<LargeCarRank> lqw = new QueryWrapper<LargeCarRank>();
        lqw.eq(field, filedValue);
        lqw.last("limit "+limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车
     */
    @Override
    public List<LargeCarRank> selectLargeCarRankListByField(String field, String filedValue) {
        QueryWrapper<LargeCarRank> lqw = new QueryWrapper<LargeCarRank>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field 字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue,String tips, Long id){
        QueryWrapper<LargeCarRank> lqw = new QueryWrapper<LargeCarRank>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if(this.getOne(lqw,false) != null){
            throw new RuntimeException(tips + "已存在");
        }
    }
}
