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

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.account.dto.shopuser.ShopUserAddDTO;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.*;
import com.czg.market.dto.MemberConfigDTO;
import com.czg.account.entity.ShopInfo;
import com.czg.account.entity.UserInfo;
import com.czg.market.service.TbMemberConfigService;
import com.czg.market.vo.MemberConfigVO;
import com.czg.exception.CzgException;
import com.czg.market.dto.MemberOrderDTO;
import com.czg.order.entity.OrderInfo;
import com.czg.order.entity.OrderPayment;
import com.czg.order.service.OrderInfoService;
import com.czg.order.service.OrderPaymentService;
import com.czg.service.market.enums.OrderStatusEnums;
import com.czg.service.market.mapper.MemberOrderMapper;
import com.czg.utils.AssertUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MemberOrder;
import com.czg.market.service.MemberOrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 会员充值订单 服务层实现。
 *
 * @author zs
 * @since 2025-09-11
 */
@DubboService
public class MemberOrderServiceImpl extends ServiceImpl<MemberOrderMapper, MemberOrder> implements MemberOrderService {
    @DubboReference
    private ShopInfoService shopInfoService;
    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private UShopUserService uShopUserService;
    @DubboReference
    private ShopUserService shopUserService;
    @DubboReference
    private TbMemberConfigService memberConfigService;
    @DubboReference
    private OrderInfoService orderInfoService;
    @DubboReference
    private OrderPaymentService paymentService;

    @Override
    public MemberOrder createMemberOrder(MemberOrderDTO orderDTO) {
        ShopInfo shopInfo = shopInfoService.getById(orderDTO.getShopId());
        AssertUtil.isNull(shopInfo, "生成订单失败，店铺信息不存在");

        UserInfo userInfo = userInfoService.getById(orderDTO.getUserId());
        AssertUtil.isNull(userInfo, "生成订单失败，用户信息不存在");


        MemberConfigVO memberConfigVO = memberConfigService.detail(shopInfo.getId());

        if (memberConfigVO.getIsSubmitInfo() == 1) {
            AssertUtil.isBlank(orderDTO.getBirthDay(), "生日不能为空");
            AssertUtil.isBlank(orderDTO.getNickName(), "名称不能为空");
            AssertUtil.isNull(orderDTO.getSex(), "性别不能为空");

            userInfo.setSex(orderDTO.getSex());
            userInfo.setNickName(orderDTO.getNickName());
            userInfo.setBirthDay(orderDTO.getBirthDay());
        }

        uShopUserService.join(orderDTO.getShopId(), orderDTO.getUserId(), new ShopUserAddDTO()
                .setPhone(userInfo.getPhone())
                .setSex(userInfo.getSex())
                .setHeadImg(userInfo.getHeadImg())
                .setAmount(BigDecimal.ZERO)
                .setBirthDay(userInfo.getBirthDay())
                .setIsVip(0)
                .setAccountPoints(0)
                .setNickName(userInfo.getNickName())
        );


        if ("PAY".equals(memberConfigVO.getOpenType())){
            if(memberConfigVO.getConfigList() == null || memberConfigVO.getConfigList().isEmpty()) {
                throw new CzgException("会员开通方案未配置，请联系店铺");
            }
            AssertUtil.isBlank(orderDTO.getName(), "方案名称不能为空");
            AssertUtil.isNull(orderDTO.getNum(), "开通周期数不为空");

            MemberConfigDTO.ConfigList configItem = memberConfigVO.getConfigList().stream()
                    .filter(item -> item.getName().equals(orderDTO.getName()))
                    .findFirst()
                    .orElseThrow(() -> new CzgException("会员开通方案未配置，请联系店铺"));

            //生成订单
            MemberOrder orderInfo = new MemberOrder();

            orderInfo.setOrderNo(orderDTO.getPlatformType() + "ME" + IdUtil.getSnowflakeNextId());
            orderInfo.setShopId(orderDTO.getShopId());
            orderInfo.setPayAmount(BigDecimal.ZERO);
            orderInfo.setStatus(OrderStatusEnums.UNPAID.getCode());
            orderInfo.setAmount(configItem.getPrice().multiply(BigDecimal.valueOf(orderDTO.getNum())));
            orderInfo.setPrice(configItem.getPrice());
            orderInfo.setName(configItem.getName());
            orderInfo.setUserId(orderDTO.getUserId());
            orderInfo.setReward(configItem.getReward());
            orderInfo.setCircleTime(configItem.getCircleTime());
            orderInfo.setCircleUnit(configItem.getCircleUnit());
            if (configItem.getCouponList() != null && !configItem.getCouponList().isEmpty()) {
                orderInfo.setCouponList(JSONObject.toJSONString(configItem.getCouponList()));
            }
            orderInfo.setNum(orderDTO.getNum());
            save(orderInfo);
            return orderInfo;
        }else {
            memberConfigService.joinMember(shopInfo.getId(), orderDTO.getUserId(), null);
            return null;
        }
    }
}
