package com.yihu.jw.gateway.service;



import com.yihu.jw.entity.iot.gateway.GcToken;
import com.yihu.jw.entity.iot.gateway.GcTokenLog;
import com.yihu.jw.gateway.dao.GcTokenDao;
import com.yihu.jw.gateway.dao.GcTokenLogDao;
import com.yihu.jw.util.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by chenweida on 2017/8/17.
 */
@Service
public class GcTokenService {
    @Autowired
    private GcTokenLogDao tokenLogDao;
    @Autowired
    private GcTokenDao tokenDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * 根据appid生成token
     *
     * @param appid
     * @param appSecret
     * @return
     */
    @Transactional
    public GcToken createToken(String appid, String appSecret, String ip, String overTime,Integer tokenTime) {
        try {
            //把该用户之前有效的token设置为无效
            tokenDao.updateDel(appid);

            //创新新的appId
            Date date = new Date();
            String token = UUID.randomUUID().toString();


            GcToken gc = new GcToken();
            gc.setAppid(appid);
            gc.setCreateTime(date);
            //2小时过期
            if(StringUtils.isEmpty(overTime)){

                if(tokenTime==null){
                    tokenTime=2;
                }
                gc.setOutTime(DateUtil.getNextMin(date, tokenTime*60));
            }else{
                gc.setOutTime(DateUtil.strToDate(overTime));
            }
            gc.setAccesstoken(token);
            gc.setDel(1);
            tokenDao.save(gc);

            //保存日志
            GcTokenLog gcTokenLog = new GcTokenLog();
            gcTokenLog.setAppIp(appid);
            gcTokenLog.setCreateTime(new Date());
            gcTokenLog.setAppIp(ip);
            gcTokenLog.setMessage("创建token");
            gcTokenLog.setAccesstoken(token);
            gcTokenLog.setFlag(1);
            tokenLogDao.save(gcTokenLog);
            return gc;
        } catch (Exception e) {
            //保存失败日志
            GcTokenLog gcTokenLog = new GcTokenLog();
            gcTokenLog.setAppIp(appid);
            gcTokenLog.setCreateTime(new Date());
            gcTokenLog.setAppIp(ip);
            gcTokenLog.setMessage("创建token");
            gcTokenLog.setFlag(0);
            tokenLogDao.save(gcTokenLog);
        }
        return null;
    }

    /**
     * 根据token获取GcToken对象
     * @param token
     * @return
     */
    public GcToken getToken(String token) {
        return tokenDao.findByToken(token);
    }

    public List<GcToken> findByAppId(String appid,String wxId){
        if (wxId.equalsIgnoreCase("xm_ykyy_wx")){
            String sql = "select ID AS \"id\",APPID as \"appId\",ACCESSTOKEN as \"accessToken\",CREATE_TIME as \"createTime\",to_char(OUT_TIME,'yyyy-MM-dd hh24:mi:ss') as \"outTime\" from GC_TOKEN where APPID = '"+appid+"' and OUT_TIME >= to_date('"+DateUtil.getStringDate()+"','YYYY-MM-DD HH24:MI:SS')";
            List<Map<String,Object>> mapList = jdbcTemplate.queryForList(sql);
            List<GcToken> gcTokens = new ArrayList<>();
            for (Map<String,Object> map:mapList){
                GcToken gcToken = new GcToken();
                gcToken.setId(Integer.parseInt(map.get("id").toString()));
                gcToken.setAccesstoken(map.get("accessToken").toString());
                gcToken.setAppid(map.get("appId").toString());
                gcToken.setOutTime(DateUtil.strToDate(map.get("outTime").toString()));
                gcTokens.add(gcToken);
                System.out.print("gcToken"+gcToken.getOutTime());
            }
            return gcTokens;
        }else {
            return tokenDao.findByAppId(appid);
        }

    }
}
