package shop.alien.storeplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import shop.alien.entity.store.StoreImg;
import shop.alien.entity.store.StoreOfficialAlbum;
import shop.alien.entity.store.vo.StoreOfficialAlbumVo;
import shop.alien.mapper.StoreImgMapper;
import shop.alien.mapper.StoreOfficialAlbumMapper;
import shop.alien.storeplatform.service.StorePlatformOfficialAlbumService;
import shop.alien.storeplatform.util.CommonConstant;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
@RequiredArgsConstructor
public class StorePlatformOfficialAlbumServiceImpl extends ServiceImpl<StoreOfficialAlbumMapper, StoreOfficialAlbum> implements StorePlatformOfficialAlbumService {

    private final StoreOfficialAlbumMapper storeOfficialAlbumMapper;
    private final StoreImgMapper storeImgMapper;

    /**
     * 创建官方相册
     *
     * @param storeOfficialAlbum 官方相册信息
     * @return storeOfficialAlbum 官方相册信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StoreOfficialAlbum createOrUpdateOfficialAlbum(StoreOfficialAlbum storeOfficialAlbum) {
        Integer id= storeOfficialAlbum.getId();
        if (id!=null && id > 0) {
            boolean updateSuccess = storeOfficialAlbumMapper.updateById(storeOfficialAlbum) > 0;
            if (!updateSuccess) {
                throw new RuntimeException("更新官方相册失败");
            }
        } else {
            boolean insertSuccess = storeOfficialAlbumMapper.insert(storeOfficialAlbum) > 0;
            if (!insertSuccess) {
                throw new RuntimeException("插入官方相册失败");
            }
        }
        return storeOfficialAlbum;
    }

    /**
     * 根据storeId查询官方相册列表
     *
     * @param storeId 店铺ID
     * @return List<StoreOfficialAlbumVo> 官方相册列表
     */
    @Override
    public List<StoreOfficialAlbumVo> getOfficialAlbumList(String storeId) {
        if(StringUtils.isNotBlank(storeId)) {
            //查询出店铺官方相册img数量并更新
            List<StoreOfficialAlbum> storeOfficialAlbumList = storeOfficialAlbumMapper.getStoreOfficialAlbumImgCount(Integer.parseInt(storeId));
            if(!CollectionUtils.isEmpty(storeOfficialAlbumList)){
                this.updateBatchById(storeOfficialAlbumList);
            }

            // 查询官方相册列表
            return storeOfficialAlbumMapper.getStoreOfficialAlbumList(Integer.parseInt(storeId));
        }
        return Collections.emptyList();
    }

    /**
     * 根据storeId查询官方相册列表
     *
     * @param storeOfficialAlbumList 根据官方相册信息删除官方相册及图片
     * @return result 删除结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOfficialAlbum(List<StoreOfficialAlbum> storeOfficialAlbumList) {

        // 校验官方相册列表
        if (storeOfficialAlbumList == null || storeOfficialAlbumList.isEmpty()) {
            return CommonConstant.ERROR_CODE_INVALID_PARAMS;
        }
        List<Integer> albumIds =storeOfficialAlbumList.stream().map(StoreOfficialAlbum::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(albumIds)) {
            return CommonConstant.ERROR_CODE_INVALID_PARAMS;
        }

        // 删除相册
        storeOfficialAlbumMapper.deleteBatchIds(albumIds);
        // 删除图片（批量删除）
        LambdaUpdateWrapper<StoreImg> wrapper = new LambdaUpdateWrapper<StoreImg>()
                .in(StoreImg::getBusinessId, albumIds)
                .eq(StoreImg::getImgType, CommonConstant.STORE_IMG_ALBUM);
        storeImgMapper.delete(wrapper);
        return CommonConstant.ERROR_CODE_VALID_PARAMS;
    }
}
