jiangping
2023-12-14 e2f8fb1fbe26ba6d92f3e5dfcaeb0c69abe76b25
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.doumee.service.business.impl.hksync;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.AcsDeviceListRequest;
import com.doumee.core.haikang.model.param.request.ParkListRequest;
import com.doumee.core.haikang.model.param.respose.AcsDeviceInfoResponse;
import com.doumee.core.haikang.model.param.respose.AcsDeviceListResponse;
import com.doumee.core.haikang.model.param.respose.ParkListResponse;
import com.doumee.core.haikang.service.HKService;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.business.DeviceMapper;
import com.doumee.dao.business.ParksMapper;
import com.doumee.dao.business.model.Device;
import com.doumee.dao.business.model.Parks;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 设备信息表Service实现
 * @author 江蹄蹄
 * @date 2023/11/30 15:33
 */
@Service
public class HkSyncDeviceServiceImpl extends HkSyncBaseServiceImpl {
 
    @Autowired
    private DeviceMapper deviceMapper;
 
    /**
     * 同步海康门禁设备数据
     * @param param
     * @return
     */
    @Override
//    @Async
    public String syncHkDevices(AcsDeviceListRequest param){
        List<Device> deleteList = new ArrayList<>();
        List<Device> addList = new ArrayList<>();
        List<Device> editList = new ArrayList<>();
        List<AcsDeviceInfoResponse> allHkList = new ArrayList<>();
        Date date = new Date();
        //查询全部门禁设备数据
        List<Device> allList = deviceMapper.selectList(null);
        boolean hasNext = true;
        int curTotal = 0;
        int curPage = 1;
        while (hasNext){
            //分页遍历循环查询所有门禁设备数据
            param = new AcsDeviceListRequest();
            param.setPageNo(curPage);
            param.setPageSize(100);
            BaseResponse<AcsDeviceListResponse> response = HKService.acsDeviceList(param);
            if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){
                throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
            }
            AcsDeviceListResponse r = response.getData();
            curTotal += 100;
            if(curTotal >= r.getTotal()){
                hasNext = false;
            }
            if(r.getList() == null || r.getList().size()==0){
                hasNext =false;
            }else{
                allHkList.addAll(r.getList());
            }
            curPage++;
        }
        /**
         * 获取增删改数据集合
         */
        getDataChangeList(allList,allHkList,addList,editList,deleteList,date);
        if(deleteList.size()>0){
            //逻辑删除
            for(Device d : deleteList){
                deviceMapper.updateById(d);
            }
        }
        if(addList.size()>0){
            deviceMapper.insertBatchSomeColumn(addList);
        }
        if(editList.size()>0){
            for(Device d : editList){
                deviceMapper.updateById(d);
            }
        }
        return "同步数据:新增【"+addList.size()+"】条,更新【"+editList.size()+"】条,删除【"+deleteList.size()+"】条";
    }
 
    private void getDataChangeList(List<Device> allList, List<AcsDeviceInfoResponse> allHkList, List<Device> addList, List<Device> editList,List<Device> deleteList, Date date) {
        if(allHkList!=null && allHkList.size()>0){
            for(AcsDeviceInfoResponse device : allHkList){
                Device model = getExistedDevice(device,allList);
                if(model !=null){
                    //如果已存在,则更新数据
                    model =  initDataByHkData(model,device,date);
                    editList.add(model);
                }else{
                    //如果不存在,则新增数据
                    model = new Device();
                    model =  initDataByHkData(model,device,date);
                    addList.add(model);
                }
            }
        }
        //判断获取删除的门禁设备,逻辑删除
        getDeleteList(allList,allHkList,deleteList,date);
    }
 
    /**
     * 初始化海康入库数据
     * @param model
     * @param device
     * @param date
     * @return
     */
    private Device initDataByHkData(Device model, AcsDeviceInfoResponse device,Date date) {
        model.setIsdeleted(Constants.ZERO);
        model.setIp(device.getIp());
        model.setHkDate(date);
        model.setCreateDate(DateUtil.getISO8601DateByStr(device.getCreateTime()));
        model.setEditDate(DateUtil.getISO8601DateByStr(device.getUpdateTime()));
        model.setNo(device.getDevSerialNum());
        model.setHkId(device.getIndexCode());
        model.setName(device.getName());
        model.setHkStatus(Constants.ONE);
        model.setManufature(device.getManufacturer());
        model.setType(Constants.ZERO);
//        model.setNo(device.getDevSerialNum());
//        model.setChannelNo(device);
        model.setResourceType(device.getResourceType());
        model.setDevTypeCode(device.getDevTypeCode());
        model.setManufature(device.getManufacturer());
        model.setPort(device.getPort());
        model.setRemark(JSONObject.toJSONString(device));
        return  model;
    }
 
    private Device getExistedDevice(AcsDeviceInfoResponse device, List<Device> allList) {
        if(allList.size()>0){
            for(Device r : allList){
                if(StringUtils.equals(r.getHkId(), device.getIndexCode())){
                    //表示未删除
                    return  r;
                }
            }
        }
        return  null;
    }
 
    private void getDeleteList(List<Device> allList, List<AcsDeviceInfoResponse> allHkList,List<Device> deleteList ,Date date) {
        if(allList!=null && allList.size()>0){
            for(Device device : allList){
                if(isDeletedDevice(device,allHkList)){
                    device.setIsdeleted(Constants.ONE);
                    device.setEditDate(date);
                    deleteList.add(device);
                }
            }
        }
    }
 
    private boolean isDeletedDevice(Device device, List<AcsDeviceInfoResponse> allHkList) {
        if(allHkList.size()>0){
            for(AcsDeviceInfoResponse r : allHkList){
                if(StringUtils.equals(device.getHkId(), r.getIndexCode())){
                    //表示未删除
                    return  false;
                }
            }
        }
        return  true;
 
    }
    
}