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

import com.czg.account.dto.user.userinfo.UserInfoDTO;
import com.czg.account.entity.ShopInfo;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.UserInfoService;
import com.czg.market.entity.MkPointsConfig;
import com.czg.market.entity.MkPointsUser;
import com.czg.market.service.*;
import com.czg.market.vo.MkDistributionConfigVO;
import com.czg.market.vo.UserCouponVO;
import com.czg.market.vo.UserHomeDataVo;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author yjjie
 * @date 2025/12/11 11:33
 */
@Service
public class UserHomeServiceImpl implements UserHomeService {

    @Resource
    private MkPointsConfigService mkPointsConfigService;

    @Resource
    private MkDistributionConfigService mkDistributionConfigService;

    @Resource
    private MkPointsUserService mkPointsUserService;

    @Resource
    private ShopCouponService couponService;

    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private ShopInfoService shopInfoService;

    @Override
    public UserHomeDataVo getUserHomeData(Long userId, Long shopId) {
        UserHomeDataVo userHomeDataVo = new UserHomeDataVo()
                .setShopId(shopId)
                .setUserId(userId)
                .setTakeout(0);

        ShopInfo shopInfo = shopInfoService.getById(shopId);
        if (shopInfo != null) {
//            userHomeDataVo.setGroup(shopInfo.getIsGroupBuy());
            userHomeDataVo.setGroup(1);
        }

        // 用户信息
        UserInfoDTO info = userInfoService.getInfo(userId);
        if (info != null) {
            userHomeDataVo
                    .setUserNickname(info.getNickName())
                    .setUserAvatar(info.getHeadImg());
        }

        // 积分商城
        MkPointsConfig pointsConfig = mkPointsConfigService.getById(shopId);
        if (pointsConfig != null) {
//            userHomeDataVo.setPointsMall(pointsConfig.getEnablePointsMall());
            userHomeDataVo.setPointsMall(1);
        }

        // 分销
        MkDistributionConfigVO distributionConfig = mkDistributionConfigService.detail(shopId);
        if (distributionConfig != null) {
            userHomeDataVo.setDistribution(distributionConfig.getIsEnable());
        }

        // 积分
        MkPointsUser pointsUser = mkPointsUserService.getPointsUser(shopId, null, userId);
        if (pointsUser != null) {
            userHomeDataVo.setPointNum(pointsUser.getPointBalance());
        }

        // 优惠券
        Page<UserCouponVO> coupon = couponService.find(userId, "", shopId, 0);
        userHomeDataVo.setCouponNum(coupon.getTotalRow());

        return userHomeDataVo;
    }
}
