package net.ferrum.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.ferrum.business.domain.QsSpecAttributeTypes;
import net.ferrum.business.mapper.QsSpecAttributeTypesMapper;
import net.ferrum.common.constant.UserConstants;
import net.ferrum.common.core.domain.R;
import net.ferrum.common.core.domain.entity.SysDept;
import net.ferrum.common.core.domain.entity.SysUser;
import net.ferrum.common.helper.DataBaseHelper;
import net.ferrum.common.utils.StreamUtils;
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.QsSpecAttributeValuesBo;
import net.ferrum.business.domain.vo.QsSpecAttributeValuesVo;
import net.ferrum.business.domain.QsSpecAttributeValues;
import net.ferrum.business.mapper.QsSpecAttributeValuesMapper;
import net.ferrum.business.service.IQsSpecAttributeValuesService;

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

/**
 * 规格参数值Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-19
 */
@RequiredArgsConstructor
@Service
public class QsSpecAttributeValuesServiceImpl implements IQsSpecAttributeValuesService {

    private final QsSpecAttributeValuesMapper baseMapper;
    private final QsSpecAttributeTypesMapper specAttributeTypesMapper;

    public TableDataInfo<QsSpecAttributeValues>selectPageSpecAttributeValuesList(QsSpecAttributeValues specAttributeValues, PageQuery pageQuery) {
        Page<QsSpecAttributeValues> page = baseMapper.selectPageSpecAttributeValuesList(pageQuery.build(), this.buildQueryWrapper(specAttributeValues));
        return TableDataInfo.build(page);
    }

    /**
     * 查询规格参数值
     */
    @Override
    public QsSpecAttributeValuesVo queryById(Long valueId){
        return baseMapper.selectVoById(valueId);
    }

    /**
     * 查询规格参数值列表
     */
    @Override
    public TableDataInfo<QsSpecAttributeValuesVo> queryPageList(QsSpecAttributeValuesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QsSpecAttributeValues> lqw = buildQueryWrapper(bo);
        Page<QsSpecAttributeValuesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询规格参数值列表
     */
    @Override
    public List<QsSpecAttributeValuesVo> queryList(QsSpecAttributeValuesBo bo) {
        LambdaQueryWrapper<QsSpecAttributeValues> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QsSpecAttributeValues> buildQueryWrapper(QsSpecAttributeValuesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QsSpecAttributeValues> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTypeId() != null, QsSpecAttributeValues::getTypeId, bo.getTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getValue()), QsSpecAttributeValues::getValue, bo.getValue());
        return lqw;
    }

    private Wrapper<QsSpecAttributeValues> buildQueryWrapper(QsSpecAttributeValues specAttributeValues) {
        //Map<String, Object> params = specAttributeValues.getParams();
        //LambdaQueryWrapper<QsSpecAttributeValues> lqw = Wrappers.lambdaQuery();
        QueryWrapper<QsSpecAttributeValues> lqw = Wrappers.query();
        lqw.eq("a.del_flag", UserConstants.NORMAL);
        lqw.eq("b.del_flag", UserConstants.NORMAL);
        lqw.eq(specAttributeValues.getTypeId() != null, "a.type_id", specAttributeValues.getTypeId());
        lqw.like(StringUtils.isNotBlank(specAttributeValues.getValue()), "a.value", specAttributeValues.getValue());

        lqw.like(StringUtils.isNotBlank(specAttributeValues.getSpecTypeName()), "b.type_name", specAttributeValues.getSpecTypeName());

        lqw.and(ObjectUtil.isNotNull(specAttributeValues.getTypeId()), w -> {
            List<QsSpecAttributeTypes> list = specAttributeTypesMapper.selectList(new LambdaQueryWrapper<QsSpecAttributeTypes>()
                .select(QsSpecAttributeTypes::getTypeId));
            List<Long> ids = StreamUtils.toList(list, QsSpecAttributeTypes::getTypeId);
            ids.add(specAttributeValues.getTypeId());
            w.in("a.type_id", ids);
        });
        return lqw;
    }

    /**
     * 新增规格参数值
     */
    @Override
    public Boolean insertByBo(QsSpecAttributeValuesBo bo) {
        QsSpecAttributeValues add = BeanUtil.toBean(bo, QsSpecAttributeValues.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setValueId(add.getValueId());
        }
        return flag;
    }

    /**
     * 修改规格参数值
     */
    @Override
    public Boolean updateByBo(QsSpecAttributeValuesBo bo) {
        QsSpecAttributeValues update = BeanUtil.toBean(bo, QsSpecAttributeValues.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    public List<QsSpecAttributeValues> selectSpecAttributeValuesByTypesId(Long typeId) {
        return baseMapper.selectSpecAttributeValuesByTypesId(typeId);
    }
}
