DeliveryRulesForm.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\DeliveryRules;
  9. use yii\base\Model;
  10. class DeliveryRulesForm extends Model
  11. {
  12. public $id;
  13. public $ids;
  14. public $name;
  15. public $type;
  16. public $times;
  17. public $status;
  18. public $days;
  19. public function rules()
  20. {
  21. return [
  22. [['status', 'type', 'id', 'days'], 'integer'],
  23. [['times', 'ids', 'name'], 'string']
  24. ];
  25. }
  26. public function search ()
  27. {
  28. try {
  29. $query = DeliveryRules::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]);
  30. if ($this->status !== null && $this->status !== '' && in_array($this->status, [0, 1])) {
  31. $query->andWhere(['status' => $this->status]);
  32. }
  33. $pagination = pagination_make($query);
  34. foreach ($pagination['list'] as &$item) {
  35. if ((int)$item['type'] === 0) {
  36. $item['times'] = date("Y-m-d H:i:s", $item['times']);
  37. }
  38. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  39. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  40. }
  41. return [
  42. 'code' => 0,
  43. 'msg' => 'success',
  44. 'data' => [
  45. 'data' => $pagination['list'],
  46. 'pageNo' => $pagination['pageNo'],
  47. 'totalCount' => $pagination['totalCount'],
  48. ]
  49. ];
  50. } catch (\Exception $e) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => $e->getMessage()
  54. ];
  55. }
  56. }
  57. public function save ()
  58. {
  59. try {
  60. if (!$this->name || (!$this->times && !$this->days)) {
  61. throw new \Exception("请将参数填充完整");
  62. }
  63. $rules = DeliveryRules::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  64. if (empty($rules)) {
  65. $rules = new DeliveryRules();
  66. $rules->store_id = get_store_id();
  67. }
  68. $rules->name = $this->name;
  69. $rules->type = (int)$this->type;
  70. // $rules->times = (int)$this->times;
  71. if ((int)$this->type === 0) {
  72. $rules->times = strtotime($this->times);
  73. } else {
  74. $rules->days = (int)$this->days;
  75. }
  76. if (!$rules->save()) {
  77. throw new \Exception(json_encode($rules->errors));
  78. }
  79. return [
  80. 'code' => 0,
  81. 'msg' => '操作成功!'
  82. ];
  83. } catch (\Exception $e) {
  84. return [
  85. 'code' => 1,
  86. 'msg' => $e->getMessage()
  87. ];
  88. }
  89. }
  90. public function setStatus ()
  91. {
  92. try {
  93. if ($this->ids) {
  94. $ids = explode(',', $this->ids);
  95. if (in_array($this->status, [0, 1])) {
  96. DeliveryRules::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  97. }
  98. if ((int)$this->status === 2) {
  99. DeliveryRules::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  100. }
  101. } else {
  102. $rules = DeliveryRules::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  103. if (empty($rules)) {
  104. throw new \Exception("规则不存在");
  105. }
  106. if (in_array($this->status, [0, 1])) {
  107. $rules->status = $this->status;
  108. }
  109. if ((int)$this->status === 2) {
  110. $rules->is_delete = 1;
  111. }
  112. if (!$rules->save()) {
  113. throw new \Exception(json_encode($rules->errors));
  114. }
  115. }
  116. return [
  117. 'code' => 0,
  118. 'msg' => '操作成功!'
  119. ];
  120. } catch (\Exception $e) {
  121. return [
  122. 'code' => 1,
  123. 'msg' => $e->getMessage()
  124. ];
  125. }
  126. }
  127. public function getSelectList()
  128. {
  129. try {
  130. $list = DeliveryRules::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])->asArray()->all();
  131. foreach ($list as &$item) {
  132. if ((int)$item['type'] === 0) {
  133. $item['times'] = date("Y-m-d H:i:s", $item['times']);
  134. }
  135. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  136. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  137. }
  138. return [
  139. 'code' => 0,
  140. 'msg' => 'success',
  141. 'data' => [
  142. 'data' => $list
  143. ]
  144. ];
  145. } catch (\Exception $e){
  146. return [
  147. 'code' => 1,
  148. 'msg' => $e->getMessage()
  149. ];
  150. }
  151. }
  152. }