package com.czg.service.account.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.PictureGalleryDTO;
import com.czg.account.entity.PictureGallery;
import com.czg.account.param.PictureGalleryParam;
import com.czg.account.service.PictureGalleryService;
import com.czg.constants.SystemConstants;
import com.czg.exception.CzgException;
import com.czg.sa.StpKit;
import com.czg.service.account.mapper.PictureGalleryMapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 图库
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-26
 */
@Service
public class PictureGalleryServiceImpl extends ServiceImpl<PictureGalleryMapper, PictureGallery> implements PictureGalleryService {

    private QueryWrapper buildQueryWrapper(PictureGalleryDTO param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        if (StrUtil.isNotEmpty(param.getUrl())) {
            queryWrapper.eq(PictureGallery::getUrl, param.getUrl());
        }
        if (ObjUtil.isNotNull(param.getPictureClassifyId())) {
            queryWrapper.eq(PictureGallery::getPictureClassifyId, param.getPictureClassifyId());
        }

        Long shopId = StpKit.USER.getShopId(0L);
        queryWrapper.and(q -> {
            q.eq(PictureGallery::getShopId, shopId).or(q1 -> {
                q1.eq(PictureGallery::getShopId, 1L);
            });
        });
        queryWrapper.eq(PictureGallery::getIsDel, SystemConstants.OneZero.ZERO);
        queryWrapper.orderBy(PictureGallery::getId, false);
        return queryWrapper;
    }

    @Override
    public Page<PictureGalleryDTO> getPictureGalleryPage(PictureGalleryDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        return super.pageAs(PageUtil.buildPage(), queryWrapper, PictureGalleryDTO.class);
    }

    @Override
    public void addPictureGallery(PictureGalleryParam param) {
        Long shopId = StpKit.USER.getShopId(0L);
        for (String url : param.getUrls()) {
            PictureGallery entity = new PictureGallery();
            entity.setShopId(shopId);
            entity.setPictureClassifyId(param.getPictureClassifyId());
            entity.setUrl(url);
            super.save(entity);
        }
    }


    @Override
    public void deletePictureGallery(Long id) {
        PictureGallery entity = super.getById(id);
        Long shopId = StpKit.USER.getShopId(0L);
        if (!shopId.equals(entity.getShopId())) {
            throw new CzgException("公共图片不可操作");
        }
        UpdateChain.of(PictureGallery.class)
                .set(PictureGallery::getIsDel, SystemConstants.OneZero.ONE)
                .eq(PictureGallery::getShopId, shopId)
                .eq(PictureGallery::getId, id)
                .update();
    }

}