package com.ruoyi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.annotations.UserLoginToken;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.controller.req.OrderMessagePageReq;
import com.ruoyi.controller.resp.MallOrderPageResp;
import com.ruoyi.controller.resp.MessageOrderPageResp;
import com.ruoyi.controller.resp.OrderDetailResp;
import com.ruoyi.frequency.mallorder.service.MallOrderService;
import com.ruoyi.frequency.order.mapper.OrderMapper;
import com.ruoyi.frequency.ordermessage.entity.OrderMessage;
import com.ruoyi.frequency.ordermessage.entity.OrderMessageUtil;
import com.ruoyi.frequency.ordermessage.service.OrderMessageService;
import com.ruoyi.controller.req.MessageReadOrDeleteReq;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.PageBasic;
import com.ruoyi.vo.RyController;
import com.ruoyi.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * 订单消息
 * @author liwenlong
 *
 */
@CrossOrigin
@RestController
@RequestMapping("/api/message")
@Api(tags = "订单消息")
public class ApiOrderMessageController extends RyController {

    @Autowired
    private OrderMessageService orderMessageService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MallOrderService mallOrderService;


    @UserLoginToken
    @PostMapping("/orderMessagePage")
    @ApiOperation(value = "订单消息列表", notes = "订单消息列表")
    public ResponseData<List<OrderMessageUtil>> orderMessageList(@RequestBody OrderMessagePageReq req) {

        UserVo userVo = getUserVo();

        List<OrderMessage> orderMessageList = this.orderMessageService.orderMessageList(req,userVo);

        if (orderMessageList.size() > 0){
            List<Long> orderIds = new ArrayList<>();

            List<Long> mallOrderIds = new ArrayList<>();

            orderMessageList.stream().forEach(s->{
                if (ObjectUtil.equal(PublicCommon.Order.订单,s.getOrderType())){
                    orderIds.add(s.getOrderId());
                }else {
                    mallOrderIds.add(s.getOrderId());
                }
            });

            //订单
            orderIds.add(-1L);
            List<OrderDetailResp> OrderDetailResps = orderMapper.orderList(orderIds, userVo);
            Map<Long, List<OrderDetailResp>> orderMap = OrderDetailResps.stream().collect(Collectors.groupingBy(OrderDetailResp::getId));

            //商城订单
            mallOrderIds.add(-1L);
            List<MallOrderPageResp> mallOrderPageResps =  mallOrderService.orderList(mallOrderIds, userVo);
            Map<Long, List<MallOrderPageResp>> mallOrderMap = mallOrderPageResps.stream().collect(Collectors.groupingBy(MallOrderPageResp::getOrderId));

            orderMessageList.forEach(orderMessage ->{

                if (ObjectUtil.equal(PublicCommon.Order.订单,orderMessage.getOrderType())){
                    List<OrderDetailResp> orderDetailRespList = orderMap.get(orderMessage.getOrderId());
                    if (orderDetailRespList != null &&  orderDetailRespList.size() > 0) {
                        orderMessage.setOrder(orderDetailRespList.get(0));
                    }
                }else {
                    List<MallOrderPageResp> mallOrderPageRespList = mallOrderMap.get(orderMessage.getOrderId());
                    if (mallOrderPageRespList != null &&  mallOrderPageRespList.size() > 0) {
                        orderMessage.setMallOrder(mallOrderPageRespList.get(0));
                    }
                }

            }) ;
        }

        return ResponseData.success(orderMessageList);
    }


    @UserLoginToken
    @PostMapping("/messageOrderPage")
    @ApiOperation(value = "订单消息订单分组列表(按订单展示)", notes = "订单消息订单分组列表(按订单展示)")
    public ResponseData<PageResult<Page<MessageOrderPageResp>>> orderMessagePage(@RequestBody PageBasic req) {

        UserVo userVo = getUserVo();

        Page<MessageOrderPageResp> messageOrderPage = this.orderMessageService.messageOrderPage(req,userVo);

        if (messageOrderPage.getRecords().size() > 0){
            List<Long> orderIds = new ArrayList<>();

            List<Long> mallOrderIds = new ArrayList<>();

            messageOrderPage.getRecords().stream().forEach(s->{
                if (ObjectUtil.equal(PublicCommon.Order.订单,s.getOrderType())){
                    orderIds.add(s.getOrderId());
                }else {
                    mallOrderIds.add(s.getOrderId());
                }
            });


            //订单
            orderIds.add(-1L);
            List<OrderDetailResp> OrderDetailResps = orderMapper.orderList(orderIds, userVo);
            Map<Long, List<OrderDetailResp>> orderMap = OrderDetailResps.stream().collect(Collectors.groupingBy(OrderDetailResp::getId));

            //商城订单
            mallOrderIds.add(-1L);
            List<MallOrderPageResp> mallOrderPageResps =  mallOrderService.orderList(mallOrderIds, userVo);
            Map<Long, List<MallOrderPageResp>> mallOrderMap = mallOrderPageResps.stream().collect(Collectors.groupingBy(MallOrderPageResp::getOrderId));

            messageOrderPage.getRecords().forEach(orderMessage ->{

                if (ObjectUtil.equal(PublicCommon.Order.订单,orderMessage.getOrderType())){
                    List<OrderDetailResp> orderDetailRespList = orderMap.get(orderMessage.getOrderId());
                    if (orderDetailRespList != null &&  orderDetailRespList.size() > 0) {
                        orderMessage.setOrder(orderDetailRespList.get(0));
                    }
                }else {
                    List<MallOrderPageResp> mallOrderPageRespList = mallOrderMap.get(orderMessage.getOrderId());
                    if (mallOrderPageRespList != null &&  mallOrderPageRespList.size() > 0) {
                        orderMessage.setMallOrder(mallOrderPageRespList.get(0));
                    }
                }

            }) ;
        }
        return ResponseData.success(new PageResult<>(messageOrderPage));
    }

    @UserLoginToken
    @PostMapping("/orderMessageReadOrDelete")
    @ApiOperation(value = "订单消息列表_已读，删除消息", notes = "订单消息列表_已读，删除消息")
    public ResponseData orderMessageReadOrDelete(@RequestBody MessageReadOrDeleteReq req) {
        return orderMessageService.orderMessageReadOrDelete(req, getUserVo());
    }

}
