package com.bofeng.controller;

import com.bofeng.Consts;
import com.bofeng.entity.StatisticsOrder;
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 AdminReportTimeController {

    @Autowired
    private OrderService orderService;

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

    @GetMapping("/admin/statistics_order_search.json")
    public Model<List<StatisticsOrder>> getOrder(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 = "statistics", required = false, defaultValue = "") String statistics) {

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

    }
}
