package com.yihu.wlyy.service.third.httplog;

import com.yihu.wlyy.entity.httpLog.HttpLog;
import com.yihu.wlyy.repository.httplog.HttpLogDao;
import com.yihu.wlyy.service.system.SystemDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by ysj on 2017/1/12.
 * 日志服务
 */
@Service
public class LogService {

    @Autowired
    private HttpLogDao httpLogDao;

    @Autowired
    private SystemDictService systemDictService;

    public String openApiType = "0";
    public String signType = "1";
    public String archiveType = "2";
    public String onepayType = "3";



    /**
     * 新增开放api调用日志
     * */
    public void saveOpenApiLog(Boolean isSuccess,String url,String content,String method,String params,String response,String error)
    {
        try {
            HttpLog log = new HttpLog();
            log.setType("0");  //开放接口
            log.setApi(url);
            log.setContent(content);
            log.setMethod(method);
            log.setParams(params);
            log.setCreateTime(new Date());
            log.setCreateTime(new Date());
            log.setResponse(response);
            log.setError(error);
            log.setStatus(isSuccess?"1":"0");
            httpLogDao.save(log);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }


    /**
     * 保存http调用日志  type 0开放api  1基卫接口  2市民健康网接口   3易联众接口
     */
    public void saveHttpLog(Boolean isSuccess,String url,String content,String method,String header,String params,String response,String error,String type)
    {
        try {
            //成功日志是否保存
            Boolean saveSuccessLog = systemDictService.getSaveSuccessLog();
            if(!isSuccess || saveSuccessLog) {
                //新增日志
                HttpLog log = new HttpLog();
                log.setType(type);
                log.setApi(url);
                log.setContent(content);
                log.setMethod(method);
                log.setHeader(header);
                log.setParams(params);
                log.setCreateTime(new Date());
                log.setResponse(response);
                log.setError(error);
                log.setStatus(isSuccess?"1":"0");
                httpLogDao.save(log);
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }


}
