package com.czg.service.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.ShopInfo;
import com.czg.account.entity.ShopUser;
import com.czg.market.vo.MkConsumeCashbackRecordVO;
import com.czg.order.entity.OrderInfo;
import com.czg.utils.MyQueryWrapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkConsumeCashbackRecord;
import com.czg.market.service.MkConsumeCashbackRecordService;
import com.czg.service.market.mapper.MkConsumeCashbackRecordMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 消费返现明细 服务层实现。
 *
 * @author zs
 * @since 2025-10-13
 */
@Service
public class MkConsumeCashbackRecordServiceImpl extends ServiceImpl<MkConsumeCashbackRecordMapper, MkConsumeCashbackRecord> implements MkConsumeCashbackRecordService {
    @Override
    public Map<String, Object> getRecord(Long mainShopId, Long shopId, String key, LocalDateTime startTime, LocalDateTime endTime) {
        QueryWrapper queryWrapper = new MyQueryWrapper().eq(MkConsumeCashbackRecord::getMainShopId, mainShopId)
                .selectAll(MkConsumeCashbackRecord.class)
                .select(ShopInfo::getShopName)
                .select(ShopUser::getPhone, ShopUser::getNickName)
                .leftJoin(ShopInfo.class).on(ShopInfo::getId, MkConsumeCashbackRecord::getShopId)
                .leftJoin(OrderInfo.class).on(OrderInfo::getId, MkConsumeCashbackRecord::getOrderId)
                .leftJoin(ShopUser.class).on(ShopUser::getId, MkConsumeCashbackRecord::getShopUserId)
                .le(MkConsumeCashbackRecord::getCreateTime, endTime)
                .ge(MkConsumeCashbackRecord::getCreateTime, startTime)
                .eq(MkConsumeCashbackRecord::getShopId, shopId);

        QueryWrapper totalQuery = new QueryWrapper()
                .leftJoin(ShopInfo.class).on(ShopInfo::getId, MkConsumeCashbackRecord::getShopId)
                .leftJoin(OrderInfo.class).on(OrderInfo::getId, MkConsumeCashbackRecord::getOrderId)
                .leftJoin(ShopUser.class).on(ShopUser::getId, MkConsumeCashbackRecord::getShopUserId)
                .eq(MkConsumeCashbackRecord::getMainShopId, mainShopId)
                .le(MkConsumeCashbackRecord::getCreateTime, endTime)
                .ge(MkConsumeCashbackRecord::getCreateTime, startTime)
                .eq(MkConsumeCashbackRecord::getShopId, shopId);
        if (StrUtil.isNotBlank(key)) {
            totalQuery.and(and -> {
                and.or(or -> {
                    or.like(ShopInfo::getShopName, key);
                }).or(or -> {
                    or.like(OrderInfo::getOrderNo, key);
                }).or(or -> {
                    or.like(ShopUser::getPhone, key);
                }).or(or -> {
                    or.like(ShopUser::getNickName, key);
                });
            });
            queryWrapper.and(and -> {
                and.or(or -> {
                    or.like(ShopInfo::getShopName, key);
                }).or(or -> {
                    or.like(OrderInfo::getOrderNo, key);
                }).or(or -> {
                    or.like(ShopUser::getPhone, key);
                }).or(or -> {
                    or.like(ShopUser::getNickName, key);
                });
            });
        }


        BigDecimal amount = getOneAs(totalQuery.select("SUM(cashback_amount)"), BigDecimal.class);
        Map<String, Object> map = BeanUtil.beanToMap(pageAs(PageUtil.buildPage(), queryWrapper, MkConsumeCashbackRecordVO.class));
        map.put("totalAmount", amount == null ? BigDecimal.ZERO : amount);
        return map;
    }
}
