package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.channel.lakala.config.LaKaLaConfig;
import com.hfkj.channel.saas.utils.SaasCommon;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.CoordCommonUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsCompany;
import com.hfkj.entity.SecRegion;
import com.hfkj.model.OpenStoreModel;
import com.hfkj.model.ResponseData;
import com.hfkj.model.StoreModel;
import com.hfkj.service.*;
import com.hfkj.service.impl.BsLklMccServiceImpl;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value="/common")
@Api(value="共用接口")
public class CommonController {

	Logger log = LoggerFactory.getLogger(CommonController.class);
	
	@Resource
	private CommonService commonService;

	@Resource
	private BsCompanyService bsCompanyService;

	@Resource
	private BsIndustryService industryService;

	@Resource
	private BsLklMccService lklMccService;

	@Resource
	private SaasCommon saasCommon;

    @Resource
    private BsStoreService storeService;

	@RequestMapping(value="/getDredgeProvince",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询已开通的省份")
	public ResponseData getDredgeProvince(){
		try {
			List<BsCompany> list = bsCompanyService.getCompany(new HashMap<>());
			List<Map<String,Object>> mapList = new ArrayList<>();
			Map<String,Object> map;
			if (list.size() > 0) {
				for (BsCompany company : list) {
					map = new HashMap<>();
					if (StringUtils.isNotBlank(company.getRegionId())) {
						map.put("companyId", company.getId());
						map.put("regionId", company.getRegionId());
						SecRegion secRegion = commonService.getRegionsById(Long.parseLong(company.getRegionId()));
						if (secRegion != null) {
							map.put("regionName", secRegion.getRegionName());
						}
						mapList.add(map);
					}
				}
			}
			return ResponseMsgUtil.success(mapList);
		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getRegionsByParentId",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "分级查询区域信息")
	public ResponseData getRegionsByParentId(@RequestParam(name = "regionId", required = false) Long regionId){
		try {
			if (regionId == null){
				return ResponseMsgUtil.success(commonService.getCities());
			}
			return ResponseMsgUtil.success(commonService.getRegionsByParentId(regionId));
		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getParentInfosByRegionId",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询父级区域信息")
	public ResponseData getParentInfosByRegionId(@RequestParam(name = "regionId", required = true) Long regionId) {
		try {
			return ResponseMsgUtil.success(commonService.getParentInfoByRegionId(regionId));
		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getRegionsById",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询区域详细信息")
	public ResponseData getRegionsById(@RequestParam(name = "regionId", required = true) Long regionId){
		try {
			return ResponseMsgUtil.success(commonService.getRegionsById(regionId));
		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}


	@RequestMapping(value="/getDictionaries",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询数据字典")
	public ResponseData getDictionaries() {
		try {

			return ResponseMsgUtil.success(commonService.getDictionaries());

		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}


	@RequestMapping(value="/getDictionaryByCodeType",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据 码值类型 查询数据字典")
	public ResponseData getDictionaryByCodeType(@RequestParam(name = "codeType", required = true) String codeType) {
		try {

			return ResponseMsgUtil.success(commonService.getDictionarys(codeType));

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

	@RequestMapping(value="/mappingSysNameOl",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据 码值类型 查询数据字典")
	public ResponseData mappingSysNameOl(@RequestParam(name = "codeType", required = true) String codeType) {
		try {

			return ResponseMsgUtil.success(commonService.mappingSysNameOl(codeType));

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

	@RequestMapping(value="/getDictionaryByCodeTypeAndExt",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据 码值类型 查询数据字典")
	public ResponseData getDictionaryByCodeTypeAndExt(@RequestParam(name = "codeType", required = true) String codeType, String ext1) {
		try {

			return ResponseMsgUtil.success(commonService.getDictionarysAndExt(codeType,ext1));

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

	@RequestMapping(value="/getIdAndNameByCodeType",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据 码值类型 查询数据字典的id和name")
	public ResponseData getIdAndNameByCodeType(@RequestParam(name = "codeType", required = true) String codeType) {
		try {

			return ResponseMsgUtil.success(commonService.getIdAndName(codeType));

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

	@RequestMapping(value="/getDictionaryByCodeTypeAndValue",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据码值类型及value查询数据字典")
	public ResponseData getDictionaryByCodeType(@RequestParam(name = "codeType", required = true) String codeType,
												@RequestParam(name = "codeValue", required = true) String codeValue) {
		try {

			return ResponseMsgUtil.success(commonService.mappingSysCode(codeType,codeValue));

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

	@RequestMapping(value="/getIndustry",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取行业数据")
	public ResponseData getIndustry() {
		try {

			return ResponseMsgUtil.success(industryService.getCacheIndustry());

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

	@RequestMapping(value="/getMccList",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取MCC数据")
	public ResponseData getMccList() {
		try {

			return ResponseMsgUtil.success(lklMccService.getMccList());

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

	@RequestMapping(value="/getBankCardBin",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取银行卡bin信息")
	public ResponseData getBankCardBin(@RequestParam(value = "cardNo" , required = true) String cardNo) {
		try {

			return ResponseMsgUtil.success(LaKaLaConfig.queryCardBin(cardNo));

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

	@RequestMapping(value="/getBankList",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取银行支行列表")
	public ResponseData getBankList(@RequestParam(value = "regionId" , required = true) Long regionId,
									@RequestParam(value = "bankName" , required = true) String bankName) {
		try {
			SecRegion region = commonService.getRegionsById(regionId);
			if (region == null) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的区域");
			}
			if (StringUtils.isBlank(region.getSaasBankCode())) {
				throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "地区暂无法查询");
			}
			return ResponseMsgUtil.success(saasCommon.getBank(region.getSaasBankCode(), bankName));

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



    @RequestMapping(value="/getStoreList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "商户查询门店列表")
    public ResponseData getStoreListByMer(
            @RequestParam(value = "name" , required = false) String name,
            @RequestParam(value = "childId" , required = false) Long childId
    ) {
        try {

            return ResponseMsgUtil.success(storeService.getStoreList(name , childId));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }



}
