package com.fjhx.service.product.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.product.ProductSpu;
import com.fjhx.mapper.product.ProductSpuMapper;
import com.fjhx.params.product.ProductSpuVo;
import com.fjhx.service.product.ProductSpuService;
import com.fjhx.uitl.code.CodeEnum;
import com.fjhx.utils.wrapperUtil.IWrapper;
import com.fjhx.utils.wrapperUtil.KeywordData;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-11-25
 */
@Service
public class ProductSpuServiceImpl extends ServiceImpl<ProductSpuMapper, ProductSpu> implements ProductSpuService {

    @Override
    public Page<ProductSpu> getPage(Map<String, Object> condition) {
        IWrapper<ProductSpu> wrapper = IWrapper.getWrapper(condition);
        wrapper.keyword(new KeywordData(ProductSpu::getName), new KeywordData(ProductSpu::getCode))
                .orderByDesc(ProductSpu::getId);
        return page(condition, wrapper);
    }

    @Override
    public void add(ProductSpuVo productSpuVo) {
        synchronized (this) {
            productSpuVo.setCode(CodeEnum.SPU.getCode());
            save(productSpuVo);
        }
    }

    @Override
    public void edit(ProductSpuVo productSpuVo) {
        updateById(productSpuVo);
    }

    @Override
    public void delete(ProductSpuVo productSpuVo) {
        // TODO 产品正在使用此spu不让删除
        removeById(productSpuVo.getId());
    }

}
