package com.gxwebsoft.shop.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.ShopActiveImage;
import com.gxwebsoft.shop.mapper.ShopActiveImageMapper;
import com.gxwebsoft.shop.param.ShopActiveImageParam;
import com.gxwebsoft.shop.service.ShopActiveImageService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 推广码底图Service实现
 *
 * @author xm
 * @since 2026-04-27 17:53:00
 */
@Service
@AllArgsConstructor
public class ShopActiveImageServiceImpl extends ServiceImpl<ShopActiveImageMapper, ShopActiveImage> implements ShopActiveImageService {

    @Override
    public PageResult<ShopActiveImage> pageRel(ShopActiveImageParam param) {
        PageParam<ShopActiveImage, ShopActiveImageParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<ShopActiveImage> list = baseMapper.selectPageRel(page, param);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(shopActiveImage -> {
                if(!StringUtils.isEmpty(shopActiveImage.getImgUrl())){
                    shopActiveImage.setImgUrlList(Arrays.asList(shopActiveImage.getImgUrl().split(",")));
                }
            });
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<ShopActiveImage> listRel(ShopActiveImageParam param) {
        List<ShopActiveImage> list = baseMapper.selectListRel(param);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(shopActiveImage -> {
                if(!StringUtils.isEmpty(shopActiveImage.getImgUrl())){
                    shopActiveImage.setImgUrlList(Arrays.asList(shopActiveImage.getImgUrl().split(",")));
                }
            });
        }
        // 排序
        PageParam<ShopActiveImage, ShopActiveImageParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public ShopActiveImage getByIdRel(Integer id) {
        ShopActiveImageParam param = new ShopActiveImageParam();
        param.setId(id);
        ShopActiveImage activeImage = param.getOne(baseMapper.selectListRel(param));
        if(activeImage != null && !StringUtils.isEmpty(activeImage.getImgUrl())){
            activeImage.setImgUrlList(Arrays.asList(activeImage.getImgUrl().split(",")));
        }
        return activeImage;
    }

}
