package cn.superdesk.uniorder.openapicall.superdeskuniorder.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dto.param._dashboard.TvHeadDataQueryCriteria;
import cn.superdesk.uniorder.dto.param._dashboard.TvMapDataQueryCriteria;
import cn.superdesk.uniorder.dto.vo._dashboard.TvHeadDataVo;
import cn.superdesk.uniorder.dto.vo._dashboard.TvMapDataVo;
import cn.superdesk.uniorder.dto.vo.sys.SysUserVo;
import cn.superdesk.uniorder.openapicall.superdeskuniorder.restapi.OctlifeCallApi;
import cn.superdesk.uniorder.openapicall.superdeskuniorder.restapi.OctlifeCallOauthApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import retrofit2.Call;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/10/26 下午4:45
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/26 下午4:45
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class OctlifeUniOrderCallApiService {

	@Autowired
	private OctlifeCallApi octlifeCallApi;

	@Autowired
	private OctlifeCallOauthApi octlifeCallOauthApi;


	/****************************************** 登陆授权 start ******************************************/


	public String loginBySupplier(Long saleId, String saleName) {


		R<SysUserVo> response = null;

//		saleId = 1L;
//		saleName = "前台优选";


		Map<String, Object> conditions = new HashMap<>();
		conditions.put("saleId", saleId);
		conditions.put("saleName", saleName);
		PageQueryParam param = new PageQueryParam();
		param.setConditions(conditions);

		log.info("{}::{}", saleId, saleName);

		try {
			Call<R<SysUserVo>> callResponse = octlifeCallOauthApi.userLoginBySupplier(param);
			response = callResponse.execute().body();

			if (ObjectUtil.isEmpty(response.getCode()) || !response.getCode().equals("200")) {
				return null;
			} else {
				return response.getData().getToken();
			}


		} catch (com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException retrofitException) {

			return null;

		} catch (IOException ioe) {

			ioe.printStackTrace();

			return null;
		}

	}


	@Value("${test.superdesk.uniorder.octlife.accessToken}")
	private String accessToken;

	@Deprecated
	public R<TvHeadDataVo> dashboardTvGetHeadData(String token, TvHeadDataQueryCriteria queryCriteria) {

		TvHeadDataQueryCriteria queryCriteriaCopy = AnBeanUtils.copy(queryCriteria, TvHeadDataQueryCriteria.class);

		queryCriteriaCopy.setSpecialHandleCallOct(null);
		queryCriteriaCopy.setSpecialHandleCallUserCount(null);

		R<TvHeadDataVo> response = null;

		try {
			Call<R<TvHeadDataVo>> callResponse = octlifeCallApi.dashboardTvGetHeadData(queryCriteriaCopy);

			response = callResponse.execute().body();
			log.info("dashboardTvGetHeadData response\n{}", JSONUtil.toJsonStr(response));
		} catch (com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException retrofitException) {

			return null;

		} catch (IOException ioe) {

			ioe.printStackTrace();

			return null;
		}

		return response;
	}

	public R<TvHeadDataVo> dashboardTvGetHeadDataOct(String token, TvHeadDataQueryCriteria queryCriteria) {

		TvHeadDataQueryCriteria queryCriteriaCopy = AnBeanUtils.copy(queryCriteria, TvHeadDataQueryCriteria.class);

		queryCriteriaCopy.setSpecialHandleCallOct(null);
		queryCriteriaCopy.setSpecialHandleCallUserCount(null);

		R<TvHeadDataVo> response = null;

		try {
			Call<R<TvHeadDataVo>> callResponse = octlifeCallApi.dashboardTvGetHeadDataOct(queryCriteriaCopy);
			response = callResponse.execute().body();
			log.info("dashboardTvGetHeadDataOct response\n{}", JSONUtil.toJsonStr(response));
		} catch (com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException retrofitException) {

			retrofitException.printStackTrace();
			return null;

		} catch (IOException ioe) {

			ioe.printStackTrace();

			return null;
		}


		return response;
	}

	@Deprecated
	public R<List<TvMapDataVo>> dashboardTvGetMapData(String token, TvMapDataQueryCriteria queryCriteria) {

		TvMapDataQueryCriteria queryCriteriaCopy = AnBeanUtils.copy(queryCriteria, TvMapDataQueryCriteria.class);

		queryCriteriaCopy.setSpecialHandleCallOct(null);
//		queryCriteriaCopy.setSpecialHandleCallUserCount(null);

		R<List<TvMapDataVo>> response = null;

		try {
			Call<R<List<TvMapDataVo>>> callResponse = octlifeCallApi.dashboardTvGetMapData(queryCriteriaCopy);

			response = callResponse.execute().body();

			log.info("dashboardTvGetMapData response\n{}", JSONUtil.toJsonStr(response));

		} catch (com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException retrofitException) {

			return null;

		} catch (IOException ioe) {

			ioe.printStackTrace();

			return null;
		}

		return response;
	}

	public R<List<TvMapDataVo>> dashboardTvGetMapDataOct(String token, TvMapDataQueryCriteria queryCriteria) {

		TvMapDataQueryCriteria queryCriteriaCopy = AnBeanUtils.copy(queryCriteria, TvMapDataQueryCriteria.class);

		queryCriteriaCopy.setSpecialHandleCallOct(null);
//		queryCriteriaCopy.setSpecialHandleCallUserCount(null);

		R<List<TvMapDataVo>> response = null;

		try {
			Call<R<List<TvMapDataVo>>> callResponse = octlifeCallApi.dashboardTvGetMapDataOct(queryCriteriaCopy);

			response = callResponse.execute().body();

			log.info("dashboardTvGetMapData response\n{}", JSONUtil.toJsonStr(response));

		} catch (com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException retrofitException) {

			return null;

		} catch (IOException ioe) {

			ioe.printStackTrace();

			return null;
		}

		return response;
	}

}
