package shop.alien.storeplatform.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.TagBusinessRelation;
import shop.alien.mapper.TagBusinessRelationMapper;
import shop.alien.storeplatform.service.StorePlatformTagBusinessRelationService;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class StorePlatformTagBusinessRelationServiceImpl extends ServiceImpl<TagBusinessRelationMapper, TagBusinessRelation> implements StorePlatformTagBusinessRelationService {
    public final TagBusinessRelationMapper tagBusinessRelationMapper;

    @Override
    public TagBusinessRelation createTagBusinessRelation(TagBusinessRelation tagBusinessRelation) {

        // 校验同一层级是否存在相同的标签
        List<TagBusinessRelation> tagCategoryList = tagBusinessRelationMapper.selectList(new LambdaQueryWrapper<TagBusinessRelation>().eq(TagBusinessRelation::getTagId, tagBusinessRelation.getTagId()).eq(TagBusinessRelation::getBusinessSection, tagBusinessRelation.getBusinessSection()));
        if(CollectionUtils.isNotEmpty(tagCategoryList)){
            throw new IllegalArgumentException("标签已经存在");
        }

        // 将数据存储到数据库中
        log.info("标签创建, 请求参数: {}", tagBusinessRelation);
        int result = tagBusinessRelationMapper.insert(tagBusinessRelation);
        log.info("标签创建, 结果: {}", result);
        // 清除相关缓存
        return tagBusinessRelation;
    }

    @Override
    public IPage<TagBusinessRelation> getTagBusinessRelationList(int page, int size, int businessSection, String businessSectionName) {
        IPage<TagBusinessRelation> iPage = new Page<>(page, size);
        LambdaQueryWrapper<TagBusinessRelation> tagCategoryLambdaQueryWrapper = new LambdaQueryWrapper<TagBusinessRelation>();
        tagCategoryLambdaQueryWrapper.eq(TagBusinessRelation::getDeleteFlag, 0);
        if(businessSection > 0) {
            tagCategoryLambdaQueryWrapper.eq(TagBusinessRelation::getTagId, businessSection);
        }
        if(StringUtils.isNotBlank(businessSectionName)) {
            tagCategoryLambdaQueryWrapper.like(TagBusinessRelation::getBusinessSectionName,businessSectionName);
        }
        tagCategoryLambdaQueryWrapper.orderByDesc(TagBusinessRelation::getCreatedTime);
        return tagBusinessRelationMapper.selectPage(iPage,tagCategoryLambdaQueryWrapper);
    }
    @Override
    public int deleteTagBusinessRelation(TagBusinessRelation tagBusinessRelation) {
        if(tagBusinessRelation == null ||tagBusinessRelation.getId() == null){
            return 0;
        }
        return tagBusinessRelationMapper.deleteById(tagBusinessRelation.getId());
    }
}
