package com.cweb.controller;

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.MemberValidateUtil;
import com.hai.common.utils.PageUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.*;
import com.hai.model.*;
import com.hai.service.*;
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.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/9 20:58
 */
@Controller
@RequestMapping(value = "/highMerchantStore")
@Api(value = "商户门店接口")
public class HighMerchantStoreController {

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

    @Autowired
    private UserCenter userCenter;

    @Resource
    private HighCouponService highCouponService;

    @Resource
    private HighMerchantStoreService highMerchantStoreService;

    @Resource
    private HighMerchantService highMerchantService;

    @Resource
    private BsCompanyService bsCompanyService;

    @Resource
    private CommonService commonService;

    @RequestMapping(value="/getMerchantStoreById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询商户门店")
    public ResponseData getMerchantStoreById(@RequestParam(name = "id", required = true) Long id) {
        try {

            HighMerchantStoreModel store = highMerchantStoreService.getMerchantStoreById(id);
            if (store != null) {
                store.setHighMerchant(highMerchantService.getMerchantById(store.getMerchantId()));
                store.setSecUser(null);
            }
            return ResponseMsgUtil.success(store);

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

    @RequestMapping(value="/getStoreListByCoupon",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据卡卷查询门店列表")
    public ResponseData getStoreListByCoupon(@RequestParam(name = "couponId", required = true) Long couponId,
                                             @RequestParam(name = "longitude", required = true) String longitude,
                                             @RequestParam(name = "latitude", required = true) String latitude,
                                             @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                             @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            HighCoupon coupon = highCouponService.getCouponById(couponId);
            if (coupon == null) {
                return ResponseMsgUtil.success(new PageInfo<>());
            }
            Map<String,Object> map = new HashMap<>();
            map.put("merchantId", coupon.getMerchantId());
            map.put("longitude", longitude);
            map.put("latitude", latitude);

            List<HighMerchantStore> storeList = highMerchantStoreService.getStoreListByLongitude(map);
            if (storeList != null && storeList.size() > 0) {
                HighMerchant merchant = highMerchantService.getMerchantById(coupon.getMerchantId());
                for (HighMerchantStore store : storeList) {
                    store.setExt1(merchant.getMerchantLogo());
                }
            }
            return ResponseMsgUtil.success(PageUtil.initPageInfoObj(pageNum, storeList.size(), pageSize, new PageInfo<>(storeList)));

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

    @RequestMapping(value="/getStoreListByMerchant",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据商户查询门店列表")
    public ResponseData getStoreListByMerchant(@RequestParam(name = "merchantId", required = true) Long merchantId,
                                               @RequestParam(name = "longitude", required = true) String longitude,
                                               @RequestParam(name = "latitude", required = true) String latitude,
                                               @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                               @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("merchantId", merchantId);
            map.put("longitude", longitude);
            map.put("latitude", latitude);

            List<HighMerchantStore> storeList = highMerchantStoreService.getStoreListByLongitude(map);
            if (storeList != null && storeList.size() > 0) {
                HighMerchant merchant = highMerchantService.getMerchantById(merchantId);
                for (HighMerchantStore store : storeList) {
                    store.setExt1(merchant.getMerchantLogo());
                }
            }
            return ResponseMsgUtil.success(PageUtil.initPageInfoObj(pageNum, storeList.size(), pageSize, new PageInfo<>(storeList)));

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

    @RequestMapping(value="/getMerchantList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户列表")
    public ResponseData getMerchantList(@RequestParam(name = "regionId", required = true) String regionId) {
        try {
            SecRegion region = commonService.getParentByRegion(Long.parseLong(regionId));
            if (region != null) {
                BsCompany bsCompany = bsCompanyService.selectCompanyByRegion(region.getRegionId().toString());
                Map<String, Object> map = new HashMap<>();
                map.put("companyId", bsCompany.getId());
                map.put("status", 1);
                map.put("ext1", true);
                return ResponseMsgUtil.success(highMerchantService.getMerchantList(map));
            }
            return ResponseMsgUtil.success(new ArrayList<>());

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

    @RequestMapping(value="/getStoreList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询门店列表")
    public ResponseData getStoreList(@RequestParam(name = "storeName", required = false) String storeName,
                                     @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                     @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {


            Map<String,Object> map = new HashMap<>();
            map.put("storeName", storeName);

            PageHelper.startPage(pageNum,pageSize);
            PageInfo<HighMerchantStore> pageInfo = new PageInfo<>(highMerchantStoreService.getMerchantStoreList(map));
            for (HighMerchantStore store : pageInfo.getList()) {
                store.setHighMerchant(highMerchantService.getDetailById(store.getMerchantId()));
            }
            return ResponseMsgUtil.success(pageInfo);

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

}
