package beilv.shopping.service.impl;

import java.util.List;

import beilv.common.core.domain.entity.SysUser;
import beilv.shopping.domain.QmjsShoppingMall;
import beilv.shopping.mapper.QmjsShoppingMallMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import beilv.shopping.service.IQmjsShoppingMallService;
import beilv.common.core.text.Convert;

/**
 * 商城Service业务层处理
 *
 * @author lc
 * @date 2025-01-02
 */
@Service
public class QmjsShoppingMallServiceImpl implements IQmjsShoppingMallService {
    @Autowired
    private QmjsShoppingMallMapper qmjsShoppingMallMapper;

    /**
     * 查询商城
     *
     * @param id 商城主键
     * @return 商城
     */
    @Override
    public QmjsShoppingMall selectQmjsShoppingMallById(Long id) {

        QmjsShoppingMall shoppingMall = qmjsShoppingMallMapper.selectQmjsShoppingMallById(id);
//        if (shoppingMall != null && shoppingMall.getGoodsImg() != null) {
//            // 将逗号分隔的字符串拆分为数组
//            String[] imageUrls = shoppingMall.getGoodsImg().split(",");
//            shoppingMall.setImageUrlsArray(imageUrls);
//        }
        return shoppingMall;
    }

    /**
     * 查询商城列表
     *
     * @param qmjsShoppingMall 商城
     * @return 商城
     */
    @Override
    public List<QmjsShoppingMall> selectQmjsShoppingMallList(QmjsShoppingMall qmjsShoppingMall) {
        List<QmjsShoppingMall> result = qmjsShoppingMallMapper.selectQmjsShoppingMallList(qmjsShoppingMall);
        if (result != null) {
            for (QmjsShoppingMall mall : result) {
                String goodsImg = mall.getGoodsImg();
                if (goodsImg != null && !goodsImg.isEmpty()) {
                    // 将多张图片路径拆分为数组
                    String[] imgArray = goodsImg.split(",");
                    if (imgArray.length > 0) {
                        // 只取第一张图片
                        mall.setGoodsImg(imgArray[0]);
                    } else {
                        mall.setGoodsImg(null); // 如果没有图片，设置为 null
                    }
                } else {
                    mall.setGoodsImg(null); // 如果 goodsImg 为空，设置为 null
                }
            }
        }
        return result;
    }

    /**
     * 新增商城
     *
     * @param qmjsShoppingMall 商城
     * @return 结果
     */
    @Override
    public int insertQmjsShoppingMall(QmjsShoppingMall qmjsShoppingMall) {
        return qmjsShoppingMallMapper.insertQmjsShoppingMall(qmjsShoppingMall);
    }

    /**
     * 修改商城
     *
     * @param qmjsShoppingMall 商城
     * @return 结果
     */
    @Override
    public int updateQmjsShoppingMall(QmjsShoppingMall qmjsShoppingMall) {
        return qmjsShoppingMallMapper.updateQmjsShoppingMall(qmjsShoppingMall);
    }

    /**
     * 批量删除商城
     *
     * @param ids 需要删除的商城主键
     * @return 结果
     */
    @Override
    public int deleteQmjsShoppingMallByIds(String ids) {
        return qmjsShoppingMallMapper.deleteQmjsShoppingMallByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商城信息
     *
     * @param id 商城主键
     * @return 结果
     */
    @Override
    public int deleteQmjsShoppingMallById(Long id) {
        return qmjsShoppingMallMapper.deleteQmjsShoppingMallById(id);
    }

    /**
     * 商城状态修改
     */
    @Override
    public int goodsStatus(QmjsShoppingMall qmjsShoppingMall) {
        return qmjsShoppingMallMapper.updateQmjsShoppingMall(qmjsShoppingMall);
    }

}
