package cn.feast.coding.controller.stock;

import javax.servlet.http.HttpServletRequest;

import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.stock.Stock;
import cn.feast.coding.service.stock.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.regex.Pattern;


@Controller
@RequestMapping("/stock")
public class StockController extends BaseController {
    @Autowired
    private StockService stockService;

    @RequestMapping(value = "/list")
    public String list(Stock stock, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request) {
        PageInfo<Stock> pageInfo = stockService.queryPage(stock, pageNum(curPage), pageSize(request, pageSize));
        model.put("pages", pageInfo);
        model.put("stock", stock);
        return "stock/list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "stock/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Stock stock, HttpServletRequest request, ModelMap model) {
        stockService.insert(stock);
        model.put("flag", 1);
        return "stock/add";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Stock stock, ModelMap model) {
        stock = stockService.selectByPrimaryKey(stock.getId());
        model.put("stock", stock);
        return "stock/edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Stock stock, HttpServletRequest request, ModelMap model) {
        stockService.update(stock);
        model.put("flag", 1);
        return "stock/edit";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public void delete(Stock stock) {
        stockService.deleteByPrimaryKey(stock.getId());
    }

}
