package com.bofeng.controller;

import com.bofeng.Consts;
import com.bofeng.entity.Order;
import com.bofeng.service.OrderService;
import com.google.common.collect.Maps;
import com.yvan.Model;
import com.yvan.PageDb;
import com.yvan.mvc.Pd;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequiresPermissions(Consts.USER_TYPE_ADMIN)
public class AdminReportOrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/admin/report_order.html")
    public ModelAndView adminReportOrder() {
        return new ModelAndView("/admin/report_order.ftl");
    }

    @GetMapping("/admin/report_order.json")
    public Model<List<Order>> getReportOrder(PageDb pageDb) {
        return Model.newSuccess(pageDb, orderService.selectAll(pageDb));
    }

    @GetMapping("/admin/order_search.json")
    public Model<List<Order>> searchOrder(PageDb pageDb,
                                          @Pd(name = "saleName", required = false, defaultValue = "") String saleName,
                                          @Pd(name = "orderStartAt", required = false, defaultValue = "") String orderStartAt,
                                          @Pd(name = "orderEndAt", required = false, defaultValue = "") String orderEndAt,
                                          @Pd(name = "roomPlace", required = false, defaultValue = "") String roomPlace,
                                          @Pd(name = "roomName", required = false, defaultValue = "") String roomName,
                                          @Pd(name = "customerName", required = false, defaultValue = "") String customerName,
                                          @Pd(name = "paid", required = false, defaultValue = "") String paid) {

        Map<String, Object> queryParam = Maps.newLinkedHashMap();
        queryParam.put("saleName", saleName);
        queryParam.put("orderStartAt", orderStartAt);
        queryParam.put("orderEndAt", orderEndAt);
        queryParam.put("roomPlace", roomPlace);
        queryParam.put("roomName", roomName);
        queryParam.put("customerName", customerName);
        queryParam.put("paid", paid);
        return Model.newSuccess(pageDb, orderService.search(pageDb, queryParam));
    }


}
