package net.ferrum.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import net.ferrum.business.domain.QsBatch;
import net.ferrum.common.utils.StringUtils;
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.springframework.stereotype.Service;
import net.ferrum.business.domain.bo.QsSpecAttributeTypesBo;
import net.ferrum.business.domain.vo.QsSpecAttributeTypesVo;
import net.ferrum.business.domain.QsSpecAttributeTypes;
import net.ferrum.business.mapper.QsSpecAttributeTypesMapper;
import net.ferrum.business.service.IQsSpecAttributeTypesService;

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

/**
 * 规格类型Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-19
 */
@RequiredArgsConstructor
@Service
public class QsSpecAttributeTypesServiceImpl implements IQsSpecAttributeTypesService {

    private final QsSpecAttributeTypesMapper baseMapper;

    /**
     * 查询规格类型
     */
    @Override
    public QsSpecAttributeTypesVo queryById(Long typeId){
        return baseMapper.selectVoById(typeId);
    }

    /**
     * 查询规格类型列表
     */
    @Override
    public TableDataInfo<QsSpecAttributeTypesVo> queryPageList(QsSpecAttributeTypesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QsSpecAttributeTypes> lqw = buildQueryWrapper(bo);
        Page<QsSpecAttributeTypesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询规格类型列表
     */
    @Override
    public List<QsSpecAttributeTypesVo> queryList(QsSpecAttributeTypesBo bo) {
        LambdaQueryWrapper<QsSpecAttributeTypes> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QsSpecAttributeTypes> buildQueryWrapper(QsSpecAttributeTypesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QsSpecAttributeTypes> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), QsSpecAttributeTypes::getTypeName, bo.getTypeName());
        return lqw;
    }

    /**
     * 新增规格类型
     */
    @Override
    public Boolean insertByBo(QsSpecAttributeTypesBo bo) {
        QsSpecAttributeTypes add = BeanUtil.toBean(bo, QsSpecAttributeTypes.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTypeId(add.getTypeId());
        }
        return flag;
    }

    /**
     * 修改规格类型
     */
    @Override
    public Boolean updateByBo(QsSpecAttributeTypesBo bo) {
        QsSpecAttributeTypes update = BeanUtil.toBean(bo, QsSpecAttributeTypes.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 查询规格参数类型
     *
     * @param type 要查询的规格参数类型
     * @return 部规格参数类型信息集合
     */
    @Override
    public List<QsSpecAttributeTypes> selectSpecAttributeTypesList(QsSpecAttributeTypes type) {
        LambdaQueryWrapper<QsSpecAttributeTypes> lqw = new LambdaQueryWrapper<>();
        if (type != null){
            lqw.eq(QsSpecAttributeTypes::getDelFlag, "0")
                .eq(ObjectUtil.isNotNull(type.getTypeId()), QsSpecAttributeTypes::getTypeId, type.getTypeId())
                .like(StringUtils.isNotBlank(type.getTypeName()), QsSpecAttributeTypes::getTypeName, type.getTypeName())
                .orderByAsc(QsSpecAttributeTypes::getTypeId)
                .orderByAsc(QsSpecAttributeTypes::getTypeName);
        }
        return baseMapper.selectSpecAttributeTypesList(lqw);
    }
}
