ApproachOrderForm.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Cache;
  4. use common\helpers\Date;
  5. use common\components\Model;
  6. use common\helpers\Form;
  7. use common\helpers\LoggerTool;
  8. use common\helpers\PayStack;
  9. use common\helpers\user\Balance;
  10. use common\helpers\user\Cash;
  11. use common\helpers\user\Info;
  12. use common\libs\logging\operate\AdminOperate;
  13. use common\models\ApproachDecOrder;
  14. use common\models\ApproachOrder;
  15. use common\models\ApproachOrderGoods;
  16. use common\models\BaUser;
  17. use common\models\DealType;
  18. use common\models\DecLevelLog;
  19. use common\models\DecOrder;
  20. use common\models\Order;
  21. use common\models\OrderGoods;
  22. use common\models\Period;
  23. use common\models\ReceiveAddress;
  24. use common\models\Region;
  25. use common\models\ShopGoods;
  26. use common\models\User;
  27. use common\models\UserNetwork;
  28. use yii\base\Exception;
  29. /**
  30. * Login form
  31. */
  32. class ApproachOrderForm extends Model
  33. {
  34. public $sn;
  35. public $expressCompany;
  36. public $orderTrackNo;
  37. public $status;
  38. public $remark;
  39. public $note;
  40. public $type;
  41. public $addressId;
  42. public $payType;
  43. public $goodsId;
  44. public $goodsNum;
  45. public $payPassword;
  46. public $email;
  47. public $userName;
  48. public $consignee;
  49. public $acceptMobile;
  50. public $province;
  51. public $city;
  52. public $county;
  53. public $lgaName;
  54. public $cityName;
  55. public $detailaddress;
  56. private $_address;
  57. private $_decAmount;
  58. private $_decPv;
  59. private $_freight;
  60. private $_payAmount;
  61. private $_orderGoods;
  62. private $_standardAmount;
  63. private $_decAmountStandard;
  64. /**
  65. * @var ApproachOrder
  66. */
  67. private $_model;
  68. public function init() {
  69. parent::init();
  70. $this->adminOperateLogger = new AdminOperate([
  71. 'fetchClass' => ApproachOrder::class,
  72. ]);
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function rules()
  78. {
  79. return [
  80. [['sn', 'expressCompany', 'orderTrackNo', 'status', 'remark','type','addressId','payType','goodsId','goodsNum', 'payPassword','userName','consignee','acceptMobile','province',/*'city','county',*/'lgaName','cityName','detailaddress','email'], 'trim'],
  81. [['sn', 'expressCompany', 'orderTrackNo', 'status', 'remark','type','addressId','payType','goodsId','goodsNum', 'payPassword','userName','consignee','acceptMobile','province',/*'city','county',*/'detailaddress'/*,'email'*/], 'required'],
  82. [['status'], 'isStatus'],
  83. [['addressId'], 'isAddress'],
  84. [['payType'], 'isPayType'],
  85. [['payPassword'], 'validatePassword'],
  86. ];
  87. }
  88. public function attributeLabels()
  89. {
  90. return [
  91. 'sn' => 'Order code',//订单号
  92. 'expressCompany' => 'ExpressCompany',//快递公司
  93. 'orderTrackNo' => 'courier number',//快递单号
  94. 'status' => 'status',//状态
  95. 'remark' => 'remark',//备注
  96. 'type' => 'order type',//订单类型
  97. 'addressId' => 'addressId',//收货地址
  98. 'payType' => 'payType',//支付方式
  99. 'goodsId' => 'Product ID',
  100. 'goodsNum' => 'Product quantity',//
  101. 'userName' => '复消会员编号',//复消会员编号
  102. 'consignee' => '收货人',//收货人
  103. 'acceptMobile' => '收货电话',//收货电话
  104. 'province' => '省',
  105. 'city' => '市',
  106. 'county' => '区',
  107. 'lgaName' => 'lga name',
  108. 'cityName' => 'City Name',
  109. 'detailaddress' => '收货详细地址',
  110. 'email' => 'Email',
  111. ];
  112. }
  113. /**
  114. * 指定校验场景
  115. * @return array
  116. */
  117. public function scenarios()
  118. {
  119. $parentScenarios = parent::scenarios();
  120. $customScenarios = [
  121. // 管理员修改订单状态
  122. 'adminStatus' => ['sn', 'status'],
  123. // 校验订单支付
  124. 'verifyPayStack' => ['sn', 'note', 'status'],
  125. // 会员下单
  126. 'userOrder' => ['type','addressId', 'payType','goodsId','goodsNum', 'note', 'payPassword'],
  127. ];
  128. return array_merge($parentScenarios, $customScenarios);
  129. }
  130. /**
  131. * 校验之前
  132. * @return bool
  133. */
  134. public function beforeValidate()
  135. {
  136. $parentValidate = parent::beforeValidate();
  137. if ($this->sn) {
  138. $this->_model = ApproachOrder::findOne(['SN' => $this->sn]);
  139. if (!$this->_model){
  140. $this->addError('sn', '订单不存在');
  141. return false;
  142. }
  143. }
  144. if ($this->scenario == 'verifyPayStack'){
  145. if ($this->_model->STATUS != \Yii::$app->params['orderStatus']['notPaid']['value']) {
  146. $this->addError('sn', '订单支付状态错误');
  147. return false;
  148. }
  149. }
  150. return $parentValidate;
  151. }
  152. /**
  153. * 判断收货地址是否存在
  154. * @param $attribute
  155. */
  156. public function isAddress($attribute){
  157. if (!$receiveAddress = ReceiveAddress::find()->where(' ID=:ID', [':ID' => $this->addressId])->asArray()->one()) {
  158. $this->addError($attribute, '收货地址不存在');
  159. } else {
  160. $this->_address = $receiveAddress;
  161. }
  162. }
  163. /**
  164. * 校验支付密码
  165. * @param $attribute
  166. * @param $params
  167. */
  168. public function validatePassword($attribute, $params) {
  169. if (!User::validatePayPassword(\Yii::$app->user->id, $this->payPassword)) {
  170. $this->addError($attribute, '支付密码不正确');
  171. }
  172. }
  173. /**
  174. * 判断支付方式
  175. * @param $attribute
  176. */
  177. public function isPayType($attribute)
  178. {
  179. if ($this->payType != 'pay_stack'){
  180. $this->addError($attribute, '只允许PayStack方式支付');
  181. return;
  182. }
  183. // 一个订单只能包含一类商品
  184. $goods = ShopGoods::find()->select('ID,CATEGORY_TYPE')->where(['in', 'ID', $this->goodsId])->andWhere(['STATUS' => 1])->asArray()->all();
  185. $goodsCategoryType = array_unique(array_column($goods, 'CATEGORY_TYPE'));
  186. if (count($goodsCategoryType) != 1) {
  187. $this->addError($attribute, 'Order cannot contain multiple product categories');//订单不能包含多种商品分类
  188. return;
  189. }
  190. // 购买方式
  191. $sellTypeLabelMap = array_column(ShopGoods::SALE_TYPE, NULL, 'label');
  192. if (!array_key_exists($this->payType, $sellTypeLabelMap)) {
  193. $this->addError($attribute, '不支持PayStack方式支付');
  194. return;
  195. }
  196. // 所选支付方式必须是商品分类支持的类型
  197. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  198. // 商品类型
  199. $currCategoryType = $goodsCategoryType[0];
  200. if (!array_key_exists($currCategoryType, $categoryType)) {
  201. $this->addError($attribute, '商品分类错误');
  202. return;
  203. }
  204. $sellType = $categoryType[$currCategoryType]['sell_type'] ?? [];
  205. if (!$sellType || !in_array($this->payType, array_column($sellType, 'label'))) {
  206. $this->addError($attribute, '支付方式错误3');
  207. }
  208. }
  209. /**
  210. * 校验类型
  211. * @param $attribute
  212. */
  213. public function isStatus($attribute){
  214. if($this->type && !in_array($this->type, \Yii::$app->params['orderStatus'])){
  215. $this->addError($attribute, '订单状态类型错误');
  216. return ;
  217. }
  218. if ($this->scenario == 'adminStatus'){
  219. if ($this->status == $this->_model['STATUS']) {
  220. $this->addError($attribute, '订单状态没有改变');
  221. return ;
  222. }
  223. if($this->status == \Yii::$app->params['orderStatus']['notPaid'] && $this->_model['STATUS'] >= \Yii::$app->params['orderStatus']['delivery']) {
  224. $this->addError($attribute, '订单已经进入物流状态不能改为未支付');
  225. return ;
  226. }
  227. elseif($this->status == \Yii::$app->params['orderStatus']['paid'] && $this->_model['STATUS'] >= \Yii::$app->params['orderStatus']['cancel']) {
  228. $this->addError($attribute, '订单已失效不能处理');
  229. return ;
  230. }
  231. elseif($this->status == \Yii::$app->params['orderStatus']['delivery']) {
  232. $this->addError($attribute, '订单不能单独处理为物流状态');
  233. return ;
  234. }
  235. elseif($this->status == \Yii::$app->params['orderStatus']['complete'] && $this->_model['STATUS'] > \Yii::$app->params['orderStatus']['cancel']) {
  236. $this->addError($attribute, '订单已失效不能处理');
  237. return ;
  238. }
  239. elseif($this->status == \Yii::$app->params['orderStatus']['cancel']) {
  240. if($this->_model['STATUS'] == \Yii::$app->params['orderStatus']['complete']) {
  241. $this->addError($attribute, '订单已完成不能取消');
  242. return ;
  243. }
  244. if($this->_model['STATUS'] == \Yii::$app->params['orderStatus']['del']) {
  245. $this->addError($attribute, '订单已删除不能取消');
  246. return ;
  247. }
  248. }
  249. elseif($this->status == \Yii::$app->params['orderStatus']['del']) {
  250. if($this->_model['STATUS'] == \Yii::$app->params['orderStatus']['complete']) {
  251. $this->addError($attribute, '订单已完成不能删除');
  252. return ;
  253. }
  254. }
  255. }
  256. }
  257. /**
  258. * 校验PayStack支付,更新订单状态.同步到正式订单.
  259. * @throws Exception
  260. */
  261. public function verifyPayStack(): ?ApproachOrder
  262. {
  263. if (!$this->validate()) {
  264. return null;
  265. }
  266. // 调用PayStack支付校验
  267. LoggerTool::info([$this->note['reference'], $this->note]);
  268. $payload = PayStack::transactionVerify($this->note['reference']);
  269. LoggerTool::info($payload);
  270. if ($payload['status'] !== true) {
  271. throw new Exception(Form::formatErrorsForApi($payload['message']));
  272. }
  273. if ($payload['data']['amount'] != $this->_model->PAY_AMOUNT * 100) {
  274. throw new Exception(Form::formatErrorsForApi('支付金额与订单金额不符'));
  275. }
  276. // 是否给BA升级的单子
  277. $isBrandAmbassadorUpgrade = $this->note['customer'][2]['value'] ?? false;
  278. $db = \Yii::$app->db;
  279. $transaction = $db->beginTransaction();
  280. try {
  281. // 更新准订单状态为已支付
  282. $this->_model->STATUS = $this->status;
  283. $this->_model->NOTE = json_encode($this->note);
  284. $this->_model->PAY_AT = Date::utcToTime($this->note['paid_at']);
  285. $this->_model->EMAIL = $this->note['email'];
  286. if (!$this->_model->save()) {
  287. throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
  288. }
  289. // 更新订单商品的支付Email
  290. ApproachOrderGoods::updateAll(['EMAIL' => $this->note['email']], 'ORDER_SN = :ORDER_SN', [':ORDER_SN' => $this->sn]);
  291. // 同步准订单到正式订单
  292. Order::insertOne($this->_model->toArray());
  293. // 同步准订单商品到正式订单商品
  294. $approachOrderGoods = ApproachOrderGoods::findAllAsArray('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $this->sn]);
  295. foreach ($approachOrderGoods as &$approachOrderGood) {
  296. $approachOrderGood['EMAIL'] = $this->email;
  297. }
  298. OrderGoods::batchInsert($approachOrderGoods);
  299. // 同步报单
  300. $approachDecOrder = ApproachDecOrder::findOneAsArray('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $this->sn]);
  301. if ($approachDecOrder) {
  302. unset($approachDecOrder['STATUS']);
  303. // 同步报单
  304. DecOrder::insertOne($approachDecOrder);
  305. // 会员升级 报单类型:2会员升级单
  306. if ($approachDecOrder['DETAIL_TYPE'] == 2) {
  307. // 为被升级人进行升级操作
  308. $decLevelLog = new DecLevelLog();
  309. $decLog = [
  310. 'userId' => $approachDecOrder['TO_USER_ID'],//会员ID
  311. 'fromId' => $approachDecOrder['ORI_LV'], // 变动前的级别
  312. 'levelId' => $approachDecOrder['UPGRADE_LV'],// 变动后的级别
  313. 'actionId' => $approachDecOrder['USER_ID'],
  314. 'remark' => $approachDecOrder['REMARK'],
  315. 'lvPv' => $this->_model->PV,
  316. ];
  317. $modifyDecLv = $decLevelLog->frontendChange($decLog);
  318. if (empty($modifyDecLv)) {
  319. $transaction->rollBack();
  320. throw new Exception("Failed to upgrade for member");//为会员升级失败
  321. }
  322. }
  323. }
  324. // 删除中间表
  325. ApproachOrder::deleteAll('SN = :SN', [':SN' => $this->sn]);
  326. ApproachOrderGoods::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $this->sn]);
  327. ApproachDecOrder::deleteAll('ORDER_SN = :ORDER_SN', [':ORDER_SN' => $this->sn]);
  328. if ($isBrandAmbassadorUpgrade) {
  329. // 修改BA会员升级状态
  330. if (!BaUser::updateAll(['WHETHER_UPGRADE' => 1, 'BA_UPGRADE_AT' => time()], 'USER_NAME=:USER_NAME', [':USER_NAME' => $isBrandAmbassadorUpgrade])) {
  331. throw new Exception(Form::formatErrorsForApi('BA升级状态错误'));
  332. }
  333. }
  334. $transaction->commit();
  335. } catch (Exception $e) {
  336. $transaction->rollBack();
  337. $this->addError('edit', $e->getFile() . ' ' . $e->getMessage());
  338. return null;
  339. }
  340. return $this->_model;
  341. }
  342. /**
  343. * 复销
  344. * @throws Exception
  345. * @throws \yii\db\Exception
  346. */
  347. public function add(){
  348. if(!$this->validate()){
  349. return null;
  350. }
  351. $ids = $this->goodsId;
  352. $totalAmount = 0;
  353. $totalPv = 0;
  354. $totalAmountStandard = 0;
  355. $goodsType = ShopGoods::GOODS_TYPE;
  356. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0); // 汇率
  357. foreach ($this->goodsNum as $k => $v) {
  358. if ($v) {
  359. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  360. if($goods['STORE_NUMS']>0){
  361. if ($goods['TYPE'] == 1 || $goods['TYPE'] == 2) {
  362. $discount = $goodsType[$goods['TYPE']]['discount'];
  363. $realPrice = $goods['SELL_PRICE'] * $discount/100;
  364. $realPv = $goods['PRICE_PV'] * $discount/100;
  365. $realPriceStandard = $goods['SELL_PRICE_STANDARD'] * $discount/100;
  366. } else {
  367. $discount = $goods['SELL_DISCOUNT'];
  368. $realPrice = $goods['SELL_PRICE'] * $discount;
  369. $realPv = $goods['PRICE_PV'] * $discount;
  370. $realPriceStandard = $goods['SELL_PRICE_STANDARD'] * $discount;
  371. }
  372. $totalAmount += $realPrice * intval($v);
  373. $totalPv += $realPv * intval($v);
  374. $totalAmountStandard += $realPriceStandard * intval($v);
  375. $this->_orderGoods[] = [
  376. 'GOODS_ID' => $goods['ID'],
  377. 'PRICE' => $goods['SELL_PRICE'],
  378. 'PV' => $goods['PRICE_PV'],
  379. 'REAL_PRICE' => $realPrice,
  380. 'REAL_PV' => $realPv,
  381. 'POINT' => $goods['POINT'],
  382. 'BUY_NUMS' => intval($v),
  383. 'TAX_RATE' => $goods['TAX_RATE'],
  384. 'SKU_CODE' => $goods['GOODS_NO'],
  385. 'GOODS_TITLE' => $goods['GOODS_NAME'],
  386. 'CATEGORY_TYPE' => $goods['CATEGORY_TYPE'],
  387. 'PAY_TYPE' => $this->payType,
  388. 'EMAIL' => $this->email,
  389. 'STANDARD_PRICE' => $goods['SELL_PRICE_STANDARD'],
  390. 'REAL_STANDARD_PRICE' => $realPriceStandard,
  391. 'EXCHANGE_RATE' => $exchangeRate,
  392. ];
  393. }
  394. }
  395. }
  396. // 汇率
  397. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  398. // 运费.奈拉
  399. $freight = floatval(Cache::getSystemConfig()['freight']['VALUE'] ?? 0);
  400. // 普通商品免运费阈值.奈拉
  401. $freeShipping = floatval(Cache::getSystemConfig()['freeShipping']['VALUE'] ?? 0);
  402. $this->_decAmount = $totalAmount;
  403. $this->_decPv = $totalPv;
  404. $this->_freight = ($totalAmount >= $freeShipping) ? 0 : $freight;
  405. if($this->_address['PROVINCE']==1){
  406. $this->_freight = 0;
  407. }
  408. $this->_payAmount = $this->_decAmount + $this->_freight;
  409. $this->_decAmountStandard = $totalAmountStandard;
  410. $this->_standardAmount = $this->_decAmountStandard + $this->_freight;
  411. $db = \Yii::$app->db;
  412. $transaction = $db->beginTransaction();
  413. try {
  414. //写入订单
  415. if (!$orderResult = $this->addOrder()) {
  416. throw new Exception(Form::formatErrorsForApi($orderResult->getErrors()));
  417. }
  418. $transaction->commit();
  419. return $orderResult;
  420. }catch (\Exception $e){
  421. $transaction->rollBack();
  422. $this->addError('add', $e->getMessage());
  423. return null;
  424. }
  425. }
  426. /**
  427. * 复销订单
  428. * @throws Exception
  429. */
  430. public function addOrder()
  431. {
  432. $periodObj = Period::instance();
  433. $nowPeriodNum = $periodObj->getNowPeriodNum();
  434. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  435. $userId = \Yii::$app->user->id;
  436. $userName = Info::getUserNameByUserId($userId);
  437. $userRealName = Info::getUserRealNameByUserId($userId);
  438. $userMobile = Info::getUserMobileByUserId($userId);
  439. $userEmail = Info::getUserEmailByUserId($userId);
  440. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  441. // 加入订单信息
  442. if ($this->_address['PROVINCE'] != 1) {
  443. $warehouse = Region::getWarehouseByCode($this->_address['PROVINCE']);//仓库
  444. if (!$warehouse) {
  445. // throw new Exception('地区2暂时不支持配送,具体联系客服');
  446. }
  447. }else{
  448. $warehouse = '01';
  449. }
  450. $_hasPV = $this->_decPv;
  451. $ordNo = $this->_generateSn();
  452. $orderModel = new ApproachOrder();
  453. $orderModel->SN = 'OS' . $ordNo;
  454. $orderModel->DEC_SN = 'DS' . $ordNo;
  455. $orderModel->ORDER_TYPE = $this->type;
  456. $orderModel->USER_ID = $userId;
  457. $orderModel->USER_NAME = $userName;
  458. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  459. $orderModel->PV = $_hasPV;
  460. $orderModel->PAY_AMOUNT = $this->_payAmount;
  461. $orderModel->PAY_PV = $_hasPV; // 兑换积分不能算业绩
  462. $orderModel->PAY_AT = 0;
  463. $orderModel->PAY_TYPE = $this->payType;
  464. $orderModel->PERIOD_NUM = $nowPeriodNum;
  465. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  466. $orderModel->FREIGHT = $this->_freight;
  467. $orderModel->PAY_FREIGHT = $this->_freight;
  468. $orderModel->CONSIGNEE = $this->_address['CONSIGNEE'];
  469. $orderModel->MOBILE = $this->_address['MOBILE'];
  470. $orderModel->PROVINCE = $this->_address['PROVINCE'];
  471. // $orderModel->CITY = $this->_address['CITY'];
  472. // $orderModel->COUNTY = $this->_address['COUNTY'];
  473. $orderModel->LGA_NAME = $this->_address['LGA_NAME'];
  474. $orderModel->CITY_NAME = $this->_address['CITY_NAME'];
  475. $orderModel->ADDRESS = $this->_address['ADDRESS'];
  476. $orderModel->FRONT_REMARK = $this->remark;
  477. $orderModel->WAREHOUSE = $warehouse;
  478. $orderModel->STATUS = \Yii::$app->params['orderStatus']['notPaid']['value'];
  479. $orderModel->CREATED_AT = Date::nowTime();
  480. $orderModel->CREATE_USER = $userName;
  481. $orderModel->EMAIL = $userEmail?$userEmail:$userName.'@elken.net';
  482. $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard;
  483. $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount;
  484. $orderModel->EXCHANGE_RATE = $exchangeRate;
  485. if($this->_address['PROVINCE']==1){
  486. $orderModel->EXPRESS_TYPE = 1;
  487. $orderModel->CONSIGNEE = $userRealName;
  488. $orderModel->MOBILE = $userMobile;
  489. $orderModel->PROVINCE = 1;
  490. $orderModel->CITY = 1;
  491. $orderModel->COUNTY = 1;
  492. $orderModel->ADDRESS = '';
  493. }
  494. if(!$orderModel->save()){
  495. $this->addErrors($orderModel->getErrors());
  496. return false;
  497. }
  498. // 加入商品到订单商品表
  499. foreach($this->_orderGoods as $key=>$value) {
  500. $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN;
  501. $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  502. }
  503. ApproachOrderGoods::batchInsert($this->_orderGoods);
  504. // TODO: 记录流水
  505. return $orderModel;
  506. }
  507. /**
  508. * 生成流水号
  509. * @return string
  510. */
  511. private function _generateSn() {
  512. return Date::today('Ymd') . $this->_random(10, 1);
  513. }
  514. /**
  515. * 生成随机数
  516. * @param $length
  517. * @param int $numeric
  518. * @return string
  519. */
  520. private function _random($length, $numeric = 0) {
  521. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  522. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  523. $hash = '';
  524. $max = strlen($seed) - 1;
  525. for ($i = 0; $i < $length; $i++) {
  526. $hash .= $seed[mt_rand(0, $max)];
  527. }
  528. return $hash;
  529. }
  530. }