package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.business.domain.bo.ErpTyrePositionBo;
import com.maintain.business.mapper.ErpTyrePositionMapper;
import com.maintain.common.utils.StringUtils;
import com.maintain.common.core.page.TableDataInfo;
import com.maintain.common.core.domain.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 com.maintain.business.domain.vo.ErpTyrePositionVo;
import com.maintain.business.domain.ErpTyrePosition;
import com.maintain.business.service.IErpTyrePositionService;

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

/**
 * 客户信息-车型-胎位Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-25
 */
@RequiredArgsConstructor
@Service
public class ErpTyrePositionServiceImpl implements IErpTyrePositionService {

    private final ErpTyrePositionMapper baseMapper;

    /**
     * 查询客户信息-车型-胎位
     */
    @Override
    public ErpTyrePositionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户信息-车型-胎位列表
     */
    @Override
    public TableDataInfo<ErpTyrePositionVo> queryPageList(ErpTyrePositionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpTyrePosition> lqw = buildQueryWrapper(bo);
        Page<ErpTyrePositionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户信息-车型-胎位列表
     */
    @Override
    public List<ErpTyrePositionVo> queryList(ErpTyrePositionBo bo) {
        LambdaQueryWrapper<ErpTyrePosition> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpTyrePosition> buildQueryWrapper(ErpTyrePositionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpTyrePosition> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getVehicleModelId() != null, ErpTyrePosition::getVehicleModelId, bo.getVehicleModelId());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), ErpTyrePosition::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpTyrePosition::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增客户信息-车型-胎位
     */
    @Override
    public Boolean insertByBo(ErpTyrePositionBo bo) {
        ErpTyrePosition add = BeanUtil.toBean(bo, ErpTyrePosition.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户信息-车型-胎位
     */
    @Override
    public Boolean updateByBo(ErpTyrePositionBo bo) {
        ErpTyrePosition update = BeanUtil.toBean(bo, ErpTyrePosition.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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