package net.ferrum.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.domain.QsSpecAttributeValues;
import net.ferrum.business.domain.bo.QsSpecAttributeValuesBo;
import net.ferrum.common.constant.UserConstants;
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.QsProjectAttributesBo;
import net.ferrum.business.domain.vo.QsProjectAttributesVo;
import net.ferrum.business.domain.QsProjectAttributes;
import net.ferrum.business.mapper.QsProjectAttributesMapper;
import net.ferrum.business.service.IQsProjectAttributesService;

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

/**
 * 项目规格Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-24
 */
@RequiredArgsConstructor
@Service
public class QsProjectAttributesServiceImpl implements IQsProjectAttributesService {

    private final QsProjectAttributesMapper baseMapper;

    /**
     * 查询项目规格
     */
    @Override
    public QsProjectAttributesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询项目规格列表
     */
    @Override
    public TableDataInfo<QsProjectAttributesVo> queryPageList(QsProjectAttributesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QsProjectAttributes> lqw = buildQueryWrapperPage(bo);
        Page<QsProjectAttributesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<QsProjectAttributes> buildQueryWrapperPage(QsProjectAttributesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QsProjectAttributes> lqw = Wrappers.lambdaQuery();
        lqw.eq(QsProjectAttributes::getDelFlag, "0");
        lqw.eq(bo.getProjectId() != null, QsProjectAttributes::getProjectId, bo.getProjectId());
        lqw.eq(bo.getTypeId() != null, QsProjectAttributes::getTypeId, bo.getTypeId());
        lqw.eq(bo.getValueId() != null, QsProjectAttributes::getValueId, bo.getValueId());
        return lqw;
    }

    /**
     * 查询项目规格列表
     */
    @Override
    public List<QsProjectAttributesVo> queryList(QsProjectAttributesBo bo) {
        QueryWrapper<QsProjectAttributes> lqw = buildQueryWrapper(bo);
        return baseMapper.selectProjSpecInfoList(lqw);
    }

    private QueryWrapper<QsProjectAttributes> buildQueryWrapper(QsProjectAttributesBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<QsProjectAttributes> lqw = Wrappers.query();

        lqw.eq("qpa.del_flag", UserConstants.NORMAL);
        lqw.eq(bo.getProjectId() != null, "qpa.project_id", bo.getProjectId());
        lqw.eq(bo.getTypeId() != null, "qpa.type_id", bo.getTypeId());
        lqw.eq(bo.getValueId() != null, "qpa.value_id", bo.getValueId());
        return lqw;
    }

    /**
     * 新增项目规格
     */
    @Override
    public Boolean insertByBo(QsProjectAttributesBo bo) {
        QsProjectAttributes add = BeanUtil.toBean(bo, QsProjectAttributes.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改项目规格
     */
    @Override
    public Boolean updateByBo(QsProjectAttributesBo bo) {
        QsProjectAttributes update = BeanUtil.toBean(bo, QsProjectAttributes.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 根据项目ID批量删除项目规格
     */
    @Override
    public boolean deleteByProjectId(Long ProjectId) {
        int rowsAffected = baseMapper.deleteByProjectId(ProjectId);
        return rowsAffected >= 0;
    }
}
