package com.czg.service.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.enums.StatusEnum;
import com.czg.exception.CzgException;
import com.czg.product.dto.ShopProdCategoryDTO;
import com.czg.product.entity.ShopProdCategory;
import com.czg.product.service.ProductService;
import com.czg.product.service.ShopProdCategoryService;
import com.czg.sa.StpKit;
import com.czg.service.product.mapper.ShopProdCategoryMapper;
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 jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 店铺商品分类
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-10
 */
@Service
public class ShopProdCategoryServiceImpl extends ServiceImpl<ShopProdCategoryMapper, ShopProdCategory> implements ShopProdCategoryService {

    @Resource
    @Lazy
    private ProductService productService;

    private QueryWrapper buildQueryWrapper(ShopProdCategoryDTO param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        if (StrUtil.isNotEmpty(param.getName())) {
            queryWrapper.like(ShopProdCategory::getName, param.getName());
        }
        if (param.getShopId() == null) {
            Long shopId = StpKit.USER.getShopId(0L);
            param.setShopId(shopId);
        }
        queryWrapper.eq(ShopProdCategory::getShopId, param.getShopId());
        queryWrapper.orderBy(ShopProdCategory::getSort, true);
        queryWrapper.orderBy(ShopProdCategory::getId, false);
        return queryWrapper;
    }

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

    @Override
    public List<ShopProdCategoryDTO> getShopProdCategoryList(ShopProdCategoryDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        queryWrapper.eq(ShopProdCategory::getStatus, StatusEnum.ENABLED.value());
        return super.listAs(queryWrapper, ShopProdCategoryDTO.class);
    }

    @Override
    public ShopProdCategoryDTO getShopProdCategoryById(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        return super.getOneAs(query().eq(ShopProdCategory::getId, id).eq(ShopProdCategory::getShopId, shopId), ShopProdCategoryDTO.class);
    }

    @Override
    public void addShopProdCategory(ShopProdCategoryDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        boolean exists = super.exists(query().eq(ShopProdCategory::getName, dto.getName()).eq(ShopProdCategory::getShopId, shopId));
        if (exists) {
            throw new CzgException("商品分类已存在");
        }
        ShopProdCategory entity = BeanUtil.copyProperties(dto, ShopProdCategory.class);
        // 简称
        entity.setShortName(dto.getName());
        if (entity.getStatus() == null) {
            entity.setStatus(StatusEnum.ENABLED.value());
        }
        entity.setPid(0L);
        entity.setShopId(shopId);
        super.save(entity);
        dto.setId(entity.getId());
    }

    @Override
    public void updateShopProdCategory(ShopProdCategoryDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        dto.setShopId(shopId);
        boolean exists = super.exists(query().eq(ShopProdCategory::getName, dto.getName()).eq(ShopProdCategory::getShopId, shopId).ne(ShopProdCategory::getId, dto.getId()));
        if (exists) {
            throw new CzgException("商品分类已存在");
        }
        ShopProdCategory entity = BeanUtil.copyProperties(dto, ShopProdCategory.class);
        entity.setPid(0L);
        super.updateById(entity);
    }

    @Override
    public void deleteShopProdCategory(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        super.remove(query().eq(ShopProdCategory::getId, id).eq(ShopProdCategory::getShopId, shopId));
        productService.clearProductCache(id);
    }

    @Override
    public void disableShopProdCategory(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        UpdateChain.of(ShopProdCategory.class)
                .set(ShopProdCategory::getStatus, StatusEnum.DISABLE.value())
                .eq(ShopProdCategory::getId, id)
                .eq(ShopProdCategory::getShopId, shopId)
                .update();
        productService.clearProductCache(id);
    }

    @Override
    public void enableShopProdCategory(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        UpdateChain.of(ShopProdCategory.class)
                .set(ShopProdCategory::getStatus, StatusEnum.ENABLED.value())
                .eq(ShopProdCategory::getId, id)
                .eq(ShopProdCategory::getShopId, shopId)
                .update();
        productService.clearProductCache(id);
    }

}