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

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.points.PointsGoodsSettingDTO;
import com.czg.account.entity.PointsGoodsSetting;
import com.czg.account.service.PointsGoodsSettingService;
import com.czg.enums.DeleteEnum;
import com.czg.sa.StpKit;
import com.czg.service.account.mapper.PointsGoodsSettingMapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 积分商品设置
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-25
 */
@Service
public class PointsGoodsSettingServiceImpl extends ServiceImpl<PointsGoodsSettingMapper, PointsGoodsSetting> implements PointsGoodsSettingService {

    private QueryWrapper buildQueryWrapper(PointsGoodsSettingDTO param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        if (StrUtil.isNotEmpty(param.getGoodsName())) {
            queryWrapper.like(PointsGoodsSetting::getGoodsName, param.getGoodsName());
        }
        if (StrUtil.isNotEmpty(param.getGoodsCategory())) {
            queryWrapper.eq(PointsGoodsSetting::getGoodsCategory, param.getGoodsCategory());
        }
        if (ObjUtil.isNotNull(param.getStatus())) {
            queryWrapper.eq(PointsGoodsSetting::getStatus, param.getStatus());
        }
        Long shopId = StpKit.USER.getShopId(0L);
        queryWrapper.eq(PointsGoodsSetting::getDelFlag, DeleteEnum.NORMAL.value());
        queryWrapper.eq(PointsGoodsSetting::getShopId, shopId);
        queryWrapper.orderBy(PointsGoodsSetting::getSort, false);
        queryWrapper.orderBy(PointsGoodsSetting::getId, false);
        return queryWrapper;
    }

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