package com.tmerclub.cloud.order.controller.multishop;

import com.google.common.collect.Lists;
import com.tmerclub.cloud.common.order.constant.RefundStatusEnum;
import com.tmerclub.cloud.common.order.dto.OrderSelfStationDTO;
import com.tmerclub.cloud.common.order.vo.OrderItemVO;
import com.tmerclub.cloud.common.order.vo.OrderSelfStationVO;
import com.tmerclub.cloud.common.order.vo.OrderVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.order.service.OrderItemService;
import com.tmerclub.cloud.order.service.OrderRefundService;
import com.tmerclub.cloud.order.service.OrderSelfStationService;
import com.tmerclub.cloud.order.service.OrderService;
import com.tmerclub.cloud.order.vo.OrderRefundVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
/**
 * @author lanhai
 */
@RestController("multishopOrderSelfStationController")
@RequestMapping("/m/order_self_station")
@Tag(name = "商家端自提订单接口")
public class OrderSelfStationController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderSelfStationService orderSelfStationService;
    @Autowired
    private OrderRefundService orderRefundService;
    @Autowired
    private OrderItemService orderItemService;

    @GetMapping("/get_order_item_and_station_info")
    @Operation(summary = "获取提货订单详情")
    public ServerResponseEntity<OrderSelfStationVO> getOrderItemAndStationInfo(@RequestParam("orderId") Long orderId) {
        OrderVO orderVO = orderService.getOrderByOrderId(orderId);
        List<OrderItemVO> orderItemVOList = orderItemService.listOrderItemAndLangByOrderId(orderId);
        orderVO.setOrderItems(orderItemVOList);
        OrderSelfStationVO orderSelfStation = orderSelfStationService.getStationByOrderId(null, orderId);
        orderSelfStation.setOrderStatus(orderVO.getStatus());
        if (Objects.nonNull(orderVO.getRefundStatus()) && !Objects.equals(orderVO.getRefundStatus(), RefundStatusEnum.DISAGREE.value())) {
            List<OrderRefundVO> refundVOList = orderRefundService.getProcessingOrderRefundByOrderId(orderVO.getOrderId());
            for (OrderRefundVO orderRefund : refundVOList) {
                //整单退款
                if (orderRefund.getRefundType() == 1) {
                    for (OrderItemVO orderItem : orderVO.getOrderItems()) {
                        orderItem.setReturnMoneySts(orderRefund.getReturnMoneySts());
                    }
                    break;
                }
                // 单项退款，每个单号都不一样
                for (OrderItemVO orderItem : orderVO.getOrderItems()) {
                    if (Objects.equals(orderItem.getOrderItemId(), orderRefund.getOrderItemId())) {
                        orderItem.setReturnMoneySts(orderRefund.getReturnMoneySts());
                    }
                }
            }
        }
        orderSelfStation.setOrderItems(orderVO.getOrderItems());
        return ServerResponseEntity.success(orderSelfStation);
    }

    @PutMapping("/change_status_and_order_station")
    @Operation(summary = "自提订单发货")
    public ServerResponseEntity<Void> changeStatusAndOrderStation(@RequestBody OrderSelfStationDTO orderSelfStationDTO) {
        List<Long> orderIdList = Lists.newArrayList();
        orderIdList.add(orderSelfStationDTO.getOrderId());
        orderService.orderStationByOrderId(orderIdList, null, orderSelfStationDTO.getStationId());
        return ServerResponseEntity.success();
    }
}
