Equipment.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\admin\model\equipment;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. class Equipment extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'equipment_equipment';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. // 追加属性
  19. protected $append = [
  20. 'work_status_text',
  21. 'status_text'
  22. ];
  23. public function getWorkStatusList()
  24. {
  25. return ['normal' => __('Normal'), 'sickness' => __('Sickness'), 'repairing' => __('Repairing'), 'scrapped' => __('Scrapped')];
  26. }
  27. public function getWorkStatusTextAttr($value, $data)
  28. {
  29. $value = $value ? $value : (isset($data['work_status']) ? $data['work_status'] : '');
  30. $list = $this->getWorkStatusList();
  31. return isset($list[$value]) ? $list[$value] : '';
  32. }
  33. public function getStatusList()
  34. {
  35. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  36. }
  37. public function getStatusTextAttr($value, $data)
  38. {
  39. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  40. $list = $this->getStatusList();
  41. return isset($list[$value]) ? $list[$value] : '';
  42. }
  43. public function archive()
  44. {
  45. return $this->belongsTo(Archive::class, 'archive_id');
  46. }
  47. public function addEquipment($archiveId, $amount = 1)
  48. {
  49. $data = [];
  50. $codings = $this->createCoding($amount, 'E');
  51. $equipmentCodes = $this->generateCode($amount);
  52. foreach ($codings as $key => $coding) {
  53. $data[] = [
  54. 'archive_id' => $archiveId,
  55. 'coding' => $coding,
  56. 'equipment_code' => $equipmentCodes[$key],
  57. 'status' => 'normal'
  58. ];
  59. }
  60. $result = $this->saveAll($data);
  61. if (!$result) {
  62. return $this->getError();
  63. }
  64. return true;
  65. }
  66. public function createCoding($amount, $codingPredix = 'E')
  67. {
  68. $codings = [];
  69. for ($i = 0; $i < $amount; $i++) {
  70. $codings[] = $codingPredix . $this->_coding();
  71. }
  72. switch ($codingPredix) {
  73. case 'P':
  74. $model = new Plan();
  75. break;
  76. case 'T':
  77. $model = new PlanTask();
  78. break;
  79. default:
  80. $model = $this;
  81. break;
  82. }
  83. if ($model->where('coding', 'in', $codings)->column('id')) {
  84. return $this->createCoding($amount, $codingPredix);
  85. } else {
  86. return $codings;
  87. }
  88. }
  89. // 生成coding
  90. private function _coding()
  91. {
  92. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // 26个字符
  93. return substr(str_shuffle($str), 0, 7);
  94. }
  95. private function generateCode($amount)
  96. {
  97. $beginNum = $this->withTrashed()->whereTime('createtime', 'today')->count() + 1;
  98. $generateCodes = [];
  99. $codeDate = date('ymd');
  100. for ($i = 0; $i < $amount; $i++) {
  101. $generateCodes[] = 'E' . $codeDate . '-' . str_pad($beginNum, 3, "0", STR_PAD_LEFT);
  102. $beginNum++;
  103. }
  104. return $generateCodes;
  105. }
  106. public function delEquipment($ids)
  107. {
  108. Db::startTrans();
  109. try {
  110. // 删除设备
  111. $result = $this->destroy($ids);
  112. if (!$result) {
  113. throw new Exception($this->getError());
  114. }
  115. // 删除设备相关计划任务
  116. $planTaskModel = new PlanTask();
  117. $planTaskIds = $planTaskModel->whereIn('equipment_id', $ids)->column('id');
  118. if (count($planTaskIds) > 0) {
  119. $planTaskResult = $planTaskModel->destroy($planTaskIds);
  120. if (!$planTaskResult) {
  121. throw new Exception($planTaskModel->getError());
  122. }
  123. }
  124. // 删除设备相关溯源记录
  125. $recordModel = new Record();
  126. $recordIds = $recordModel->whereIn('equipment_id', $ids)->column('id');
  127. if (count($recordIds) > 0) {
  128. $recordResult = $recordModel->destroy($recordIds);
  129. if (!$recordResult) {
  130. throw new Exception($recordModel->getError());
  131. }
  132. }
  133. Db::commit();
  134. return true;
  135. } catch (Exception $exception) {
  136. Db::rollback();
  137. return $exception->getMessage();
  138. }
  139. }
  140. }