Order.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\common\service\order;
  10. use mall\basic\Store;
  11. use mall\basic\Users;
  12. use think\facade\Db;
  13. use think\facade\Session;
  14. use app\common\service\Service;
  15. use mall\utils\CString;
  16. use app\common\models\order\Order as OrderModel;
  17. use app\common\models\order\OrderLog as OrderLogModel;
  18. use app\common\models\order\OrderRefundment as OrderRefundmentModel;
  19. use app\common\models\order\OrderGroup as OrderGroupModel;
  20. use app\common\models\order\OrderGoods as OrderGoodsModel;
  21. use app\common\models\order\OrderCollection as OrderCollectionModel;
  22. use app\common\models\order\OrderDelivery as OrderDeliveryModel;
  23. use app\common\models\users\Users as UsersModel;
  24. use app\common\models\users\UsersLog as UsersLogModel;
  25. use app\common\service\Spread\Spread;
  26. use app\common\models\goods\Goods as GoodsModel;
  27. use app\common\models\goods\GoodsItem as GoodsItemModel;
  28. use app\common\models\users\UsersComment as UsersCommentModel;
  29. use app\common\models\wechat\WechatUsers as WechatUsersModel;
  30. use app\common\service\subscribe\mini\Subscribe as SubscribeMini;
  31. use app\common\service\subscribe\wechat\Subscribe as SubscribeWechat;
  32. use app\common\models\system\Users as SystemUsersModel;
  33. use app\common\models\Payment as PaymentModel;
  34. use app\common\library\payment\alipay\Alipay as AlipayPayment;
  35. use app\common\library\payment\wechat\WeChat as WeChatPayment;
  36. use app\common\models\goods\GoodsCardTemplate as GoodsCardTemplateModel;
  37. class Order extends Service {
  38. /**
  39. * 创建订单
  40. * @param array $data
  41. * @return mixed
  42. * @throws \Exception
  43. */
  44. public static function createOrder($data = []){
  45. try{
  46. Db::startTrans();
  47. $order_id = OrderModel::create([
  48. "activity_id" => $data["activity_id"]??0,
  49. "shipping_type" => $data["shipping_type"]??1,
  50. "store_id" => $data["store_id"]??0,
  51. "type" => $data['type'],
  52. "user_id" => Users::get("id"),
  53. "order_no" => orderNo(),
  54. "pay_type" => $data['payment']["id"],
  55. "distribution_id" => 0,
  56. "accept_name" => $data["address"]["accept_name"]??"",
  57. "zip" => $data["address"]["zip"]??"",
  58. "mobile" => $data["address"]["mobile"]??"",
  59. "phone" => $data["address"]["phone"]??"",
  60. "province" => $data["address"]["province"]??"",
  61. "city" => $data["address"]["city"]??"",
  62. "area" => $data["address"]["area"]??"",
  63. "address" => $data["address"]["address"]??"",
  64. "message" => $data["remarks"],
  65. "promotions" => $data["promotions"]??0,
  66. "discount" => $data["discount"]??0,
  67. "real_freight" => $data["real_freight"],
  68. "payable_freight" => $data["payable_freight"],
  69. "real_amount" => $data["real_amount"],
  70. "real_point" => $data["real_point"]??0,
  71. "order_amount" => $data["order_amount"],
  72. "payable_amount" => $data["payable_amount"],
  73. "shipping_code" => isset($data["shipping_type"]) && $data["shipping_type"] == 2 ? Store::getUniqueCode() : "",
  74. "exp" => $data["exp"],
  75. "point" => $data["point"],
  76. "source" => $data["source"],
  77. "create_time" => time()
  78. ])->id;
  79. foreach($data["item"] as $val){
  80. $val["order_id"] = $order_id;
  81. $val["thumb_image"] = str_replace(getDomain(),"",$val["thumb_image"]);
  82. $val["goods_array"] = json_encode([
  83. "title"=>$val["title"],
  84. "spec"=>!empty($val["goods_array"]) ? implode(", ",array_map(function ($res){
  85. return $res["name"] . ':' . $res['value'];
  86. },$val["goods_array"])) : ""
  87. ],JSON_UNESCAPED_UNICODE);
  88. OrderGoodsModel::create($val);
  89. }
  90. Db::commit();
  91. return $order_id;
  92. }catch (\Exception $ex){
  93. Db::rollback();
  94. throw new \Exception("服务器繁忙,请稍后在试",0);
  95. }
  96. }
  97. /**
  98. * 是否允许退款申请
  99. * @param $order
  100. * @param $message
  101. * @return bool
  102. */
  103. public static function refundmentApply($order, $message) {
  104. if(!in_array($order["status"],[2,7])){
  105. throw new \Exception("您的订单不允许做退款处理",0);
  106. }
  107. $orderGoods = Db::name("order_goods")->where([
  108. "order_id"=>$order["id"]
  109. ])->select()->toArray();
  110. $arr = [];
  111. foreach ($orderGoods as $val) {
  112. if ($val['is_send'] == 2) {
  113. throw new \Exception("该订单已经有商品做了退款处理", 0);
  114. }
  115. if (Db::name("order_refundment")
  116. ->where("is_delete",0)->where("pay_status",0)
  117. ->where('FIND_IN_SET(' . $val["id"] . ',order_goods_id)')->count()) {
  118. throw new \Exception("您已经对此商品提交了退款申请,请耐心等待", 0);
  119. }
  120. $arr[] = $val["id"];
  121. }
  122. Db::startTrans();
  123. try{
  124. Db::name("order_refundment")->insert([
  125. "order_no"=>$order["order_no"],
  126. "order_id"=>$order["id"],
  127. "user_id"=>$order["user_id"],
  128. "pay_status"=>0,
  129. "content"=>$message,
  130. "amount"=>$order['order_amount'],
  131. "order_goods_id"=>implode(',',$arr),
  132. "create_time"=>time(),
  133. ]);
  134. self::updateOrderGroupStatus($order,3,1);
  135. Db::commit();
  136. }catch (\Exception $e){
  137. Db::rollback();
  138. throw new \Exception("提交退款申请失败,请稍后在试。",0);
  139. //throw new \Exception($e->getMessage(),0);
  140. }
  141. return true;
  142. }
  143. public static function sendDeliverGoods($params){
  144. if (empty($params["distribution_code"])) {
  145. throw new \Exception("请填写配送单号", 0);
  146. }
  147. if (empty($params["freight_id"])) {
  148. throw new \Exception("请选择物流公司", 0);
  149. }
  150. if(!$order = OrderModel::where(["id"=>$params["id"]])->find()){
  151. throw new \Exception("您要操作的订单不存在!",0);
  152. }
  153. if(!$orderGoodsList = OrderGoodsModel::where("id","in",$params["order_goods_id"])->select()->toArray()){
  154. throw new \Exception("请选择要发货的商品", 0);
  155. }
  156. if($refund = OrderRefundmentModel::where([ "order_id"=>$order["id"],"pay_status"=>0,"is_delete"=>0 ])->find()){
  157. throw new \Exception("此订单有未处理的退款申请",0);
  158. }
  159. $data = [
  160. 'order_id' => $order["id"],
  161. 'admin_id' => $params["admin_id"]??0,
  162. 'user_id' => $order["user_id"],
  163. 'name' => $params["accept_name"] ?? $order["accept_name"],
  164. 'zip' => $params["zip"] ?? $order["zip"],
  165. 'phone' => $params["phone"] ?? $order["phone"],
  166. 'province' => $params["province"] ?? $order["province"],
  167. 'city' => $params["city"] ?? $order["city"],
  168. 'area' => $params["area"] ?? $order["area"],
  169. 'address' => $params["address"] ?? $order["address"],
  170. 'mobile' => $params["mobile"] ?? $order["mobile"],
  171. 'freight' => $order["real_freight"],
  172. 'distribution_code' => $params["distribution_code"],
  173. 'distribution_id' => $order["distribution_id"],
  174. 'note' => $params["remarks"]??"",
  175. 'create_time' => time(),
  176. 'freight_id' => $params["freight_id"]
  177. ];
  178. $delivery_id = OrderDeliveryModel::create($data)->id;
  179. $admin = SystemUsersModel::where(["id"=>$params["admin_id"]??0])->find();
  180. if ($order['pay_type'] == 0) {
  181. //减少库存量
  182. foreach ($orderGoodsList as $val) {
  183. self::updateStock([
  184. "goods_id" => $val["goods_id"],
  185. "product_id" => $val["product_id"],
  186. "goods_nums"=>$val["goods_nums"]
  187. ], "-");
  188. }
  189. }
  190. foreach ($orderGoodsList as $val) {
  191. if(!empty($val["fictitious_array"])){
  192. $fictitious_array = json_decode($val["fictitious_array"],true);
  193. if(!empty($fictitious_array) && $fictitious_array["goods_type"] == 1){
  194. $cardTemp = GoodsCardTemplateModel::where([
  195. ["card_id","=",$fictitious_array["value"]],
  196. ["order_id","<>",$order["id"]],
  197. ["status","=",1]
  198. ])->find();
  199. if(empty($cardTemp)){
  200. throw new \Exception("卡密为空,请添加新的卡密",0);
  201. }
  202. $fictitious_array["card"] = "帐号" . $cardTemp["name"] . " 密码:" . $cardTemp["content"];
  203. OrderGoodsModel::where("id",$val["id"])->save([
  204. "fictitious_array"=>json_encode($fictitious_array,JSON_UNESCAPED_UNICODE)
  205. ]);
  206. GoodsCardTemplateModel::where("id",$cardTemp["id"])->save([
  207. "order_id"=>$order["id"],
  208. "user_id"=>$order["user_id"],
  209. "status"=>2
  210. ]);
  211. }
  212. }
  213. }
  214. //更新发货状态
  215. $orderGoods = OrderGoodsModel::field('count(*) as num')->where([ "is_send"=>0,"order_id"=>$order["id"] ])->find();
  216. $sendStatus = 2; //部分发货
  217. if (count($params["order_goods_id"]) >= $orderGoods['num']) {
  218. $sendStatus = 1; //全部发货
  219. }
  220. foreach ($params["order_goods_id"] as $val) {
  221. OrderGoodsModel::where(["id"=>$val])->save([ "is_send" => 1, "distribution_id" => $delivery_id ]);
  222. }
  223. //更新发货状态
  224. OrderModel::where(['id'=>$order["id"]])->save([ 'distribution_status' => $sendStatus, 'send_time' =>time() ]);
  225. OrderLogModel::create([
  226. 'order_id' => $order["id"],
  227. 'username' => $admin["username"],
  228. 'action' => '发货',
  229. 'result' => '成功',
  230. 'note' => '订单【' . $order["order_no"] . '】由【管理员】' . $admin["username"] . '发货',
  231. 'create_time' => time()
  232. ]);
  233. $user = WechatUsersModel::where("user_id",$order["user_id"])->find();
  234. if(!empty($user["mp_openid"])){
  235. SubscribeMini::deliveryNotice($user["mp_openid"],$delivery_id);
  236. }
  237. if(!empty($user["openid"])){
  238. SubscribeWechat::delivery($user["openid"],$delivery_id);
  239. }
  240. sendSMS(["mobile"=>$order["mobile"],"order_no"=>$order["order_no"]],"deliver_goods");
  241. return true;
  242. }
  243. /**
  244. * 退款处理
  245. * @param $refunds_id
  246. * @param int $admin_id
  247. * @return bool
  248. * @throws \think\db\exception\DataNotFoundException
  249. * @throws \think\db\exception\DbException
  250. * @throws \think\db\exception\ModelNotFoundException
  251. */
  252. public static function refund($refunds_id,$admin_id=0){
  253. $refunds = OrderRefundmentModel::where(["id"=>$refunds_id])->find();
  254. $orderGoodsList = OrderGoodsModel::where("id","in",$refunds['order_goods_id'])->where("is_send","<>","2")->select()->toArray();
  255. if (!$orderGoodsList) {
  256. throw new \Exception("订单中没有符合退货条件的商品!",0);
  257. }
  258. //退款的商品关联信息
  259. $autoMount = 0;
  260. $orderRow = [ 'exp' => 0, 'point' => 0, 'order_no' => $refunds['order_no'] ];
  261. foreach ($orderGoodsList as $val) {
  262. $autoMount += $val['goods_nums'] * $val['real_price'];
  263. //库存增加
  264. self::updateStock(["goods_id" => $val["goods_id"], "product_id" => $val["product_id"], "goods_nums"=>$val["goods_nums"]], '+');
  265. //更新退款状态
  266. OrderGoodsModel::where('id',$val['id'])->save(['is_send' => 2]);
  267. //退款积分,经验
  268. $goodsRow = GoodsModel::where('id',$val['goods_id'])->find();
  269. $orderRow['exp'] += $goodsRow['exp'] * $val['goods_nums'];
  270. $orderRow['point'] += $goodsRow['point'] * $val['goods_nums'];
  271. }
  272. //如果管理员自定义了退款金额。否则就使用默认的付款商品金额
  273. $amount = $refunds['amount'] > 0 ? $refunds['amount'] : $autoMount;
  274. //更新order表状态,查询是否订单中还有未退款的商品,判断是订单退款状态:全部退款或部分退款
  275. $isSendData = OrderGoodsModel::where('order_id',$refunds['order_id'])->where('is_send','<>','2')->find();
  276. $orderStatus = 6; //全部退款
  277. if ($isSendData) {
  278. $orderStatus = 7; //部分退款
  279. }
  280. OrderModel::where(["id"=>$refunds['order_id']])->save(['status' => $orderStatus]);
  281. $order = OrderModel::where('id',$refunds['order_id'])->find();
  282. if ($orderStatus == 6) {
  283. //在订单商品没有发货情况下,返还运费,报价,税金
  284. $isDeliveryData = OrderGoodsModel::where('order_id',$refunds['order_id'])->where('distribution_id','>','0')->find();
  285. if (!$isDeliveryData) {
  286. $amount += $order['real_freight'] + $order['insured'] + $order['taxes'];
  287. }
  288. }
  289. $out_refund_no = orderNo();
  290. //更新退款表
  291. OrderRefundmentModel::where(["id"=>$refunds_id])->save([
  292. 'out_refund_no'=>$out_refund_no,
  293. 'amount' => $amount,
  294. 'pay_status' => 2,
  295. 'dispose_time' =>time()
  296. ]);
  297. $admin = SystemUsersModel::where(["id"=>$refunds["admin_id"]])->find();
  298. if($refunds["type"] == 0){
  299. UsersModel::where(["id"=>$order["user_id"]])->inc("amount",$amount)->update();
  300. UsersLogModel::create([
  301. "order_no"=>$order["order_no"],
  302. "user_id"=>$order["user_id"],
  303. "admin_id"=>Session::get("system_user_id"),
  304. "action"=>3,
  305. "operation"=>1,
  306. "amount"=>$amount,
  307. "description"=>'退款订单号:' . $refunds['order_no'] . '中的商品,退款金额 -¥' . $amount,
  308. "create_time"=>time()
  309. ]);
  310. }
  311. if($orderRow['exp'] > 0){
  312. //更新用户的信息
  313. $users = UsersModel::where(["id"=>$refunds["user_id"]])->find();
  314. $exp = $users['exp'] - $orderRow['exp'];
  315. if($exp > 0) {
  316. UsersModel::where(["id" => $refunds["user_id"]])->save([ "exp"=>$exp ]);
  317. }
  318. $log = '退款订单号:' . $refunds['order_no'] . '中的商品,减掉经验 -' . $orderRow['exp'];
  319. UsersLogModel::create([
  320. "order_no"=>$order["order_no"],
  321. "user_id"=>$refunds["user_id"],
  322. "admin_id"=>$admin_id ? $admin_id : "-1",
  323. "action"=>2,
  324. "operation"=>1,
  325. "point"=>$orderRow['exp'],
  326. "description"=>$log,
  327. "create_time"=>time()
  328. ]);
  329. }
  330. if($orderRow['point'] > 0){
  331. $log = '退款订单号:' . $refunds['order_no'] . '中的商品,减掉积分 -' . $orderRow['point'];
  332. UsersModel::where(["id"=>$order["user_id"]])->dec("point",$orderRow['point'])->update();
  333. UsersLogModel::create([
  334. "order_no"=>$order["order_no"],
  335. "user_id"=>$refunds["user_id"],
  336. "admin_id"=>$admin_id ? $admin_id : "-1",
  337. "action"=>1,
  338. "operation"=>1,
  339. "point"=>$orderRow['point'],
  340. "description"=>$log,
  341. "create_time"=>time()
  342. ]);
  343. }
  344. $user = WechatUsersModel::where("user_id",$order["user_id"])->find();
  345. if(!empty($user["mp_openid"])){
  346. SubscribeMini::refundNotice($user["mp_openid"],$refunds['order_no']);
  347. }
  348. OrderLogModel::create([
  349. 'order_id' => $refunds["order_id"],
  350. 'username' => $admin["username"],
  351. 'action' => '退款',
  352. 'result' => '成功',
  353. 'note' => '订单【' . $refunds["order_no"] . '】退款,退款金额:' . $amount,
  354. 'create_time' => time()
  355. ]);
  356. self::updateOrderGroupStatus($order,3,1);
  357. return true;
  358. }
  359. /**
  360. * 获取已退款金额
  361. * @param array $data
  362. * @return float|int
  363. */
  364. public static function getRefundAmount($data){
  365. $list = OrderRefundmentModel::where([ "order_id"=>$data["id"],"pay_status"=>2 ])->select()->toArray();
  366. $refundFee = 0.00;
  367. foreach ($list as $val) {
  368. $refundFee += $val['amount'];
  369. }
  370. return number_format($refundFee,2);
  371. }
  372. /**
  373. * 支付成功后修改订单状态
  374. */
  375. public static function payment($order_no,$admin_id=0,$note="",$trade_no=""){
  376. if(!$order = OrderModel::where(["order_no"=>$order_no])->find()){
  377. throw new \Exception("您要查找的订单不存在!",0);
  378. }
  379. if($order["pay_status"] == 1){
  380. throw new \Exception("您查找的订单已支付!",0);
  381. }
  382. if(!OrderModel::where(["order_no"=>$order_no])->save([
  383. "status" => ($order['status'] == 5) ? 5 : 2,
  384. "pay_time" => time(),
  385. "pay_status" => 1,
  386. "note" => $note,
  387. "trade_no"=>$trade_no,
  388. "admin_id"=>$admin_id
  389. ])){
  390. throw new \Exception("操作订单失败,请重试!",0);
  391. }
  392. //插入收款单
  393. OrderCollectionModel::create([
  394. 'order_id' => $order['id'],
  395. 'user_id' => $order['user_id'],
  396. 'amount' => $order['order_amount'],
  397. 'create_time' => time(),
  398. 'payment_id' => $order['pay_type'],
  399. 'pay_status' => 1,
  400. 'is_delete' => 0,
  401. 'note' => $note,
  402. 'admin_id' => $admin_id ? $admin_id : 0
  403. ]);
  404. $orderGoodsList = OrderGoodsModel::where(['order_id' => $order['id']])->select()->toArray();
  405. //减少库存量
  406. if ($order['pay_type'] != 0) {
  407. foreach ($orderGoodsList as $val) {
  408. self::updateStock([
  409. "goods_id" => $val["goods_id"],
  410. "product_id" => $val["product_id"],
  411. "goods_nums" => $val["goods_nums"]
  412. ], "-");
  413. GoodsModel::where('id',$val["goods_id"])->save([ "sale"=>Db::raw("sale+1") ]);
  414. }
  415. }
  416. // 核销订单,购买后发货
  417. if($order["shipping_type"] == 2){
  418. foreach ($orderGoodsList as $val) {
  419. OrderGoodsModel::where(["id"=>$val["id"]])->save([
  420. "is_send" => 1,
  421. "distribution_id" => 0
  422. ]);
  423. }
  424. //更新发货状态
  425. OrderModel::where(['id'=>$order['id']])->save([
  426. 'distribution_status' => 1,
  427. 'send_time' =>time()
  428. ]);
  429. OrderLogModel::create([
  430. 'order_id' => $order['id'],
  431. 'username' => "system",
  432. 'action' => '发货',
  433. 'result' => '成功',
  434. 'note' => '订单【' . $order["order_no"] . '】由 system 发货',
  435. 'create_time' => time()
  436. ]);
  437. }
  438. $user = WechatUsersModel::where("user_id",$order["user_id"])->find();
  439. if(!empty($user["mp_openid"])){
  440. SubscribeMini::orderPaySuccess($user["mp_openid"],$order["id"]);
  441. }
  442. if(!empty($user["openid"])){
  443. SubscribeWechat::pay($user["openid"],$order["id"]);
  444. }
  445. return true;
  446. }
  447. /**
  448. * 订单完成
  449. */
  450. public static function complete($order_no,$admin_id=0){
  451. if(!$order = OrderModel::where(["order_no"=>$order_no])->find()){
  452. throw new \Exception("您要查找的订单不存在,请刷新重试!",0);
  453. }
  454. if(!$users = UsersModel::where(["id"=>$order["user_id"]])->find()){
  455. if($order['exp'] > 0){
  456. $log = '成功购买了订单号:' . $order['order_no'] . '中的商品,奖励经验' . $order['exp'];
  457. UsersModel::where(["id"=>$order["user_id"]])->inc("exp",$order['exp'])->update();
  458. UsersLogModel::create([
  459. "order_no"=>$order_no,
  460. "user_id"=>$order["user_id"],
  461. "admin_id"=> $admin_id ? $admin_id : 0,
  462. "action"=>2,
  463. "operation"=>0,
  464. "exp"=>$order['exp'],
  465. "description"=>$log,
  466. "create_time"=>time()
  467. ]);
  468. }
  469. if($order['point'] > 0){
  470. $log = '成功购买了订单号:' . $order['order_no'] . '中的商品,奖励积分' . $order['point'];
  471. UsersModel::where(["id"=>$order["user_id"]])->inc("point",$order['point'])->update();
  472. UsersLogModel::create([
  473. "order_no"=>$order_no,
  474. "user_id"=>$order["user_id"],
  475. "admin_id"=> $admin_id ? $admin_id : 0,
  476. "action"=>1,
  477. "operation"=>0,
  478. "point"=>$order['point'],
  479. "description"=>$log,
  480. "create_time"=>time()
  481. ]);
  482. }
  483. Spread::backBrokerage($order);
  484. }
  485. //获取此订单中的商品种类
  486. $orderList = OrderGoodsModel::where(['order_id'=>$order["id"]])->group('goods_id')->select()->toArray();
  487. $orderData = [
  488. "point"=>0,
  489. "describes"=>0,
  490. "service"=>0,
  491. "logistics"=>0,
  492. ];
  493. if($order["status"] == 5){
  494. $orderData["point"] = 5;
  495. $orderData["describes"] = 5;
  496. $orderData["service"] = 5;
  497. $orderData["logistics"] = 5;
  498. $orderData["comment_time"] = time();
  499. }
  500. //对每类商品进行评论开启
  501. foreach ($orderList as $val) {
  502. if (GoodsModel::where(['id'=>$val['goods_id']])->find()) {
  503. UsersCommentModel::create(array_merge($orderData,[
  504. 'goods_id' => $val['goods_id'],
  505. 'order_no' => $order['order_no'],
  506. 'user_id' => $order['user_id'],
  507. 'create_time' => time()
  508. ]));
  509. }
  510. }
  511. self::updateOrderGroupStatus($order,2);
  512. $user = WechatUsersModel::where("user_id",$order["user_id"])->find();
  513. if(!empty($user["mp_openid"])){
  514. SubscribeMini::orderComplete($user["mp_openid"],$order["id"]);
  515. }
  516. return true;
  517. }
  518. /**
  519. * 更新拼团订单状态
  520. * @param $order
  521. * @param $status
  522. * @param int $refundStatus
  523. * @return false
  524. */
  525. public static function updateOrderGroupStatus($order,$status,$refundStatus=0){
  526. if($order["type"] != 5){
  527. return false;
  528. }
  529. $condition = ["order_id"=>$order["id"],"user_id"=>$order["user_id"],"status"=>1,"is_refund"=>0];
  530. if($order["type"] == 5 && OrderGroupModel::where($condition)->count()){
  531. return OrderGroupModel::where($condition)->update([
  532. "is_refund"=>$refundStatus,"status"=>$status,"complete_time"=>time()
  533. ]);
  534. }
  535. return false;
  536. }
  537. /**
  538. * 更新库存
  539. * @return boolean
  540. */
  541. public static function updateStock($data, $type = "-") {
  542. if ($data["product_id"] > 0) {
  543. $product = GoodsItemModel::where([ "goods_id"=>$data["goods_id"],"id"=>$data["product_id"] ])->find();
  544. }
  545. $product_store = 0;
  546. $goods = GoodsModel::where(["id"=>$data["goods_id"]])->find();
  547. switch ($type) {
  548. case "-":
  549. if (!empty($product)) {
  550. $product_store = $product["store_nums"] - $data["goods_nums"];
  551. }
  552. $goods_store = $goods["store_nums"] - $data["goods_nums"];
  553. break;
  554. case "+":
  555. if (!empty($product)) {
  556. $product_store = $product["store_nums"] + $data["goods_nums"];
  557. }
  558. $goods_store = $goods["store_nums"] + $data["goods_nums"];
  559. break;
  560. }
  561. if ($data["product_id"] > 0) {
  562. GoodsItemModel::where([ "goods_id"=>$data["goods_id"],"id"=>$data["product_id"] ])->save(["store_nums" => $product_store]);
  563. }
  564. GoodsModel::where([ "id"=>$data["goods_id"]])->save(["store_nums" => $goods_store]);
  565. return true;
  566. }
  567. /**
  568. * 获取退款类型
  569. * @param $code
  570. * @return string
  571. */
  572. public static function getRefundmentText($code) {
  573. $result = ['0' => '申请退款', '1' => '退款失败', '2' => '退款成功'];
  574. return isset($result[$code]) ? $result[$code] : '';
  575. }
  576. public static function getOrderActive($order){
  577. if($order["pay_status"] == 0){
  578. return 0;
  579. }
  580. if($order["status"] == 2 && $order["distribution_status"] == 0){
  581. return 1;
  582. }else if($order["status"] == 2 && $order["distribution_status"]){
  583. return 2;
  584. }
  585. if($order["status"] == 5 && in_array($order["evaluate_status"],[0,2])){
  586. return 3;
  587. }
  588. if($order["status"] == 5 && $order["evaluate_status"] == 1){
  589. return 4;
  590. }
  591. return -1;
  592. }
  593. /**
  594. * 获取订单状态码
  595. * @param $order
  596. * @return int
  597. */
  598. public static function getStatus($order){
  599. if(empty($order)){
  600. return 0;
  601. }
  602. if($order["status"] == 1 && $order["pay_status"] == 0){ // 待付款
  603. return 1;
  604. }else if($order["status"] == 2){
  605. // 检查是否有退款
  606. $refund = OrderRefundmentModel::where([ "order_id"=>$order['id'],"is_delete"=>0 ])->find();
  607. if(!empty($refund)){
  608. if($refund["pay_status"] == 0){
  609. return 11;
  610. }
  611. if($refund["pay_status"] == 1){
  612. if($order["distribution_status"] == 1){
  613. return 3;
  614. }else if($order["distribution_status"] == 2){
  615. return 4;
  616. }
  617. return 12;
  618. }
  619. }
  620. if($order["distribution_status"] == 0){ // 待发货
  621. return 2;
  622. }else if($order["distribution_status"] == 1){ // 待收货
  623. return 3;
  624. }else if($order["distribution_status"] == 2){ // 部份发货
  625. return 4;
  626. }
  627. }
  628. if($order["status"] == 5){
  629. if(in_array($order["evaluate_status"],[0,2])){ // 待评价
  630. return 5;
  631. }else if($order["evaluate_status"] == 1){ // 已完成
  632. return 6;
  633. }
  634. }else if ($order['status'] == 3 || $order['status'] == 4) { // 取消或者作废订单
  635. return 7;
  636. }else if ($order['status'] == 6) { // 退款
  637. return 8;
  638. }else if ($order['status'] == 7) { // 部分退款
  639. if ($order['distribution_status'] == 1) { // 发货
  640. return 10;
  641. } else { // 未发货
  642. return 9;
  643. }
  644. }
  645. return 0;
  646. }
  647. /**
  648. * 获取订单状态
  649. * @param $code
  650. * @return string
  651. */
  652. public static function getStatusText($code) {
  653. $result = [
  654. 0=>'未知',1=>'等待付款',2=>'等待发货',3=>'待收货',4 => '部份发货',
  655. 5=>'待评价',6=>'已完成',7=>'已取消',8=>'退款成功',9=>'未发货',
  656. 10=>'部分退款',11=>'申请退款',12=>'拒绝退款'
  657. ];
  658. return isset($result[$code]) ? $result[$code] : '';
  659. }
  660. /**
  661. * 获取支付状态
  662. * @param $status
  663. * @return string
  664. */
  665. public static function getPaymentStatusText($status){
  666. return $status == 0 ? "未支付" : "已支付";
  667. }
  668. /**
  669. * 获取订单类型
  670. * @param $type
  671. * @param int $length
  672. * @return string
  673. */
  674. public static function getOrderTypeText($type,$length=-1) {
  675. switch ($type) {
  676. case "1":
  677. $string = '积分订单';
  678. break;
  679. case "2":
  680. $string = '团购订单';
  681. break;
  682. case "3":
  683. $string = "秒杀订单";
  684. break;
  685. case "5":
  686. $string = "拼团订单";
  687. break;
  688. case '4':
  689. default:
  690. $string = '普通订单';
  691. }
  692. return $length == -1 ? $string : CString::msubstr($string,$length,false);
  693. }
  694. /**
  695. * 获取订单发货状态
  696. * @param $code
  697. * @return string
  698. */
  699. public static function getSendStatus($code) {
  700. $data = [0 => '等待发货', 1 => '已发货', 2=>"部份发货", 3 => '已退货'];
  701. return isset($data[$code]) ? $data[$code] : '未配货';
  702. }
  703. /**
  704. * 获取订单类型num
  705. * @param string $type
  706. * @return int
  707. */
  708. public static function getOrderType($type=""){
  709. $arr = ["point"=>1,"regiment"=>2,"second"=>3,"special"=>4,"group"=>5,"buy"=>0,"cart"=>0];
  710. return isset($arr[$type]) ? $arr[$type] : 0;
  711. }
  712. }