package cn.pluss.platform.controller.memberOrder;

import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.MemberOrder;
import cn.pluss.platform.memberOrder.MemberOrderService;
import cn.pluss.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("memberOrder")
public class MemberOrderController extends BaseNoModelController {


    private Integer PAGE_SIZE = 12;

    @Autowired
    private MemberOrderService memberOrderService;

    /**
     * 引导页
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {

        return "memberOrder/memberOrderList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryMemberOrderByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryMemberOrderByPage(Integer currPage, Integer id) {

        if (currPage == null) {
            currPage = 1;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        map.put("id", id);
        List<MemberOrder> memberOrderList = memberOrderService.queryMemberOrderPage(map);
        Integer count = memberOrderService.queryMemberOrderPageCount(map);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("memberOrderList", memberOrderList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMemberOrderDetails", method = RequestMethod.GET)
    public String queryMemberOrderDetails(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            MemberOrder memberOrder = new MemberOrder();
            memberOrder.setId(id);
            memberOrder = memberOrderService.queryMemberOrder(memberOrder);
            request.setAttribute("memberOrder", memberOrder);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "memberOrder/memberOrderDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMemberOrderBaseInfo", method = RequestMethod.GET)
    public String queryMemberOrderBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            MemberOrder memberOrder = new MemberOrder();
            memberOrder.setId(id);
            memberOrder = memberOrderService.queryMemberOrder(memberOrder);
            request.setAttribute("memberOrder", memberOrder);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "memberOrder/memberOrderBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMemberOrderExtendsInfo", method = RequestMethod.GET)
    public String queryMemberOrderExtendsInfo() {

        return "memberOrder/memberOrderExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMemberOrderFunctionsInfo", method = RequestMethod.GET)
    public String queryMemberOrderFunctionsInfo() {

        return "memberOrder/memberOrderFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/saveMemberOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveMemberOrder(MemberOrder memberOrder) {

        if (memberOrder.getId() != null && memberOrder.getId() != 0) {
            memberOrderService.updateMemberOrder(memberOrder);
        } else {
            memberOrderService.saveMemberOrder(memberOrder);
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 修改对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/updateMemberOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateMemberOrder(Integer id) {

        MemberOrder memberOrder = new MemberOrder();
        memberOrder.setId(id);
        memberOrderService.updateMemberOrder(memberOrder);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/deleteMemberOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteMemberOrder(Integer id) {
        MemberOrder memberOrder = new MemberOrder();
        memberOrder.setId(id);
        memberOrderService.deleteMemberOrder(memberOrder);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

}


