package com.cpxt.biz.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpxt.biz.domain.BizCar;
import com.cpxt.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cpxt.biz.mapper.BizCarModelMapper;
import com.cpxt.biz.domain.BizCarModel;
import com.cpxt.biz.service.IBizCarModelService;

/**
 * 车型Service业务层处理
 * 
 * @author YIN
 * @date 2024-12-16
 */
@Service
public class BizCarModelServiceImpl implements IBizCarModelService 
{
    @Autowired
    private BizCarModelMapper bizCarModelMapper;

    /**
     * 查询车型
     * 
     * @param id 车型主键
     * @return 车型
     */
    @Override
    public BizCarModel selectBizCarModelById(Long id)
    {
        return bizCarModelMapper.selectById(id);
    }

    /**
     * 查询车型列表
     *
     * @param bizCarModel 车型
     * @return 车型
     */
    @Override
    public Page<BizCarModel> selectBizCarModelPage(BizCarModel bizCarModel, int pageNum, int pageSize)
    {
        LambdaQueryWrapper<BizCarModel> queryWrapper = buildQueryWrapper(bizCarModel);
        return bizCarModelMapper.selectPage(new Page<BizCarModel>(pageNum,pageSize),queryWrapper);
    }

    private LambdaQueryWrapper<BizCarModel> buildQueryWrapper(BizCarModel bizCarModel) {
        LambdaQueryWrapper<BizCarModel> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizCarModel.getBrand())) {
            queryWrapper.like(BizCarModel::getBrand, bizCarModel.getBrand());
        }
        if (ObjectUtil.isNotEmpty(bizCarModel.getName())) {
            queryWrapper.like(BizCarModel::getName, bizCarModel.getName());
        }
        queryWrapper.eq(BizCarModel::getStatus,1);
        return queryWrapper;
    }

    /**
     * 查询车型列表
     * 
     * @param bizCarModel 车型
     * @return 车型
     */
    @Override
    public List<BizCarModel> selectBizCarModelList(BizCarModel bizCarModel)
    {
        LambdaQueryWrapper<BizCarModel> queryWrapper = buildQueryWrapper(bizCarModel);
        return bizCarModelMapper.selectList(queryWrapper);
    }

    /**
     * 新增车型
     * 
     * @param bizCarModel 车型
     * @return 结果
     */
    @Override
    public int insertBizCarModel(BizCarModel bizCarModel)
    {
        return bizCarModelMapper.insert(bizCarModel);
    }

    /**
     * 修改车型
     * 
     * @param bizCarModel 车型
     * @return 结果
     */
    @Override
    public int updateBizCarModel(BizCarModel bizCarModel)
    {
        return bizCarModelMapper.updateById(bizCarModel);
    }

    /**
     * 批量删除车型
     * 
     * @param ids 需要删除的车型主键
     * @return 结果
     */
    @Override
    public int deleteBizCarModelByIds(Long[] ids)
    {
        UpdateWrapper<BizCarModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", (Object[]) ids);
        updateWrapper.set("status","2"); //状态改为删除
        return bizCarModelMapper.update(updateWrapper);
    }

    /**
     * 删除车型信息
     * 
     * @param id 车型主键
     * @return 结果
     */
    @Override
    public int deleteBizCarModelById(Long id)
    {
        UpdateWrapper<BizCarModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status","2"); //状态改为删除
        return bizCarModelMapper.update(null,updateWrapper);
    }
}
