package cn.superdesk.uniorder.controller._openapi.superdeskmall.core;


import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.dto.vo.sys.SysUserVo;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysUserDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysSupplierDubboConsumerService;
import cn.superdesk.uniorder.common.security.crypt.SuperdeskUniorderRsaUtils;
import cn.superdesk.uniorder.common.numerical.AnLongUtils;
import cn.superdesk.uniorder.common.numerical.AnStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 10/10/20 2:49 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 10/10/20 2:49 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/openapi/superdeskmall/core/oauth")
public class OAuthController {

	@Autowired
	private SysSupplierDubboConsumerService unifiedSupplierDubboConsumerService;

	@Autowired
	private SysUserDubboConsumerService sysUserDubboConsumerService;

	/**
	 * 生成 passport 给商城 api 调用 生成 passport
	 *
	 * @param param
	 * @return
	 */
	@PostMapping("/passport")
	@ResponseBody
	public R<String> passport(@RequestBody PageQueryParam param) {

		R<String> result = new R<>();

		if (null == param.getConditions().get("saleId") || null == param.getConditions().get("saleName")) {
			result.setSuccess(false);
			result.setCode(500);
			result.setMessage("信息错误或缺失");
		}


		Long   saleId   = AnLongUtils.getLong(param.getConditions().get("saleId"));
		String saleName = AnStringUtils.getString(param.getConditions().get("saleName"));

		String content = saleId + "::" + saleName;
		try {
			String encode = SuperdeskUniorderRsaUtils.encode(content);

			log.info(encode);

			result.setSuccess(true);
			result.setCode(200);
			result.setMessage("SUCCESS");
			result.setData(encode);

//			String decode = MyTools.decode(encode);
//
//			log.info(decode);

		} catch (Exception e) {

		}

		return result;
	}

	/**
	 * 为了测试
	 *
	 * @param param
	 * @return
	 */
	@PostMapping("/passport/decode")
	@ResponseBody
	public R<String> passportDecode(@RequestBody PageQueryParam param) {

		R<String> result = new R<>();

		if (null == param.getConditions().get("passport")) {
			result.setSuccess(false);
			result.setCode(500);
			result.setMessage("信息错误");
		}

		try {


			String decode = SuperdeskUniorderRsaUtils.decode(param.getConditions().get("passport").toString());

			log.info(decode);

			result.setSuccess(true);
			result.setCode(200);
			result.setMessage("SUCCESS");
			result.setData(decode);

		} catch (Exception e) {

		}

		return result;
	}


	@PostMapping("/loginByPassport")
	@ResponseBody
	public R<SysUserVo> userLoginByPassport(
			@RequestBody PageQueryParam param,
			HttpServletRequest request,
			HttpSession session) {

		log.info(AnJsonUtils.toJson(param));

		String localIp = "";

		R<SysUserVo> response = new R<>();

		if (null == param.getConditions().get("passport")) {
			response.setSuccess(false);
			response.setCode(500);
			response.setMessage("信息错误");
			return response;
		}

		try {


			String decode = SuperdeskUniorderRsaUtils.decode(param.getConditions().get("passport").toString());
			log.info("loginByPassport passport decode {} ", decode);

			String[] content = decode.split("::");

			Map<String, Object> loginParam = new HashMap<>();
			loginParam.put("saleId", content[0]);
			loginParam.put("saleName", content[1]);

			SysUserVo sysUserVo = login(loginParam, request.getRemoteAddr());

			if (null == sysUserVo) {
				response.setSuccess(false);
				response.setCode(500);
				response.setMessage("登陆失败!供应商不存在");
			} else {
				response.setData(sysUserVo);
			}


		} catch (Exception e) {

		}

		return response;
	}


//	{
//		"conditions" : {
//		"saleId" : 1,
//				"saleName" : "前台优选"
//	}
//	}

	/**
	 * 供应商登陆? 给供应商端用 彩娇
	 * 拿 unified_supplier sale_id sale_name | 后边的是没数据的 contacts_name contacts_phone enabled 来验证
	 *
	 * @param param
	 * @return
	 */
	@PostMapping("/loginBySupplier")
	@ResponseBody
	public R<SysUserVo> userLoginBySupplier(
			@RequestBody PageQueryParam param,
			HttpServletRequest request,
			HttpSession session) {


//saleId
//saleName
//serviceNo


//		UnifiedSupplier unifiedSupplier = new UnifiedSupplier();
//
//		unifiedSupplier.setSaleId();
//		unifiedSupplier.setSaleName();


		R<SysUserVo> response = new R<>();

		SysUserVo sysUserVo = login(param.getConditions(), request.getRemoteAddr());

		if (ObjectUtil.isNull(sysUserVo)) {
			response.setSuccess(false);
			response.setCode(500);
			response.setMessage("登陆失败!供应商不存在");
			return response;
		} else {
			response.setData(sysUserVo);
		}

		return response;

	}

	/**
	 * 内部方法 共用登陆-供应商
	 *
	 * @param param
	 * @return
	 */
	private SysUserVo login(Map<String, Object> param, String remoteIp) {

		log.info("login param -> {} ; remoteIp -> {}", AnJsonUtils.toJson(param), remoteIp);

//		param
//		{
//			"conditions" : {
//				"saleId" : 1,
//				"saleName" : "前台优选"
//			}
//		}

		String localIp = "";

		SysUserVo sysUserVo = sysUserDubboConsumerService.userLoginBySupplier(param);

		if (ObjectUtil.isNull(sysUserVo)) {
			return null;
		}

		log.info("login sysUserVo -> {}", AnJsonUtils.toJson(sysUserVo));
		// 记录登陆日志
		sysUserDubboConsumerService.createLog(sysUserVo.getId(), remoteIp, localIp);


		return sysUserVo;
	}
}
