ClerkForm.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\Clerk;
  9. use app\models\Shop;
  10. use app\models\User;
  11. use yii\base\Model;
  12. class ClerkForm extends Model
  13. {
  14. public $id;
  15. public $store_id;
  16. public $user_id;
  17. public $shop_id;
  18. public $order_count;
  19. public $money_count;
  20. public $card_count;
  21. public $is_delete;
  22. //搜索
  23. public $search_key;
  24. public $search_is_show;
  25. const SCENARIO_ADD = 'add';
  26. const SCENARIO_EDIT = 'edit';
  27. const SCENARIO_DEL = 'del';
  28. const SCENARIO_LIST = 'list';
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['id', 'store_id', 'user_id', 'shop_id', 'order_count', 'card_count', 'is_delete'], 'integer'],
  36. [['money_count'], 'number'],
  37. ['is_delete', 'default', 'value' => Clerk::IS_DELETE_NO],
  38. [['order_count', 'card_count', 'money_count'], 'default', 'value' => 0],
  39. [['user_id', 'shop_id', 'order_count', 'card_count'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
  40. [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
  41. ['search_key', 'string', 'on' => self::SCENARIO_LIST]
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => '主键',
  51. 'store_id' => '商城id',
  52. 'user_id' => '用户id',
  53. 'shop_id' => '自提点id',
  54. 'order_count' => '核销订单数',
  55. 'money_count' => '核销总额',
  56. 'card_count' => '核销卡券数',
  57. 'is_delete' => '是否删除',
  58. ];
  59. }
  60. public function scenarios()
  61. {
  62. $scenarios = parent::scenarios();
  63. return $scenarios;
  64. }
  65. public function saveClerk()
  66. {
  67. if ($this->validate()) {
  68. $t = \Yii::$app->db->beginTransaction();
  69. if ($this->scenario == self::SCENARIO_EDIT) {
  70. $model = Clerk::findOne(['id' => $this->id]);
  71. } else {
  72. $model = Clerk::findOne(['user_id' => $this->user_id]);
  73. $shop = Shop::findOne(['user_id' => $this->user_id, 'is_delete' => Shop::IS_DELETE_NO]);
  74. if($model || $shop){
  75. return [
  76. 'code' => 1,
  77. 'msg' => '该用户已是核销员'
  78. ];
  79. }
  80. $model = new Clerk();
  81. }
  82. $model->attributes = $this->attributes;
  83. if (!$model->save()) {
  84. $t->rollBack();
  85. return [
  86. 'code' => 1,
  87. 'msg' => $model->getErrorSummary(false)[0]
  88. ];
  89. }
  90. // 判断社区是否绑定会员,如果没有绑定,将核销元绑定社区
  91. $shop = Shop::findOne($this->shop_id);
  92. if (!$shop->user_id) {
  93. $shop->user_id = $this->user_id;
  94. if (!$shop->save()) {
  95. $t->rollBack();
  96. return [
  97. 'code' => 1,
  98. 'msg' => '绑定出错'
  99. ];
  100. }
  101. }
  102. // 核銷園同步到user表
  103. if ($model->user_id > 0) {
  104. $user = User::findOne(['id' => $model->user_id]);
  105. if ($model->is_delete == $model::IS_DELETE_NO) {
  106. $user->is_clerk = 1;
  107. $user->shop_id = $model->id;
  108. } else {
  109. $user->is_clerk = 0;
  110. $user->shop_id = 0;
  111. }
  112. $user->save();
  113. }
  114. $t->commit();
  115. return [
  116. 'code' => 0,
  117. 'msg' => '保存成功'
  118. ];
  119. } else {
  120. // 验证失败:$errors 是一个包含错误信息的数组
  121. return [
  122. 'code' => 1,
  123. "msg" => $this->getErrorSummary(false)[0]
  124. ];
  125. }
  126. }
  127. public function searchClerk()
  128. {
  129. $query = Clerk::find()->alias('c')
  130. ->leftJoin(['u' => User::tableName()], 'u.id=c.user_id')
  131. ->leftJoin(['s' => Shop::tableName()], 's.id=c.shop_id')
  132. ->select('c.*, u.nickname user_name, u.avatar_url user_head_url, s.name shop_name');
  133. $query->where(['c.is_delete' => Clerk::IS_DELETE_NO])->orderBy("id desc");
  134. // 搜索
  135. if ($this->search_key) {
  136. $query->andWhere(['like', 'u.nickname', $this->search_key]);
  137. }
  138. $list = pagination_make($query);
  139. foreach ($list['list'] as &$val) {
  140. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  141. }
  142. $shop_list = ShopForm::getShopList();
  143. return [
  144. 'code' => 0,
  145. 'msg' => 'success',
  146. 'data' => [
  147. 'type_list' => $shop_list,
  148. 'data' => $list['list'],
  149. 'pageNo' => $list['pageNo'],
  150. 'totalCount' => $list['totalCount']
  151. ]
  152. ];
  153. }
  154. public function delClerk()
  155. {
  156. if ($this->validate()) {
  157. $t = \Yii::$app->db->beginTransaction();
  158. if ($this->scenario !== self::SCENARIO_DEL) {
  159. return [
  160. 'code' => 1,
  161. 'msg' => '删除失败'
  162. ];
  163. }
  164. $model = Clerk::findOne(['id' => $this->id]);
  165. $model->is_delete = Clerk::IS_DELETE_YES;
  166. // 核銷園同步到user表
  167. if ($model->user_id > 0) {
  168. $user = User::findOne(['id' => $model->user_id]);
  169. if ($user) {
  170. $user->is_clerk = 0;
  171. $user->shop_id = 0;
  172. $user->save();
  173. }
  174. }
  175. if (!$model || !$model->save()) {
  176. $t->rollBack();
  177. return [
  178. 'code' => 1,
  179. 'msg' => $model->getErrorSummary(false)[0]
  180. ];
  181. }
  182. $t->commit();
  183. return [
  184. 'code' => 0,
  185. 'msg' => '删除成功'
  186. ];
  187. // 所有输入数据都有效 all inputs are valid
  188. } else {
  189. // 验证失败:$errors 是一个包含错误信息的数组
  190. return [
  191. 'code' => 1,
  192. "msg" => $this->getErrorSummary(false)[0]
  193. ];
  194. }
  195. }
  196. public static function getClerkList()
  197. {
  198. return Clerk::find()->where(['is_delete' => Clerk::IS_DELETE_NO])->orderBy(['id desc'])->select('*')->asArray()->all();
  199. }
  200. }