package org.dromara.system.service.impl;

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.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TblDzbcKyzBo;
import org.dromara.system.domain.vo.TblDzbcKyzVo;
import org.dromara.system.domain.TblDzbcKyz;
import org.dromara.system.mapper.TblDzbcKyzMapper;
import org.dromara.system.service.ITblDzbcKyzService;

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

/**
 * 定制班车-客运站Service业务层处理
 *
 * @author Lion Li
 * @date 2025-02-12
 */
@RequiredArgsConstructor
@Service
public class TblDzbcKyzServiceImpl implements ITblDzbcKyzService {

    private final TblDzbcKyzMapper baseMapper;

    /**
     * 查询定制班车-客运站
     */
    @Override
    public TblDzbcKyzVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询定制班车-客运站列表
     */
    @Override
    public TableDataInfo<TblDzbcKyzVo> queryPageList(TblDzbcKyzBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblDzbcKyz> lqw = buildQueryWrapper(bo);
        Page<TblDzbcKyzVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询定制班车-客运站列表
     */
    @Override
    public List<TblDzbcKyzVo> queryList(TblDzbcKyzBo bo) {
        LambdaQueryWrapper<TblDzbcKyz> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblDzbcKyz> buildQueryWrapper(TblDzbcKyzBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblDzbcKyz> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblDzbcKyz::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAddr()), TblDzbcKyz::getAddr, bo.getAddr());
        lqw.eq(StringUtils.isNotBlank(bo.getMid()), TblDzbcKyz::getMid, bo.getMid());
        lqw.like(StringUtils.isNotBlank(bo.getMname()), TblDzbcKyz::getMname, bo.getMname());
        lqw.like(StringUtils.isNotBlank(bo.getContactName()), TblDzbcKyz::getContactName, bo.getContactName());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), TblDzbcKyz::getContactPhone, bo.getContactPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblDzbcKyz::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblDzbcKyz::getExt2, bo.getExt2());
        return lqw;
    }

    /**
     * 新增定制班车-客运站
     */
    @Override
    public Boolean insertByBo(TblDzbcKyzBo bo) {
        TblDzbcKyz add = MapstructUtils.convert(bo, TblDzbcKyz.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改定制班车-客运站
     */
    @Override
    public Boolean updateByBo(TblDzbcKyzBo bo) {
        TblDzbcKyz update = MapstructUtils.convert(bo, TblDzbcKyz.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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