package com.sifang.modules.q7link.service;

import com.q7link.openapi.model.Openid;
import com.sifang.modules.q7link.dao.Q7linkRelDao;
import com.sifang.modules.q7link.entity.CrmQ7linkRel;
import com.sifang.modules.q7link.utils.Q7linkUtils;
import com.sifang.modules.sys.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class Q7linkApiService {

    private static Logger logger = LoggerFactory.getLogger(Q7linkApiService.class);

    @Autowired
    private Q7linkRelDao relDao;

    @Transactional(rollbackFor = Exception.class)
    public String syscOpenId(String code, String requestId) {

        Openid result = Q7linkUtils.getCode(code);
        if (result == null || StringUtil.isBlank(result.getOpenid())) {
            return "";
        }
        CrmQ7linkRel linkRel = relDao.get(requestId);
        if (linkRel == null) {
            linkRel = new CrmQ7linkRel();
            linkRel.setRequestId(requestId);
        }
        linkRel.setCode(code);
        linkRel.setOpenid(result.getOpenid());
        relDao.save(linkRel);
        return linkRel.getOpenid();
    }




}
