shicf
2024-08-23 2fef20fe45a1fc901b51243bcc60682524447990
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.product.device.service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import com.product.admin.service.PublicService;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.device.config.DeviceCode;
import com.product.device.config.DeviceConst;
import com.product.device.service.idel.IDeviceInspectionService;
import com.product.module.sys.entity.SystemUser;
import com.product.util.BaseUtil;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
 
/**
 * 设备点检
 *
 * @Author
 * @Date 2022/3/8 16:33
 * @Desc 功能相关 service
 */
@Service
public class CommonInspectionService extends AbstractBaseService implements IDeviceInspectionService {
 
    @Autowired
    public BaseDao baseDao;
    @Autowired
    DeviceManagerService deviceManagerService;
    @Autowired
    PublicService publicService;
    @Autowired
    DeviceTaskService deviceTaskService;
    /**
     * 获取设备信息 包括子设备
     *
     * @param device_sn
     * @return
     */
    public FieldSetEntity createInspection(String device_sn) {
        FieldSetEntity dev = deviceManagerService.findDevice( device_sn);
        baseDao.loadPromptData(dev);
        if (dev != null) {
            DataTableEntity dt = baseDao.listTable(DeviceConst.TABLE_PRODUCT_DEVICE_INSPECT_ITEM_CONFIG, " uuid in( select config_uuid from" + DeviceConst.TABLE_PRODUCT_INSPECT_SETTING_SUB + " where  main_uuid=(select uuid from " + DeviceConst.TABLE_PRODUCT_INSPECT_SETTING + " where device_type=? )", new String[]{dev.getString("device_type")});
            if (!DataTableEntity.isEmpty(dt)) {
                Map<String, DataTableEntity> sub = new HashMap<>();
                sub.put("sub", dt);
                dev.setSubData(sub);
                dev.addSubDataTable(dt);
            }
        }
        return dev;
    }
 
    public DataTableEntity getPatrolMonthlView(FieldSetEntity fliter)  throws BaseException{
        StringBuffer sb = new StringBuffer();
        ArrayList<String> parme = new ArrayList<>();
        sb.append(" SELECT a.name obName,GROUP_CONCAT( CONCAT(check_time,'&',result) ) check_time FROM(");
        sb.append(" SELECT c.name,b.result,DATE_FORMAT( a.check_time, '%d' ) check_time FROM ");
 
        sb.append(" "+fliter.getTableName()+" a LEFT JOIN "+fliter.getTableName()+"_sub b on a.uuid =b.record_uuid ");
 
        sb.append(" LEFT JOIN product_device_patrol_config c on b.patrol_uuid=c.uuid where 1=1");
 
 
        if (!StringUtils.isEmpty(fliter.getString("device_number"))) {
            sb.append(" and a.device_number=?");
            parme.add(fliter.getString("device_number"));
        }
        if (!StringUtils.isEmpty(fliter.getString("patrol_user"))) {
            sb.append(" and a.patrol_user=?");
            parme.add(fliter.getString("patrol_user"));
        }
        if (!StringUtils.isEmpty(fliter.getString("check_time"))) {
            sb.append(" and DATE_FORMAT( a.check_time, '%Y-%m' )=?");
            parme.add(fliter.getString("check_time").substring(0, 7));
        } else {
            throw new BaseException("0","年月不能为空");
        }
        sb.append(" GROUP BY c.name,b.result,DATE_FORMAT( a.check_time, '%d' )  ");
        sb.append(") a GROUP BY a.name ");
        String[] pArr = parme.toArray(new String[parme.size()]);
        DataTableEntity dt = baseDao.listTable(sb.toString(), pArr);
        for (int i = 0; i <dt.getRows() ; i++) {
            FieldSetEntity fs = dt.getFieldSetEntity(i);
            String check_time = fs.getString("check_time");
            if (!StringUtils.isEmpty(check_time)){
                String[] check_time_arr = check_time.split(",");
                for (int j = 0; j <check_time_arr.length ; j++) {
                    String[] v = check_time_arr[j].split("&");
                    fs.setValue("m"+v[0],v[1]);
                }
 
            }
        }
        return dt;
    }
 
    public FieldSetEntity getInspectionSetting(String device_type) {
        FieldSetEntity fs = baseDao.getFieldSetByFilter(DeviceConst.TABLE_PRODUCT_INSPECTION_SETTING, " device_type=? ", new String[]{device_type}, false);
        return fs;
    }
 
