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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.PictureClassifyDTO;
import com.czg.account.entity.PictureClassify;
import com.czg.account.service.PictureClassifyService;
import com.czg.constants.SystemConstants;
import com.czg.exception.CzgException;
import com.czg.sa.StpKit;
import com.czg.service.account.mapper.PictureClassifyMapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 图片分类
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-26
 */
@Service
public class PictureClassifyServiceImpl extends ServiceImpl<PictureClassifyMapper, PictureClassify> implements PictureClassifyService {

    private QueryWrapper buildQueryWrapper(PictureClassifyDTO param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        if (StrUtil.isNotEmpty(param.getName())) {
            queryWrapper.like(PictureClassify::getName, param.getName());
        }
        Long shopId = StpKit.USER.getShopId(0L);
        queryWrapper.and(q -> {
            q.eq(PictureClassify::getShopId, shopId).or(q1 -> {
                q1.eq(PictureClassify::getShopId, 1L);
            }).or(q2 -> {
                q2.eq(PictureClassify::getIsSystem, SystemConstants.OneZero.ONE);
            });
        });
        queryWrapper.eq(PictureClassify::getIsDel, SystemConstants.OneZero.ZERO);
        queryWrapper.orderBy(PictureClassify::getSort, true);
        queryWrapper.orderBy(PictureClassify::getId, false);
        return queryWrapper;
    }

    @Override
    public List<PictureClassifyDTO> getPictureClassifyList(PictureClassifyDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        return super.listAs(queryWrapper, PictureClassifyDTO.class);
    }

    @Override
    public void addPictureClassify(PictureClassifyDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        boolean exists = super.exists(query().eq(PictureClassify::getName, dto.getName())
                .and(q -> {
                    q.eq(PictureClassify::getShopId, shopId).or(q1 -> {
                        q1.eq(PictureClassify::getShopId, 1L);
                    }).or(q2 -> {
                        q2.eq(PictureClassify::getIsSystem, SystemConstants.OneZero.ONE);
                    });
                })
        );
        if (exists) {
            throw new CzgException("图片分类已存在");
        }
        PictureClassify entity = BeanUtil.copyProperties(dto, PictureClassify.class);
        entity.setIsSystem(SystemConstants.OneZero.ZERO);
        entity.setShopId(shopId);
        super.save(entity);
    }

    @Override
    public void updatePictureClassify(PictureClassifyDTO dto) {
        checkPictureClassify(dto.getId());
        Long shopId = StpKit.USER.getShopId(0L);
        dto.setShopId(shopId);
        boolean exists = super.exists(query().eq(PictureClassify::getName, dto.getName())
                .ne(PictureClassify::getId, dto.getId())
                .and(q -> {
                    q.eq(PictureClassify::getShopId, shopId).or(q1 -> {
                        q1.eq(PictureClassify::getShopId, 1L);
                    }).or(q2 -> {
                        q2.eq(PictureClassify::getIsSystem, SystemConstants.OneZero.ONE);
                    });
                })
        );
        if (exists) {
            throw new CzgException("图片分类已存在");
        }
        PictureClassify entity = BeanUtil.copyProperties(dto, PictureClassify.class);
        entity.setIsSystem(SystemConstants.OneZero.ZERO);
        super.updateById(entity);
    }

    @Override
    public void deletePictureClassify(Long id) {
        checkPictureClassify(id);
        Long shopId = StpKit.USER.getShopId(0L);
        UpdateChain.of(PictureClassify.class)
                .set(PictureClassify::getIsDel, SystemConstants.OneZero.ONE)
                .eq(PictureClassify::getShopId, shopId)
                .eq(PictureClassify::getId, id)
                .update();
    }

    private void checkPictureClassify(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        PictureClassify entity = super.getById(id);
        if (entity == null) {
            throw new CzgException("图片分类不存在");
        }
        if (entity.getIsSystem() == SystemConstants.OneZero.ONE) {
            throw new CzgException("系统预设分类不可操作");
        }
        if (!shopId.equals(entity.getShopId())) {
            throw new CzgException("公共分类不可操作");
        }
    }

}