Archive.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Archive extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'equipment_archive';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. // 追加属性
  19. protected $append = [
  20. 'purchasetime_text',
  21. 'status_text'
  22. ];
  23. public function getStatusList()
  24. {
  25. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  26. }
  27. public function getPurchasetimeTextAttr($value, $data)
  28. {
  29. $value = $value ? $value : (isset($data['purchasetime']) ? $data['purchasetime'] : '');
  30. return is_numeric($value) ? date("Y年m月d日", $value) : $value;
  31. }
  32. public function getStatusTextAttr($value, $data)
  33. {
  34. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  35. $list = $this->getStatusList();
  36. return isset($list[$value]) ? $list[$value] : '';
  37. }
  38. protected function setPurchasetimeAttr($value)
  39. {
  40. if(!is_numeric($value)) {
  41. $value = str_replace(['年', '月', '日'], ['-', '-', ''], $value);
  42. }
  43. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  44. }
  45. public function supplier()
  46. {
  47. return $this->belongsTo(Supplier::class, 'supplier_id', 'id', [], 'LEFT')->setEagerlyType(0);
  48. }
  49. public function responsibleUser()
  50. {
  51. return $this->belongsTo(\app\admin\model\User::class, 'responsible_uid', 'id', [], 'LEFT')->setEagerlyType(0);
  52. }
  53. public function loadSupplier()
  54. {
  55. return $this->belongsTo(Supplier::class, 'supplier_id');
  56. }
  57. public function loadResponsibleUser()
  58. {
  59. return $this->belongsTo(\app\admin\model\User::class, 'responsible_uid');
  60. }
  61. public function equipments()
  62. {
  63. return $this->hasMany(Equipment::class, 'archive_id', 'id');
  64. }
  65. public function refreshAmount($ids)
  66. {
  67. $archives = $this->whereIn('id', $ids)->with('equipments')->select();
  68. $update = [];
  69. foreach ($archives as $archive) {
  70. $update[] = [
  71. 'id' => $archive['id'],
  72. 'amount' => count($archive['equipments'])
  73. ];
  74. }
  75. $result = $this->isUpdate(true)->saveAll($update);
  76. if(!$result) return $this->getError();
  77. return true;
  78. }
  79. public function addArchive($data)
  80. {
  81. Db::startTrans();
  82. try {
  83. $result = $this->isUpdate(false)->data($data,true)->save();
  84. if (!$result) {
  85. throw new Exception($this->getError());
  86. }
  87. $equipmentModel = new Equipment();
  88. $equipmentResult = $equipmentModel->addEquipment($this->id, $data['amount']);
  89. if($equipmentResult != true) {
  90. throw new Exception($equipmentResult);
  91. }
  92. Db::commit();
  93. return true;
  94. } catch (\Exception $e) {
  95. Db::rollback();
  96. return $e->getMessage();
  97. }
  98. }
  99. public function editArchive($data, $ids)
  100. {
  101. $rawData = $this->get($ids);
  102. if($data['amount'] < $rawData['amount']) {
  103. return __("The number of devices must not be less than the current value");
  104. }
  105. Db::startTrans();
  106. try {
  107. $data['id'] = $ids;
  108. $result = $this->isUpdate(true)->save($data);
  109. if (!$result) {
  110. throw new Exception($this->getError());
  111. }
  112. if($data['amount'] > $rawData['amount']) {
  113. $diff = $data['amount'] - $rawData['amount'];
  114. $equipmentModel = new Equipment();
  115. $equipmentResult = $equipmentModel->addEquipment($ids, $diff);
  116. if($equipmentResult != true) {
  117. throw new Exception($equipmentResult);
  118. }
  119. }
  120. Db::commit();
  121. return true;
  122. } catch (\Exception $e) {
  123. Db::rollback();
  124. return $e->getMessage();
  125. }
  126. }
  127. }