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.MemberPointsLogDTO;
import com.czg.account.entity.MemberPointsLog;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.MemberPointsLogService;
import com.czg.sa.StpKit;
import com.czg.service.account.mapper.MemberPointsLogMapper;
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;

import static com.czg.account.entity.table.MemberPointsLogTableDef.MEMBER_POINTS_LOG;
import static com.czg.account.entity.table.MemberPointsTableDef.MEMBER_POINTS;
import static com.czg.account.entity.table.ShopUserTableDef.SHOP_USER;

/**
 * 会员积分变动记录
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-25
 */
@Service
public class MemberPointsLogServiceImpl extends ServiceImpl<MemberPointsLogMapper, MemberPointsLog> implements MemberPointsLogService {

    private QueryWrapper buildQueryWrapper(MemberPointsLogDTO param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        if (ObjUtil.isNotNull(param.getUserId())) {
            queryWrapper.eq(MemberPointsLog::getShopUserId, param.getUserId());
        }
        if (StrUtil.isNotBlank(param.getNickName())) {
            queryWrapper.like(ShopUser::getNickName, param.getNickName());
        }
        if (StrUtil.isNotBlank(param.getPhone())) {
            queryWrapper.like(ShopUser::getPhone, param.getPhone());
        }
        if (StrUtil.isNotBlank(param.getContent())) {
            queryWrapper.like(MemberPointsLog::getContent, param.getContent());
        }
        if (StrUtil.isNotBlank(param.getFloatType())) {
            queryWrapper.eq(MemberPointsLog::getFloatType, param.getFloatType());
        }
        if (StrUtil.isNotBlank(param.getOrderNo())) {
            queryWrapper.eq(MemberPointsLog::getOrderNo, param.getOrderNo());
        }
        if (StrUtil.isNotEmpty(param.getBeginDate())) {
            queryWrapper.ge(MemberPointsLog::getCreateTime, param.getBeginDate() + " 00:00:00");
        }
        if (StrUtil.isNotEmpty(param.getEndDate())) {
            queryWrapper.le(MemberPointsLog::getCreateTime, param.getEndDate() + " 23:59:59");
        }
        Long shopId = StpKit.USER.getShopId(0L);
        queryWrapper.eq(MemberPointsLog::getShopId, shopId);
        queryWrapper.orderBy(MemberPointsLog::getId, false);
        return queryWrapper;
    }

    private QueryWrapper buildFullQueryWrapper(MemberPointsLogDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        queryWrapper.select(MEMBER_POINTS.DEFAULT_COLUMNS)
                .select(SHOP_USER.NICK_NAME.as(MemberPointsLogDTO::getNickName), SHOP_USER.HEAD_IMG.as(MemberPointsLogDTO::getHeadImg), SHOP_USER.PHONE.as(MemberPointsLogDTO::getPhone))
                .from(MEMBER_POINTS_LOG)
                .leftJoin(SHOP_USER).on(SHOP_USER.ID.eq(MEMBER_POINTS.SHOP_USER_ID));
        return queryWrapper;
    }

    @Override
    public Page<MemberPointsLogDTO> getMemberPointsLogPage(MemberPointsLogDTO param) {
        QueryWrapper queryWrapper = buildFullQueryWrapper(param);
        return super.pageAs(PageUtil.buildPage(), queryWrapper, MemberPointsLogDTO.class);
    }
}