AccountLog.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. use app\modules\admin\models\wechat_mp\CardForm;
  11. use app\utils\IotCloudHelper;
  12. /**
  13. * This is the model class for table "{{%account_log}}".
  14. *
  15. * @property integer $id
  16. * @property integer $store_id
  17. * @property integer $order_type
  18. * @property integer $order_id
  19. * @property integer $user_id
  20. * @property integer $amount
  21. * @property string $desc
  22. * @property integer $before
  23. * @property integer $after
  24. * @property string $operator
  25. * @property integer $operator_id
  26. * @property integer $log_type
  27. * @property integer $type
  28. * @property integer $operator_type
  29. * @property string $pic_url
  30. * @property string $explain
  31. * @property integer $created_at
  32. * @property integer $from
  33. * @property integer $from_user_id
  34. * @property integer $profit
  35. * @property integer $saas_id
  36. */
  37. class AccountLog extends \yii\db\ActiveRecord
  38. {
  39. /**
  40. * 收入类型
  41. */
  42. CONST LOG_TYPE_INCOME = 1;
  43. /**
  44. * 支出类型
  45. */
  46. CONST LOG_TYPE_EXPEND = 2;
  47. public static $type_budget = [
  48. self::LOG_TYPE_INCOME,
  49. self::LOG_TYPE_EXPEND
  50. ];
  51. /**
  52. * 充值订单
  53. */
  54. CONST TYPE_RECHARGE_ORDER = 0;
  55. /**
  56. * 商城订单
  57. */
  58. CONST TYPE_PLATFORM_ORDER = 1;
  59. /**
  60. * 秒杀订单
  61. */
  62. CONST TYPE_MS_ORDER = 2;
  63. /**
  64. * 拼团订单
  65. */
  66. CONST TYPE_PT_ORDER = 3;
  67. /**
  68. * 商城订单退款
  69. */
  70. CONST TYPE_PLATFORM_REFUND_ORDER = 4;
  71. /**
  72. * 充值订单退款(减余额)
  73. */
  74. CONST TYPE_RECHARGE_REFUND_ORDER = 41;
  75. /**
  76. * 商城秒杀退款
  77. */
  78. CONST TYPE_MS_REFUND_ORDER = 5;
  79. /**
  80. * 商城拼团退款
  81. */
  82. CONST TYPE_PT_REFUND_ORDER = 6;
  83. /**
  84. * 商城预约退款
  85. */
  86. CONST TYPE_YY_REFUND_ORDER = 7;
  87. /**
  88. * 预约订单
  89. */
  90. CONST TYPE_YY_ORDER = 8;
  91. /**
  92. * 预约订单
  93. */
  94. CONST TYPE_SCRATCH_ORDER = 9;
  95. /**
  96. * 大转盘
  97. */
  98. CONST TYPE_POND_ORDER = 10;
  99. /**
  100. * 供货商订单
  101. */
  102. CONST TYPE_SUPPLIER_ORDER = 11;
  103. /**
  104. * 会员购买订单
  105. */
  106. CONST TYPE_LEVEL_ORDER = 12;
  107. /**
  108. * 当面付
  109. */
  110. CONST TYPE_SCAN_CODE_PAY = 13;
  111. /**
  112. * saas积分商品订单
  113. */
  114. CONST TYPE_SAAS_INTEGRAL_ORDER = 14;
  115. /**
  116. * saas积分商品订单
  117. */
  118. CONST TYPE_BALANCE_TO_COMMISSION = 15;
  119. /**
  120. * 签到送积分
  121. */
  122. CONST TYPE_REGISTER_INTEGRAL = 16;
  123. /**
  124. * 问答专区提问
  125. */
  126. CONST TYPE_QUESTION_INTEGRAL = 17;
  127. /**
  128. * 问答专区看视频广告
  129. */
  130. const TYPE_QUESTION_AD = 18;
  131. /**
  132. * 充值上级佣金
  133. */
  134. const TYPE_RECHARGE_COMMISSION = 20;
  135. /**
  136. * 充值分销佣金
  137. */
  138. const TYPE_DISTRIBUTION_COMMISSION = 21;
  139. /**
  140. * p大转盘抽奖
  141. */
  142. const TYPE_POND_COMMISSION = 23;
  143. /**
  144. * 采购金
  145. */
  146. const TYPE_CLOUD_INVENTORY_PURCHASE_MONEY = 25;
  147. const TYPE_PURCHASE_CLOUD_INVENTORY_PURCHASE_MONEY = 26;
  148. /**
  149. * 贡献积分
  150. */
  151. const TYPE_COIN = 3;
  152. /**
  153. * 余额
  154. */
  155. const TYPE_BALANCE = 2;
  156. /**
  157. * 积分
  158. */
  159. const TYPE_INTEGRAL = 1;
  160. /**
  161. * 商品余额抵扣
  162. */
  163. const TYPE_GOODS_BALANCE = 19;
  164. /**
  165. * 微信
  166. */
  167. CONST TYPE_WECHAT = 3;
  168. /**
  169. * 支付宝
  170. */
  171. CONST TYPE_ALIPAY = 4;
  172. /**
  173. * 采购金
  174. */
  175. CONST TYPE_PURCHASE_MONEY = 5;
  176. public static $valid_type = [
  177. self::TYPE_BALANCE,
  178. self::TYPE_INTEGRAL,
  179. self::TYPE_WECHAT,
  180. self::TYPE_ALIPAY,
  181. self::TYPE_PURCHASE_MONEY
  182. ];
  183. /**
  184. * 后台改动
  185. */
  186. CONST TYPE_OPERATOR_BACK = 2;
  187. /**
  188. * 前台变化
  189. */
  190. CONST TYPE_OPERATOR_NORMAL= 1;
  191. /**
  192. * AI变化
  193. */
  194. CONST TYPE_OPERATOR_AI= 3;
  195. /**
  196. * @var int[]
  197. */
  198. CONST FROM_TYPE_ORIGINAL = 0; //原始
  199. CONST FROM_TYPE_GIFT = 1; //赠送
  200. CONST FROM_TYPE_GIFTED_GIFT = 1; //被赠送
  201. CONST FROM_TYPE_COMMISSION_TO_BALANCE = 2; //佣金转余额
  202. CONST FROM_TYPE_RECHARGE = 3; //充值
  203. CONST FROM_TYPE_TRANSFER_PURCHASE_MONEY = 4; //云库存结算余额转采购金
  204. public static $type_order = [
  205. self::TYPE_RECHARGE_ORDER,
  206. self::TYPE_PLATFORM_ORDER,
  207. self::TYPE_MS_ORDER,
  208. self::TYPE_PT_ORDER,
  209. self::TYPE_PLATFORM_REFUND_ORDER,
  210. self::TYPE_MS_REFUND_ORDER,
  211. self::TYPE_PT_REFUND_ORDER
  212. ];
  213. /**
  214. * @inheritdoc
  215. */
  216. public static function tableName()
  217. {
  218. return '{{%account_log}}';
  219. }
  220. /**
  221. * @inheritdoc
  222. */
  223. public function rules()
  224. {
  225. return [
  226. [['user_id', 'type', 'amount', 'desc', 'order_type', 'before', 'after',
  227. 'operator_type', 'log_type', 'type'], 'required'],
  228. [['user_id', 'type', 'created_at', 'order_type', 'order_id', 'operator_type', 'log_type', 'operator_id', 'from', 'from_user_id', 'saas_id'
  229. ], 'integer'],
  230. [['amount', 'before', 'after', 'profit'], 'number'],
  231. [['desc', 'pic_url', 'explain', 'operator'], 'string'],
  232. ];
  233. }
  234. /**
  235. * @inheritdoc
  236. */
  237. public function attributeLabels()
  238. {
  239. return [
  240. 'id' => 'ID',
  241. 'user_id' => 'User ID',
  242. 'log_type' => '类型:1=收入,2=支出',
  243. 'type' => '类型:2=余额,1=积分',
  244. 'amount' => '变动数',
  245. 'desc' => '变动说明',
  246. 'before' => '变动前',
  247. 'after' => '变动后',
  248. 'operator' => '操作者',
  249. 'operator_id' => '操作者id',
  250. 'operator_type' => '1:前台,2:后台',
  251. 'pic_url' => '图片',
  252. 'explain' => '说明',
  253. 'created_at' => '添加时间',
  254. 'order_type' => '订单类型 0--充值 1--商城订单 2--秒杀订单 3--拼团订单 4--商城订单退款 5--秒杀订单退款 6--拼团订单退款 7--后台改动,15、团购券, 23、大转盘抽奖',
  255. 'order_id' => '订单ID',
  256. 'from' => '是否为转增',
  257. 'saas_id' => '联盟用户'
  258. ];
  259. }
  260. /**
  261. * 仅适用客户端使用
  262. * @param $user_id
  263. * @param $amount
  264. * @param $type
  265. * @param $log_type
  266. * @param int $order_type
  267. * @param int $order_id
  268. * @param string $desc
  269. * @return bool
  270. */
  271. public static function saveLog($user_id, $amount, $type, $log_type, $order_type = 0, $order_id = 0, $desc = "", $from = 0, $from_user_id = 0, $profit = 0)
  272. {
  273. $form = new self();
  274. $user_info = User::findOne($user_id);
  275. $form->store_id = $user_info->store_id;
  276. $form->user_id = $user_id;
  277. $form->amount = $amount;
  278. $form->type = $type;
  279. $form->before = $type == AccountLog::TYPE_INTEGRAL ? $user_info->integral : $user_info->money;
  280. if ($log_type == AccountLog::LOG_TYPE_INCOME) {
  281. if ($type == AccountLog::TYPE_INTEGRAL) {
  282. $form->after = $user_info->integral + $amount;
  283. } else {
  284. $form->after = $user_info->money + $amount;
  285. }
  286. } else {
  287. if ($type == AccountLog::TYPE_INTEGRAL) {
  288. $form->after = $user_info->integral - $amount;
  289. } else {
  290. $form->after = $user_info->money - $amount;
  291. }
  292. }
  293. $form->desc = $desc;
  294. $form->order_type = $order_type;
  295. $form->order_id = $order_id;
  296. $form->operator = '';
  297. $form->operator_id = 0;
  298. $form->log_type = $log_type;
  299. $form->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  300. $form->created_at = time();
  301. if ($from) {
  302. $form->from = $from;
  303. }
  304. if ($from_user_id) {
  305. $form->from_user_id = $from_user_id;
  306. }
  307. if ($profit) {
  308. $form->profit = $profit;
  309. }
  310. if ($form->save()) {
  311. if ($type == AccountLog::TYPE_INTEGRAL) {
  312. if ($log_type == AccountLog::LOG_TYPE_INCOME) {
  313. $user_info->integral += $amount;
  314. } else {
  315. $user_info->integral -= $amount;
  316. }
  317. } else {
  318. if ($log_type == AccountLog::LOG_TYPE_INCOME) {
  319. $user_info->money += $amount;
  320. } else {
  321. $user_info->money -= $amount;
  322. }
  323. }
  324. $save = $user_info->save();
  325. if(!$save){
  326. \Yii::error([__METHOD__, $user_info->getErrors()]);
  327. }
  328. return $save;
  329. }
  330. }
  331. public function afterSave($insert, $changedAttributes)
  332. {
  333. parent::afterSave($insert, $changedAttributes);
  334. if ($insert) {
  335. self::wxCardSync($this, $changedAttributes);
  336. self::voiceSendMessage($this);
  337. }
  338. }
  339. public static function wxCardSync($form, $ca = [])
  340. {
  341. \Yii::error([__METHOD__, $form, $ca]);
  342. if ($form->type != AccountLog::TYPE_INTEGRAL) {
  343. return true;
  344. }
  345. $cf = new CardForm();
  346. $bind = $cf->getBindByUserId($form->user_id, $form->store_id);
  347. if (!$bind) {
  348. return true;
  349. }
  350. $amount = $form->amount;
  351. if ($form->log_type != AccountLog::LOG_TYPE_INCOME) {
  352. $amount = -$amount;
  353. }
  354. $cf->updateUser($bind, [
  355. "record_bonus" => $form->desc,
  356. "bonus" => $form->after,
  357. "add_bonus" => $amount,
  358. ]);
  359. return true;
  360. }
  361. // 余额支付语音播报
  362. public static function voiceSendMessage($form)
  363. {
  364. if (
  365. $form->log_type == self::LOG_TYPE_EXPEND &&
  366. $form->type == self::TYPE_BALANCE &&
  367. $form->operator_type == self::TYPE_OPERATOR_NORMAL &&
  368. $form->order_id > 0
  369. ) {
  370. $store = Store::findOne($form->store_id);
  371. if ($store && !empty($store->device_name)) {
  372. $total_fee = $form->amount;
  373. IotCloudHelper::sendMessage($form->store_id, '{"cmd":"voice","msg":"会员卡消费收款'.$total_fee.'元","msgid":"'.$form->created_at . $form->order_id.'"}');
  374. }
  375. }
  376. }
  377. }