MrShi
2025-01-02 77d8f6ccb72cb64c29e77c39fcc8298781d008bb
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
package com.doumee.service.business.impl.hksync;
 
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.haikang.model.HKConstants;
import com.doumee.core.haikang.model.param.BaseResponse;
import com.doumee.core.haikang.model.param.request.*;
import com.doumee.core.haikang.model.param.respose.*;
import com.doumee.core.haikang.service.HKService;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.DeviceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.net.URLEncoder;
 
/**
 * 设备信息表Service实现
 * @author 江蹄蹄
 * @date 2023/11/30 15:33
 */
@Service
public class HkSyncLoginAuthServiceImpl extends HkSyncBaseServiceImpl {
 
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    /**
     * 同步海康门禁设备数据
     * @param param
     * @return
     */
    @Override
    public   String getServiceUrl(HKGetServiceLoginUrlRequest param){
        if(StringUtils.isBlank(param.getLabel())||StringUtils.isBlank(param.getUsername())){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,登录组件失败,请稍后重试!");
        }
        //优先从缓存取数据,如果有缓存,直接返回缓存地址
        /*String cacheUrl = (String) redisTemplate.opsForValue().get(Constants.REDIS_HK_TOKEN_KEY+param.getUsername()+param.getLabel());
        if(StringUtils.isNotBlank(cacheUrl)){
            return cacheUrl;
        }*/
        String url = systemDictDataBiz.queryByCode(Constants.HK_PARAM,param.getLabel()).getCode();
        if(StringUtils.isBlank(url)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        ApplyCTGTRequest ctgtParam = new ApplyCTGTRequest();
        ctgtParam.setLanguage("zh_CN");
        ctgtParam.setUserCode(param.getUsername());
        ctgtParam.setLoginType("2");
        BaseResponse<ApplyCTGTResponse> response = HKService.applyCTGT(ctgtParam);
        if(response !=null && StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE) &&response.getData()!=null &&StringUtils.isNotBlank(response.getData().getCTGT())){
            //处理新增成功的数据,修改海康同步状态
            String ctgt = response.getData().getCTGT();
            ApplySTequest stParam = new ApplySTequest();
            stParam.setCtgt(ctgt);
            stParam.setService(url);
            BaseResponse<ApplySTResponse> result = HKService.applyST(stParam);
            if(result !=null && StringUtils.equals(result.getCode(),HKConstants.RESPONSE_SUCCEE)
                    &&result.getData()!=null &&StringUtils.isNotBlank(result.getData().getST())){
                //处理新增成功的数据,修改海康同步状态
                String st = result.getData().getST();
                try {
                    String serviceUrl =   systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_NGINX_URL).getCode()
                            +(HKConstants.InterfacePath.tokenLoginUrl[0].replace("${st}", st).replace("${service}", URLEncoder.encode(url, "UTF-8")));
//                    redisTemplate.opsForValue().set(Constants.REDIS_HK_TOKEN_KEY+param.getUsername()+param.getLabel(),serviceUrl,1, TimeUnit.HOURS);
                    return serviceUrl;
                }catch (Exception e){
                    throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,登录组件失败,请稍后重试!");
                }
            }else{
                throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,登录组件失败,请稍后重试!");
            }
        }else{
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,登录组件失败,请稍后重试!");
        }
    }
}