package com.bweb.controller.OrderStatistics;

import com.alibaba.fastjson.JSONObject;
import com.bweb.controller.ApiProductController;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.OrderStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/homeOrderStatistics")
@Api(value = "首页订单统计")
public class HomeOrderStatisticsController {

    Logger log = LoggerFactory.getLogger(ApiProductController.class);

    @Resource
    private OrderStatisticsService orderStatisticsService;

    @Autowired
    private UserCenter userCenter;

    @RequestMapping(value = "/orderStatisticsByYMD", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据年月日统计订单")
    public ResponseData orderStatisticsByYMD(
            @RequestParam(name = "dateType", required = false) Integer dateType,
            @RequestParam(name = "num", required = false) Integer num,
            HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (userInfoModel.getSecRole().getRoleType() !=  1) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂无权限");
            }

            JSONObject object = new JSONObject();
            object.put("dateType" , dateType);
            object.put("num" , num);

            return ResponseMsgUtil.success(orderStatisticsService.orderStatisticsByYMD(object));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/orderStatisticsByProductType", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取订单统计根据年月周日 产品类型相关数据")
    public ResponseData orderStatisticsByProductType(
            @RequestParam(name = "dateType", required = false) Integer dateType,
            HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (userInfoModel.getSecRole().getRoleType() !=  1) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂无权限");
            }

            JSONObject object = new JSONObject();
            object.put("dateType" , dateType);

            return ResponseMsgUtil.success(orderStatisticsService.orderStatisticsByProductType(object));

        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
