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.account.service.ShopUserService;
import com.czg.constant.TableValueConstant;
import com.czg.market.vo.MkDistributionWithdrawFlowVO;
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.MkDistributionWithdrawFlow;
import com.czg.market.service.MkDistributionWithdrawFlowService;
import com.czg.service.market.mapper.MkDistributionWithdrawFlowMapper;
import org.apache.dubbo.config.annotation.DubboReference;
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 MkDistributionWithdrawFlowServiceImpl extends ServiceImpl<MkDistributionWithdrawFlowMapper, MkDistributionWithdrawFlow> implements MkDistributionWithdrawFlowService{
    @DubboReference
    private ShopUserService shopUserService;

    @Override
    public Page<MkDistributionWithdrawFlow> pageInfo(long userId) {
        return page(PageUtil.buildPage(), new QueryWrapper().eq(MkDistributionWithdrawFlow::getUserId, userId));
    }

    @Override
    public Map<String, Object> withdrawPageInfo(Long userId, LocalDateTime startTime, LocalDateTime endTime, String key) {
        QueryWrapper queryWrapper = new MyQueryWrapper()
                .selectAll(MkDistributionWithdrawFlow.class)
                .leftJoin(UserInfo.class).on(UserInfo::getId, MkDistributionWithdrawFlow::getUserId)
                .select(UserInfo::getNickName, UserInfo::getPhone)
                .eq(MkDistributionWithdrawFlow::getUserId, userId)
                .ge(MkDistributionWithdrawFlow::getCreateTime, startTime)
                .le(MkDistributionWithdrawFlow::getCreateTime, endTime);
        if (StrUtil.isNotBlank(key)){
            queryWrapper.and(and -> {
                and.or(or -> {
                    or.like(UserInfo::getId, key);
                });

                and.or(or -> {
                    or.like(UserInfo::getNickName, key);
                });

                and.or(or -> {
                    or.like(UserInfo::getPhone, key);
                });
            });
        }
        Page<MkDistributionWithdrawFlowVO> page = pageAs(PageUtil.buildPage(), queryWrapper.orderBy(MkDistributionWithdrawFlow::getId, false), MkDistributionWithdrawFlowVO.class);
        Map<String, Object> map = BeanUtil.beanToMap(page);
        map.put("pending", getOneAs(new QueryWrapper().eq(MkDistributionWithdrawFlow::getStatus, TableValueConstant.DistributionWithdrawFlow.Status.PENDING.getCode()).select("sum(amount)"), BigDecimal.class));
        map.put("finish", getOneAs(new QueryWrapper().eq(MkDistributionWithdrawFlow::getStatus, TableValueConstant.DistributionWithdrawFlow.Status.FINISH.getCode()).select("sum(amount)"), BigDecimal.class));
        return map;
    }
}
