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.utils.ResponseMsgUtil;
import com.hai.entity.HighCoupon;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: CouponController
 * @author: HuRui
 * @date: 2023/7/12
 **/
@Controller
@RequestMapping(value = "/couponStatistics")
@Api(value = "卡券统计")
public class CouponStatisticsController {
    Logger log = LoggerFactory.getLogger(CouponStatisticsController.class);

    @Resource
    private HighCouponService couponService;

    @RequestMapping(value = "/useCouponStatistics", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "卡券使用统计")
    public ResponseData useCouponStatistics(@RequestParam(name = "couponSource", required = false) Integer couponSource,
                                            @RequestParam(name = "salesPrice", required = false) BigDecimal salesPrice,
                                            @RequestParam(name = "status", required = false) Integer status,
                                            @RequestParam(name = "salesTimeS", required = false) Long salesTimeS,
                                            @RequestParam(name = "salesTimeE", required = false) Long salesTimeE) {
        try {
            JSONObject param = new JSONObject();
            param.put("couponSource" , couponSource);
            param.put("salesPrice" , salesPrice);
            param.put("salesTimeS" , salesTimeS);
            param.put("salesTimeE" , salesTimeE);
            List<Map<String, Object>> couponStatistics = couponService.getUseCouponStatistics(param);

            Map<String, Object> returnMap = new HashMap<>();
            List<Map<String, Object>> couponMapList = new ArrayList<>();
            Map<String, Object> couponMap;

            for (Map<String,Object> map : couponStatistics) {
                couponMap = new HashMap<>();
                couponMap.put("salesPrice", map.get("sales_price"));
                couponMap.put("status", 20);
                couponMap.put("count", map.get("notUseCount"));
                couponMapList.add(couponMap);

                couponMap = new HashMap<>();
                couponMap.put("salesPrice", map.get("sales_price"));
                couponMap.put("status", 40);
                couponMap.put("count", map.get("useCount"));
                couponMapList.add(couponMap);
            }
            if (status != null) {
                couponMapList = couponMapList.stream().filter(o -> MapUtils.getInteger(o, "status").equals(status)).collect(Collectors.toList());
            }
            returnMap.put("salesPriceList", couponMapList);
            return ResponseMsgUtil.success(returnMap);

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

}