    public DataTableEntity getTreeArchives() {
        DataTableEntity dt = baseDao.listTable(DeviceConst.TABLE_PRODUCT_DEVICE_ARCHIVE, new String[]{});
        return dt;
    }
 
    /**
     * 保持设备配置(点检、巡检、保养)
     * @param fs
     * @return
     */
    @Transactional
    public boolean saveSetting(FieldSetEntity fs) {
        DataTableEntity subTable = fs.getSubDataTable("subTable");
        boolean re = true;
        for (int i = 0; i < subTable.getRows(); i++) {
            FieldSetEntity sub = subTable.getFieldSetEntity(i);
            sub.setTableName(fs.getTableName());
            if (!StringUtils.isEmpty(sub.getString("~type~"))) {
                publicService.delete(sub);
                continue;
            }
            sub.setValue("device_number", fs.getString("device_number"));
            sub.setValue("cycle", fs.getString("cycle"));
            baseDao.executeUpdate("update product_device_maintenance_setting set device_number=? ",new String[] {fs.getString("device_number")});
 
            if ("product_device_maintenance_setting".equals(fs.getTableName())){
                sub.setValue("type", fs.getString("type"));
                sub.setValue("ahead_notice_day", fs.getString("ahead_notice_day"));
            }
            if ("product_device_inspection_setting".equals(fs.getTableName())){
                sub.setValue("start_time", fs.getDate("start_time"));
                sub.setValue("warn_time", fs.getDate("warn_time"));
            }
            String uuid = publicService.saveFieldSetEntity(sub);
            if (StringUtils.isEmpty(uuid)) {
                re = false;
            }
        }
        return re;
    }
 
    @Transactional
    public boolean batchReview(FieldSetEntity fs) {
        String uuid = fs.getString("uuid");
        if (StringUtils.isEmpty(uuid)) {
            return false;
        }
        String[] split = uuid.split(",");
        boolean re = true;
        for (int i = 0; i < split.length; i++) {
            FieldSetEntity recordFs = new FieldSetEntity();
            recordFs.setTableName(fs.getTableName());
            recordFs.setValue("uuid", split[i]);
            recordFs.setValue("check_user", SpringMVCContextHolder.getCurrentUser().getUser_id());
            recordFs.setValue("check_time", new Date());
            String uuids = publicService.saveFieldSetEntity(recordFs);
            if (StringUtils.isEmpty(uuids)) {
                re = false;
            }
 
        }
        return re;
    }
 
