package io.modules.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.util.StrUtil;
import io.common.service.impl.CrudServiceImpl;
import io.modules.item.dao.OrderDao;
import io.modules.item.dto.OrderDTO;
import io.modules.item.dto.UserDTO;
import io.modules.item.entity.OrderEntity;
import io.modules.item.service.OrderService;
import io.modules.item.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 订单表
 */
@Service
public class OrderServiceImpl extends CrudServiceImpl<OrderDao, OrderEntity, OrderDTO> implements OrderService {

    @Autowired
    private UserService userService;

    @Override
    public QueryWrapper<OrderEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String orderStatus = (String)params.get("orderStatus");
        String paymentStatus = (String)params.get("paymentStatus");
        String userId = (String)params.get("userId");
        String itemId = (String)params.get("itemId");
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.eq(StrUtil.isNotBlank(itemId), "item_id", itemId);
        wrapper.eq(StrUtil.isNotBlank(orderStatus), "order_status", orderStatus);
        wrapper.eq(StrUtil.isNotBlank(paymentStatus), "payment_status", paymentStatus);
        wrapper.eq(StrUtil.isNotBlank(userId), "user_id", userId);
        return wrapper;
    }

    /**
     * 获取评论数据列表
     * @param id
     * @return
     */
    @Override
    public List<OrderDTO> getCommintList(Long id) {
        LambdaQueryWrapper<OrderEntity> lwq = new LambdaQueryWrapper<>();
        lwq.eq(OrderEntity::getOrderStatus,3);
        List<OrderEntity> list = baseDao.selectList(lwq);
        List<OrderDTO> stream = list.stream().map(e -> {
            OrderDTO dto = new OrderDTO();
            BeanUtils.copyProperties(e, dto);
            if (dto.getUserId() != null) {
                UserDTO userDTO = userService.get(e.getUserId());
                if (userDTO!= null) {
                    dto.setUser(userDTO);
                }
            }
            return dto;
        }).collect(Collectors.toList());
        return stream;
    }
}