package com.bonus.canteen.core.supermarket.service.impl;

import com.bonus.canteen.core.supermarket.domain.SupermarketProduct;
import com.bonus.canteen.core.supermarket.mapper.SupermarketProductMapper;
import com.bonus.canteen.core.supermarket.service.SupermarketProductService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品Service业务层处理
 * 
 * @author xsheng
 * @date 2025-04-05
 */
@Service
public class SupermarketProductServiceImpl implements SupermarketProductService {
    @Autowired
    private SupermarketProductMapper supermarketProductMapper;

    /**
     * 查询商品列表
     * 
     * @param SupermarketProduct 商品
     * @return 商品
     */
    @Override
    public List<SupermarketProduct> selectSupermarketProductList(SupermarketProduct SupermarketProduct) {
        return supermarketProductMapper.selectSupermarketProductList(SupermarketProduct);
    }

    @Override
    public List<SupermarketProduct> selectNoSupermarketProductList(SupermarketProduct SupermarketProduct) {
        return supermarketProductMapper.selectNoSupermarketProductList(SupermarketProduct);
    }
    /**
     * 新增商品
     * 
     * @param SupermarketProduct 商品
     * @return 结果
     */
    @Override
    public int insertSupermarketProduct(SupermarketProduct SupermarketProduct) {
        SupermarketProduct.setCreateTime(DateUtils.getNowDate());
        try {
            return supermarketProductMapper.insertSupermarketProduct(SupermarketProduct);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 修改商品
     * 
     * @param SupermarketProduct 商品
     * @return 结果
     */
    @Override
    public int updateSupermarketProduct(SupermarketProduct SupermarketProduct) {
        SupermarketProduct.setUpdateTime(DateUtils.getNowDate());
        try {
            return supermarketProductMapper.updateSupermarketProduct(SupermarketProduct);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    @Override
    public int updateBatchSupermarketProduct(SupermarketProduct supermarketProduct) {
        supermarketProduct.setUpdateTime(DateUtils.getNowDate());
        try {
            int kk=0;
            String[] productids=supermarketProduct.getProductId().split(",");
            for(int i=0;i<productids.length;i++){
                supermarketProduct.setProductId(productids[i]);
                kk=supermarketProductMapper.updateSupermarketProduct(supermarketProduct);
            }
            return kk;
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    @Override
    public int updateSupermarketProductState(SupermarketProduct SupermarketProduct) {
        SupermarketProduct.setUpdateTime(DateUtils.getNowDate());
        try {
            return supermarketProductMapper.updateSupermarketProductState(SupermarketProduct);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }
    /**
     * 批量删除商品
     * 
     * @param productIds 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketProductByProductIds(SupermarketProduct productIds) {
        String[] productIdsl=productIds.getProductId().split(",");
        return supermarketProductMapper.deleteSupermarketProductBySupermarketIds(productIdsl);
    }

    /**
     * 删除商品信息
     * 
     * @param productId 商品主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketProductByProductId(SupermarketProduct productId) {
        Long productIdl=Long.parseLong(productId.getProductId());
        return supermarketProductMapper.deleteSupermarketProductBySupermarketId(productIdl);
    }
}
