BlindBoxBonusLogDetail.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace addons\BlindBox\common\models;
  3. use addons\BlindBox\common\enums\BlindBoxEnum;
  4. use common\enums\AddonsEnum;
  5. use common\enums\OrderStatusEnum;
  6. use common\enums\StatusEnum;
  7. use common\enums\UserWalletEnum;
  8. use common\models\base\BaseActiveRecord;
  9. use common\models\order\Order;
  10. use common\models\user\User;
  11. use services\common\UserWalletService;
  12. use Yii;
  13. /**
  14. * This is the model class for table "qimall_addons_blind_box_bonus_log_detail".
  15. *
  16. * @property int $id
  17. * @property int $mall_id 商城id
  18. * @property int $bonus_log_id 分红记录id
  19. * @property int $user_id 会员id
  20. * @property string $order_no 订单编号
  21. * @property float $money 订单贡献金额
  22. * @property float $total_contribution 总贡献值
  23. * @property float $contribution 订单贡献值
  24. * @property float $total_bonus 分红总额
  25. * @property float $order_bonus 订单分红
  26. * @property int $is_settlement 是否结算:0:否:1:是
  27. * @property int $is_feedback 是否退款:0:否:1:是
  28. * @property int $settlement_time 结算时间
  29. * @property int $status 状态[-1:删除;0:禁用;1启用]
  30. * @property int $created_at
  31. * @property int $updated_at
  32. */
  33. class BlindBoxBonusLogDetail extends BaseActiveRecord
  34. {
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return '{{%addons_blind_box_bonus_log_detail}}';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['mall_id', 'bonus_log_id', 'user_id', 'is_settlement', 'is_feedback', 'settlement_time', 'status', 'created_at', 'updated_at'], 'integer'],
  49. [['money', 'total_contribution', 'contribution', 'total_bonus', 'order_bonus'], 'number'],
  50. [['order_no'], 'string', 'max' => 255],
  51. ];
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'id' => 'ID',
  60. 'mall_id' => '商城id',
  61. 'bonus_log_id' => '分红记录id',
  62. 'user_id' => '会员id',
  63. 'order_no' => '订单编号',
  64. 'money' => '订单贡献金额',
  65. 'total_contribution' => '总贡献值',
  66. 'contribution' => '订单贡献值',
  67. 'total_bonus' => '分红总额',
  68. 'order_bonus' => '订单分红',
  69. 'is_settlement' => '是否结算:0:否:1:是',
  70. 'is_feedback' => '是否退款:0:否:1:是',
  71. 'settlement_time' => '结算时间',
  72. 'status' => '状态[-1:删除;0:禁用;1启用]',
  73. 'created_at' => 'Created At',
  74. 'updated_at' => 'Updated At',
  75. ];
  76. }
  77. public static function addData(array $params)
  78. {
  79. try {
  80. $model = self::findOne([
  81. 'mall_id' => $params['mall_id'],
  82. 'user_id' => $params['user_id'],
  83. 'bonus_log_id' => $params['bonus_log_id'],
  84. 'order_no' => $params['order_no'],
  85. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  86. if (!empty($model)) throw new \Exception('重复执行');
  87. $model = new self();
  88. $model->loadDefaultValues();
  89. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  90. $res = $model->save();
  91. if ($res == false) throw new \Exception($model->getErrorMessage());
  92. return $model;
  93. } catch (\Exception $e) {
  94. self::$static_error = $e->getMessage();
  95. return false;
  96. }
  97. }
  98. public static function settlementOld($mall_id)
  99. {
  100. $t = Yii::$app->db->beginTransaction();
  101. try {
  102. $basic = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_BLIND_BOX, 'basic');
  103. if (empty($basic['bonus_time'])) throw new \Exception('结算分红时间没有设置');
  104. if (empty($basic['is_enable'])) throw new \Exception('没有开启奖金盲盒');
  105. $time = time();
  106. $date = date('Y-m-d', $time);
  107. $bonus_time = $date . ' ' . $basic['bonus_time'] . ':00';
  108. $bonus_time = strtotime($bonus_time);
  109. if ($bonus_time > $time) throw new \Exception(date('结算周期:' . 'Y-m-d H:i:s', $bonus_time) . ';此刻时间是:' . date('Y-m-d H:i:s', $time));
  110. $res = BlindBoxBonusLogDetail::findOne(['mall_id' => $mall_id, 'settlement_time' => $bonus_time]);
  111. if ($res) throw new \Exception('结算周期:' . date('Y-m-d H:i:s', $bonus_time) . '已经结算过');
  112. $where = [
  113. ['mall_id' => $mall_id],
  114. ['is_settlement' => BlindBoxEnum::UNSETTLED],
  115. ['<=', 'created_at', $bonus_time]
  116. ];
  117. $params = [
  118. 'where' => $where,
  119. 'select' => 'user_id,sum(order_bonus) as order_bonus',
  120. 'group' => 'user_id'
  121. ];
  122. $list = BlindBoxBonusLogDetail::lists($params);
  123. if (empty($list)) throw new \Exception('无符合条件的待结算分红记录');
  124. $insert_wallet = [];
  125. foreach ($list as $item) {
  126. $insert_wallet [] = [
  127. 'mall_id' => $mall_id, 'user_id' => $item['user_id'],
  128. 'is_frozen' => false,
  129. 'wallet_type' => 'commission',
  130. 'money' => $item['order_bonus'],
  131. 'desc' => '奖金盲盒-分红结算',
  132. 'source_table' => self::tableName(),
  133. 'source_table_id' => 0,
  134. 'from_type' => AddonsEnum::ADDONS_NAME_BLIND_BOX,
  135. 'capital_type' => UserWalletEnum::BLIND_BOX_BONUS,
  136. 'order_no' => '',
  137. 'contributor_name' => '',
  138. 'contributor_id' => 0,
  139. 'contributor_money' => 0,
  140. ];
  141. }
  142. //投递到资产队列
  143. if ($insert_wallet) {
  144. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  145. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  146. foreach ($insert_wallet as $k => $user) {
  147. $model = BlindBoxUser::findOne(['user_id' => $user['user_id'], 'mall_id' => $mall_id]);
  148. if (empty($model)) continue;
  149. $model->total_commission += $user['money'];
  150. $model->frozen_commission -= $user['money'];
  151. if ($model->frozen_commission < 0) $model->frozen_commission = 0;
  152. $res = $model->save();
  153. if (empty($res)) throw new \Exception($model->getErrorMessage());
  154. }
  155. }
  156. $attributes = ['is_settlement' => BlindBoxEnum::SETTLED, 'settlement_time' => $bonus_time];
  157. array_unshift($where, 'and');
  158. $res = BlindBoxBonusLogDetail::updateAll($attributes, $where);
  159. if (empty($res)) throw new \Exception('更新分红详情表错误');
  160. $t->commit();
  161. //插入通知
  162. self::insertNotice($mall_id, $bonus_time);
  163. return true;
  164. } catch (\Exception $e) {
  165. $t->rollBack();
  166. echo $mall_id . ':BlindBoxBonusLogDetail:settlement:' . $e->getMessage();
  167. return false;
  168. }
  169. }
  170. public static function settlement($mall_id, $settle_method_self_order, $bonus_log_id)
  171. {
  172. try {
  173. $time = time();
  174. $where_detail = [
  175. ['mall_id' => $mall_id],
  176. ['is_settlement' => BlindBoxEnum::UNSETTLED],
  177. ['is_feedback' => BlindBoxEnum::NO_FEEDBACK],
  178. ['<>', 'bonus_log_id', $bonus_log_id],
  179. ];
  180. $params = [
  181. 'where' => $where_detail,
  182. 'select' => 'id,user_id, order_bonus,bonus_log_id',
  183. ];
  184. $bonus_log_detail_list = BlindBoxBonusLogDetail::lists($params);
  185. if (empty($bonus_log_detail_list)) return true;
  186. $insert_wallet = [];
  187. $ids = [];
  188. if ($settle_method_self_order == 1) {
  189. //订单完成
  190. $bonus_log_ids = array_column($bonus_log_detail_list, 'bonus_log_id');
  191. $where = [
  192. ['mall_id' => $mall_id],
  193. ['id' => $bonus_log_ids],
  194. ['is_feedback' => BlindBoxEnum::NO_FEEDBACK],
  195. ];
  196. $params = [
  197. 'where' => $where,
  198. 'select' => 'id,order_no,type',
  199. ];
  200. $bonus_log_list = BlindBoxBonusLog::lists($params);
  201. $order_nos = [];
  202. foreach ($bonus_log_list as $value) {
  203. if ($value['type'] == BlindBoxEnum::ORDER_CONSUME) {
  204. $order_nos[] = $value['order_no'];
  205. }
  206. }
  207. $order_list = Order::lists(['where' => [
  208. ['order_no' => $order_nos],
  209. ['order_status' => OrderStatusEnum::ACCOMPLISH]
  210. ], 'select' => 'order_no']);
  211. $order_nos = [];
  212. if (!empty($order_list)) {
  213. $order_nos = array_column($order_list, 'order_no');
  214. }
  215. $bonus_log_ids = [];
  216. foreach ($bonus_log_list as $val) {
  217. switch ($val['type']) {
  218. case BlindBoxEnum::ORDER_CONSUME:
  219. if (in_array($val['order_no'], $order_nos)) {
  220. $bonus_log_ids[] = $val['id'];
  221. }
  222. break;
  223. case BlindBoxEnum::DIRECT_CONSUME:
  224. case BlindBoxEnum::NEW_PEOPLE:
  225. $bonus_log_ids[] = $val['id'];
  226. break;
  227. }
  228. }
  229. foreach ($bonus_log_detail_list as $item) {
  230. if (in_array($item['bonus_log_id'], $bonus_log_ids)) {
  231. $ids[] = $item['id'];
  232. if ($item['order_bonus'] > 0) {
  233. $insert_wallet [] = self::getInsertWalletArr($mall_id, $item['user_id'], $item['order_bonus'], $item['id']);
  234. }
  235. }
  236. }
  237. } else {
  238. foreach ($bonus_log_detail_list as $item) {
  239. $ids[] = $item['id'];
  240. if ($item['order_bonus'] > 0) {
  241. $insert_wallet [] = self::getInsertWalletArr($mall_id, $item['user_id'], $item['order_bonus'], $item['id']);
  242. }
  243. }
  244. }
  245. $where_detail[] = ['id' => $ids];
  246. //投递到资产队列
  247. if ($insert_wallet) {
  248. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  249. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  250. foreach ($insert_wallet as $k => $user) {
  251. $model = BlindBoxUser::findOne(['user_id' => $user['user_id'], 'mall_id' => $mall_id]);
  252. if (empty($model)) continue;
  253. $model->total_commission += $user['money'];
  254. $model->frozen_commission -= $user['money'];
  255. if ($model->frozen_commission < 0) $model->frozen_commission = 0;
  256. $res = $model->save();
  257. if (empty($res)) throw new \Exception($model->getErrorMessage());
  258. }
  259. }
  260. $attributes = ['is_settlement' => BlindBoxEnum::SETTLED, 'settlement_time' => $time];
  261. array_unshift($where_detail, 'and');
  262. $res = BlindBoxBonusLogDetail::updateAll($attributes, $where_detail);
  263. if (empty($res)) throw new \Exception('更新分红详情表错误');
  264. //插入通知
  265. self::insertNotice($mall_id, $ids);
  266. return true;
  267. } catch (\Exception $e) {
  268. echo $mall_id . ':BlindBoxBonusLogDetail:settlement:' . $e->getMessage() . $e->getFile() . $e->getLine();
  269. return false;
  270. }
  271. }
  272. protected static function getInsertWalletArr($mall_id, $user_id, $money, $source_table_id)
  273. {
  274. return [
  275. 'mall_id' => $mall_id, 'user_id' => $user_id,
  276. 'is_frozen' => false,
  277. 'wallet_type' => 'commission',
  278. 'money' => $money,
  279. 'desc' => '奖金盲盒-分红结算',
  280. 'source_table' => self::tableName(),
  281. 'source_table_id' => $source_table_id,
  282. 'from_type' => AddonsEnum::ADDONS_NAME_BLIND_BOX,
  283. 'capital_type' => UserWalletEnum::BLIND_BOX_BONUS,
  284. 'order_no' => '',
  285. 'contributor_name' => '',
  286. 'contributor_id' => 0,
  287. 'contributor_money' => 0,
  288. ];
  289. }
  290. public static function insertNotice($mall_id, $ids)
  291. {
  292. $time = time();
  293. $params = [
  294. 'where' => [
  295. ['mall_id' => $mall_id],
  296. ['id' => $ids],
  297. ['is_settlement' => BlindBoxEnum::SETTLED],
  298. ],
  299. 'select' => 'user_id, order_bonus,bonus_log_id,created_at',
  300. ];
  301. $list = BlindBoxBonusLogDetail::lists($params);
  302. $insert_notice = [];
  303. $user_ids = array_column($list, 'user_id');
  304. $bonus_log_ids = array_column($list, 'bonus_log_id');
  305. $user_list = User::lists(['where' => [['id' => $user_ids]], 'index' => 'id', 'select' => 'id,nickname']);
  306. $bonus_log_list = BlindBoxBonusLog::lists(['where' => [['id' => $bonus_log_ids]], 'index' => 'id', 'select' => 'id,order_no']);
  307. $recommend_user_log_list = BlindBoxRecommendUserLog::lists(['where' => [['bonus_log_id' => $bonus_log_ids]]]);
  308. $recommend_user_log_arr = [];
  309. if (!empty($recommend_user_log_list)) {
  310. foreach ($recommend_user_log_list as $val) {
  311. $recommend_user_log_arr[$val['bonus_log_id']][] = $val['user_id'];
  312. }
  313. }
  314. foreach ($list as $item) {
  315. $content = '尊敬的';
  316. if (!empty($user_list[$item['user_id']]['nickname'])) {
  317. $content .= $user_list[$item['user_id']]['nickname'];
  318. } else {
  319. $content .= $item['user_id'];
  320. }
  321. $order_no = $bonus_log_list[$item['bonus_log_id']]['order_no'] ?? '';
  322. $user_id_str = '';
  323. if (!empty($order_no)) {
  324. $type = $order_no[0];
  325. if ($type == 't' && isset($recommend_user_log_arr[$item['bonus_log_id']])) {
  326. $user_id_str = join(',', $recommend_user_log_arr[$item['bonus_log_id']]);
  327. }
  328. }
  329. if (isset($bonus_log_list[$item['bonus_log_id']])) $content .= ',订单:' . $order_no;
  330. $date = date('m-d', $item['created_at']);
  331. $content .= ',' . $date . '产生分红' . $item['order_bonus'] . '元。';
  332. if ($user_id_str) {
  333. $content .= '直推会员id:' . $user_id_str;
  334. }
  335. $insert_notice[] = [
  336. 'mall_id' => $mall_id,
  337. 'user_id' => $item['user_id'],
  338. 'content' => $content,
  339. 'status' => StatusEnum::ENABLED,
  340. 'created_at' => $time,
  341. 'updated_at' => $time,
  342. ];
  343. }
  344. if (!empty($insert_notice)) {
  345. BlindBoxNotice::setData($insert_notice);
  346. }
  347. }
  348. }