package com.sptg.gateway.controller.estate;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.security.annotation.Security;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.entity.User;
import com.sptg.estate.api.feigns.UserFeign;
import com.sptg.pay.api.dto.UserOrderDto;
import com.sptg.pay.api.entity.UserOrder;
import com.sptg.pay.api.feigns.UserOrderFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/estate/userOrder")
@RequiredArgsConstructor
public class UserOrderController {

    private final UserOrderFeign userOrderFeign;
    private final ConfFeign confFeign;
    private final UserFeign userFeign;

    /**
     * 功能描述:订单管理页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping("/index")
    @Security(token = EstateSecurityToken.ESTATE_USER_ORDER_MANAGE)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("estate/user-order-manage");
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    @GetMapping(value = "/showUserOrderPage")
    public ResultBean<Page<UserOrder>> showUserOrderPage(UserOrderDto dto,Page<UserOrder> page){
        dto.setPage(page);
        ResultBean<Page<UserOrder>> bean = this.userOrderFeign.showUserOrderPage(dto);
        if (bean.getData() != null){
            if (bean.getData().getRecords().size() > 0){
                List<UserOrder> userOrderList = bean.getData().getRecords();
                if(null != userOrderList && userOrderList.size() > 0){
                    userOrderList.forEach(userOrder -> {
                        User user =  this.userFeign.findUserById(userOrder.getUserId());
                        if(user != null){
                            userOrder.setImageUri(user.getImageUri());
                        }
                    });
                }
            }
        }
        return bean;
    }

    @PostMapping(value = "/orderQuery")
    public ResultBean<Boolean> orderQuery(UserOrderDto dto){
        return this.userOrderFeign.orderQuery(dto);
    }
}
