WarehouseZoneForm.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\agent\front_erp;
  8. use app\models\Warehouse;
  9. use app\models\WarehouseZone;
  10. class WarehouseZoneForm extends Model
  11. {
  12. public $store_id;
  13. public $id;
  14. public $name;
  15. public $sort;
  16. public $is_delete;
  17. public $warehouse_id;
  18. public function rules()
  19. {
  20. return [
  21. [['id', 'store_id', 'sort', 'warehouse_id'], 'integer'],
  22. [['name'], 'string'],
  23. [['name'], 'safe'],
  24. ];
  25. }
  26. public function search ()
  27. {
  28. try {
  29. $query = WarehouseZone::find()->alias('wz')
  30. ->leftJoin(['w' => Warehouse::tableName()], 'w.id = wz.warehouse_id')
  31. ->where(['wz.is_delete' => 0, 'wz.store_id' => $this->store_id]);
  32. if (!empty($this->name)) {
  33. $query->andWhere(['like', 'wz.name', $this->name]);
  34. }
  35. if (!empty($this->warehouse_id)) {
  36. $query->andWhere(['wz.warehouse_id' => $this->warehouse_id]);
  37. }
  38. $query->orderBy('wz.sort DESC')->select('wz.*,w.name as warehouse_name');
  39. $pagination = pagination_make($query);
  40. return [
  41. 'code' => 0,
  42. 'msg' => 'success',
  43. 'data' => $pagination,
  44. ];
  45. } catch (\Exception $e) {
  46. \Yii::error($e);
  47. return [
  48. 'code' => 1,
  49. 'msg' => $e->getMessage()
  50. ];
  51. }
  52. }
  53. public function save ()
  54. {
  55. $t = \Yii::$app->db->beginTransaction();
  56. try {
  57. $model = WarehouseZone::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
  58. if (empty($model)) {
  59. $model = new WarehouseZone();
  60. $model->store_id = $this->store_id;
  61. }
  62. $model->name = $this->name;
  63. $model->warehouse_id = $this->warehouse_id;
  64. $model->sort = $this->sort;
  65. if (!$model->save()) {
  66. \Yii::error([__METHOD__, $model->attributes]);
  67. throw new \Exception(array_shift($model->getFirstErrors()));
  68. }
  69. $t->commit();
  70. return [
  71. 'code' => 0,
  72. 'msg' => '操作成功!'
  73. ];
  74. } catch (\Exception $e) {
  75. \Yii::error($e);
  76. $t->rollBack();
  77. return [
  78. 'code' => 1,
  79. 'msg' => $e->getMessage()
  80. ];
  81. }
  82. }
  83. public function del ()
  84. {
  85. try {
  86. if ($this->id) {
  87. WarehouseZone::updateAll(['is_delete' => 1], ['id' => $this->id]);
  88. }
  89. return [
  90. 'code' => 0,
  91. 'msg' => '操作成功!'
  92. ];
  93. } catch (\Exception $e) {
  94. \Yii::error($e);
  95. return [
  96. 'code' => 1,
  97. 'msg' => $e->getMessage()
  98. ];
  99. }
  100. }
  101. }