UsersSiteJob.java 5.82 KB
package com.cnlive.shenhe.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.mapper.ShySitesMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.cnlive.shenhe.utils.HttpUtil;
import com.cnlive.shenhe.utils.OpenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Auther: 周伟鹏
 * @Date: 2018/11/30 11:31
 * @Description:同步用户和站点信息
 */
@Component
public class UsersSiteJob {

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

    @Value("${sync_users_api}")
    private String syncUserApi;

    @Value("${sync_users_app_key}")
    private String app_key;

    @Autowired
    ShyUsersMapper shyUsersMapper;

    @Autowired
    ShySitesMapper shySitesMapper;


    @Scheduled(fixedRate = 10 * 60 * 1000)
    public void syncUsers() {
        try {
            String tm = "0";
            Example usersExample = new Example(ShyUsers.class);
            usersExample.setOrderByClause("tm desc");
            List<ShyUsers> usersList = shyUsersMapper.selectByExample(usersExample);
            if (!usersList.isEmpty()) {
                Long tmStr = usersList.get(0).getTm();
                if (CommonUtils.isNotNull(tmStr)) {
                    tm = tmStr + "";
                }
            }
            HashMap<String, String> dataMap = new HashMap<>();
            dataMap.put("tm", tm);
            dataMap.put("timestamp", System.currentTimeMillis() / 1000 + "");
            String httpUrl = OpenUtil.buildURL(syncUserApi, dataMap, app_key);
            logger.info("httpUrl===" + httpUrl);
            Map<String, String> resultMap = HttpUtil.init().post(httpUrl);
            JSONObject result = JSON.parseObject(resultMap.get("result"));
            logger.info("请求用户云结果,result:{}", result);
            String errorCode = result.getString("errorCode");
            if ("0".equals(errorCode)) {
                JSONArray data = result.getJSONArray("data");
                for (int i = 0; i < data.size(); i++) {
                    //用户信息入库
                    JSONObject user = data.getJSONObject(i);
                    Integer id = user.getInteger("sid");
                    String user_id = CommonUtils.md5(id + "");
                    String email = user.getString("email");
                    Integer spid = user.getInteger("new_sp_id");
                    String userLogo = user.getString("userLogo");
                    boolean master = user.getBoolean("master");
                    Long utm = user.getLong("tm");
                    String username = user.getString("contacts");
                    boolean status = user.getIntValue("status") == CommonConst.USER_STATUS_USE;
                    if (CommonUtils.isEmpty(username)) {
                        username = Integer.toString(id);
                    }
                    String mobile = user.getString("mobile");
                    String company_brief = user.getString("sname");
                    String company = user.getString("company");
                    ShyUsers shyUsers = new ShyUsers();
                    shyUsers.setEmail(email);
                    shyUsers.setId(id);
                    shyUsers.setUser_id(user_id);
                    shyUsers.setSp_id(spid);
                    shyUsers.setMaster(master);
                    shyUsers.setState(status);
                    shyUsers.setMobile(mobile);
                    shyUsers.setUsername(username);
                    shyUsers.setCompany_brief(company_brief);
                    shyUsers.setCompany_name(company);
                    shyUsers.setUser_logo(userLogo);
                    shyUsers.setTm(utm);
                    ShyUsers old_shyUsers = shyUsersMapper.selectByPrimaryKey(id);
                    if (CommonUtils.isNull(old_shyUsers)) {
                        shyUsersMapper.insertSelective(shyUsers);
                    } else {
                        shyUsersMapper.updateByPrimaryKeySelective(shyUsers);
                    }
                    //站点信息入库
                    ShySites shySites = new ShySites();
                    shySites.setSpid(spid);
                    List<ShySites> shySitesList = shySitesMapper.select(shySites);
                    if (shySitesList.isEmpty()) {
                        shySites.setCompany(company);
                        shySites.setName(company_brief);
                        shySites.setUpdated_at(CommonUtils.getCurrentTime());
                        shySites.setCreated_at(CommonUtils.getCurrentTime());
                        shySitesMapper.insertSelective(shySites);
                    } else if (CommonUtils.isNotNull(shySitesList.get(0))) {
                        shySites = shySitesList.get(0);
                        shySites.setCompany(company);
                        shySites.setName(company_brief);
                        shySites.setUpdated_at(CommonUtils.getCurrentTime());
                        shySitesMapper.updateByPrimaryKeySelective(shySites);
                    }
                }
            } else {
                logger.error("请求用户云失败,errorCode:{},errorMessage{}", errorCode, result.getString("errorMessage"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}