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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.ShopUserService;
import com.czg.constant.TableValueConstant;
import com.czg.market.entity.MkDistributionAmountFlow;
import com.czg.market.vo.MkDistributionAmountFlowVO;
import com.czg.market.vo.MkDistributionFlowVO;
import com.czg.order.entity.OrderInfo;
import com.czg.utils.MyQueryWrapper;
import com.czg.utils.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkDistributionFlow;
import com.czg.market.service.MkDistributionFlowService;
import com.czg.service.market.mapper.MkDistributionFlowMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

/**
 * 分销记录表 服务层实现。
 *
 * @author ww
 * @since 2025-10-27
 */
@Service
public class MkDistributionFlowServiceImpl extends ServiceImpl<MkDistributionFlowMapper, MkDistributionFlow> implements MkDistributionFlowService{
    @DubboReference
    private ShopInfoService shopInfoService;
    @DubboReference
    private ShopUserService shopUserService;

    @Override
    public Map<String, Object> pageInfo(Long shopId, LocalDateTime startTime, LocalDateTime endTime, String key, String status, Long id) {
        PageHelper.startPage(PageUtil.buildPageHelp());
        List<MkDistributionFlowVO> list = mapper.pageInfo(shopId, startTime, endTime, status, key, id, null, null);
        Page<MkDistributionFlowVO> page = PageUtil.convert(new PageInfo<>(list));
        Map<String, Object> map = BeanUtil.beanToMap(page);
        map.put("successAmount", getOneAs(new QueryWrapper().eq(MkDistributionFlow::getShopId, shopId)
                .eq(MkDistributionFlow::getStatus, TableValueConstant.DistributionFlow.Status.SUCCESS.getCode())
                .select("sum(reward_amount)"), BigDecimal.class));
        map.put("pendingAmount", getOneAs(new QueryWrapper().eq(MkDistributionFlow::getShopId, shopId)
                .eq(MkDistributionFlow::getStatus, TableValueConstant.DistributionFlow.Status.PENDING.getCode())
                .select("sum(reward_amount)"), BigDecimal.class));
        map.put("balanceAmount", shopInfoService.getById(shopId).getAmount());
        return map;
    }

    @Override
    public Map<String, Object> distributionFlow(long userId, String startTime, String endTime, Long shopId, String status) {
        ShopUser shopUserInfo = null;
        if (shopId != null) {
            shopUserInfo = shopUserService.getShopUserInfo(shopId, userId);
        }
        PageHelper.startPage(PageUtil.buildPageHelp());
        List<MkDistributionFlowVO> list = mapper.pageInfo(shopId, StrUtil.isBlank(startTime) ? null : DateUtil.parseLocalDateTime(startTime),
                StrUtil.isBlank(endTime) ? null : DateUtil.parseLocalDateTime(endTime), status,null, shopUserInfo == null ? null : shopUserInfo.getId(), null, userId);
        Page<MkDistributionFlowVO> page = PageUtil.convert(new PageInfo<>(list));
        Map<String, Object> map = BeanUtil.beanToMap(page);
        map.put("totalAmount", mapper.totalAmount(shopId, StrUtil.isBlank(startTime) ? null : DateUtil.parseLocalDateTime(startTime),
                StrUtil.isBlank(endTime) ? null : DateUtil.parseLocalDateTime(endTime), status,null, shopUserInfo == null ? null : shopUserInfo.getId(), null, userId));
        return map;
    }
}
