AccountLogForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\admin;
  8. use app\models\AccountLog;
  9. use app\models\ReOrder;
  10. use app\models\User;
  11. use yii\base\Model;
  12. use Yii;
  13. class AccountLogForm extends Model
  14. {
  15. public $admin;
  16. public $store_id;
  17. public $money;
  18. public $integral;
  19. public $explain;
  20. public $type;
  21. public $recharge_type;
  22. public $user_id;
  23. public $is_we7;
  24. public $is_ind;
  25. public $pic_url;
  26. public $dateStart;
  27. public $dateEnd;
  28. public $name;
  29. /**
  30. * @return array the validation rules.
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['explain', 'name'], 'trim'],
  36. [['explain', 'name', 'dateStart', 'dateEnd'], 'string'],
  37. [['type', 'recharge_type', 'user_id'], 'integer'],
  38. [['money', 'integral'], 'number'],
  39. ];
  40. }
  41. /**
  42. * 充值积分或者余额
  43. * @return array
  44. * @throws \yii\base\Exception
  45. */
  46. public function recharge()
  47. {
  48. if (!in_array($this->type, AccountLog::$valid_type)
  49. || !in_array($this->recharge_type, AccountLog::$type_budget)
  50. ) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => '参数错误'
  54. ];
  55. }
  56. $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
  57. if (!$user) {
  58. return [
  59. 'code' => 1,
  60. 'msg' => '用户不存在,或已删除',
  61. ];
  62. }
  63. if ($this->validate()) {
  64. // 积分
  65. if ($this->type == AccountLog::TYPE_INTEGRAL) {
  66. if (empty($this->integral)) {
  67. return [
  68. 'code' => 1,
  69. 'msg' => '积分设置不正确'
  70. ];
  71. }
  72. $accountLog = new AccountLog();
  73. $accountLog->store_id = get_store_id();
  74. $accountLog->user_id = $user->id;
  75. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  76. if ($this->integral > $user->integral) {
  77. return [
  78. 'code' => 1,
  79. 'msg' => '用户当前积分不足',
  80. ];
  81. }
  82. $user->integral -= $this->integral;
  83. } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
  84. $user->integral += $this->integral;
  85. $user->total_integral += $this->integral;
  86. }
  87. if (!$user->save()) {
  88. foreach ($user->errors as $error) {
  89. return [
  90. 'code' => 1,
  91. 'msg' => $error
  92. ];
  93. }
  94. }
  95. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  96. $accountLog->desc = "管理员: " . $this->admin->nickname . " 后台操作账号:" . $user->nickname
  97. . " 积分扣除:" . $this->integral . " 积分";
  98. } elseif ($this->recharge_type == AccountLog::LOG_TYPE_INCOME) {
  99. $accountLog->desc = "管理员: " . $this->admin->nickname . " 后台操作账号:" . $user->nickname
  100. . " 积分充值:" . $this->integral . " 积分";
  101. }
  102. $accountLog->amount = $this->integral;
  103. $accountLog->created_at = time();
  104. $accountLog->operator = $this->admin->nickname ? $this->admin->nickname : '未知';
  105. $accountLog->store_id = $this->store_id;
  106. $accountLog->operator_id = $this->admin->id;
  107. $accountLog->order_type = AccountLog::TYPE_RECHARGE_ORDER;
  108. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  109. $accountLog->before = $user->integral + $this->integral;
  110. $accountLog->after = $user->integral;
  111. } else {
  112. $accountLog->before = $user->integral - $this->integral;
  113. $accountLog->after = $user->integral;
  114. }
  115. $accountLog->log_type = $this->recharge_type;
  116. $accountLog->type = AccountLog::TYPE_INTEGRAL;
  117. $accountLog->operator_type = AccountLog::TYPE_OPERATOR_BACK;
  118. $accountLog->explain = $this->explain;
  119. if ($accountLog->save()) {
  120. return [
  121. 'code' => 0,
  122. 'msg' => '操作成功',
  123. ];
  124. } else {
  125. return [
  126. 'code' => 1,
  127. 'msg' => '操作失败',
  128. ];
  129. }
  130. }
  131. // 余额
  132. if ($this->type == AccountLog::TYPE_BALANCE) {
  133. if (!$this->validate()) {
  134. return [
  135. 'code' => 1,
  136. 'msg' => '参数有误'
  137. ];
  138. }
  139. $this->money = floatval($this->money);
  140. if ($this->money < 0) {
  141. return [
  142. 'code' => 1,
  143. 'msg' => '输入数值不能小于0'
  144. ];
  145. }
  146. $accountLog = new AccountLog();
  147. $accountLog->store_id = $this->store_id;
  148. $accountLog->user_id = $this->user_id;
  149. $accountLog->pic_url = $this->pic_url;
  150. $accountLog->explain = $this->explain;
  151. switch ($this->recharge_type) {
  152. case AccountLog::LOG_TYPE_INCOME:
  153. $user->money += $this->money;
  154. $accountLog->desc = "管理员: " . $this->admin->nickname . " 后台操作账号:"
  155. . $user->nickname . " 余额充值:" . $this->money . " 元";
  156. $accountLog->log_type = AccountLog::LOG_TYPE_INCOME;
  157. break;
  158. case AccountLog::LOG_TYPE_EXPEND:
  159. if ($user->money < $this->money) {
  160. return [
  161. 'code' => 1,
  162. 'msg' => '扣除数值大于当前用户余额'
  163. ];
  164. }
  165. $user->money -= $this->money;
  166. $accountLog->desc = "管理员: " . $this->admin->nickname . " 后台操作账号:" . $user->nickname
  167. . " 余额扣除:" . $this->money . " 元";
  168. $accountLog->log_type = AccountLog::LOG_TYPE_EXPEND;
  169. break;
  170. default:
  171. return [
  172. 'code' => 1,
  173. 'msg' => '网络异常,请刷新重试'
  174. ];
  175. }
  176. if ($user->save()) {
  177. $accountLog->amount = $this->money;
  178. $accountLog->operator = $this->admin->nickname;
  179. $accountLog->operator_id = $this->admin->id;
  180. $accountLog->created_at = time();
  181. $accountLog->type = AccountLog::TYPE_BALANCE;
  182. $accountLog->order_type = AccountLog::TYPE_RECHARGE_ORDER;
  183. if ($this->recharge_type == AccountLog::LOG_TYPE_EXPEND) {
  184. $accountLog->before = $user->money + $this->money;
  185. $accountLog->after = $user->money;
  186. } else {
  187. $accountLog->before = $user->money - $this->money;
  188. $accountLog->after = $user->money;
  189. }
  190. $accountLog->log_type = $this->recharge_type;
  191. $accountLog->operator_type = AccountLog::TYPE_OPERATOR_BACK;
  192. if (!$accountLog->save()) {
  193. Yii::error($accountLog->errors);
  194. return [
  195. 'code' => 1,
  196. 'msg' => '记录日志错误'
  197. ];
  198. } else {
  199. return [
  200. 'code' => 0,
  201. 'msg' => '操作成功'
  202. ];
  203. }
  204. } else {
  205. return [
  206. 'code' => 1,
  207. 'msg' => $this->getErrorSummary(false)[0],
  208. ];
  209. }
  210. }
  211. }
  212. return [
  213. 'code' => 1,
  214. 'msg' => $this->getErrorSummary(false)[0],
  215. ];
  216. }
  217. /**
  218. * 充值记录展示
  219. */
  220. public function getRechargeList() {
  221. if ($this->type == AccountLog::TYPE_INTEGRAL) {
  222. return $this->getIntegralRechargeList();
  223. }
  224. $query = AccountLog::find()->alias('al')->where(['al.type' => $this->type, 'al.store_id' => $this->store_id,
  225. ])->leftJoin(['u' => User::tableName()], 'u.id=al.user_id');
  226. if ($this->name) {
  227. $query->andWhere(['like', 'u.nickname', $this->name]);
  228. }
  229. if ($this->dateStart) {
  230. $query->andWhere(['>=', 'al.created_at', strtotime($this->dateStart)]);
  231. }
  232. if ($this->dateEnd) {
  233. $query->andWhere(['<=', 'al.created_at',strtotime($this->dateEnd)]);
  234. }
  235. $query->orderBy('al.created_at desc')->select('al.*, u.nickname, u.avatar_url');
  236. $pagination = pagination_make($query);
  237. $list = $pagination['list'];
  238. foreach ($list as $key => $value) {
  239. $list[$key]['send_price'] = 0.00;
  240. if ($value['order_type'] == AccountLog::TYPE_RECHARGE_ORDER
  241. && $value['operator_type'] == AccountLog::TYPE_OPERATOR_NORMAL) {
  242. $reOrder = ReOrder::find()->where(['id' => $value['order_id'],'is_pay' => ReOrder::IS_PAY,
  243. 'is_delete' => ReOrder::NOT_DELETE])->asArray()->One();
  244. $list[$key]['send_price'] = $reOrder['send_price'];
  245. }
  246. }
  247. return [
  248. 'code' => 0,
  249. 'msg' => 'success',
  250. 'data' => [
  251. 'data' => $list,
  252. 'pageNo' => $pagination['pageNo'],
  253. 'totalCount' => $pagination['totalCount'],
  254. ],
  255. ];
  256. }
  257. /**
  258. * 充值记录展示
  259. */
  260. public function getIntegralRechargeList() {
  261. $query = AccountLog::find()->alias('al')->where(['al.type' => $this->type, 'al.store_id' => $this->store_id]
  262. )->leftJoin(['u' => User::tableName()], 'u.id=al.user_id')
  263. ->select('al.*, u.nickname, u.avatar_url');
  264. if ($this->name) {
  265. $query->andWhere(['like', 'u.nickname', $this->name]);
  266. }
  267. if ($this->dateStart) {
  268. $query->andWhere(['>=', 'al.created_at', strtotime($this->dateStart)]);
  269. }
  270. if ($this->dateEnd) {
  271. $query->andWhere(['<=', 'al.created_at',strtotime($this->dateEnd)]);
  272. }
  273. $query->orderBy('al.created_at desc');
  274. $pagination = pagination_make($query);
  275. $list = $pagination['list'];
  276. return [
  277. 'code' => 0,
  278. 'msg' => 'success',
  279. 'data' => [
  280. 'data' => $list,
  281. 'pageNo' => $pagination['pageNo'],
  282. 'totalCount' => $pagination['totalCount'],
  283. ],
  284. ];
  285. }
  286. }