package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
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.RedisUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.ApiIpAddress;
import com.hai.entity.ApiMerchants;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.ApiIpAddressService;
import com.hai.service.ApiMerchantsService;
import com.hai.service.CommonService;
import com.hai.service.SecConfigService;
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.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/apiIpAddress")
@Api(value = "Ip地址管理")
public class ApiIpAddressController {

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

    @Resource
    private ApiIpAddressService apiIpAddressService;

    @Autowired
    private UserCenter userCenter;

    @Resource
    private SecConfigService secConfigService;

    @Resource
    private ApiMerchantsService apiMerchantsService;


    @RequestMapping(value="/editApiIpAddress",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑ip地址")
    public ResponseData editApiIpAddress(@RequestBody ApiIpAddress apiIpAddress, HttpServletRequest request) {
        try {
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
            if (userInfoModel.getBsCompany() == null) {
                log.error("HighMerchantController -> updateMerchant() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            if (apiIpAddress.getMchId() == null) {
                log.error("ApiMemberProductController -> insertMemberProduct() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            ApiIpAddress apiIpAddressEdit = apiIpAddressService.findByMchId(apiIpAddress.getMchId());

            if (apiIpAddress.getIpAddress().length() > 250) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "超过最大可配置内容");
            }

            // 判断是否存在ip地址内容
            if (apiIpAddressEdit == null ) {
                apiIpAddress.setCreateTime(new Date());
                apiIpAddress.setUpdateTime(new Date());
                apiIpAddressService.insertApiIpAddress(apiIpAddress);
            } else {
                apiIpAddressEdit.setUpdateTime(new Date());
                apiIpAddressEdit.setIpAddress(apiIpAddress.getIpAddress());
                apiIpAddressService.updateApiIpAddress(apiIpAddressEdit);
            }


            return ResponseMsgUtil.success("新增成功");
        } catch (Exception e) {
            log.error("ApiMerchantsController -> insertMerchant() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/findByMchId", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据商户号查询详情")
    public ResponseData findByMchId(@RequestParam(value = "mchId", required = true) String mchId) {
        try {

            return ResponseMsgUtil.success(apiIpAddressService.findByMchId(mchId));

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

    @RequestMapping(value = "/getSecConfigList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据商户号查询已配置接口")
    public ResponseData getSecConfigList(@RequestParam(value = "mchId", required = true) String mchId) {
        try {

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

            return ResponseMsgUtil.success(secConfigService.getSecConfigList(map));

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


}
