wangrong
2022-07-08 7e9badac4f3df3650e72bd419f9cdaa485b054d6
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
import { Provide } from '@midwayjs/decorator';
import { BaseService, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { DataService } from '../../base/service/dataapi/dataapi';
import { Devicegroup } from '../entity/devicegroup';
import { Basedeviceinfo } from '../entity/basedeviceinfo';
import { CoolSocket } from '@cool-midway/socket';
/**
        * 设备分组
        */
@Provide()
export class DevicegroupService extends DataService {
  @InjectEntityModel(Basedeviceinfo)
  Basedeviceinfo: Repository<Basedeviceinfo>;
  @InjectEntityModel(Devicegroup)
  Devicegroup: Repository<Devicegroup>;
 
 
 
 
 
  /**
     * 分页查询
     * @param query
     */
  async page(query) {
    const sql = `SELECT
   * 
  FROM
      t_base_device_group a
  WHERE
      1=1
      `
    return this.sqlRenderPage(sql, query);
  }
 
 
 
  async add(param: any) {
    let ids = param.logic_id
    let deviceinfo = this.Basedeviceinfo
    let data_len = deviceinfo.find()
    console.log((await data_len).length, "data_len")
    let id_str = ''
    if (ids.length == (await data_len).length) {
      throw new CoolCommException("是否更新周期任务")
      // console.log("是否更新周期任务")
      // return await super.add(param)
    } else {
      for (let x = 0; x < ids.length; x++) {
        let change_message = await deviceinfo.findOne({ logic_id: ids[x] })
        if (change_message.status == 1) {
          id_str += `${ids[x]},`
        }
      }
      if (id_str == '') {
        if (id_str == '') {
          for (let x = 0; x < ids.length; x++) {
            let change_message = await deviceinfo.findOne({ logic_id: ids[x] })
            change_message.status = 1
            await this.nativeQuery(
              'update t_base_deviceinfo set status = 1 where logic_id = ?',
              [ids[x]]
            );
          }
        }
        param.logic_id = param.logic_id.toString();
        return await super.add(param)
      } else {
        throw new CoolCommException(`${id_str}设备已经在其他分组中存在,请重新选择`)
      }
    }
  }
 
 
 
  async update(param: any): Promise<void> {
    console.log(param, "param")
    param.logic_id = param.logic_id.toString();
    await this.Devicegroup.update(param.id, param)
  }
 
 
 
 
 
 
  async delete(ids: number[]) {
    console.log(ids, "ids")
    for (let i = 0; i < ids.length; i++) {
      let change_message = this.Devicegroup.findOne({ id: ids[i] })
      let logic_id_s = (await change_message).logic_id.split(",")
      console.log(logic_id_s, "logic_ids")
      await this.nativeQuery(
        `update t_base_deviceinfo set status = 0 where logic_id in (?)`,
        [logic_id_s]
      )
      await this.nativeQuery(
        `delete from t_base_device_group where id = ?`,
        [ids[i]]
      )
    }
 
 
 
  }
 
 
 
}