WorkerLevelForm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\worker;
  8. use app\models\Worker;
  9. use app\models\WorkerLevel;
  10. class WorkerLevelForm extends Model
  11. {
  12. public $id;
  13. public $name; //名称
  14. public $pic_url; //图片
  15. public $status; //是否显示
  16. public $level;
  17. public $profit_rate;
  18. public $order_money;
  19. public $order_count;
  20. public $store_id;
  21. public function rules()
  22. {
  23. return [
  24. [['id', 'sort', 'status', 'order_count', 'store_id'], 'integer'],
  25. [['pic_url', 'name'], 'string'],
  26. [['profit_rate', 'order_money', 'level'], 'number']
  27. ];
  28. }
  29. /*
  30. * 服务人员等级列表
  31. */
  32. public function workerLevelList() {
  33. try {
  34. $name = $this->name;
  35. $status = $this->status;
  36. $store_id = $this->store_id;
  37. $query = WorkerLevel::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
  38. $level_query = clone $query;
  39. if (!is_null($status) && in_array($status, [0, 1])) {
  40. $query->andWhere(['status' => $status]);
  41. }
  42. if ($name) {
  43. $query->andWhere(['LIKE', 'name', $name]);
  44. }
  45. $query->select('id, name, pic_url, level, profit_rate, status, created_at, updated_at, profit_rate, order_money, order_count')
  46. ->orderBy('level ASC');
  47. $list = pagination_make($query);
  48. foreach ($list['list'] as &$item) {
  49. $item['status'] = (int)$item['status'];
  50. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  51. $item['updated_at'] = $item['updated_at'] > 0 ? date('Y-m-d H:i:s', $item['updated_at']) : '-';
  52. }
  53. $filter_level = $level_query->select('level')->column();
  54. $arr = range(1, 99);
  55. $level = array_values(array_diff($arr, $filter_level));
  56. return [
  57. 'code' => 0,
  58. 'msg' => 'success',
  59. 'data' => [
  60. 'data' => $list['list'],
  61. 'pageNo' => $list['pageNo'],
  62. 'totalCount' => $list['totalCount'],
  63. 'level' => $level
  64. ],
  65. ];
  66. } catch (\Exception $e) {
  67. return [
  68. 'code' => 1,
  69. 'msg' => $e->getMessage()
  70. ];
  71. }
  72. }
  73. /*
  74. * 服务等级设置
  75. */
  76. public function workerLevelSave() {
  77. $t = \Yii::$app->db->beginTransaction();
  78. try {
  79. $id = $this->id;
  80. $name = $this->name;
  81. $pic_url = $this->pic_url;
  82. $status = (int)$this->status ?? 1;
  83. $level = $this->level;
  84. $profit_rate = $this->profit_rate ?? '0.00';
  85. $order_money = $this->order_money ?? '0.00';
  86. $order_count = $this->order_count ?? 0;
  87. $store_id = $this->store_id;
  88. //名称或类型错误
  89. if (!$name || !in_array($status, [0, 1]) || !$level) {
  90. throw new \Exception('参数错误');
  91. }
  92. $work_level_ = WorkerLevel::findOne(['level' => $level, 'is_delete' => 0, 'store_id' => $store_id]);
  93. if ($work_level_ && intval($work_level_->id) !== intval($id)) {
  94. throw new \Exception('当前等级已存在');
  95. }
  96. $work_level = WorkerLevel::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id]);
  97. if (!$work_level) {
  98. $work_level = new WorkerLevel();
  99. $work_level->store_id = $store_id;
  100. $work_level->status = $status;
  101. }
  102. $work_level->level = $level;
  103. $work_level->name = $name;
  104. $work_level->pic_url = $pic_url;
  105. $work_level->profit_rate = $profit_rate;
  106. $work_level->order_money = $order_money;
  107. $work_level->order_count = $order_count;
  108. if (!$work_level->save()) {
  109. throw new \Exception(json_encode($work_level->errors));
  110. }
  111. $t->commit();
  112. return [
  113. 'code' => 0,
  114. 'msg' => '操作成功'
  115. ];
  116. } catch (\Exception $e) {
  117. $t->rollBack();
  118. return [
  119. 'code' => 1,
  120. 'msg' => $e->getMessage()
  121. ];
  122. }
  123. }
  124. /*
  125. * 服务人员等级状态修改
  126. */
  127. public function workerLevelStatus() {
  128. $t = \Yii::$app->db->beginTransaction();
  129. try {
  130. $id = $this->id;
  131. $store_id = $this->store_id;
  132. $status = (int)$this->status;
  133. $work_level = WorkerLevel::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id]);
  134. if (!$work_level) {
  135. throw new \Exception('数据不存在');
  136. }
  137. if (in_array($status, [2, 0])) {
  138. $worker = Worker::findOne(['level' => $work_level->level, 'status' => Worker::STATUS_VALID, 'store_id' => $store_id]);
  139. if ($worker) {
  140. throw new \Exception('该等级下有用户,不可操作');
  141. }
  142. }
  143. if ($status === 2) { //删除
  144. $work_level->is_delete = 1;
  145. } else { //修改状态
  146. if (!in_array($status, [0, 1])) {
  147. throw new \Exception('状态错误');
  148. }
  149. $work_level->status = $status;
  150. }
  151. if (!$work_level->save()) {
  152. throw new \Exception(json_encode($work_level->errors));
  153. }
  154. $t->commit();
  155. return [
  156. 'code' => 0,
  157. 'msg' => '操作成功'
  158. ];
  159. } catch (\Exception $e) {
  160. $t->rollBack();
  161. return [
  162. 'code' => 1,
  163. 'msg' => $e->getMessage()
  164. ];
  165. }
  166. }
  167. }