package cn.ey.biz.module.external.api.hr.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.ey.biz.common.exception.ServiceException;
import cn.ey.biz.module.external.api.hr.feign.IHrFeignService;
import cn.ey.biz.module.external.api.hr.feign.config.HrConfig;
import cn.ey.biz.module.external.api.hr.feign.reqdto.GetAccessTokenRequest;
import cn.ey.biz.module.external.api.hr.feign.reqdto.GetDataRequest;
import cn.ey.biz.module.external.api.hr.feign.reqdto.RecycleAccessTokenRequest;
import cn.ey.biz.module.external.api.hr.feign.resdto.ResponseResult;
import cn.ey.biz.module.external.api.hr.service.IHrService;
import cn.ey.biz.module.external.api.hr.service.enums.HrConstant;
import cn.ey.biz.module.external.api.hr.service.enums.HrFunctionIdEnum;
import cn.ey.biz.module.external.api.hr.service.requestdto.CodeTableReqDTO;
import cn.ey.biz.module.external.api.hr.service.responsedto.CodeTableResDTO;
import cn.ey.biz.module.external.utils.SpringUtils;


@Service
public class HrServiceImpl implements IHrService{
	
	private final Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private IHrFeignService hrFeignService;

	@Override
	public String getAccessToken(String acc, String pwd) {
		GetAccessTokenRequest request = new GetAccessTokenRequest();
		request.setAcc(acc);
		request.setPwd(pwd);
		ResponseResult<String> responseResult = hrFeignService.getAccessToken(request);
		if(responseResult.getMsgID() !=0){
			throw new ServiceException(responseResult.getMsgID(), responseResult.getMsg());
		}
		return responseResult.getResult();
	}
	
	@Override
	public boolean recycleAccessToken(String accessToken) {
		RecycleAccessTokenRequest request = new RecycleAccessTokenRequest();
		request.setAccessToken(accessToken);
		ResponseResult<String> responseResult = hrFeignService.recycleAccessToken(request);
		return responseResult.getMsgID()==0?true:false;
	}
	
	@Override
	public List<JSONObject> getCodeTableData(CodeTableReqDTO param) {
		//获取token
		String accessToken = this.getAccessToken(SpringUtils.getObject(HrConfig.class).getAccount(),SpringUtils.getObject(HrConfig.class).getPassword());
		GetDataRequest getDataRequest = new GetDataRequest();
		getDataRequest.setFuncId(HrFunctionIdEnum.CODE_TABLE_FUNCTION_ID.getCode());
		getDataRequest.setParas(param);
		getDataRequest.setDataFormat(HrConstant.DATA_FORMAT_JSON);
		getDataRequest.setDataPart(HrConstant.DATA_PART_DS);
		getDataRequest.setAccessToken(accessToken);
		// 请求数据
		log.info("HR码表数据请求入参=====>>\n"+JSON.toJSONString(getDataRequest));
		ResponseResult<CodeTableResDTO> responseResult = hrFeignService.getData(getDataRequest);
		//回收token
		this.recycleAccessToken(accessToken);
		if(responseResult.getMsgID() !=0){
			throw new ServiceException(responseResult.getMsgID(), responseResult.getMsg());
		}	
		return responseResult.getResult().getYourOBJKEY().getRow();	
	}
	
}
