package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.bo.ErpMaterialVendorBo;
import com.maintain.business.domain.vo.ErpMaterialVendorVo;
import com.maintain.business.domain.ErpMaterialVendor;
import com.maintain.business.mapper.ErpMaterialVendorMapper;
import com.maintain.business.service.IErpMaterialVendorService;

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

/**
 * 物资供应商Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-03
 */
@RequiredArgsConstructor
@Service
public class ErpMaterialVendorServiceImpl implements IErpMaterialVendorService {

    private final ErpMaterialVendorMapper baseMapper;

    /**
     * 查询物资供应商
     */
    @Override
    public ErpMaterialVendorVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物资供应商列表
     */
    @Override
    public TableDataInfo<ErpMaterialVendorVo> queryPageList(ErpMaterialVendorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMaterialVendor> lqw = buildQueryWrapper(bo);
        Page<ErpMaterialVendorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物资供应商列表
     */
    @Override
    public List<ErpMaterialVendorVo> queryList(ErpMaterialVendorBo bo) {
        LambdaQueryWrapper<ErpMaterialVendor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpMaterialVendor> buildQueryWrapper(ErpMaterialVendorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMaterialVendor> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getIsEnable() != null, ErpMaterialVendor::getIsEnable, bo.getIsEnable());
        lqw.like(StringUtils.isNotBlank(bo.getVendorName()), ErpMaterialVendor::getVendorName, bo.getVendorName());
        lqw.like(StringUtils.isNotBlank(bo.getVendorContact()), ErpMaterialVendor::getVendorContact, bo.getVendorContact());
        lqw.like(StringUtils.isNotBlank(bo.getVendorPhone()), ErpMaterialVendor::getVendorPhone, bo.getVendorPhone());
        lqw.like(StringUtils.isNotBlank(bo.getVendorAddress()), ErpMaterialVendor::getVendorAddress, bo.getVendorAddress());
        lqw.like(StringUtils.isNotBlank(bo.getTaxpayerId()), ErpMaterialVendor::getTaxpayerId, bo.getTaxpayerId());
        return lqw;
    }

    /**
     * 新增物资供应商
     */
    @Override
    public Boolean insertByBo(ErpMaterialVendorBo bo) {
        ErpMaterialVendor add = BeanUtil.toBean(bo, ErpMaterialVendor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物资供应商
     */
    @Override
    public Boolean updateByBo(ErpMaterialVendorBo bo) {
        ErpMaterialVendor update = BeanUtil.toBean(bo, ErpMaterialVendor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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