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

import java.util.ArrayList;
import java.util.List;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.supermarket.mapper.SupermarketProductMapper;
import com.bonus.canteen.core.supermarket.domain.SupermarketProduct;
import com.bonus.canteen.core.supermarket.service.ISupermarketProductService;

/**
 * 超市商城商品Service业务层处理
 *
 * @author xsheng
 * @date 2025-06-03
 */
@Service
public class SupermarketProductServiceImpl implements ISupermarketProductService {
    @Autowired
    private SupermarketProductMapper supermarketProductMapper;

    /**
     * 查询超市商城商品
     *
     * @param productId 超市商城商品主键
     * @return 超市商城商品
     */
    @Override
    public SupermarketProduct selectSupermarketProductByProductId(Long productId) {
        return supermarketProductMapper.selectSupermarketProductByProductId(productId);
    }

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

    @Override
    public List<SupermarketProduct> selectSupermarketAddProductList(SupermarketProduct supermarketProduct) {
        return supermarketProductMapper.selectSupermarketAddProductList(supermarketProduct);
    }
    /**
     * 新增超市商城商品
     *
     * @param supermarketProduct 超市商城商品
     * @return 结果
     */
    @Override
    public int insertSupermarketProduct(SupermarketProduct supermarketProduct) {
        supermarketProduct.setCreateTime(DateUtils.getNowDate());
        supermarketProduct.setCreateBy(SecurityUtils.getUsername());
        try {
            return supermarketProductMapper.insertSupermarketProduct(supermarketProduct);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public int insertSupermarketProducts(SupermarketProduct supermarketProduct) {
        supermarketProduct.setCreateTime(DateUtils.getNowDate());
        supermarketProduct.setCreateBy(SecurityUtils.getUsername());
        try {
            List<SupermarketProduct> sps=new ArrayList<>();
            sps=supermarketProductMapper.selectSupermarketAddProductList(supermarketProduct);
            for(SupermarketProduct vo:sps){
                vo.setSupermarketId(supermarketProduct.getSupermarketId());
                supermarketProductMapper.insertSupermarketProduct(vo);
            }
            return 1;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
    /**
     * 修改超市商城商品
     *
     * @param supermarketProduct 超市商城商品
     * @return 结果
     */
    @Override
    public int updateSupermarketProduct(SupermarketProduct supermarketProduct) {
        supermarketProduct.setUpdateTime(DateUtils.getNowDate());
        supermarketProduct.setUpdateBy(SecurityUtils.getUsername());
        try {
            return supermarketProductMapper.updateSupermarketProduct(supermarketProduct);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除超市商城商品
     *
     * @param productIds 需要删除的超市商城商品主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketProductByProductIds(Long[] productIds) {
        return supermarketProductMapper.deleteSupermarketProductByProductIds(productIds);
    }
    @Override
    public int deleteSupermarketProductByProductIdss(String[] productIds) {
        return supermarketProductMapper.deleteSupermarketProductByProductIdss(productIds);
    }
    @Override
    public int onlineSupermarketProductByProductIdss(String[] productIds) {
        return supermarketProductMapper.onlineSupermarketProductByProductIdss(productIds);
    }
    /**
     * 删除超市商城商品信息
     *
     * @param productId 超市商城商品主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketProductByProductId(Long productId) {
        return supermarketProductMapper.deleteSupermarketProductByProductId(productId);
    }
}
