package com.wzj.soopin.order.business.impl;

import com.wzj.soopin.member.domain.po.Member;
import com.wzj.soopin.member.service.IMemberService;
import com.wzj.soopin.order.business.IOrderBusiness;
import com.wzj.soopin.order.convert.OrderConvert;
import com.wzj.soopin.order.convert.OrderItemConvert;
import com.wzj.soopin.order.domain.bo.OrderBo;
import com.wzj.soopin.order.domain.entity.Order;
import com.wzj.soopin.order.domain.entity.OrderItem;
import com.wzj.soopin.order.domain.vo.OrderItemVO;
import com.wzj.soopin.order.domain.vo.OrderVO;
import com.wzj.soopin.order.service.OrderItemService;
import com.wzj.soopin.order.service.OrderService;
import com.wzj.soopin.order.service.VerificationCodeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.web.core.BusinessImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Slf4j
public class OrderBusinessImpl extends BusinessImpl<OrderService, OrderConvert, OrderVO, OrderBo, Order> implements IOrderBusiness {
    private final IMemberService memberService;
    private final OrderItemService orderItemService;
    private final OrderItemConvert orderItemConvert;
    private final VerificationCodeService verificationCodeService;

    @Override
    public OrderVO info(Serializable id) {
        OrderVO vo= super.info(id);
        Member member = memberService.getById(vo.getMemberId());
        vo.setMemberNickname(member.getNickname());
        vo.setMemberPhoneEncrypted(member.getPhoneHidden());

        //设置明细
        List<OrderItem> orderItemList = orderItemService.findByOrderId(vo.getId());
        List<OrderItemVO> itemVOList = orderItemList.stream().map(item -> {
            OrderItemVO itemVO=orderItemConvert.toVO(item);
            //赋值
            return itemVO;
        }).collect(Collectors.toList());

        vo.setItems(itemVOList);
        //核销码
        vo.setVerificationCodes(verificationCodeService.getByOrderId(vo.getId()));
        return vo;
    }
}
