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.common.core.domain.search.PageParamVo;
import com.project.common.utils.StringUtils;
import com.project.zcustom.domain.addional.LargeUnitRank;
import com.project.zcustom.mapper.car.PlatCarMapper;
import com.project.zcustom.mapper.unit.LargeUnitRankMapper;
import com.project.zcustom.service.unit.ILargeUnitRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LargeUnitRankServiceImpl extends ServiceImpl<LargeUnitRankMapper, LargeUnitRank> implements ILargeUnitRankService {

    @Autowired
    private PlatCarMapper platCarMapper;
    public List<LargeUnitRank> getList(){
        return platCarMapper.getUnitRankList();
    }

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeUnitRank> getLambdaQueryWrapper(LargeUnitRank largeUnitRank) {
        LambdaQueryWrapper<LargeUnitRank> lqw = new LambdaQueryWrapper<LargeUnitRank>();
        if (StringUtils.isNotNull(largeUnitRank.getTotalCarNum())){
            lqw.eq(LargeUnitRank::getTotalCarNum ,largeUnitRank.getTotalCarNum());
        }
        if (StringUtils.isNotNull(largeUnitRank.getPcdNum())){
            lqw.eq(LargeUnitRank::getPcdNum ,largeUnitRank.getPcdNum());
        }
        if (StringUtils.isNotBlank(largeUnitRank.getAppEncode())){
            lqw.eq(LargeUnitRank::getAppEncode ,largeUnitRank.getAppEncode());
        }
        return lqw;
    }

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

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

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

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

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

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

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 公务用车
     */
    @Override
    public List<LargeUnitRank> selectLargeUnitRankListByField(String field, String filedValue) {
        QueryWrapper<LargeUnitRank> lqw = new QueryWrapper<LargeUnitRank>();
        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<LargeUnitRank> lqw = new QueryWrapper<LargeUnitRank>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if(this.getOne(lqw,false) != null){
            throw new RuntimeException(tips + "已存在");
        }
    }
}
