package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.entity.*;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/discountPackage")
@Api(value = "优惠券包接口")
public class HighDiscountPackageController {

    private static Logger log = LoggerFactory.getLogger(HighDiscountPackageController.class);

    @Autowired
    private UserCenter userCenter;

    @Resource
    private CommonService commonService;

    @Resource
    private BsCompanyService bsCompanyService;

    @Resource
    private HighDiscountPackageService highDiscountPackageService;

    @Resource
    private HighDiscountPackageDetailsService highDiscountPackageDetailsService;

    @Resource
    private HighDiscountService highDiscountService;

    @RequestMapping(value = "/getDiscountPackageList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取优惠券包列表")
    public ResponseData getDiscountPackageList(@RequestParam(name = "regionId", required = true) String regionId,
                                               @RequestParam(name = "usingAttribution", required = false) Integer usingAttribution,
                                               @RequestParam(name = "title", required = false) String title,
                                               @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                               @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            SecRegion region = commonService.getParentByRegion(Long.parseLong(regionId));
            if (region != null) {
                BsCompany bsCompany = bsCompanyService.selectCompanyByRegion(region.getRegionId().toString());
                if (bsCompany != null) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("companyId", bsCompany.getId());
                    map.put("usingAttribution", usingAttribution);
                    map.put("title" , title);
                    map.put("salesType" , 1);
                    map.put("status" , 1);
                    PageHelper.startPage(pageNum, pageSize);
                    return ResponseMsgUtil.success(new PageInfo<>(highDiscountPackageService.getDiscountPackageList(map)));
                }
            }
            return ResponseMsgUtil.success(new PageInfo<>());

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

    @RequestMapping(value = "/getDiscountPackageDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取优惠券包详情")
    public ResponseData getDiscountPackageDetail(@RequestParam(name = "packageId", required = true) Integer packageId) {
        try {

            Map<String, Object> map = new HashMap<>();

            HighDiscountPackage discountPackage = highDiscountPackageService.findDiscountPackageById(packageId);
            if (discountPackage != null) {
                map.put("packageDetail", discountPackage);
                // 查询包内的优惠券
                List<HighDiscountPackageDetails> detailsList = highDiscountPackageDetailsService.getDetailByPackage(packageId);
                for (HighDiscountPackageDetails discount: detailsList) {
                    discount.setDiscountDetail(highDiscountService.getDiscountById(Long.parseLong(discount.getDiscountId().toString())));
                }
                map.put("discountList", detailsList);
                return ResponseMsgUtil.success(map);
            }
            return ResponseMsgUtil.success(null);

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



}
