package com.tax.sign.service.impl;

import com.tax.sign.mapper.SgSignLogMapper;
import com.tax.sign.model.SgSignLog;
import com.tax.sign.model.SignDateInfo;
import com.tax.sign.model.SignReq;
import com.tax.sign.service.api.SgSignLogService;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.utils.IpUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Transactional(rollbackFor = Exception.class)
@Component
@ComponentScan(basePackages = {"com.tax.sign.dao"})
public class SgSignLogServiceImpl implements SgSignLogService {
    @Autowired
    private SgSignLogMapper sgSignLogMapper;
    @Autowired
    protected RedisService redisService;

    @Override
    public void saveSgSignLog(SgSignLog sgSignLog) {
//        sgSignLogMapper.insert(sgSignLog);
    }

    @Override
    public SgSignLog getLog(SignReq signReq, SignDateInfo signDateInfoByRedis, HttpServletRequest request) {
        SgSignLog sgSignLog = new SgSignLog();
        String reqContext = JSONObject.fromObject(signReq).toString();
        if(reqContext.length()>1000){
            reqContext = reqContext.substring(0,999);
        }
        sgSignLog.setReqContext(reqContext);
        sgSignLog.setReqDate(new Date());
        sgSignLog.setTaxno(signReq.getTaxno());
        sgSignLog.setUserCode(signReq.getAccessKeyID());
        sgSignLog.setZone(signReq.getDqdm());
        if(signDateInfoByRedis!=null) {
            sgSignLog.setServerDateWay(signDateInfoByRedis.getServerDateWay());
        }
        sgSignLog.setIpAddress(IpUtil.getRequestRealIp(request));
        return sgSignLog;
    }

    public void saveSgSignLogOn(SgSignLog sgSignLog) {
        sgSignLogMapper.insert(sgSignLog);
    }

}
