package cn.ysk.cashier.mybatis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapProxy;
import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.mybatis.entity.TbMemberPointsLog;
import cn.ysk.cashier.mybatis.mapper.TbMemberPointsLogMapper;
import cn.ysk.cashier.mybatis.service.TbMemberPointsLogService;
import cn.ysk.cashier.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 会员积分变动记录
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 2.0 2024-10-25
 */
@Service
public class TbMemberPointsLogServiceImpl extends ServiceImpl<TbMemberPointsLogMapper, TbMemberPointsLog> implements TbMemberPointsLogService {

    private LambdaQueryWrapper<TbMemberPointsLog> getWrapper(Map<String, Object> params) {
        MapProxy mapProxy = MapProxy.create(params);
        String beginDate = mapProxy.getStr("beginDate");
        String endDate = mapProxy.getStr("endDate");
        TbMemberPointsLog param = BeanUtil.toBean(params, TbMemberPointsLog.class);

        LambdaQueryWrapper<TbMemberPointsLog> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbMemberPointsLog::getShopId, param.getShopId());
        wrapper.eq(param.getMemberId() != null, TbMemberPointsLog::getMemberId, param.getMemberId());
        wrapper.like(StrUtil.isNotEmpty(param.getMemberName()), TbMemberPointsLog::getMemberName, param.getMemberName());
        wrapper.like(StrUtil.isNotEmpty(param.getMobile()), TbMemberPointsLog::getMobile, param.getMobile());
        wrapper.like(StrUtil.isNotEmpty(param.getContent()), TbMemberPointsLog::getContent, param.getContent());
        wrapper.eq(StrUtil.isNotEmpty(param.getFloatType()), TbMemberPointsLog::getFloatType, param.getFloatType());
        wrapper.eq(StrUtil.isNotEmpty(param.getOrderNo()), TbMemberPointsLog::getOrderNo, param.getOrderNo());
        if (StrUtil.isNotEmpty(beginDate)) {
            wrapper.apply("create_time >= str_to_date({0}, '%Y-%m-%d %H:%i:%s')", beginDate + " 00:00:00");
        }
        if (StrUtil.isNotEmpty(endDate)) {
            wrapper.apply("create_time <= str_to_date({0}, '%Y-%m-%d %H:%i:%s')", endDate + " 23:59:59");
        }
        wrapper.orderByDesc(TbMemberPointsLog::getId);
        return wrapper;
    }

    @Override
    public Map<String, Object> page(Map<String, Object> params) {
        MapProxy mapProxy = MapProxy.create(params);
        int pageNum = mapProxy.getInt("page", 1);
        int pageSize = mapProxy.getInt("size", 10);
        LambdaQueryWrapper<TbMemberPointsLog> wrapper = getWrapper(params);
        Page<TbMemberPointsLog> page = super.page(new Page<>(pageNum, pageSize), wrapper);
        return PageUtil.toPlusPage(page.getRecords(), Convert.toInt(page.getTotal()));
    }
}