    public FieldSetEntity findPatrolSetting() {
        DataTableEntity dataTableEntity = baseDao.listTable(DeviceConst.TABLE_PRODUCT_PATROL_SETTING, "");
        FieldSetEntity fs = new FieldSetEntity();
        fs.setTableName(DeviceConst.TABLE_PRODUCT_PATROL_SETTING);
 
        DataTableEntity dt = new DataTableEntity();
 
        for (int i = 0; i < dataTableEntity.getRows(); i++) {
            FieldSetEntity sub = dataTableEntity.getFieldSetEntity(i);
            FieldSetEntity fsSub = new FieldSetEntity();
            fsSub.setTableName(DeviceConst.TABLE_PRODUCT_PATROL_SETTING);
            fsSub.setValue("patrol_config_uuid", sub.getString("patrol_config_uuid"));
            fsSub.setValue("remark", sub.getString("remark"));
            fsSub.setValue("uuid", sub.getString("uuid"));
            dt.addFieldSetEntity(fsSub);
            fs.setValue("device_number", sub.getString("device_number"));
            fs.setValue("cycle", sub.getString("cycle"));
 
        }
        Map<String, DataTableEntity> sub = new HashMap<>();
        sub.put("subTable", dt);
        fs.setSubData(sub);
        return fs;
    }
    public FieldSetEntity findInspectionSetting() {
        DataTableEntity dataTableEntity = baseDao.listTable(DeviceConst.TABLE_PRODUCT_INSPECTION_SETTING, "");
        FieldSetEntity fs = new FieldSetEntity();
        fs.setTableName(DeviceConst.TABLE_PRODUCT_INSPECTION_SETTING);
 
        DataTableEntity dt = new DataTableEntity();
 
        for (int i = 0; i < dataTableEntity.getRows(); i++) {
            FieldSetEntity sub = dataTableEntity.getFieldSetEntity(i);
            FieldSetEntity fsSub = new FieldSetEntity();
            fsSub.setTableName(DeviceConst.TABLE_PRODUCT_INSPECTION_SETTING);
            fsSub.setValue("inspection_uuid", sub.getString("inspection_uuid"));
            fsSub.setValue("remark", sub.getString("remark"));
            fsSub.setValue("uuid", sub.getString("uuid"));
            fsSub.setValue("back_type", sub.getString("back_type"));
 
            dt.addFieldSetEntity(fsSub);
            fs.setValue("device_number", sub.getString("device_number"));
            fs.setValue("cycle", sub.getString("cycle"));
            fs.setValue("start_time", sub.getString("start_time"));
            fs.setValue("warn_time", sub.getString("warn_time"));
 
        }
        Map<String, DataTableEntity> sub = new HashMap<>();
        sub.put("subTable", dt);
        fs.setSubData(sub);
        return fs;
    }
    /**
     * 设备的点检配置
     * @param asset_code
     * @return
     */
    public FieldSetEntity findDeviceSetting(String device_uuid) {
        FieldSetEntity device=deviceManagerService.findDevice(device_uuid);
        String sql="select c.* from product_device_inspection_setting a join product_device_inspection_setting_sub b on a.uuid=b.main_uuid left join product_device_inspection_item_config c on b.config_uuid=c.uuid and c.is_used=1 where status=1 and a.device_uuid=? ";
        DataTableEntity dataTableEntity = baseDao.listTable(sql,new String[] {device_uuid});
        device.addSubDataTable(dataTableEntity);
        return device;
    }
    /**
     * 设备的点检记录
     * @param asset_code
     * @return
     */
    public FieldSetEntity findInspectionRecord(String uuid) {
        FieldSetEntity record=baseDao.getFieldSetEntity("product_device_inspection_record", 
                new String[]{"device_uuid","inspect_user","inspect_final_time","check_user","check_time","is_finish"},uuid, false);
        
        if(!FieldSetEntity.isEmpty(record)) {
            
            FieldSetEntity device=deviceManagerService.findDevice(record.getString("device_uuid"));
            record.setValue("device_uuid", device.getString("device_name"));
            record.setValue("device_uuid_save_value", device.getString("uuid"));
            
            String sql="select s.*,c.* from product_device_inspection_setting a left join product_device_inspection_item_config c on a.inspection_item=c.uuid and c.is_used=1 left join product_device_inspection_record_sub s on s.inspect_item=c.uuid and record_uuid=?  where a.device_uuid=? ";
            DataTableEntity dt = baseDao.listTable(sql,new String[] {uuid,record.getString("device_uuid_save_value")});
            record.addSubDataTable(dt);  //点检项
//            record.removeSubData("product_device_inspection_record_sub");
        }
        return record;
    }
    /**
     * 保存点检信息
     * @param fs
     * @return
     */
    @Transactional
    public String saveInspection(FieldSetEntity fs) throws BaseException{
        if(!FieldSetEntity.isEmpty(fs) ) {
            FieldSetEntity rfs=new FieldSetEntity(DeviceConst.PRODUCT_DEVICE_INSPECTION_RECORD);
            SystemUser user = SpringMVCContextHolder.getCurrentUser();
            rfs.setValue("org_level_uuid", user.getOrg_level_uuid());
//            rfs.setValue("device_uuid", fs.getValue("device_number"));
            rfs.setValue("device_uuid", fs.getValue("uuid"));
            rfs.setValue("is_finish", 0);
            rfs.setValue("use_dept", user.getOrg_level_uuid());
            rfs.setValue("inspect_user", user.getUser_id());
            rfs.setValue("inspect_final_time",new Date());
//            rfs.setValue("task_uuid", fieldValue);
            DataTableEntity dt=fs.getSubDataTable(DeviceConst.PRODUCT_DEVICE_INSPECTION_RECORD_SUB);
            if(!DataTableEntity.isEmpty(dt)) {
                rfs.addSubDataTable(dt);
                if(baseDao.saveFieldSetEntity(rfs)) {//创建任务
                    deviceTaskService.automaticCreateTask(
                            String.format(DeviceConst.TASK_TITLE_FORMAT_SPOT_CHECK, fs.getString(DeviceConst.DEVICE_NAME))
                            ,DeviceConst.TASK_TYPE_SPOT_CHECK
                            ,rfs.getUUID()
                            ,fs.getString(DeviceConst.DEVICE_MANAGER)
                            );
                    
                }
            }
            return  rfs.getUUID();
        }
        
        return null;
    }
    
    
    
}