package cn.pluss.platform.controller.store;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.device.MercOrderDetailService;
import cn.pluss.platform.device.MercOrderNewService;
import cn.pluss.platform.dto.MercOrderNewDTO;
import cn.pluss.platform.entity.MercOrderDetail;
import cn.pluss.platform.entity.MercOrderNew;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.vo.MercOrderNewVO;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author SSS
 */
@RestController
@RequestMapping("/mercOrderNew")
public class MercOrderNewController {

    @Autowired
    private MercOrderNewService monService;

    @Autowired
    private MercOrderDetailService modService;

    @Resource
    private UserAppService userAppService;


    @GetMapping("/page")
    @ApiOperation(tags = {"设备商城-我的-我的订单"}, value = "查询我的订单", notes = "我的订单分页查询", httpMethod = "GET")
    public Result<Page<MercOrderNewVO>> page(Page<MercOrderNewVO> page, MercOrderNewVO condition) {
        setUserApp(condition);
        page.addOrder(OrderItem.desc("id"));
        page = monService.pageInfo(page, condition);
        List<MercOrderNewVO> records = page.getRecords();
        if (!records.isEmpty()){
            for (MercOrderNewVO vo : records) {
                List<MercOrderDetail> detailList = modService.getDetailByOrderNo(vo.getOrderNo(),condition.getPStatus());
                vo.setDetailList(detailList);
            }
        }
        return ResultGenerator.genSuccessResult(page);
    }

    private void setUserApp(MercOrderNew condition) {
        UserApp userApp = userAppService.queryUserAppByToken();
        if(userApp == null){
            MsgException.throwException("登录用户异常");
        }
        condition.setUserId(userApp.getUserId());
    }

    @PostMapping("/create")
    public Result<Object> createOrder(@RequestBody MercOrderNewDTO orderNewDTO) {
        Object order = monService.apiCreateOrder(orderNewDTO);
        return ResultGenerator.genSuccessResult("操作成功", order);
    }

    /**
     * 获取商品邮费
     * @param orderNewDTO
     * @return
     */
    @PostMapping("/getPostageInfo")
    @ApiOperation(tags = {"设备商城-我的-我的订单"}, value = "获取地区邮费配置价格", notes = "获取地区邮费配置价格", httpMethod = "GET")
    public Result<Object> getPostageInfo(@RequestBody MercOrderNewDTO orderNewDTO) {
        Object result = monService.getOrderPostageInfo(orderNewDTO);
        return ResultGenerator.genSuccessResult("获取成功",result);
    }

    /**
     * 查询订单支付状态
     * @param orderNewDTO
     * @return
     */
    @GetMapping("/getOrderStatusInfo")
    public Result<Object> getOrderStatusInfo(MercOrderNewDTO orderNewDTO) {
        Object result = monService.getOrderStatusInfo(orderNewDTO);
        return ResultGenerator.genSuccessResult("获取成功",result);
    }
}
