package com.fufan.mtsaas.modules.common.bo.request;

import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.constant.SystemConstant;
import com.fufan.mtsaas.modules.sysmgr.journal.model.AiIntrmgrProcess;
import com.fufan.mtsaas.modules.sysmgr.common.msg.Msg;
//import com.fufan.mtsaas.modules.sysmgr.service.journal.AiIntrMgrSvc;
//import com.fufan.mtsaas.modules.sysmgr.dao.journal.AiIntrmgrProcessDao;
//import com.fufan.mtsaas.modules.sysmgr.journal.model.AiIntrmgrProcess;
import com.fufan.mtsaas.modules.common.bo.response.Result;
//import com.fufan.mtsaas.modules.sysmgr.service.journal.AiIntrMgrSvc;
import com.fufan.mtsaas.tools.AESTool;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import com.fufan.mtsaas.util.Hmac;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Objects;

/**
 * 返回结果封装
 *
 * @author Liby
 * @create 2018/4/17
 * */
@Slf4j
public class ResultPack<T> {

    public Result<T> doResultPack(Result<T> result, String intfId, String key){

       /* RedisTemplate<Object, Object> redisTemplate = (RedisTemplate<Object, Object>) SpringBeanFactoryUtils.getBean("redisTemplate");
        JSONObject json = (JSONObject) redisTemplate.opsForValue().get(intfId);
        json.put("intfRespMessage",JSON.toJSONString(result));
        json.put("sysEnd",new Date());
        json.put("sysDatetime",new Date());
        if(ResStatus.STATUS_SUCCESS.equals(result.getResStatus())){
            json.put("processStatus","1");
        }else {
            json.put("processStatus","0");
        }
        json.put("sysName","SYS");

        LogSave logSave = SpringBeanFactoryUtils.getBean(LogSave.class);
        logSave.save(JSON.toJSONString(json));*/

        if(Objects.isNull(key)){
            key = "1111111";
        }
        //加签
        if (Objects.nonNull(result) && result.getResData() != null) {
            String jsonStr;
            int features = Feature.UseBigDecimal.getMask();
            JSON.DEFAULT_PARSER_FEATURE = features;
            JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
            jsonStr = JSON.toJSONString(result.getResData(), SerializerFeature.WriteDateUseDateFormat);
            String sign = Hmac.encryptHmacMD5(key, jsonStr);
//            result.setSign(sign);
        }
        return result;
    }

   public String doResultPack(HttpSession session, Msg<T> msg, Boolean format){
       /**
        * 更新AI处理日志表
        * */
       AiIntrmgrProcess aiIntrmgrProcess = new AiIntrmgrProcess();
       aiIntrmgrProcess.setIntfId((String) session.getAttribute(session.getId()+"aiIntrmgrProcess"));
       aiIntrmgrProcess.setIntfRespMessage(msg.toString());
       if(SystemConstant.UDI_SUCCESSFUL_EXECUTION.equals(msg.getCode())){
           aiIntrmgrProcess.setProcessStatus("1");
       }else {
           aiIntrmgrProcess.setProcessStatus("0");
       }
       aiIntrmgrProcess.setSysEnd(new Date());
       aiIntrmgrProcess.setSysDatetime(new Date());
//       AiIntrMgrSvc aiIntrMgrSvc = (AiIntrMgrSvc) SpringBeanFactoryUtils.getBean("aiIntrMgrSvc");
//       aiIntrMgrSvc.updateById(aiIntrmgrProcess);
       String result = null;
       String jsonStr;
       int features = Feature.UseBigDecimal.getMask();
       JSON.DEFAULT_PARSER_FEATURE =features;
       if(format){
           JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
           jsonStr = JSON.toJSONString(msg, SerializerFeature.WriteDateUseDateFormat);
       }else{
           JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
           jsonStr = JSONObject.toJSONString(msg,SerializerFeature.WriteDateUseDateFormat);
       }
       log.info(jsonStr);
       /**
        * 返回数据加密
        * */
       try {
//           result = jsonStr;
           result = AESTool.encrypt(jsonStr);
       } catch (Exception e) {
           log.info("加密失败：", e);
       }
       return result;
   }

    public String doResultPack(HttpSession session, Msg<T> msg){
        /**
         * 更新AI处理日志表
         * */
        AiIntrmgrProcess aiIntrmgrProcess = new AiIntrmgrProcess();
        aiIntrmgrProcess.setIntfId((String) session.getAttribute(session.getId()+"aiIntrmgrProcess"));
        aiIntrmgrProcess.setIntfRespMessage(msg.toString());
        if(SystemConstant.UDI_SUCCESSFUL_EXECUTION.equals(msg.getCode())){
            aiIntrmgrProcess.setProcessStatus("1");
        }else {
            aiIntrmgrProcess.setProcessStatus("0");
        }
        aiIntrmgrProcess.setSysEnd(new Date());
        aiIntrmgrProcess.setSysDatetime(new Date());
//        AiIntrMgrSvc aiIntrMgrSvc = (AiIntrMgrSvc) SpringBeanFactoryUtils.getBean("aiIntrMgrSvc");
////        AiIntrmgrProcessDao aiIntrMgrSvc = (AiIntrmgrProcessDao) SpringBeanFactoryUtils.getBean("aiIntrMgrProcessDao");
//        aiIntrMgrSvc.updateById(aiIntrmgrProcess);
        String result = null;
        String jsonStr;
        int features = Feature.UseBigDecimal.getMask();
        JSON.DEFAULT_PARSER_FEATURE =features;
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM";
        jsonStr = JSON.toJSONString(msg, SerializerFeature.WriteDateUseDateFormat);
        log.info(jsonStr);
        /**
         * 返回数据加密
         * */
        try {
//            result = jsonStr;
            result = AESTool.encrypt(jsonStr);
        } catch (Exception e) {
            log.info("加密失败：", e);
        }
        return result;
    }

    public String doResult(HttpSession session,Msg<T> msg){
        AiIntrmgrProcess aiIntrmgrProcess = new AiIntrmgrProcess();
        aiIntrmgrProcess.setIntfId((String) session.getAttribute(session.getId()+"aiIntrmgrProcess"));
        aiIntrmgrProcess.setIntfRespMessage(msg.toString());
        if(SystemConstant.UDI_SUCCESSFUL_EXECUTION.equals(msg.getCode())){
            aiIntrmgrProcess.setProcessStatus("1");
        }else {
            aiIntrmgrProcess.setProcessStatus("0");
        }
        aiIntrmgrProcess.setSysEnd(new Date());
        aiIntrmgrProcess.setSysDatetime(new Date());
//        AiIntrMgrSvc aiIntrMgrSvc = (AiIntrMgrSvc) SpringBeanFactoryUtils.getBean("aiIntrMgrSvc");
//        aiIntrMgrSvc.updateById(aiIntrmgrProcess);
        String jsonStr;
        int features = Feature.UseBigDecimal.getMask();
        JSON.DEFAULT_PARSER_FEATURE =features;
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        jsonStr = JSON.toJSONString(msg, SerializerFeature.WriteDateUseDateFormat);
        log.info(jsonStr);
        return jsonStr;
    }
}
