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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.ShopUser;
import com.czg.account.entity.UserInfo;
import com.czg.constant.TableValueConstant;
import com.czg.market.vo.MkDistributionAmountFlowVO;
import com.czg.order.entity.OrderInfo;
import com.czg.order.entity.OrderPayment;
import com.czg.utils.MyQueryWrapper;
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 com.czg.market.entity.MkDistributionAmountFlow;
import com.czg.market.service.MkDistributionAmountFlowService;
import com.czg.service.market.mapper.MkDistributionAmountFlowMapper;
import org.springframework.stereotype.Service;

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

/**
 * 分销运营余额扣减记录 服务层实现。
 *
 * @author ww
 * @since 2025-10-27
 */
@Service
public class MkDistributionAmountFlowServiceImpl extends ServiceImpl<MkDistributionAmountFlowMapper, MkDistributionAmountFlow> implements MkDistributionAmountFlowService{

    @Override
    public Map<String, Object> pageInfo(Long shopId, String type, String key, LocalDateTime startTime, LocalDateTime endTime) {
        QueryWrapper queryWrapper = new MyQueryWrapper()
                .selectAll(MkDistributionAmountFlow.class)
                .leftJoin(OrderInfo.class).on(OrderInfo::getId, MkDistributionAmountFlow::getSourceId)
                .leftJoin(ShopUser.class).on(ShopUser::getId, OrderInfo::getUserId)
                .eq(MkDistributionAmountFlow::getShopId, shopId)
                .le(MkDistributionAmountFlow::getCreateTime, endTime)
                .ge(MkDistributionAmountFlow::getCreateTime, startTime)
                .ne(MkDistributionAmountFlow::getType, TableValueConstant.DistributionAmountFlow.Type.OPEN.getCode())
                .select(OrderInfo::getOrderNo);
        if (StrUtil.isNotBlank(type)) {
            String[] split = type.split(",");
            if (split.length != 0) {
                queryWrapper.in(MkDistributionAmountFlow::getType, split);
            }else {
                queryWrapper.eq(MkDistributionAmountFlow::getType, type);
            }
        }
        if (StrUtil.isNotBlank(key)) {
            queryWrapper.and(and -> {
                and.or(or -> {
                   or.like(OrderInfo::getOrderNo, key);
                });
                and.or(or -> {
                    or.like(ShopUser::getNickName, key);
                });
                and.or(or -> {
                    or.like(ShopUser::getPhone, key);
                });
            });
        }

        Page<MkDistributionAmountFlowVO> pageInfo = pageAs(PageUtil.buildPage(), queryWrapper.orderBy(MkDistributionAmountFlow::getCreateTime, false), MkDistributionAmountFlowVO.class);
        Map<String, Object> map = BeanUtil.beanToMap(pageInfo);
        map.put("totalRecharge", getOneAs(new QueryWrapper().eq(MkDistributionAmountFlow::getShopId, shopId)
                .in(MkDistributionAmountFlow::getType, TableValueConstant.DistributionAmountFlow.Type.MANUAL_RECHARGE.getCode(), TableValueConstant.DistributionAmountFlow.Type.SELF_RECHARGE.getCode())
                .select("sum(change_amount)"), BigDecimal.class));
        map.put("totalSub", getOneAs(new QueryWrapper().eq(MkDistributionAmountFlow::getShopId, shopId)
                .in(MkDistributionAmountFlow::getType, TableValueConstant.DistributionAmountFlow.Type.SUB.getCode(), TableValueConstant.DistributionAmountFlow.Type.MANUAL_SUB.getCode())
                .select("sum(change_amount)"), BigDecimal.class));
        return map;
    }

    @Override
    public Map<String, Object> openPageInfo(Long shopId, LocalDateTime startTime, LocalDateTime endTime, String key) {
        QueryWrapper queryWrapper = new MyQueryWrapper()
                .selectAll(MkDistributionAmountFlow.class)
                .leftJoin(OrderPayment.class).on(OrderPayment::getId, MkDistributionAmountFlow::getSourceId)
                .leftJoin(ShopUser.class).on(ShopUser::getId, OrderPayment::getSourceId)
                .eq(MkDistributionAmountFlow::getShopId, shopId)
                .select(OrderPayment::getOrderNo)
                .eq(MkDistributionAmountFlow::getType, TableValueConstant.DistributionAmountFlow.Type.OPEN.getCode())
                .select(ShopUser::getPhone, ShopUser::getNickName);
        if (startTime != null) {
            queryWrapper.ge(MkDistributionAmountFlow::getCreateTime, startTime);
        }

        if (endTime != null) {
            queryWrapper.le(MkDistributionAmountFlow::getCreateTime, endTime);
        }

        if (StrUtil.isNotBlank(key)) {
            queryWrapper.and(and -> {
                and.or(or -> {
                    or.like(OrderPayment::getOrderNo, key);
                });
                and.or(or -> {
                    or.like(ShopUser::getNickName, key);
                });
                and.or(or -> {
                    or.like(ShopUser::getPhone, key);
                });
            });
        }

        Page<MkDistributionAmountFlowVO> pageInfo = pageAs(PageUtil.buildPage(), queryWrapper.orderBy(MkDistributionAmountFlow::getCreateTime, false), MkDistributionAmountFlowVO.class);
        Map<String, Object> map = BeanUtil.beanToMap(pageInfo);
        map.put("totalAmount", getOneAs(new QueryWrapper().eq(MkDistributionAmountFlow::getShopId, shopId)
                .in(MkDistributionAmountFlow::getType, TableValueConstant.DistributionAmountFlow.Type.OPEN.getCode())
                .select("sum(change_amount)"), BigDecimal.class));
        map.put("totalCount", count(new QueryWrapper().eq(MkDistributionAmountFlow::getShopId, shopId)
                .in(MkDistributionAmountFlow::getType, TableValueConstant.DistributionAmountFlow.Type.OPEN.getCode())));
        return map;
    }

    public static void main(String[] args) {
        QueryWrapper queryWrapper = new MyQueryWrapper()
                .selectAll(MkDistributionAmountFlow.class)
                .leftJoin(OrderInfo.class).on(OrderInfo::getId, MkDistributionAmountFlow::getSourceId)
                .leftJoin(ShopUser.class).on(ShopUser::getId, OrderInfo::getUserId)
                .select(OrderInfo::getOrderNo);
            queryWrapper.and(and -> {

            });
        System.out.println(queryWrapper.toSQL());
    }
}
