Blame view

application/admin/model/YonyouBipBaseData.php 2.91 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
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
  <?php
  
  namespace app\admin\model;
  
  use think\Model;
  
  /**
   * 用友BIP基础数据模型
   * 用于缓存从用友BIP获取的基础数据
   */
  class YonyouBipBaseData extends Model
  {
      // 表名
      protected $name = 'yonyou_bip_base_data';
      
      // 自动写入时间戳字段
      protected $autoWriteTimestamp = 'int';
  
      // 定义时间戳字段名
      protected $createTime = 'createtime';
      protected $updateTime = 'updatetime';
      protected $deleteTime = false;
  
      /**
       * 数据类型常量
       */
      const TYPE_ORGANIZATION = 'organization';      // 销售组织
      const TYPE_TRANSACTION_TYPE = 'transaction_type';  // 交易类型
      const TYPE_DEPARTMENT = 'department';        // 销售部门
      const TYPE_PERSON = 'person';                // 销售业务员
      const TYPE_CUSTOMER = 'customer';           // 客户
  
      /**
       * 根据类型和编码获取数据
       * @param string $type 数据类型
       * @param string $code 编码
       * @return array|null
       */
      public static function getByTypeAndCode($type, $code)
      {
          return self::where(['data_type' => $type, 'bip_code' => $code])->find();
      }
  
      /**
       * 根据类型和本地ID获取数据
       * @param string $type 数据类型
       * @param int $localId 本地ID
       * @return array|null
       */
      public static function getByTypeAndLocalId($type, $localId)
      {
          return self::where(['data_type' => $type, 'local_id' => $localId])->find();
      }
  
      /**
       * 获取指定类型的所有数据
       * @param string $type 数据类型
       * @return array
       */
      public static function getListByType($type)
      {
          return self::where(['data_type' => $type])->select();
      }
  
      /**
       * 保存或更新基础数据
       * @param string $type 数据类型
       * @param string $bipCode 用友BIP编码
       * @param string $bipName 用友BIP名称
       * @param int|null $localId 本地ID(可选)
       * @param array $extraData 额外数据
       * @return bool
       */
      public static function saveOrUpdate($type, $bipCode, $bipName, $localId = null, $extraData = [])
      {
          $data = self::where(['data_type' => $type, 'bip_code' => $bipCode])->find();
          
          $saveData = [
              'data_type' => $type,
              'bip_code' => $bipCode,
              'bip_name' => $bipName,
              'local_id' => $localId,
              'extra_data' => json_encode($extraData, JSON_UNESCAPED_UNICODE),
              'updatetime' => time(),
          ];
  
          if ($data) {
              // 更新
              return $data->save($saveData);
          } else {
              // 新增
              $saveData['createtime'] = time();
              return self::create($saveData);
          }
      }
  
      /**
       * 清除指定类型的数据
       * @param string $type 数据类型
       * @return bool
       */
      public static function clearByType($type)
      {
          return self::where(['data_type' => $type])->delete();
      }
  }