package com.bofeng.wx.controller;

import com.bofeng.entity.Order;
import com.bofeng.entity.UserOpen;
import com.bofeng.service.OrderService;
import com.bofeng.service.UserOpenService;
import com.yvan.ModelOps;
import com.yvan.mvc.Pd;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class CustMarketListController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxMpService mpService;

    @Autowired
    private UserOpenService userOpenService;

    @GetMapping("/cust/mark_list.html")
    public ModelAndView getMarkList(@RequestParam(name = "openId", required = false, defaultValue = "") String openId,
                                    ModelMap model) {
        UserOpen userOpen = userOpenService.getUserByOpenId(openId);
        List<Order> orderList = orderService.serchCurrentMarkList(userOpen.getUserId());

        model.put("orderList", orderList);
        model.put("userId", userOpen.getUserId());
        model.put("openId", openId);
        return new ModelAndView("/cust/mark_list.ftl", model);
    }

    @GetMapping("/cust/delete_order.json")
    public ModelOps deleteOrder(@Pd(name = "orderId") Long orderId,
                                @Pd(name = "userId") Long userId) {
        int res = orderService.deleteOrder(orderId);
        if (res > 0)
            return ModelOps.newSuccess();
        return ModelOps.newFail("取消失败");
    }


}
