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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.czg.account.service.ShopUserService;
import com.czg.market.dto.MemberLevelDTO;
import com.czg.market.entity.MemberLevelConfig;
import com.czg.market.service.MemberLevelConfigService;
import com.czg.market.service.ShopCouponService;
import com.czg.market.vo.MemberLevelVO;
import com.czg.service.market.mapper.TbMemberLevelConfigMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员等级配置 服务层实现。
 *
 * @author zs
 * @since 2025-09-10
 */
@DubboService
public class TbMemberLevelConfigServiceImpl extends ServiceImpl<TbMemberLevelConfigMapper, MemberLevelConfig> implements MemberLevelConfigService {
    @Resource
    private ShopCouponService shopCouponService;
    @DubboReference
    private ShopUserService shopUserService;

    @Override
    public ArrayList<MemberLevelVO> listInfo(Long shopId) {
        List<MemberLevelConfig> levelConfigs = list(new QueryWrapper().eq(MemberLevelConfig::getShopId, shopId));
        ArrayList<MemberLevelVO> memberLevels = new ArrayList<>();
        levelConfigs.forEach(memberLevelConfig -> memberLevels.add(transVO(memberLevelConfig)));
        return memberLevels;
    }

    @Override
    public Boolean removeLevel(Long id, Long shopId) {
        boolean result = remove(new QueryWrapper().eq(MemberLevelConfig::getId, id).eq(MemberLevelConfig::getShopId, shopId));
        if (result) {
            MemberLevelConfig one = getOne(query().lt(MemberLevelConfig::getId, id).eq(MemberLevelConfig::getShopId, shopId).orderBy(MemberLevelConfig::getId, false));
            if (one != null) {
                shopUserService.upMemberLevel(one.getId(), id);
            } else {
                //设置等级id为null
                shopUserService.upMemberLevel(null, id);
            }
        }
        return result;
    }

    private MemberLevelVO transVO(MemberLevelConfig memberLevelConfig) {
        MemberLevelVO levelVO = BeanUtil.copyProperties(memberLevelConfig, MemberLevelVO.class, "cycleRewardCouponList");
        if (StrUtil.isNotBlank(memberLevelConfig.getCycleRewardCouponList())) {
            List<MemberLevelDTO.ConfigCoupon> coupons = JSONArray.parseArray(memberLevelConfig.getCycleRewardCouponList()).toList(MemberLevelDTO.ConfigCoupon.class);
            coupons.forEach(item -> item.setCoupon(shopCouponService.getById(item.getCoupon().getId())));
            levelVO.setCycleRewardCouponList(coupons);
        }
        return levelVO;
    }

    @Override
    public MemberLevelVO detail(Long memberLevelId) {

        MemberLevelConfig memberLevelConfig = getById(memberLevelId);
        if (memberLevelConfig == null) {
            return null;
        }
        return transVO(memberLevelConfig);
    }
}
