package com.cool.store.dao;

import com.cool.store.entity.PreFriedProductsDO;
import com.cool.store.mapper.PreFriedProductsMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author suzhuhong
 * @Date 2025/6/20 19:11
 * @Version 1.0
 */
@Repository
public class PreFriedProductsDAO {


    @Resource
    PreFriedProductsMapper preFriedProductsMapper;

    public int createProduct(PreFriedProductsDO preFriedProductsDO){
        return preFriedProductsMapper.insertSelective(preFriedProductsDO);
    }

    public int updateProduct(PreFriedProductsDO product) {
        return preFriedProductsMapper.forceUpdate(product);
    }

    public List<PreFriedProductsDO>  selectByProductIds( List<Long> productIds) {
        if (CollectionUtils.isEmpty(productIds)){
            return Lists.newArrayList();
        }
        return preFriedProductsMapper.selectByProductIds(productIds);
    }


    public PreFriedProductsDO queryById(Long id){
        return preFriedProductsMapper.selectByPrimaryKey(id);
    }

    public PreFriedProductsDO queryByProductCode(String productCode){
        if (StringUtils.isEmpty(productCode)){
            return null;
        }
        return preFriedProductsMapper.queryByProductCode(productCode);
    }

    public List<PreFriedProductsDO> selectByCondition(String productCode, String productName, Integer status){
        return preFriedProductsMapper.selectByCondition(productCode,productName,status);
    }


    /**
     * 批量删除（带业务校验）
     */
    public int batchDeleteProducts(List<Long> ids, String userId) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new IllegalArgumentException("ID列表不能为空");
        }
        return preFriedProductsMapper.batchDelete(ids,userId);
    }

    /**
     * 批量上下架
     */
    public int batchUpdateProductStatus(List<Long> ids, Integer status, String userId) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new IllegalArgumentException("ID列表不能为空");
        }
        return preFriedProductsMapper.batchUpdateStatus(ids, status,userId);
    }

    public int forceUpdate(PreFriedProductsDO preFriedProductsDO, String userId) {
        if (preFriedProductsDO!=null&&preFriedProductsDO.getId()!=null) {
            return 0;
        }
        return preFriedProductsMapper.forceUpdate(preFriedProductsDO);
    }



}
