package net.ferrum.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import net.ferrum.business.domain.bo.QsSupplierBo;
import net.ferrum.business.domain.vo.QsSupplierVo;
import net.ferrum.common.core.page.TableDataInfo;
import net.ferrum.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.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import net.ferrum.business.domain.bo.QsUserSupplierBo;
import net.ferrum.business.domain.vo.QsUserSupplierVo;
import net.ferrum.business.domain.QsUserSupplier;
import net.ferrum.business.mapper.QsUserSupplierMapper;
import net.ferrum.business.service.IQsUserSupplierService;

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

/**
 * userSupplierService业务层处理
 *
 * @author tzx
 * @date 2025-07-13
 */
@RequiredArgsConstructor
@Service
public class QsUserSupplierServiceImpl implements IQsUserSupplierService {

    private final QsUserSupplierMapper baseMapper;

    /**
     * 查询userSupplier
     */
    @Override
    public QsUserSupplierVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询userSupplier列表
     */
    @Override
    public TableDataInfo<QsUserSupplierVo> queryPageList(QsUserSupplierBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QsUserSupplier> lqw = buildQueryWrapper(bo);
        Page<QsUserSupplierVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询userSupplier列表
     */
    @Override
    public List<QsUserSupplierVo> queryList(QsUserSupplierBo bo) {
        LambdaQueryWrapper<QsUserSupplier> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QsUserSupplier> buildQueryWrapper(QsUserSupplierBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QsUserSupplier> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, QsUserSupplier::getId, bo.getId());
        lqw.eq(bo.getUserId() != null, QsUserSupplier::getUserId, bo.getUserId());
        lqw.eq(bo.getSupplierId() != null, QsUserSupplier::getSupplierId, bo.getSupplierId());
        return lqw;
    }

    /**
     * 新增userSupplier
     */
    @Override
    public Boolean insertByBo(QsUserSupplierBo bo) {
        QsUserSupplier add = BeanUtil.toBean(bo, QsUserSupplier.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改userSupplier
     */
    @Override
    public Boolean updateByBo(QsUserSupplierBo bo) {
        QsUserSupplier update = BeanUtil.toBean(bo, QsUserSupplier.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    public TableDataInfo<QsSupplierVo> selectSuppliersByUsersPages(QsSupplierBo bo,PageQuery pageQuery){
        //return baseMapper.selectSuppliersByUsersPages(bo.getSupplierManager());
        Page<QsSupplierVo> result = baseMapper.selectSuppliersByUsersPages(pageQuery.build(),bo);
        return TableDataInfo.build(result);
    }

    public List<QsUserSupplierVo> selectUsersbySupplierId(@Param("supplierId")Long supplierId){
        //return baseMapper.selectSuppliersByUsersPages(bo.getSupplierManager());
        List<QsUserSupplierVo> result = baseMapper.selectUsersbySupplierId(supplierId);
        return result;
    }
}
