张晓波
2023-12-12 d3e6773ef7cd0e11a444b41ec3da15938fabd85b
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
160
161
162
163
164
165
166
167
168
169
package com.thhy.mobile.modules.biz.login.service.impl;
 
import com.thhy.general.common.BasicMessage;
import com.thhy.general.common.BasicStatus;
import com.thhy.general.common.entity.SysUserDto;
import com.thhy.general.config.BasicContext;
import com.thhy.general.config.GlobalConfig;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.exception.BasicException;
import com.thhy.general.utils.DeepCopyUtils;
import com.thhy.general.utils.UUIDUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.general.utils.password.PasswdUtils;
import com.thhy.mobile.modules.biz.login.entity.*;
import com.thhy.mobile.modules.biz.login.mapper.SysUserMapper;
import com.thhy.mobile.modules.biz.login.service.LoginService;
import com.thhy.mobile.modules.biz.menu.entity.Menu;
import com.thhy.mobile.modules.biz.menu.mapper.MenuMapper;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
@Service
public class LoginServiceImpl implements LoginService {
 
    @Autowired
    private SysUserMapper sysUsersMapper;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private GlobalConfig globalConfig;
 
    @Autowired
    private MenuMapper menuMapper;
 
    @Override
    public LoginVo login(SysUserDto sysUserDto) {
        //根据用户名获取用户信息
        SysUsers sysUser = sysUsersMapper.queryByUsername(sysUserDto.getUsername());
        if(sysUser==null){
            throw new BasicException(BasicStatus.LOGIN_FAILD); //用户名不存在导致登录失败
        }
        //解析并校验密码
        boolean result = PasswdUtils.validatePassword(sysUserDto.getPassword(),sysUser.getPassword());
        if(!result){
            throw new BasicException(BasicStatus.USER_NOT_EXIST);
        }
        if(sysUser.getSubAdmin()==1){
            HashMap<String,Object> map = sysUsersMapper.queryCompanyByComAdmin(sysUser.getUserId());
            sysUser.setCompanyId(map.get("companyId").toString());
            sysUser.setCompanyName(map.get("companyName").toString());
        }
        if(sysUser.getIsPlat()==1){
            HashMap<String,Object> map = sysUsersMapper.queryCompanyByUser(sysUser.getUserId());
            sysUser.setCompanyId(map.get("companyId").toString());
            sysUser.setCompanyName(map.get("companyName").toString());
        }
        SysUserInfo sysUserInfo = new SysUserInfo();
        DeepCopyUtils.copy(sysUser,sysUserInfo); //这里不需要密码
 
        String token = UUIDUtils.create()+sysUserInfo.getUserId();
        redissonClient.getBucket(globalConfig.getKeyPrefix()+":usertoken:"+ token).trySet(sysUserInfo,1000, TimeUnit.MINUTES);
        //用户菜单权限
        List<PdaMenuVo> menuList = menuMapper.findMenuListByUser(sysUserInfo.getUserId());
        if(menuList.size()<1){
            throw new BasicException(BasicStatus.NO_ENABLE_MENU);
        }
        BasicContext.setToken(token);
        return new LoginVo(token,menuList,sysUserInfo);
    }
 
    @Override
    public List<HashMap<String,Object>> proLines() {
        return sysUsersMapper.queryAllProLine();
    }
 
    @Override
    public Map<String, Object> choseLine(ProLineDto proLineDto, HttpServletRequest request) {
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        String token = request.getHeader("usertoken");
        String proLineName = sysUsersMapper.queryProLineNameById(proLineDto.getProLineId());
        userInfo.setProLineId(proLineDto.getProLineId());
        userInfo.setProLineName(proLineName);
        long min = tokentime();
        redissonClient.getBucket(globalConfig.getKeyPrefix()+":usertoken:"+ token).trySet(userInfo,min, TimeUnit.MINUTES);
 
        int count = sysUsersMapper.countTodayLinePlanByLineId(proLineDto.getProLineId());
        Map<String,Object> map = new HashMap<>();
        if(count>0){
            map.put("dateset",false);
        }else {
            map.put("dateset",true);
        }
        return map;
    }
 
    @Transactional
    @Override
    public void commitLinePlan(ProLineDto proLineDto) {
        List<String> ringNums = proLineDto.getRingNums();
        String ringNumExits = sysUsersMapper.checkPipeExitsByRingNums(ringNums);
        if(!StringUtils.isEmpty(ringNumExits)){
            throw new BasicException(new BasicMessage("00900",ringNumExits+"环号已存在"));
        }
        String ringNumPlanExits = sysUsersMapper.checkPlanExitsByRingNums(ringNums);
        if(!StringUtils.isEmpty(ringNumPlanExits)){
            throw new BasicException(new BasicMessage("00901",ringNumPlanExits+"环号其他产线已存在"));
        }
        ProLinePlan proLinePlan = new ProLinePlan();
        proLinePlan.setProLineId(proLineDto.getProLineId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date start = sdf.parse(proLineDto.getDateStr()+" 07:00:00");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(start);
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date end = calendar.getTime();
            proLinePlan.setSetDayStart(start);
            proLinePlan.setSetDayEnd(end);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        for(String ringNum : ringNums){
            proLinePlan.setRingNum(ringNum);
            sysUsersMapper.insertProLinePlan(proLinePlan);
        }
    }
 
    public long tokentime(){
        Date currentTime = new Date();
        SimpleDateFormat sdfStand = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdfSeven = new SimpleDateFormat("yyyy-MM-dd 07:00:00");
        String todaySevenStr = sdfSeven.format(currentTime);
        Date todaySeven = null;
        try {
            todaySeven = sdfStand.parse(todaySevenStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
 
        if(currentTime.getTime()>todaySeven.getTime()){
            //当前时间大于 当天7点
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(todaySeven);
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date tommSeven = calendar.getTime();
            long diff = (tommSeven.getTime()-currentTime.getTime())/1000/60;
            return diff;
        }else{
            long diff = (todaySeven.getTime()-currentTime.getTime())/1000/60;
            return diff;
        }
    }
 
    public static void main(String[] args) throws Exception{
 
    }
}