package com.lightinit.hsdataportal.controller.open;

import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicPublishSettingChargeMethod;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IOrderService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/open/order")
public class OrderOpenController {

    @Autowired
    private IOrderService orderService ;

    @RequiresAuthentication
    @RequestMapping(value = "index.htm")
    public ModelAndView openList(HttpSession session, OrderSearchModel orderSearchModel, @RequestParam(defaultValue = "1") int pageNo){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ModelAndView modelAndView = new ModelAndView("open/order/index");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        orderSearchModel.setUserId(loginUser.getId());
        //orderSearchModel.setPage(pager);
        orderSearchModel.setPlatformType(DicPlatformType.OPEN);
        List<OrderListModel> outputModel = orderService.queryOpenList(orderSearchModel);
        pager.setTotal(orderService.queryOpenCount(orderSearchModel));
        modelAndView.addObject("outputModel", outputModel);
        modelAndView.addObject("pager", pager);
        modelAndView.addObject("mianfei", DicPublishSettingChargeMethod.FREE);
        return modelAndView;
    }

    //取消订单
    @RequestMapping(value = "deleteOrder.action/{id}")
    public String deleteOrder(@PathVariable Long id){
        orderService.deleteOrder(id);
        return "redirect:/open/order/index.htm";
    }

}
