ElectronCouponCode.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. namespace addons\ElectronCoupon\common\models;
  3. use addons\Clerk\common\models\ClerkLog;
  4. use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
  5. use addons\Store\common\models\Store;
  6. use addons\Store\common\models\StoreClerk;
  7. use addons\Store\common\models\StoreClerkIncome;
  8. use addons\Store\common\models\StoreClerkIncomeData;
  9. use common\enums\ClerkEnum;
  10. use common\enums\StatusEnum;
  11. use common\enums\UserWalletEnum;
  12. use common\helpers\FormatHelper;
  13. use common\models\mall\Mall;
  14. use common\models\mall\MallAddons;
  15. use common\models\user\User;
  16. use common\models\user\UserWallet;
  17. use services\common\UserWalletService;
  18. use addons\Store\common\models\StoreCommission;
  19. use Yii;
  20. use yii\db\Exception;
  21. /**
  22. * This is the model class for table "{{%addons_electron_coupon_code}}".
  23. *
  24. * @property int $id 自增id
  25. * @property int $user_e_coupon_id 会员电子券id
  26. * @property int $user_id 会员电子券所有者
  27. * @property int $amount 数量
  28. * @property string $code 核销码唯一
  29. * @property int $created_at
  30. * @property int $updated_at
  31. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  32. * @property int $coupon_status 状态;1已核销;0未核销
  33. * @property int $approvaler_user_id 核销员用户id
  34. * @property string|null $approval_at 核销时间
  35. * @property int $mall_id 商城id
  36. * @property int $store_id 门店id
  37. */
  38. class ElectronCouponCode extends \common\models\base\BaseActiveRecord
  39. {
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%addons_electron_coupon_code}}';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['user_e_coupon_id', 'user_id', 'amount', 'code', 'mall_id'], 'required'],
  54. [['user_e_coupon_id', 'user_id', 'amount', 'created_at', 'updated_at', 'status', 'coupon_status', 'clerk_user_id', 'clerk_admin_id', 'mall_id', 'store_id'], 'integer'],
  55. [['approval_at', 'code'], 'safe']
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => '自增id',
  65. 'user_e_coupon_id' => '会员电子券id',
  66. 'user_id' => '会员电子券所有者',
  67. 'amount' => '数量',
  68. 'code' => '核销码唯一',
  69. 'created_at' => 'Created At',
  70. 'updated_at' => 'Updated At',
  71. 'status' => '状态[-1:删除;0:禁用;1启用]',
  72. 'coupon_status' => '状态;1已核销;0未核销',
  73. 'clerk_user_id' => '核销员用户id',
  74. 'clerk_admin_id' => '核销员管理员id',
  75. 'approval_at' => '核销时间',
  76. 'mall_id' => '商城id',
  77. 'store_id' => '门店id',
  78. ];
  79. }
  80. /**
  81. * 数据设置
  82. */
  83. public static function setData(array $params)
  84. {
  85. try{
  86. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  87. $mall_id = $params['mall_id'];
  88. if (isset($params['id']) && !empty($params['id'])) {
  89. $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  90. if(empty($model)) throw new Exception('核销码记录不存在或已删除');
  91. } else {
  92. $model = new self();
  93. $model->loadDefaultValues();
  94. $model->mall_id = $mall_id;
  95. }
  96. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  97. return $model;
  98. }catch(Exception $e){
  99. self::setStaticError($e->getMessage());
  100. return false;
  101. }
  102. }
  103. // 获取核销码
  104. public static function getCodeContent(array $params)
  105. {
  106. $must_keys = ['user_e_coupon_id', 'amount', 'mall_id', 'user_id'];
  107. foreach ($must_keys as $key) {
  108. if (!in_array($key, array_keys($params))) {
  109. self::setStaticError('缺少必要的参数' . $key);
  110. return false;
  111. }
  112. }
  113. // 有没有对应未核销的记录
  114. $info = self::getOne([
  115. ['user_e_coupon_id' => $params['user_e_coupon_id']],
  116. ['mall_id' => $params['mall_id']],
  117. ['user_id' => $params['user_id']],
  118. ['amount' => $params['amount']],
  119. ['status' => StatusEnum::ENABLED],
  120. ['coupon_status' => ElectronCouponEnums::COUPON_CODE_CREATE],
  121. ], true, '', '', ['id', 'code']);
  122. if ($info) {
  123. return $info['code'];
  124. }
  125. $config = Yii::$app->services->setting->addons->get($params['mall_id'], ElectronCouponEnums::ADDONS_NAME, 'basic');
  126. $customize_name = $config['customize_name'] ?? '电子券';
  127. // 有没有对应已激活的电子券来核销
  128. $user_coupon = ElectronCouponUserCoupon::getOne([
  129. ['id' => $params['user_e_coupon_id']],
  130. ['mall_id' => $params['mall_id']],
  131. ['user_id' => $params['user_id']],
  132. ], true, '', '', ['active_amount', 'use_end_time']);
  133. if (!$user_coupon) {
  134. self::setStaticError($customize_name . '不存在');
  135. return false;
  136. }
  137. if ($user_coupon['active_amount'] < $params['amount']) {
  138. self::setStaticError('已激活' . $customize_name . '数量不足');
  139. return false;
  140. }
  141. if ($user_coupon['use_end_time'] < time()) {
  142. self::setStaticError('该' . $customize_name . '已过期');
  143. return false;
  144. }
  145. while (true){
  146. $code = self::createCode();
  147. $res = self::findOne(['code'=>$code]);
  148. if(empty($res)) {
  149. break;
  150. }
  151. }
  152. $params['code'] = $code;
  153. $params['status'] = StatusEnum::ENABLED;
  154. $params['coupon_status'] = ElectronCouponEnums::COUPON_CODE_CREATE;
  155. $params['clerk_admin_id'] = 0;
  156. $params['clerk_user_id'] = 0;
  157. $params['approval_at'] = 0;
  158. $params['store_id'] = 0;
  159. $created = self::setData($params);
  160. if (!$created) {
  161. self::setStaticError('生成核销码失败');
  162. return false;
  163. }
  164. return $code;
  165. }
  166. /**
  167. * 核销操作
  168. * code 核销码
  169. * mall_id 商城id
  170. * clerk_admin_id 核销管理员id 可为空,为空时 clerk_user_id 不为空
  171. * clerk_user_id 核销会员id 可为空,为空时 clerk_admin_id 不为空
  172. */
  173. public static function doClerkCode($params)
  174. {
  175. if (empty($params['store_id'])) {
  176. throw new Exception('缺少门店ID');
  177. }
  178. $tran = Yii::$app->db->beginTransaction();
  179. try {
  180. if (!isset($params['code']) || !$params['code']) {
  181. throw new Exception('缺少核销码参数');
  182. }
  183. if (!isset($params['mall_id']) || !$params['mall_id']) {
  184. throw new Exception('缺少商城ID');
  185. }
  186. if ( (!isset($params['clerk_admin_id']) && !$params['clerk_admin_id']) &&
  187. (!isset($params['clerk_user_id']) && !$params['clerk_user_id'])
  188. ) {
  189. throw new Exception('缺少核销员参数');
  190. }
  191. // 核销码记录
  192. $coupon_code = self::getOne([['code' => $params['code']], ['<>', 'status', StatusEnum::DELETE], ['mall_id' => $params['mall_id']]], true);
  193. if (!$coupon_code) {
  194. throw new Exception('核销码错误');
  195. }
  196. if ($coupon_code['coupon_status'] == 1) {
  197. throw new Exception('请勿重复核销');
  198. }
  199. if ($params['clerk_user_id']) {
  200. $clerk_user = User::getOne([
  201. ['id' => $params['clerk_user_id']],
  202. [ 'mall_id' => $params['mall_id']],
  203. ['status' => StatusEnum::ENABLED]
  204. ], true);
  205. if (!$clerk_user) {
  206. throw new Exception('核销员ID不存在');
  207. }
  208. }
  209. $config = Yii::$app->services->setting->addons->get($params['mall_id'], ElectronCouponEnums::ADDONS_NAME, 'basic');
  210. $customize_name = $config['customize_name'] ?? '电子券';
  211. // 会员电子券
  212. $user_coupon = ElectronCouponUserCoupon::getOne([
  213. ['id' => $coupon_code['user_e_coupon_id']],
  214. ['mall_id' => $params['mall_id']],
  215. ['user_id' => $coupon_code['user_id']]
  216. ], true);
  217. if (!$user_coupon) {
  218. throw new Exception('会员' . $customize_name . '不存在');
  219. }
  220. if ($user_coupon['use_begin_time'] > time()) {
  221. throw new Exception('会员' . $customize_name . '未到使用时间 ' . date('Y-m-d H:i', $user_coupon['use_begin_time']));
  222. }
  223. if ($user_coupon['use_end_time'] < time()) {
  224. throw new Exception('会员' . $customize_name . '已过期 ' . date('Y-m-d H:i', $user_coupon['use_end_time']));
  225. }
  226. // 当前数量够不够
  227. if ($coupon_code['amount'] > $user_coupon['active_amount']) {
  228. throw new Exception('会员' . $customize_name . '已激活数量不足');
  229. }
  230. $coupon_info = ElectronCoupon::getOne([['id' => $user_coupon['e_coupon_id']], ['mall_id' => $params['mall_id']]], true);
  231. // 更改核销状态
  232. $update_code_data = [
  233. 'id' => $coupon_code['id'],
  234. 'coupon_status' => ElectronCouponEnums::COUPON_STATUS_ACTIVE,
  235. 'approval_at' => time(),
  236. 'clerk_admin_id' => $params['clerk_admin_id'] ?? 0,
  237. 'clerk_user_id' => $params['clerk_user_id'] ?? 0,
  238. 'mall_id' => $params['mall_id'] ?? 0,
  239. ];
  240. $update_coupon_code = self::setData($update_code_data);
  241. if ($update_coupon_code == false) {
  242. throw new Exception(self::getStaticError());
  243. }
  244. // 会员电子券减少激活数量,增加兑换数量
  245. $user_coupon_data = [
  246. 'active_amount' => -1 * $coupon_code['amount'],
  247. 'used_amount' => $coupon_code['amount'],
  248. 'last_used_time' => time(),
  249. 'mall_id' => $params['mall_id'],
  250. 'id' => $user_coupon['id']
  251. ];
  252. $update_user_coupon = ElectronCouponUserCoupon::setData($user_coupon_data);
  253. if ($update_user_coupon == false) {
  254. throw new Exception(ElectronCouponUserCoupon::getStaticError());
  255. }
  256. // 电子券增加兑换数量
  257. $update_coupon_info = ElectronCoupon::setData([
  258. 'id' => $coupon_info['id'],
  259. 'mall_id' => $params['mall_id'],
  260. 'used_amount' => $coupon_code['amount']
  261. ]);
  262. if ($update_coupon_info == false) {
  263. throw new Exception(ElectronCoupon::getStaticError());
  264. }
  265. // 使用记录
  266. $usage_data = [
  267. 'mall_id' => $params['mall_id'],
  268. 'user_id' => $user_coupon['user_id'],
  269. 'user_e_coupon_id' => $coupon_code['user_e_coupon_id'],
  270. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  271. 'e_coupon_name' => $coupon_info['name'],
  272. 'amount' => $coupon_code['amount'],
  273. 'clerk_user_id' => $update_code_data['clerk_user_id'],
  274. 'clerk_admin_id' => $update_code_data['clerk_admin_id'],
  275. 'proper_user_id' => $user_coupon['proper_id'],
  276. 'receive_time' => $user_coupon['last_receive_time'],
  277. 'usage_time' => time(),
  278. 'status' => StatusEnum::ENABLED
  279. ];
  280. $insert_usage = ElectronCouponUsageLog::setData($usage_data);
  281. if ($insert_usage == false) {
  282. throw new Exception(ElectronCouponUsageLog::getStaticError());
  283. }
  284. // 赠积分、佣金
  285. // 是否是专有者
  286. if ($user_coupon['is_proper'] == ElectronCouponEnums::COUPON_IS_PROPER && $user_coupon['proper_id'] > 0) {
  287. // 专有者为奖励者
  288. $reward_user_id = $user_coupon['proper_id'];
  289. } else {
  290. // 使用者为奖励者
  291. $reward_user_id = $user_coupon['user_id'];
  292. }
  293. // 奖励充值
  294. $insert_wallet = [];
  295. // 奖励积分
  296. if ($coupon_info['give_score'] > 0) {
  297. $reward_amount = (float) bcmul($coupon_info['give_score'], $coupon_code['amount'], 2);
  298. $wallet_type = UserWalletService::WALLET_TYPE_SCORE;
  299. $reward_desc = $customize_name . '兑换积分';
  300. // 收益明细表
  301. $approval_income_log = [
  302. 'e_coupon_code_id' => $coupon_code['id'],
  303. 'user_e_coupon_id' => $coupon_code['user_e_coupon_id'],
  304. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  305. 'status' => StatusEnum::ENABLED,
  306. 'user_id' => $reward_user_id,
  307. 'e_coupon_name' => $coupon_info['name'],
  308. 'amount' => $coupon_code['amount'],
  309. 'mall_id' => $params['mall_id'],
  310. 'price_type' => ElectronCouponEnums::COUPON_PRICE_TYPE_SCORE,
  311. 'commission' => $reward_amount,
  312. 'clerk_user_id' => $update_code_data['clerk_user_id'],
  313. 'clerk_admin_id' => $update_code_data['clerk_admin_id'],
  314. ];
  315. $income_log = ElectronCouponApprovalIncomeLog::setData($approval_income_log);
  316. if ($income_log == false) {
  317. throw new Exception(ElectronCouponApprovalIncomeLog::getStaticError());
  318. }
  319. $insert_wallet[] = [
  320. 'mall_id' => $params['mall_id'],
  321. 'user_id' => $reward_user_id,
  322. 'is_frozen' => false,
  323. 'wallet_type' => $wallet_type,
  324. 'money' => $reward_amount,
  325. 'desc' => $reward_desc,
  326. 'source_table' => self::tableName(),
  327. 'source_table_id' => $coupon_code['id'],
  328. 'from_type' => UserWalletEnum::RECHARGE,
  329. 'capital_type'=> UserWalletEnum::GIVE
  330. ];
  331. }
  332. // 奖励佣金
  333. if ($coupon_info['give_price'] > 0 && $params['clerk_user_id']) {
  334. $reward_user_id = $params['clerk_user_id'];
  335. $reward_amount = (float) bcmul($coupon_info['give_price'], $coupon_code['amount'], 2);
  336. $wallet_type = UserWalletService::WALLET_TYPE_COMMISSION;
  337. $reward_desc = $customize_name . '兑换佣金';
  338. // 收益明细表
  339. $approval_income_log = [
  340. 'e_coupon_code_id' => $coupon_code['id'],
  341. 'user_e_coupon_id' => $coupon_code['user_e_coupon_id'],
  342. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  343. 'status' => StatusEnum::ENABLED,
  344. 'user_id' => $reward_user_id,
  345. 'e_coupon_name' => $coupon_info['name'],
  346. 'amount' => $coupon_code['amount'],
  347. 'mall_id' => $params['mall_id'],
  348. 'price_type' => ElectronCouponEnums::COUPON_PRICE_TYPE_COMM,
  349. 'commission' => $reward_amount,
  350. 'clerk_user_id' => $update_code_data['clerk_user_id'],
  351. 'clerk_admin_id' => $update_code_data['clerk_admin_id'],
  352. ];
  353. $income_log = ElectronCouponApprovalIncomeLog::setData($approval_income_log);
  354. if ($income_log == false) {
  355. throw new Exception(ElectronCouponApprovalIncomeLog::getStaticError());
  356. }
  357. $insert_wallet[] = [
  358. 'mall_id' => $params['mall_id'],
  359. 'user_id' => $reward_user_id,
  360. 'is_frozen' => false,
  361. 'wallet_type' => $wallet_type,
  362. 'money' => $reward_amount,
  363. 'desc' => $reward_desc,
  364. 'source_table' => self::tableName(),
  365. 'source_table_id' => $coupon_code['id'],
  366. 'from_type' => UserWalletEnum::RECHARGE,
  367. 'capital_type'=> UserWalletEnum::GIVE
  368. ];
  369. }
  370. /**
  371. * 电子券结算至门店
  372. * 作者:zengfanhua
  373. * 创建时间:2024/05/08 01:02:36
  374. */
  375. if ($coupon_info['give_store_price'] > 0 && $params['store_id']>0 && $params['clerk_user_id']>0) {
  376. //$give_store_price=(float) bcmul(bcdiv($coupon_info['give_store_price'],100,2), $coupon_info['price'], 2);
  377. $give_store_price=$coupon_info['give_store_price'];
  378. $give_store_price= bcmul($give_store_price,$coupon_code['amount'],2);
  379. $income_total_amount= bcmul($coupon_info['price'],$coupon_code['amount'],2);
  380. $paramsTO = [
  381. 'mall_id' => $params['mall_id'],
  382. 'store_id' => $params['store_id'],
  383. 'user_id' => $user_coupon['user_id'],
  384. 'order_no' => 'user_e_coupon_id_' . $coupon_code['user_e_coupon_id'],
  385. 'amount' => $income_total_amount,
  386. 'income' => $give_store_price,
  387. 'user_e_coupon_id' => $coupon_code['user_e_coupon_id'],
  388. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  389. 'e_coupon_name' => $coupon_info['name'],
  390. 'num' => $coupon_code['amount'],
  391. 'clerk_user_id' => $params['clerk_admin_id'] ?? 0,
  392. 'clerk_admin_id' => $params['clerk_admin_id'] ?? 0,
  393. 'proper_user_id' => $user_coupon['proper_id'],
  394. 'mobile' => $params['mobile']
  395. ];
  396. $res = StoreClerkIncome::setData($paramsTO);
  397. if (empty($res)) throw new Exception(StoreClerkIncome::getStaticError());
  398. //更新统计表
  399. $incomeDataModel=StoreClerkIncomeData::getOne([['mall_id'=>$params['mall_id']],['store_id'=>$params['store_id'],'status'=>1]]);
  400. if($incomeDataModel)
  401. {
  402. $incomeDataModel->total_amount+= $income_total_amount;
  403. $incomeDataModel->total_count+=1;
  404. $incomeDataModel->total_income+=$give_store_price;
  405. $res=$incomeDataModel->save();
  406. if (empty($res)) throw new Exception(StoreClerkIncomeData::getStaticError());
  407. }
  408. else{
  409. $res = StoreClerkIncomeData::setData([
  410. 'store_id' => $params['store_id'],
  411. 'mall_id' => $params['mall_id'],
  412. 'total_amount' => $income_total_amount,
  413. 'total_count' => 1,
  414. 'total_income' => $give_store_price
  415. ]);
  416. if (empty($res)) throw new Exception(StoreClerkIncomeData::getStaticError());
  417. }
  418. }
  419. //使用后
  420. $res = ElectronCouponGiveLogs::useAfter($user_coupon['user_id'],$coupon_code['user_e_coupon_id'],time());
  421. if ($res == false) {
  422. throw new Exception(ElectronCouponGiveLogs::getStaticError());
  423. }
  424. // if(!empty($coupon_info['reward_money'])&&!empty($params['clerk_user_id'])){
  425. // $commission = bcmul($coupon_code['amount'],$coupon_info['reward_money'],2);
  426. // self::clerkReward($coupon_info['mall_id'],$params['clerk_user_id'],$commission,$insert_usage['id'],$coupon_info['name']);
  427. // //todo 核销记录
  428. //
  429. // }
  430. //生成核销记录
  431. if(MallAddons::isInstall($params['mall_id'],'Clerk')){
  432. $insert = [
  433. 'mall_id' => $params['mall_id'],
  434. 'store_id' => $params['store_id'],
  435. 'clerk_id' => $params['clerk_id'],
  436. 'user_id' => $params['user_id'] ?? 0,
  437. 'source_table_id' => $coupon_code['id'],
  438. 'source_table' => ElectronCouponCode::tableName(),
  439. 'commission' => $reward_amount ?? 0,
  440. 'money' => $coupon_info['price'] ?? 0,
  441. 'mobile' => $params['mobile'],
  442. 'title' => $coupon_info['name'],
  443. 'pic' => $coupon_info['pic'],
  444. ];
  445. $res = ClerkLog::setData($insert);
  446. if (empty($res)) throw new Exception(ClerkLog::getStaticError());
  447. }
  448. $store = Store::getOne([['id' => $params['store_id']]], true, [], false, 'id,user_id,balance,logo_img,store_name');
  449. $add_clerk_log_data = [
  450. 'mall_id' => $params['mall_id'],
  451. 'user_id' => $params['user_id'],
  452. 'mall_logo' => $store['logo_img'],
  453. 'mall_name' => $store['store_name'],
  454. 'name' => $coupon_info['name'],
  455. 'pic' => $coupon_info['pic'],
  456. 'clerk_code' => (string)$params['clerk_code'],
  457. 'type' => ClerkEnum::ELECTRON_COUPON_CLERK_CODE_PRE,
  458. 'extra' => json_encode([
  459. 'e_coupon_id'=> $user_coupon['e_coupon_id']
  460. ]),
  461. ];
  462. $clerk_log_model = \common\models\common\ClerkLog::setData($add_clerk_log_data);
  463. if ($clerk_log_model == false) {
  464. throw new \Exception(\common\models\common\ClerkLog::getStaticError());
  465. }
  466. $tran->commit();
  467. if ($insert_wallet) {
  468. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  469. if (empty($res)) throw new Exception(UserWalletService::getStaticError());
  470. }
  471. //核销奖励
  472. return compact( 'insert_wallet', 'coupon_info');
  473. } catch (Exception $e) {
  474. $tran->rollBack();
  475. Yii::error(FormatHelper::exception($e, "电子券核销出错"));
  476. self::setStaticError($e->getMessage());
  477. return false;
  478. }
  479. }
  480. //核销奖励
  481. public static function clerkReward($mall_id,$user_id,$money,$source_table_id,$contributor_name){
  482. $desc = '用户(' . $user_id . '),获得电子券核销奖励,佣金:' . $money;
  483. $insert_wallet [] = [
  484. 'mall_id' => $mall_id,
  485. 'user_id' => $user_id,
  486. 'is_frozen' => false,
  487. 'wallet_type' => 'commission',
  488. 'money' => $money,
  489. 'desc' => $desc,
  490. 'source_table' => ElectronCouponUsageLog::tableName(),
  491. 'source_table_id' => $source_table_id,
  492. 'from_type' => ElectronCouponEnums::ADDONS_NAME,
  493. 'capital_type' => UserWalletEnum::E_COUPON_CLERK,
  494. 'order_no' =>$source_table_id,
  495. 'contributor_name' =>$contributor_name,
  496. 'contributor_id' =>0,
  497. 'contributor_money' =>0,
  498. ];
  499. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  500. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  501. return true;
  502. }
  503. /**
  504. * 生成核销码
  505. * @return string
  506. */
  507. public static function createCode()
  508. {
  509. return ClerkEnum::getCode(ClerkEnum::ELECTRON_COUPON_CLERK_CODE_PRE);
  510. }
  511. public static function updateCode(){
  512. $i = 1;
  513. $page_size = 1000;
  514. while ($list = self::find()
  515. ->andWhere(['=','status',1])
  516. ->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  517. foreach ($list as $item) {
  518. while (true){
  519. $code = self::createCode();
  520. $res = self::findOne(['code'=>$code]);
  521. if(empty($res)) {
  522. break;
  523. }
  524. }
  525. $item->code = $code;
  526. $item->save();
  527. var_dump($item->code);
  528. }
  529. $i++;
  530. }
  531. }
  532. public static function updateOldData(){
  533. try {
  534. $mall_list = Mall::lists(['where'=>[
  535. ['>','id',0]
  536. ]]);
  537. foreach ($mall_list as $mall) {
  538. if (!MallAddons::isInstall($mall['id'], 'Clerk')) {
  539. continue;
  540. }
  541. $i = 1;
  542. $page_size = 100;
  543. while (true) {
  544. $list = ClerkLog::find()->where(['>', 'source_table_id', 0])
  545. ->andWhere(['mall_id' => $mall['id']])
  546. ->offset(($i - 1) * $page_size)->limit($page_size)->all();
  547. if (empty($list)) {
  548. break;
  549. }
  550. $clerk_ids = array_column($list, 'clerk_id');
  551. $store_ids = array_column($list, 'store_id');
  552. $source_table_ids = array_column($list, 'source_table_id');
  553. $e_code_list = ElectronCouponCode::lists(['where' => [['id' => $source_table_ids]], 'index' => 'id']);
  554. $store_clerk_list = StoreClerk::lists(['where' => [['id' => $clerk_ids]], 'index' => 'id']);
  555. $store_list = Store::lists(['where' => [['id' => $store_ids]], 'index' => 'id']);
  556. foreach ($list as $item) {
  557. if (empty($store_clerk_list[$item['clerk_id']]) || empty($store_list[$item['store_id']]) || empty($e_code_list[$item['source_table_id']])) {
  558. continue;
  559. }
  560. $e_code_model = $e_code_list[$item['source_table_id']];
  561. if (\common\models\common\ClerkLog::findOne(['clerk_code' => $e_code_model['code'], 'type' => ClerkEnum::ELECTRON_COUPON_CLERK_CODE_PRE])) {
  562. continue;
  563. }
  564. $store_clerk = $store_clerk_list[$item['clerk_id']];
  565. $store = $store_list[$item['store_id']];
  566. $e_coupon_user_model = ElectronCouponUserCoupon::findOne(['id' => $e_code_model['user_e_coupon_id']]);
  567. $e_coupon_model = ElectronCoupon::findOne(['id' => $e_coupon_user_model['e_coupon_id']]);
  568. $add_clerk_log_data = [
  569. 'mall_id' => $item['mall_id'],
  570. 'user_id' => $store_clerk['user_id'],
  571. 'mall_logo' => $store['logo_img'],
  572. 'mall_name' => $store['store_name'],
  573. 'name' => $e_coupon_model['name'],
  574. 'pic' => $e_coupon_model['pic'],
  575. 'clerk_code' => (string)$e_code_model['code'],
  576. 'type' => ClerkEnum::ELECTRON_COUPON_CLERK_CODE_PRE,
  577. 'extra' => json_encode([
  578. 'e_coupon_id' => $e_coupon_user_model['e_coupon_id']
  579. ]),
  580. ];
  581. \common\models\common\ClerkLog::setData($add_clerk_log_data);
  582. var_dump($item['source_table_id']);
  583. }
  584. $i++;
  585. }
  586. }
  587. }catch (\Exception $e){
  588. var_dump($e->getMessage().$e->getFile().$e->getLine());
  589. }
  590. }
  591. }