郑永安
2023-06-19 59e91a4e9ddaf23cebb12993c774aa899ab22d16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.gk.firework.Scheduls.AssessTask;
 
import com.gk.firework.Domain.AccessAssessApply;
import com.gk.firework.Domain.AssessApply;
import com.gk.firework.Domain.Enterprise;
import com.gk.firework.Domain.Enum.AssessPunishment;
import com.gk.firework.Domain.Enum.EnterpriseStatus;
import com.gk.firework.Domain.Enum.PunishStatus;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.UserInfo;
import com.gk.firework.Service.AccessAssessApplyService;
import com.gk.firework.Service.AssessApplyService;
import com.gk.firework.Service.EnterpriseService;
import com.gk.firework.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @Description: 0点执行从处罚中恢复
* @date 2021/7/12 15:42
*/
@Component
@EnableScheduling
public class RecoverFromPunishmentTask {
 
    @Value("${com.gk.firework.schedules.all.switch}")
    private Boolean switchBtn;
    //销售评定
    @Autowired
    private AssessApplyService assessApplyService;
    //准入评定
    @Autowired
    private AccessAssessApplyService accessAssessApplyService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private UserService userService;
 
 
    @Scheduled(cron = "0 0 0 * * ?")
    public void RecoverFromPunishment() {
 
        if (switchBtn) {
            Date currentTime = new Date();
 
            //1.获取销售评定处罚中的单子
            List<AssessApply> allPunishingSaleList = assessApplyService.selectAllPunishingSaleAssessApply();
            if (allPunishingSaleList.size() > 0) {
                for (AssessApply assessApply : allPunishingSaleList) {
                    //企业信息检查
                    Enterprise enterprise = enterpriseService.selectEnterpriseByNumber(assessApply.getEnterprisenumber());
                    if (enterprise == null)  throw new BusinessException("单位编号:" + assessApply.getEnterprisenumber() + ",企业信息为空");
                    //用户检查 处罚中的企业状态都为1
                    List<UserInfo> userInfos = userService.selectByCompanyId(enterprise.getId(), (byte) 1);
                    if (userInfos.size() != 2) throw new BusinessException("数据异常:用户对象个数不匹配");
 
                    switch (assessApply.getPunishmentmeasure()) {
                            case M0:
                            throw new BusinessException("数据异常,处罚中没有吊销");
                        case M1:
                            this.recoverUserAndEndPunishment(userInfos,1,currentTime,assessApply);
                            break;
                        case M2:
                            this.recoverUserAndEndPunishment(userInfos,3,currentTime,assessApply);
                            break;
                        case M3:
                            this.recoverUserAndEndPunishment(userInfos,6,currentTime,assessApply);
                            break;
                    }
                }
            }
 
 
            //2.获取准入评定处罚中的单子
            List<AccessAssessApply> allPunishingAccessList = accessAssessApplyService.selectAllPunishingAccessAssessApply();
            if (allPunishingAccessList.size() > 0) {
                for (AccessAssessApply accessAssessApply : allPunishingAccessList) {
                    //企业信息检查
                    Enterprise enterprise = enterpriseService.selectEnterpriseByNumber(accessAssessApply.getEnterprisenumber());
                    if (enterprise == null)  throw new BusinessException("单位编号:" + accessAssessApply.getEnterprisenumber() + ",企业信息为空");
                    //用户检查 处罚中的企业状态都为1
                    List<UserInfo> userInfos = userService.selectByCompanyId(enterprise.getId(), (byte) 1);
                    if (userInfos.size() != 2) throw new BusinessException("数据异常:用户对象个数不匹配");
 
                    switch (accessAssessApply.getPunishmentmeasure()) {
                        case M0:
                            throw new BusinessException("数据异常,处罚中没有吊销");
                        case M1:
                            this.recoverUserAndEndPunishment(userInfos, 1, currentTime, accessAssessApply);
                            break;
                        case M2:
                            this.recoverUserAndEndPunishment(userInfos, 3, currentTime, accessAssessApply);
                            break;
                        case M3:
                            this.recoverUserAndEndPunishment(userInfos, 6, currentTime, accessAssessApply);
                            break;
                    }
                }
            }
        }
 
    }
 
 
    //停用企业
    private void activateEnterprise(Enterprise enterprise,List<UserInfo> userInfos) {
        //停用企业
        enterpriseService.setEnterpriseStatus(EnterpriseStatus.ON, enterprise.getId());
        //停用用户
        for (UserInfo user : userInfos) {
            userService.recoverOneById(user.getId());
        }
    }
 
 
    //评定恢复
    private void recoverUserAndEndPunishment(List<UserInfo> userInfos,int num,Date currentTime,AccessAssessApply accessAssessApply) {
 
        if (addMonth(accessAssessApply.getDeadline(), num).after(currentTime)) {
            //用户恢复
            for (UserInfo user : userInfos) {
                userService.recoverOneById(user.getId());
            }
            //单子处罚结束
            accessAssessApply.setPunishstatus(PunishStatus.END);
            accessAssessApplyService.updateById(accessAssessApply);
        }
    }
 
    //销售恢复
    private void recoverUserAndEndPunishment(List<UserInfo> userInfos,int num,Date currentTime,AssessApply assessApply) {
 
        if (addMonth(assessApply.getApprovetime(), num).after(currentTime)) {
            //用户恢复
            for (UserInfo user : userInfos) {
                userService.recoverOneById(user.getId());
            }
            //单子处罚结束
            assessApply.setPunishstatus(PunishStatus.END);
            assessApplyService.updateById(assessApply);
        }
    }
 
 
    //往后推n个月
    private Date addMonth(Date date,int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, n);
        return calendar.getTime();
    }
 
}