CASIPSendTimeTask.java 5.32 KB
Newer Older
lzxry committed
1 2 3 4 5
package track.task;

import adi.model.ADIUser;
import adi.service.ADIAccountService;
import common.model.Contract;
lzxry committed
6
import common.model.PackageBase;
lzxry committed
7
import common.repository.ContractRepository;
lzxry committed
8
import common.repository.PackageBaseRepository;
lzxry committed
9 10
import org.joda.time.DateTime;
import org.joda.time.Days;
kangxiaoshan committed
11 12
import org.json.JSONArray;
import org.json.JSONObject;
lzxry committed
13 14 15 16
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
kangxiaoshan committed
17
import util.Constant;
lzxry committed
18
import util.DateUtil;
kangxiaoshan committed
19
import util.HttpClientUtil;
lzxry committed
20

kangxiaoshan committed
21
import java.util.ArrayList;
lzxry committed
22
import java.util.HashMap;
lzxry committed
23
import java.util.List;
lzxry committed
24
import java.util.Map;
lzxry committed
25 26 27 28 29 30 31 32 33 34 35 36 37

/**
 * @author liyin
 * @description
 * @date
 */
public class CASIPSendTimeTask {
    private final Logger logger = LoggerFactory.getLogger(CASIPSendTimeTask.class);

    @Autowired
    ADIAccountService adiAccountService;
    @Autowired
    ContractRepository contractRepository;
lzxry committed
38 39
    @Autowired
    PackageBaseRepository packageBaseRepository;
lzxry committed
40 41


kangxiaoshan committed
42
    public void task() {
lzxry committed
43
        logger.info("CAS的IP定制发送日定时任务");
lzxry committed
44 45 46 47 48
        List<PackageBase> packageBases = packageBaseRepository.findByPlatAndStatus("cas", 1);
        Map<Long, PackageBase> packageBaseMap = new HashMap<>();
        for (PackageBase base : packageBases) {
            packageBaseMap.put(base.getId(), base);
        }
kangxiaoshan committed
49
        List<Contract> contracts = contractRepository.findByPlatformAndShareSign("cas", 0);
lzxry committed
50
        for (Contract contract : contracts) {
lzxry committed
51
            PackageBase packageBase = packageBaseMap.get(contract.getPriceLevel());
kangxiaoshan committed
52 53
            ADIUser adiUser = adiAccountService.findOne(contract.getEmail(), packageBase == null ? "" : packageBase.getPackageName());
            if (adiUser != null && !StringUtils.isEmpty(adiUser.getSendTime())) {
lzxry committed
54
                contract.setShareSign(1);
kangxiaoshan committed
55
                DateTime sendTime = new DateTime(DateUtil.parseDate(DateUtil.C_TIME_PATTON_DEFAULT, adiUser.getSendTime()));
lzxry committed
56 57

                DateTime startDate = new DateTime(contract.getStartDate());
kangxiaoshan committed
58
                if (sendTime.isBefore(startDate)) {
lzxry committed
59 60 61 62 63 64 65 66 67 68 69
                    sendTime = startDate;
                }
                DateTime endDate = new DateTime(contract.getEndDate());
                String validStartDateStr = sendTime.toString(DateUtil.C_DATE_PATTON_DEFAULT);
                int contractAllDay = Days.daysBetween(startDate, endDate).getDays();//合同总天数-1 ,用于计算结束日期
                String validEndDateStr = sendTime.plusDays(contractAllDay).toString("yyyy-MM-dd");
                contract.setValidStartDate(validStartDateStr);
                contract.setValidEndDate(validEndDateStr);
                contractRepository.save(contract);
            }
        }
kangxiaoshan committed
70 71

        //定时同步变更的账号到对应合同
kangxiaoshan committed
72
        syncChangedAccount(packageBaseMap);
kangxiaoshan committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }

    //定时同步变更的账号到对应合同 (packType: 国内 海外)
    public void syncChangedAccount(Map<Long, PackageBase> packageBaseMap) {
        String ds = DateTime.now().plusDays(-1).toString("yyyy-MM-dd");
        //国内
        String url = Constant.adiUrl.split(",")[0] + "/adi/api/user/change/day?date=" + ds;
        syncChangedAccount(packageBaseMap, url, "国内");

        //海外
        url = Constant.adiUrl.split(",")[1] + "/adi/api/user/overseas/change/day?date=" + ds;
        syncChangedAccount(packageBaseMap, url, "海外");


    }

    //定时同步变更的账号到对应合同 (packType: 国内 海外)
    public void syncChangedAccount(Map<Long, PackageBase> packageBaseMap, String url, String packType) {
        String request = HttpClientUtil.doHttpGetRequest(url, "");
        try {
            JSONObject rs = new JSONObject(request);
            if ("200".equals(rs.get("code") + "")) {

                if (rs.get("content") != null && !rs.get("content").toString().startsWith("[")) {
                    return;
                }

                JSONArray array = (JSONArray) rs.get("content");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = (JSONObject) array.get(i);
                    String newEmail = object.getString("newEmail");
                    String originEmail = object.getString("originEmail");
                    if (StringUtils.isEmpty(newEmail) || StringUtils.isEmpty(originEmail)) {
                        continue;
                    }
                    List<Contract> contracts = contractRepository.findbyEmailAndPlatform("cas", originEmail);
                    if (contracts == null) {
                        continue;
                    }
                    List<Contract> updateList = new ArrayList<>();
                    for (Contract contract : contracts) {
                        PackageBase packageBase = packageBaseMap.get(contract.getPriceLevel());
                        if (packageBase.getPackageName().contains(packType)) {
                            contract.setEmail(newEmail);
                            updateList.add(contract);
                        }
                    }

                    if (!updateList.isEmpty()) {
                        //更新账号
                        contractRepository.save(contracts);
                    }

                }
            }
        } catch (Exception e) {
            logger.error("syncChangedAccount erro " + packType, e);
        }
lzxry committed
131 132
    }
}