截流自动化的商城平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

OrderLogic.php 84KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\basics\Logic;
  4. use app\common\enum\ClientEnum;
  5. use app\common\enum\FootprintEnum;
  6. use app\common\enum\FreightEnum;
  7. use app\common\enum\GoodsEnum;
  8. use app\common\enum\OrderEnum;
  9. use app\common\enum\OrderGoodsEnum;
  10. use app\common\enum\OrderInvoiceEnum;
  11. use app\common\enum\OrderLogEnum;
  12. use app\common\enum\PayEnum;
  13. use app\common\enum\ShopEnum;
  14. use app\common\enum\TeamEnum;
  15. use app\common\logic\OrderLogLogic;
  16. use app\common\logic\OrderRefundLogic;
  17. use app\common\model\Cart;
  18. use app\common\model\dev\DevRegion;
  19. use app\common\model\Freight;
  20. use app\common\model\goods\Goods;
  21. use app\common\model\Delivery;
  22. use app\common\model\goods\GoodsItem;
  23. use app\common\model\integral\IntegralOrder;
  24. use app\common\model\order\Order;
  25. use app\common\model\order\OrderGoods;
  26. use app\common\model\order\OrderLog;
  27. use app\common\model\order\OrderRefund;
  28. use app\common\model\order\OrderTrade;
  29. use app\common\model\Pay;
  30. use app\common\model\RechargeOrder;
  31. use app\common\model\shop\Shop;
  32. use app\common\model\coupon\Coupon;
  33. use app\common\model\coupon\CouponList;
  34. use app\common\model\team\TeamFound;
  35. use app\common\model\team\TeamJoin;
  36. use app\common\model\user\User;
  37. use app\common\model\user\UserLevel;
  38. use app\common\server\UrlServer;
  39. use app\common\model\seckill\SeckillGoods;
  40. use app\common\model\bargain\BargainLaunch;
  41. use app\common\model\user\UserAddress;
  42. use app\common\server\AreaServer;
  43. use app\common\server\ConfigServer;
  44. use app\common\server\JsonServer;
  45. use expressage\Kd100;
  46. use expressage\Kdniao;
  47. use think\Exception;
  48. use think\facade\Db;
  49. use think\facade\Env;
  50. /**
  51. * Class OrderLogic
  52. * @package app\api\logic
  53. */
  54. class OrderLogic extends Logic
  55. {
  56. public static $order_type = OrderEnum::NORMAL_ORDER;
  57. /**
  58. * @notes 下单
  59. * @param $post
  60. * @return array|false
  61. * @throws Exception
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\PDOException
  66. * @author suny
  67. * @date 2021/7/13 6:19 下午
  68. */
  69. public static function add($post)
  70. {
  71. // 商品类型 0普通 1虚拟
  72. $goods_type = input('goods_type/d', GoodsEnum::TYPE_ACTUAL);
  73. // 初始化支付方式
  74. $post['pay_way'] = 0;
  75. if (!empty($post['goods'])) {
  76. $goods = is_array($post['goods']) ? $post['goods'] : json_decode($post['goods'], true);
  77. } else {
  78. $where = [
  79. ['id', 'in', $post['cart_id']]
  80. ];
  81. $goods = Cart::where($where)
  82. ->field(['goods_id', 'item_id', 'shop_id', 'goods_num as num'])
  83. ->select()->toArray();
  84. }
  85. foreach ($goods as &$good) {
  86. $good['delivery_type'] = !isset($good['delivery_type']) ? GoodsEnum::DELIVERY_EXPRESS : $good['delivery_type'];
  87. }
  88. $post['goods'] = $goods;
  89. Db::startTrans();
  90. try {
  91. // 砍价订单验证
  92. if (isset($post['bargain_launch_id']) and $post['bargain_launch_id'] > 0) {
  93. self::$order_type = OrderEnum::BARGAIN_ORDER;
  94. $bargainLaunchModel = new BargainLaunch();
  95. $launch = $bargainLaunchModel->where(['id' => (int)$post['bargain_launch_id']])->find();
  96. if (!$launch) {
  97. throw new Exception('砍价异常');
  98. }
  99. if ($launch['status'] == 2) {
  100. throw new Exception('砍价失败,禁止下单');
  101. }
  102. if ($launch['payment_limit_time'] < time() and $launch['payment_limit_time'] > 0) {
  103. throw new Exception('下单失败,超出限制时间');
  104. }
  105. if ($launch['order_id'] > 0) {
  106. throw new Exception('您已下单了, 请勿重复操作');
  107. }
  108. }
  109. // 校验商品
  110. self::checkGoods($goods);
  111. $address = UserAddress::where('id', $post['address_id'] ?? 0)
  112. ->field('contact,telephone,province_id,city_id,district_id,address')
  113. ->find();
  114. if (empty($address) && $goods_type == GoodsEnum::TYPE_ACTUAL && $post['delivery_type'] == GoodsEnum::DELIVERY_EXPRESS) {
  115. throw new Exception('请选择地址');
  116. }
  117. // 校验发票信息 返回以店铺id为键,原发票参数为值的数组
  118. $invoice = OrderInvoiceLogic::checkOrderInvoice($post);
  119. if (false === $invoice) {
  120. throw new Exception(OrderInvoiceLogic::getError());
  121. }
  122. $order_trade_add = self::addOrderTrade($post, $address);
  123. $shop_goods = [];
  124. $order_goods_datas_insert = [];
  125. $order_log_datas_insert = [];
  126. foreach ($post['goods'] as $key => $value) { //按店铺区分商品
  127. $res = self::checkShop($value); //判断商家营业状态
  128. if ($res !== true) {
  129. throw new Exception($res);
  130. }
  131. $shop_goods[$value['shop_id']][] = $value;
  132. }
  133. foreach ($shop_goods as $key => $value) {
  134. foreach ($value as $val) {
  135. $seckill_goods_price = GoodsItem::isSeckill($val['item_id']);
  136. if ($seckill_goods_price != 0) {//是秒杀商品
  137. $sales_sum_res = self::setSeckillSaleSum($val['item_id'], $val['num']);
  138. if ($sales_sum_res !== true) {
  139. throw new Exception('秒杀商品销量设置失败');
  140. }
  141. }
  142. }
  143. $order_add = self::addOrder($order_trade_add, $value, $post, $key, $address);
  144. // 增加发票
  145. OrderInvoiceLogic::insertOrderInvoice($key, $post['user_id'], $order_add, $invoice);
  146. $order_log_add_data = self::getOrderLogData($key, $post['user_id'], $key);
  147. $order_log_datas_insert[] = $order_log_add_data;
  148. $order_goods_data = self::getOrderGoodsData($order_add, $value,$post['user_id']);
  149. $order_goods_datas_insert = array_merge($order_goods_datas_insert, $order_goods_data);
  150. }
  151. // 订单日志
  152. OrderLog::insertAll($order_log_datas_insert);
  153. // 订单商品
  154. OrderGoods::insertAll($order_goods_datas_insert);
  155. //商品库存减少
  156. self::subGoodsStock($post['goods']);
  157. //购物车删除
  158. if (isset($post['cart_id']) && $post['cart_id'] != 0) {
  159. self::delCart($post['cart_id']);
  160. }
  161. // 砍价订单处理
  162. if (isset($post['bargain_launch_id']) and $post['bargain_launch_id'] > 0) {
  163. $bargainLaunchModel = new BargainLaunch();
  164. $bargainLaunchModel->where(['id' => (int)$post['bargain_launch_id']])
  165. ->update(['order_id' => $order_add, 'status' => 1]);
  166. }
  167. // 检测包邮活动
  168. $orders = Db::name('order')->where('trade_id', $order_trade_add)->select()->toArray();
  169. foreach($orders as $item) {
  170. // 虚拟商品直接包邮
  171. $is_free = true;
  172. if ($goods_type == GoodsEnum::TYPE_ACTUAL) {
  173. $is_free = self::isFreeShipping($item['shop_id'], $item['goods_price'] - $item['discount_amount'], [
  174. 'province_id' => $item['province'],
  175. 'city_id' => $item['city'],
  176. 'district_id' => $item['district'],
  177. ]);
  178. }
  179. // 符合包邮条件,去邮费
  180. if($is_free) {
  181. Db::name('order')->where('id', $item['id'])->update([
  182. 'order_amount' => $item['order_amount'] - $item['shipping_price'],
  183. 'total_amount' => $item['total_amount'] - $item['shipping_price'],
  184. 'shipping_price' => 0,
  185. ]);
  186. }
  187. }
  188. // 更新主订单金额
  189. $newOrderAmount = Db::name('order')->where('trade_id', $order_trade_add)->sum('order_amount');
  190. $newTotalAmount = Db::name('order')->where('trade_id', $order_trade_add)->sum('total_amount');
  191. Db::name('order_trade')->where('id', $order_trade_add)->update([
  192. 'order_amount' => $newOrderAmount,
  193. 'total_amount' => $newTotalAmount,
  194. ]);
  195. Db::commit();
  196. return ['trade_id' => $order_trade_add, 'order_id' => $order_add, 'type' => 'trade'];
  197. } catch (Exception $e) {
  198. Db::rollback();
  199. self::$error = $e->getMessage();
  200. return false;
  201. }
  202. }
  203. /**
  204. * @notes 结算页数据
  205. * @param $post
  206. * @return array|false
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\DbException
  209. * @throws \think\db\exception\ModelNotFoundException
  210. * @author suny
  211. * @date 2021/7/13 6:19 下午
  212. */
  213. public static function settlement($post)
  214. {
  215. // 商品类型 0普通 1虚拟
  216. $goods_type = input('goods_type/d', GoodsEnum::TYPE_ACTUAL);
  217. if (!empty($post['goods'])) {
  218. $goods = json_decode($post['goods'], true);
  219. $post['goods'] = $goods;
  220. } else {
  221. $where = [[
  222. 'id', 'in', $post['cart_id']]
  223. ];
  224. $post['goods'] = $goods = Cart::where($where)
  225. ->field(['goods_id', 'item_id', 'shop_id', 'goods_num as num'])
  226. ->select()->toArray();
  227. }
  228. // 检查店铺营业状态
  229. foreach($post['goods'] as $good) {
  230. $shop = Shop::field('name,expire_time,is_run,is_freeze')->where(['del' => 0, 'id' => $good['shop_id']])->findOrEmpty();
  231. if($shop->isEmpty()) {
  232. self::$error = '部分商品所属店铺不存在';
  233. return false;
  234. }
  235. // 获取原始数据(不经获取器)
  236. $shop = $shop->getData();
  237. if(!empty($shop['expire_time']) && ($shop['expire_time'] <= time())) {
  238. self::$error = '部分商品所属店铺已到期';
  239. return false;
  240. }
  241. if($shop['is_freeze']) {
  242. self::$error = '部分商品所属店铺已被冻结';
  243. return false;
  244. }
  245. if(!$shop['is_run']) {
  246. self::$error = '部分商品所属店铺暂停营业中';
  247. return false;
  248. }
  249. if ($good['num'] <= 0) {
  250. self::$error = '商品数量不能小于0';
  251. return false;
  252. }
  253. }
  254. $Goods = new Goods();
  255. $GoodsItem = new GoodsItem();
  256. $Shop = new Shop();
  257. if (isset($post['address_id']) && !empty($post['address_id'])) {
  258. $where = [
  259. 'id' => $post['address_id'],
  260. 'del' => 0,
  261. ];
  262. $address = UserAddress::where($where)
  263. ->field('id,contact,telephone,province_id,city_id,district_id,address')
  264. ->find()->toArray();
  265. } else {
  266. $address = UserAddress::where(['user_id' => $post['user_id'], 'is_default' => 1,'del' => 0])
  267. ->field('id,contact,telephone,province_id,city_id,district_id,address')
  268. ->find();
  269. }
  270. if (!empty($address) && $goods_type == GoodsEnum::TYPE_ACTUAL ) {
  271. $address['province'] = AreaServer::getAddress($address['province_id']);
  272. $address['city'] = AreaServer::getAddress($address['city_id']);
  273. $address['district'] = AreaServer::getAddress($address['district_id']);
  274. } else {
  275. $address = [];
  276. }
  277. // 校验发票信息 返回以店铺id为键,原发票参数为值的数组
  278. $invoice = OrderInvoiceLogic::checkOrderInvoice($post);
  279. if (false === $invoice) {
  280. self::$error = OrderInvoiceLogic::getError();
  281. return false;
  282. }
  283. $shop = [];
  284. foreach ($goods as &$good) {
  285. $goods_item = $GoodsItem->alias('gi')
  286. ->join('goods g', 'g.id = gi.goods_id')
  287. ->where(['gi.id' => $good['item_id'], 'gi.goods_id' => $good['goods_id']])
  288. ->field('gi.price,gi.spec_value_str,gi.image,g.image,g.name as goods_name, g.type as goods_type, g.delivery_type as delivery_types')
  289. ->find()
  290. ->toArray();
  291. $good['type'] = $goods_item['goods_type'];
  292. $good['name'] = $goods_item['goods_name'];
  293. $good['price'] = $goods_item['price'];
  294. $good['original_price'] = $goods_item['price'];
  295. $good['spec_value'] = $goods_item['spec_value_str'];
  296. $good['image'] = empty($goods_item['image']) ? $goods_item['image'] : $goods_item['image'];
  297. $good['image'] = UrlServer::getFileUrl($good['image']);
  298. $good['delivery_types'] = $goods_item['delivery_types'] ? explode(',', $goods_item['delivery_types']) : [];
  299. $good['delivery_type'] = $good['delivery_type'] ?? GoodsEnum::DELIVERY_EXPRESS;
  300. // 配送列表
  301. if ($good['type'] == GoodsEnum::TYPE_VIRTUAL) {
  302. // 虚拟商品, 配送方式为 虚拟发货
  303. $shop[$good['shop_id']]['delivery_types'][] = GoodsEnum::DELIVERY_VIRTUAL;
  304. } else {
  305. // 快递和自提
  306. foreach ($good['delivery_types'] as $ko => $vo) {
  307. $shop[$good['shop_id']]['delivery_types'][] = (int) $vo;
  308. }
  309. }
  310. unset($good['delivery_types']);
  311. }
  312. foreach ($shop as $shop_id => $shops_info) {
  313. $shop[$shop_id]['delivery_types_arr'] = GoodsEnum::getDeliveryLists($shops_info['delivery_types']);
  314. }
  315. foreach ($goods as $key => &$goods_info) {
  316. if (! in_array($goods_info['delivery_type'], $shop[$goods_info['shop_id']]['delivery_types'])) {
  317. $goods_info['delivery_type'] = $shop[$goods_info['shop_id']]['delivery_types_arr'][0]['delivery_type'];
  318. }
  319. $shop[$goods_info['shop_id']]['delivery_type'] = $goods_info['delivery_type'];
  320. $shop[$goods_info['shop_id']]['delivery_type_text'] = GoodsEnum::getDeliveryTypeDesc($goods_info['delivery_type']);
  321. }
  322. foreach ($goods as $key => $value) { //按店铺区分商品
  323. $shop_data = $Shop->where('id', $value['shop_id'])->find();
  324. $shop[$value['shop_id']]['shop_id'] = $value['shop_id'];
  325. $shop[$value['shop_id']]['shop_name'] = $shop_data['name'];
  326. $shop[$value['shop_id']]['open_invoice'] = $shop_data['open_invoice']; // 发票开关
  327. $shop[$value['shop_id']]['spec_invoice'] = $shop_data['spec_invoice']; // 是否支持专票
  328. // 经营信息
  329. $shop[$value['shop_id']]['run_start_time'] = $shop_data['run_start_time'] ? date('H:i:s', $shop_data['run_start_time']) : '';
  330. $shop[$value['shop_id']]['run_end_time'] = $shop_data['run_end_time'] ? date('H:i:s', $shop_data['run_end_time']) : '';
  331. $shop[$value['shop_id']]['weekdays'] = $shop_data['weekdays'];
  332. // 店铺地址
  333. $shop[$value['shop_id']]['province'] = DevRegion::getAreaName($shop_data['province_id']);
  334. $shop[$value['shop_id']]['city'] = DevRegion::getAreaName($shop_data['city_id']);
  335. $shop[$value['shop_id']]['district'] = DevRegion::getAreaName($shop_data['district_id']);
  336. $shop[$value['shop_id']]['address'] = $shop_data['address'];
  337. $array = $post;
  338. $array['shop_id'] = $value['shop_id'];
  339. $shop_coupon_list = CouponLogic::getShopCouponList($array);
  340. $shop[$value['shop_id']]['coupon_list'] = $shop_coupon_list['suit'];
  341. //是否为秒杀
  342. $value['is_seckill'] = 0;
  343. $seckill_goods_price = GoodsItem::isSeckill($value['item_id']);
  344. if ($seckill_goods_price != 0) {
  345. $value['price'] = $seckill_goods_price;
  346. $value['is_seckill'] = 1;
  347. self::$order_type = OrderEnum::SECKILL_ORDER; //秒杀订单
  348. }
  349. // 如果是砍价的商品,则替换信息
  350. if (isset($post['bargain_launch_id']) and $post['bargain_launch_id'] > 0) {
  351. $bargainLaunchModel = new BargainLaunch();
  352. $launch = $bargainLaunchModel->field(true)
  353. ->where(['id' => (int)$post['bargain_launch_id']])
  354. ->find();
  355. $bargainImage = $launch['goods_snap']['image'] == '' ? $launch['goods_snap']['goods_iamge'] : $launch['goods_snap']['image'];
  356. $value['goods_name'] = $launch['goods_snap']['name'];
  357. $value['image_str'] = UrlServer::getFileUrl($bargainImage);
  358. $value['price'] = $launch['current_price'];
  359. $value['spec_value_str'] = $launch['goods_snap']['spec_value_str'];
  360. self::$order_type = OrderEnum::BARGAIN_ORDER;//砍价订单
  361. }
  362. $shop[$value['shop_id']]['goods'][] = $value;
  363. $shop[$value['shop_id']]['shipping_price'] = 0;
  364. if ($shop[$value['shop_id']]['delivery_type'] == GoodsEnum::DELIVERY_EXPRESS) {
  365. $shop[$value['shop_id']]['shipping_price'] = self::calculateFreight($shop[$value['shop_id']]['goods'], $address);
  366. }
  367. $user = User::where('id', $post['user_id'])->find();
  368. $discount = UserLevel::where(['id' => $user['level'],'del' => 0])->value('discount');
  369. // 普通订单才参与会员价
  370. if (self::$order_type == OrderEnum::NORMAL_ORDER) {
  371. if($discount == 0){
  372. $discount = 10;
  373. }
  374. $shop[$value['shop_id']]['total_amount'] = round(self::calculateGoodsPrice($shop[$value['shop_id']]['goods'],$discount) + $shop[$value['shop_id']]['shipping_price'], 2);
  375. } else {
  376. $shop[$value['shop_id']]['total_amount'] = round(round($value['price'] * $value['num'], 2) + $shop[$value['shop_id']]['shipping_price'], 2);
  377. }
  378. //优惠券
  379. $discount_amount = 0;
  380. if (isset($post['coupon_id']) && !empty($post['coupon_id'])) {
  381. $result = self::checkCoupon($post['coupon_id'], $value['shop_id']);
  382. if ($result) {
  383. $discount_amount = self::getDiscountAmount($post['coupon_id'], $value['shop_id'])['money'] ?? 0;
  384. }
  385. }
  386. $shop[$value['shop_id']]['discount_amount'] = $discount_amount;
  387. if ($shop[$value['shop_id']]['total_amount'] > $discount_amount) {
  388. $shop[$value['shop_id']]['total_amount'] = round($shop[$value['shop_id']]['total_amount'] - $discount_amount, 2);
  389. } else { //优惠金额大于当前商品总价,总价为0
  390. $shop[$value['shop_id']]['discount_amount'] = $shop[$value['shop_id']]['total_amount'];
  391. $shop[$value['shop_id']]['total_amount'] = 0;
  392. }
  393. $num = 0;
  394. foreach ($shop[$value['shop_id']]['goods'] as &$item) {
  395. $is_member = Goods::where('id',$item['goods_id'])->value('is_member');
  396. $price_sum = round($item['price'] * $item['num'],2);
  397. // 商品参与会员价 并且订单是 普通订单
  398. if ($is_member && self::$order_type == OrderEnum::NORMAL_ORDER) {
  399. $item['is_member'] = 1;
  400. } else {
  401. $item['is_member'] = 0;
  402. }
  403. if($item['is_member']) {
  404. $member_amount = max(round($item['price']*$discount/10,2), 0.01);
  405. $item['member_amount'] = $member_amount;
  406. $price_sum = round($member_amount * $item['num'],2);
  407. }
  408. $item['sum_price'] = $price_sum;
  409. $num += $item['num'];
  410. }
  411. $shop[$value['shop_id']]['total_num'] = $num;
  412. }
  413. foreach ($shop as $ko => $shop_info) {
  414. unset($shop[$ko]['delivery_types']);
  415. }
  416. $shop = array_values($shop);
  417. $total_amount = array_sum(array_column($shop, 'total_amount'));
  418. $orders['address'] = $address;
  419. $orders['shop'] = $shop;
  420. $orders['order_type'] = self::$order_type;
  421. $orders['total_amount'] = $total_amount;
  422. $orders['pay_way_text'] = "微信支付";
  423. $orders['pay_way'] = PayEnum::WECHAT_PAY;
  424. // 检验是否参与包邮活动,若参与去除邮费
  425. $orders = self::checkFreeShipping($orders);
  426. // 重新算总价
  427. $orders['total_amount'] = array_sum(array_column($orders['shop'], 'total_amount'));
  428. $orders['total_amount'] = round($orders['total_amount'], 2);
  429. $orders['invoice'] = array_values($invoice);
  430. return $orders;
  431. }
  432. /**
  433. * @notes 校验是否符合包邮条件
  434. * @param $orders
  435. * @author Tab
  436. * @date 2021/8/31 15:11
  437. */
  438. public static function checkFreeShipping($orders)
  439. {
  440. if (empty($orders['address'])) {
  441. return $orders;
  442. }
  443. $address = $orders['address'];
  444. foreach($orders['shop'] as &$item) {
  445. if (self::isFreeShipping($item['shop_id'], ($item['total_amount'] - $item['shipping_price']), $address)) {
  446. // 原总价格已算上邮费需减掉
  447. $item['total_amount'] = $item['total_amount'] - $item['shipping_price'];
  448. // 符合包邮条件,去邮费
  449. $item['shipping_price'] = 0;
  450. }
  451. }
  452. return $orders;
  453. }
  454. /**
  455. * @notes 是否满足包邮条件
  456. * @param $shopId
  457. * @param $orderAmount
  458. * @param $address
  459. * @return bool
  460. * @author Tab
  461. * @date 2021/8/31 15:42
  462. */
  463. public static function isFreeShipping($shopId, $orderAmount, $address)
  464. {
  465. $config = Db::name('free_shipping_config')->where([
  466. 'shop_id' => $shopId,
  467. 'del' => 0
  468. ])->findOrEmpty();
  469. if (empty($config) || $config['status'] == 0) {
  470. // 未设置 或 未开启包邮活动
  471. return false;
  472. }
  473. // 校验区级设置
  474. $district = Db::name('free_shipping_region')->where([
  475. 'shop_id' => $shopId,
  476. 'del' => 0
  477. ])->whereFindInSet('region', $address['district_id'])->findOrEmpty();
  478. if (!empty($district) && $orderAmount >= $district['order_amount']) {
  479. // 符合包邮条件
  480. return true;
  481. }
  482. if (!empty($district) && $orderAmount < $district['order_amount']) {
  483. // 不符合条件,不再校验市级
  484. return false;
  485. }
  486. // 校验市级设置
  487. $city = Db::name('free_shipping_region')->where([
  488. 'shop_id' => $shopId,
  489. 'del' => 0
  490. ])->whereFindInSet('region', $address['city_id'])->findOrEmpty();
  491. if (!empty($city) && $orderAmount >= $city['order_amount']) {
  492. // 符合包邮条件
  493. return true;
  494. }
  495. if (!empty($city) && $orderAmount < $city['order_amount']) {
  496. // 不符合条件,不再校验省级
  497. return false;
  498. }
  499. // 校验省级设置
  500. $province = Db::name('free_shipping_region')->where([
  501. 'shop_id' => $shopId,
  502. 'del' => 0
  503. ])->whereFindInSet('region', $address['province_id'])->findOrEmpty();
  504. if (!empty($province) && $orderAmount >= $province['order_amount']) {
  505. // 符合包邮条件
  506. return true;
  507. }
  508. if (!empty($province) && $orderAmount < $province['order_amount']) {
  509. // 不符合条件,不再校验全国设置
  510. return false;
  511. }
  512. // 校验全国设置
  513. $all = Db::name('free_shipping_region')->where([
  514. 'shop_id' => $shopId,
  515. 'del' => 0,
  516. 'region' => 'all'
  517. ])->findOrEmpty();
  518. if (!empty($all) && $orderAmount >= $all['order_amount']) {
  519. // 符合包邮条件
  520. return true;
  521. }
  522. return false;
  523. }
  524. /**
  525. * @notes 获取优惠金额
  526. * @param $coupon_id
  527. * @return int|mixed
  528. * @throws \think\db\exception\DataNotFoundException
  529. * @throws \think\db\exception\DbException
  530. * @throws \think\db\exception\ModelNotFoundException
  531. * @author suny
  532. * @date 2021/7/13 6:19 下午
  533. */
  534. public static function getDiscountAmount($coupon_id, $shopId = null)
  535. {
  536. $result = [
  537. 'money' => 0,
  538. 'shop_id' => $shopId,
  539. 'coupon_list_id' => 0,
  540. ];
  541. if (!isset($coupon_id) || empty($coupon_id)) {
  542. return $result;
  543. }
  544. //优惠金额
  545. foreach ($coupon_id as $item) {
  546. $Coupon_list = CouponList::where('id', $item)->find();
  547. if (is_null($shopId)) {
  548. $where = ['id' => $Coupon_list['coupon_id'], 'del' => 0];
  549. } else {
  550. $where = ['id' => $Coupon_list['coupon_id'], 'del' => 0, 'shop_id' => $shopId];
  551. }
  552. $coupon = Coupon::where($where)
  553. ->find();
  554. if (!empty($coupon)) {
  555. $result['money'] = $coupon['money'];
  556. $result['shop_id'] = $shopId;
  557. $result['coupon_list_id'] = $item;
  558. }
  559. }
  560. return $result;
  561. }
  562. /**
  563. * @notes shop优惠券
  564. * @param $coupon_ids
  565. * @param $shop_id
  566. * @return bool
  567. * @throws \think\db\exception\DataNotFoundException
  568. * @throws \think\db\exception\DbException
  569. * @throws \think\db\exception\ModelNotFoundException
  570. * @author suny
  571. * @date 2021/7/13 6:20 下午
  572. */
  573. public static function checkCoupon($coupon_ids, $shop_id)
  574. {
  575. $coupons = CouponList::where([['id', 'in', $coupon_ids]])->select()->toArray();
  576. if ($coupons) {
  577. foreach ($coupons as $item) {
  578. $coupon_id = $item['coupon_id'];
  579. $where = [
  580. 'id' => $coupon_id
  581. ];
  582. $result = Coupon::where($where)->value('shop_id');
  583. if ($shop_id == $result) {
  584. return true;
  585. }
  586. }
  587. } else {
  588. return false;
  589. }
  590. }
  591. /**
  592. * @notes 检查商品库存
  593. * @param $goods
  594. * @return bool
  595. * @throws \think\db\exception\DataNotFoundException
  596. * @throws \think\db\exception\DbException
  597. * @throws \think\db\exception\ModelNotFoundException
  598. * @author suny
  599. * @date 2021/7/13 6:20 下午
  600. */
  601. public static function checkGoods($goods)
  602. {
  603. if (!is_array($goods)) {
  604. throw new Exception('商品数据格式不正确');
  605. }
  606. // 商品信息
  607. $item_ids = array_column($goods, 'item_id');
  608. $field = 'i.id as item_id,g.id as goods_id,g.name,g.del,g.status,i.stock,
  609. g.delivery_type as goods_delivery,s.delivery_type as shop_delivery';
  610. $goodsData = (new Goods())->alias('g')
  611. ->join('goods_item i','i.goods_id = g.id')
  612. ->join('shop s','s.id = g.shop_id')
  613. ->field($field)
  614. ->whereIn('i.id', $item_ids)
  615. ->select()->toArray();
  616. $goodsData = array_column($goodsData, null, 'item_id');
  617. $shopData = Shop::whereIn('id', array_column($goods, 'shop_id'))->column('id,name', 'id');
  618. foreach ($goods as $key => $item) {
  619. if (!isset($goodsData[$item['item_id']])) {
  620. continue;
  621. }
  622. $goodsInfo = $goodsData[$item['item_id']];
  623. $goodsName = text_out_hidden($goodsInfo['name'], 8);
  624. $shopInfo = $shopData[$item['shop_id']] ?? [];
  625. $shopName = $shopInfo['name'] ?? '商家';
  626. if ($goodsInfo['del'] || !$goodsInfo['status']) {
  627. throw new Exception($goodsName . '商品不存在/未上架');
  628. }
  629. if ($item['num'] <= 0) {
  630. throw new Exception('请选择商品' . $goodsName . '数量');
  631. }
  632. if ($goodsInfo['stock'] < $item['num']) {
  633. throw new Exception($goodsName . '商品库存不足');
  634. }
  635. // 校验配送方式
  636. if ($item['delivery_type'] == GoodsEnum::DELIVERY_EXPRESS) {
  637. // 快递发货-商家设置
  638. if (!in_array(ShopEnum::DELIVERY_EXPRESS, explode(',', $goodsInfo['shop_delivery']))) {
  639. throw new Exception( '商家(' . $shopName . ')未开启快递配送');
  640. }
  641. // 商品设置
  642. if (!in_array(GoodsEnum::DELIVERY_EXPRESS, explode(',', $goodsInfo['goods_delivery']))) {
  643. throw new Exception('商品('. $goodsName . ')暂不支持快递配送');
  644. }
  645. }
  646. if ($item['delivery_type'] == GoodsEnum::DELIVERY_SELF) {
  647. // 线下自提-商家设置
  648. if (!in_array(ShopEnum::DELIVERY_SELF, explode(',', $goodsInfo['shop_delivery']))) {
  649. throw new Exception( '商家(' . $shopName . ')未开启线下自提');
  650. }
  651. // 商品设置
  652. if (!in_array(GoodsEnum::DELIVERY_SELF, explode(',', $goodsInfo['goods_delivery']))) {
  653. throw new Exception( '商品('. $goodsName .')暂不支持线下自提');
  654. }
  655. }
  656. }
  657. return true;
  658. }
  659. /**
  660. * @notes 计算商品总价格
  661. * @param $goods
  662. * @return false|float|int
  663. * @author suny
  664. * @date 2021/7/13 6:20 下午
  665. */
  666. public static function calculateGoodsPrice($goods,$discount)
  667. {
  668. if (!is_array($goods)) {
  669. return false;
  670. }
  671. $GoodsItem = new GoodsItem();
  672. $all_goods_price = 0;
  673. foreach ($goods as $key => $value) {
  674. $goods_price = $GoodsItem->sumGoodsPrice($value['goods_id'], $value['item_id'], $value['num'],$discount);
  675. $all_goods_price = round($goods_price + $all_goods_price, 2);
  676. }
  677. return $all_goods_price;
  678. }
  679. /**
  680. * @notes 计算会员折扣金额
  681. * @param $goods
  682. * @param $discount
  683. * @return false|float|int
  684. * @author suny
  685. * @date 2021/9/7 4:42 下午
  686. */
  687. public static function memberGoodsPrice($goods,$discount)
  688. {
  689. if (!is_array($goods)) {
  690. return false;
  691. }
  692. $GoodsItem = new GoodsItem();
  693. $all_goods_price = 0;
  694. foreach ($goods as $key => $value) {
  695. $goods_price = $GoodsItem->sumMemberPrice($value['goods_id'], $value['item_id'], $value['num'],$discount);
  696. $all_goods_price = round($goods_price + $all_goods_price, 2);
  697. }
  698. return $all_goods_price;
  699. }
  700. /**
  701. * @notes 根据goods计算商品总运费
  702. * @param $goodsList
  703. * @param $address
  704. * @return false|int|string
  705. * @throws \think\db\exception\DataNotFoundException
  706. * @throws \think\db\exception\DbException
  707. * @throws \think\db\exception\ModelNotFoundException
  708. * @author suny
  709. * @date 2021/7/13 6:20 下午
  710. */
  711. public static function calculateFreight($goodsList, $address)
  712. {
  713. if (!is_array($goodsList)) {
  714. return 0;
  715. }
  716. $templateList = [];
  717. $freight = 0;
  718. if (empty($address)) {
  719. return $freight;
  720. }
  721. foreach ($goodsList as $key => $goods) {
  722. // 不是快递配送时不计算
  723. if ($goods['delivery_type'] != GoodsEnum::DELIVERY_EXPRESS) {
  724. continue;
  725. }
  726. $express = (new Goods())->getExpressType($goods['goods_id']);
  727. switch ($express['express_type']) {
  728. // 统一运费
  729. case 2:
  730. $price = $express['express_money'] * $goods['num'];
  731. $freight = round(($freight + $price), 2);
  732. break;
  733. // 运费模板
  734. case 3:
  735. $templateList[$express['express_template_id']][] = $goods;
  736. break;
  737. default:
  738. break;
  739. }
  740. }
  741. foreach ($templateList as $templateId => $templateGoods) {
  742. $total_weight = 0;
  743. $total_volume = 0;
  744. $total_num = 0;
  745. foreach ($templateGoods as $goodsInfo) {
  746. $goods_item = (new GoodsItem())->where('id', $goodsInfo['item_id'])->field('stock,volume,weight')->find()->toArray();
  747. $total_weight = bcadd(($goods_item['weight'] ? : 0) * $goodsInfo['num'], $total_weight, 3);
  748. $total_volume = bcadd(($goods_item['volume'] ? : 0) * $goodsInfo['num'], $total_volume, 3);
  749. $total_num += $goodsInfo['num'];
  750. }
  751. $freightInfo = Freight::findOrEmpty($templateId);
  752. switch ($freightInfo['charge_way']) {
  753. // 重量
  754. case FreightEnum::CHARGE_WAY_WEIGHT:
  755. $nums = $total_weight;
  756. break;
  757. // 体积
  758. case FreightEnum::CHARGE_WAY_VOLUME:
  759. $nums = $total_volume;
  760. break;
  761. // 件数
  762. case FreightEnum::CHARGE_WAY_PIECE:
  763. $nums = $total_num;
  764. break;
  765. default:
  766. continue 2;
  767. }
  768. $price = (new Freight())->sumFreight($address, $templateId, $nums);
  769. $freight = round(($freight + $price), 2);
  770. }
  771. return $freight;
  772. }
  773. /**
  774. * @notes 添加父订单
  775. * @param $post
  776. * @param $address
  777. * @return false|mixed
  778. * @throws \think\db\exception\DataNotFoundException
  779. * @throws \think\db\exception\DbException
  780. * @throws \think\db\exception\ModelNotFoundException
  781. * @throws \think\exception\DbException
  782. * @author suny
  783. * @date 2021/7/13 6:20 下午
  784. */
  785. public static function addOrderTrade($post, $address)
  786. {
  787. $OrderTrade = new OrderTrade();
  788. $order_amount = 0;
  789. $total_amount = 0;
  790. //计算商品总价格
  791. $user = User::where('id', $post['user_id'])->find();
  792. $discount = UserLevel::where('id', $user['level'])->value('discount');
  793. if($discount == 0 || self::$order_type != OrderEnum::NORMAL_ORDER){
  794. $discount = 10;
  795. }
  796. $all_goods_price = self::calculateGoodsPrice($post['goods'],$discount);
  797. //计算商品运费
  798. $all_freight = self::calculateFreight($post['goods'], $address);
  799. $total_amount = $all_goods_price + $all_freight;
  800. //计算优惠券优惠的金额
  801. $discount_amount = 0;
  802. if (isset($post['coupon_id'])) {
  803. foreach ($post['goods'] as $value) {
  804. $discount_amount += self::getDiscountAmount($post['coupon_id'], $value['shop_id'])['money'] ?? 0;
  805. }
  806. }
  807. if ($total_amount > $discount_amount) {
  808. $total_amount = round(($total_amount - $discount_amount), 2);
  809. } else {
  810. //优惠金额大于当前商品总价,总价为0
  811. $discount_amount = $total_amount;
  812. $total_amount = 0;
  813. }
  814. // 砍价订单
  815. if (isset($post['bargain_launch_id']) and $post['bargain_launch_id'] > 0) {
  816. foreach ($post['goods'] as $goods) {
  817. $bargainLaunchModel = new BargainLaunch();
  818. $launch = $bargainLaunchModel->field(true)
  819. ->where(['id' => (int)$post['bargain_launch_id']])
  820. ->find();
  821. $total_amount = round($launch['current_price'] * $goods['num'], 2);
  822. }
  823. }
  824. // 记录访问足迹
  825. event('Footprint', [
  826. 'type' => FootprintEnum::PLACE_ORDER,
  827. 'user_id' => $post['user_id'],
  828. 'total_money' => $total_amount
  829. ]);
  830. $trade_order_data = [];
  831. $trade_order_data['t_sn'] = createSn('order_trade', 't_sn');
  832. // 拿shop_id,连接成字符串存入order_trade表shop_id中
  833. $shop_id = '';
  834. foreach ($post['goods'] as $key => $value) {
  835. $shop_id .= ',' . $value['shop_id'];
  836. }
  837. $shop_id = substr($shop_id, 1);
  838. $trade_order_data['shop_id'] = $shop_id;
  839. $trade_order_data['user_id'] = $post['user_id'];
  840. $trade_order_data['goods_price'] = $all_goods_price;
  841. $trade_order_data['order_amount'] = $total_amount;
  842. $trade_order_data['total_amount'] = $total_amount;
  843. $trade_order_data['discount_amount'] = $discount_amount;
  844. $trade_order_data['create_time'] = time();
  845. $order_trade_create = $OrderTrade->create($trade_order_data);
  846. if (false === $order_trade_create) {
  847. return false;
  848. }
  849. return $order_trade_create->id;
  850. }
  851. /**
  852. * @notes 添加子订单
  853. * @param $order_id
  854. * @param $goods
  855. * @param $post
  856. * @param $shop_id
  857. * @param $address
  858. * @return false|mixed
  859. * @throws \think\db\exception\DataNotFoundException
  860. * @throws \think\db\exception\DbException
  861. * @throws \think\db\exception\ModelNotFoundException
  862. * @throws \think\exception\DbException
  863. * @author suny
  864. * @date 2021/7/13 6:21 下午
  865. */
  866. public static function addOrder($order_id, $goods, $post, $shop_id, $address)
  867. {
  868. $Order = new Order();
  869. $remarks = isset($post['remark']) ? json_decode($post['remark'], true) : '';
  870. if ($remarks != '') {
  871. foreach ($remarks as $key => $value) {
  872. $user_remark[$value['shop_id']] = $value['remark'];
  873. }
  874. if (array_key_exists($shop_id, $user_remark)) {
  875. $remark = $user_remark[$shop_id];
  876. } else $remark = '';
  877. } else {
  878. $remark = $remarks;
  879. }
  880. $user = User::where('id', $post['user_id'])->find();
  881. $discount = UserLevel::where('id', $user['level'])->value('discount');
  882. if($discount == 0 || self::$order_type != OrderEnum::NORMAL_ORDER){
  883. $discount = 10;
  884. }
  885. $goods_price = self::calculateGoodsPrice($goods,$discount); //商品价格
  886. $member_amount = self::memberGoodsPrice($goods,$discount); //会员优惠价格
  887. $shipping_price = self::calculateFreight($goods, $address);
  888. //计算优惠券优惠的金额
  889. $coupon_list_id = 0;
  890. $discount_amount = 0;
  891. if (isset($post['coupon_id'])) {
  892. $discount = self::getDiscountAmount($post['coupon_id'], $shop_id);
  893. $discount_amount = $discount['money'];
  894. $coupon_list_id = $discount['coupon_list_id'];
  895. }
  896. $order_amount = $goods_price + $shipping_price;
  897. // 优惠金额大于实际支付的时候
  898. $discount_amount = min($order_amount, $discount_amount);
  899. $order_amount = $order_amount - $discount_amount;
  900. $total_amount = $order_amount;
  901. // 砍价订单
  902. if (isset($post['bargain_launch_id']) and $post['bargain_launch_id'] > 0) {
  903. foreach ($post['goods'] as $post_goods) {
  904. $bargainLaunchModel = new BargainLaunch();
  905. $launch = $bargainLaunchModel->field(true)
  906. ->where(['id' => (int)$post['bargain_launch_id']])
  907. ->find();
  908. $order_amount = $total_amount = round($launch['current_price'] * $post_goods['num'], 2);
  909. }
  910. }
  911. $order_data = [];
  912. $order_data['trade_id'] = $order_id;
  913. $order_data['shop_id'] = $shop_id;
  914. $order_data['user_id'] = $post['user_id'];
  915. $order_data['order_sn'] = createSn('order_trade', 't_sn');
  916. $order_data['order_type'] = self::$order_type;
  917. $order_data['order_source'] = $post['client'];
  918. $order_data['order_status'] = OrderEnum::ORDER_STATUS_NO_PAID;
  919. $order_data['pay_status'] = OrderEnum::PAY_STATUS_NO_PAID;
  920. $order_data['pay_way'] = $post['pay_way'];
  921. $order_data['delivery_type'] = OrderEnum::getChangeDeliveryTypeItem($goods[0]['delivery_type'] ?? 0);
  922. $order_data['aftersale_status'] = OrderEnum::AFTERSALE_STATUS_NO_SALE;
  923. $order_data['consignee'] = $address['contact'] ?? '';
  924. $order_data['province'] = $address['province_id'] ?? 0;
  925. $order_data['city'] = $address['city_id'] ?? 0;
  926. $order_data['district'] = $address['district_id'] ?? 0;
  927. $order_data['address'] = $address['address'] ?? '';
  928. $order_data['mobile'] = $address['telephone'] ?? '';
  929. $order_data['goods_price'] = $goods_price;
  930. $order_data['shipping_price'] = $shipping_price;
  931. $order_data['order_amount'] = $order_amount;
  932. $order_data['discount_amount'] = $discount_amount;
  933. $order_data['member_amount'] = $member_amount;
  934. $order_data['total_amount'] = $total_amount;
  935. $order_data['total_num'] = array_sum(array_column($goods, 'num'));
  936. $order_data['user_remark'] = $remark;
  937. $order_data['distribution_money'] = 0;
  938. $order_data['coupon_list_id'] = $coupon_list_id;
  939. $order_data['create_time'] = time();
  940. // 线下自提
  941. if ($goods[0]['delivery_type'] == GoodsEnum::DELIVERY_SELF) {
  942. $order_data['pickup_code'] = create_rand_number('order', 'pickup_code', 6);
  943. }
  944. $order_create = $Order->create($order_data);
  945. if (false === $order_create) {
  946. return false;
  947. }
  948. if (!empty($coupon_list_id)) {
  949. self::editCoupon($coupon_list_id, $order_create->id);
  950. }
  951. return $order_create->id;
  952. }
  953. /**
  954. * @notes 添加订单商品
  955. * @param $order_id
  956. * @param $goods
  957. * @return array
  958. * @throws \think\db\exception\DataNotFoundException
  959. * @throws \think\db\exception\DbException
  960. * @throws \think\db\exception\ModelNotFoundException
  961. * @author suny
  962. * @date 2021/7/13 6:21 下午
  963. */
  964. public static function getOrderGoodsData($order_id, $goods,$user_id)
  965. {
  966. $user = User::where('id', $user_id)->find();
  967. $discount = UserLevel::where('id', $user['level'])->value('discount');
  968. if($discount == 0 || self::$order_type != OrderEnum::NORMAL_ORDER){
  969. $discount = 10;
  970. }
  971. $goods_data = [];
  972. foreach ($goods as $key => $value) {
  973. $Goods = Goods::where('id', $value['goods_id'])->field('name,image,shop_id')->find();
  974. $GoodsItem = GoodsItem::where([
  975. ['id', '=', $value['item_id']],
  976. ['goods_id', '=', $value['goods_id']],
  977. ])->field('id,goods_id,price,image,spec_value_ids,spec_value_str,weight')
  978. ->find();
  979. $goodsItemPrice = GoodsItem::getGoodsItemPrice($GoodsItem);
  980. $goods_data[$key]['order_id'] = $order_id;
  981. $goods_data[$key]['shop_id'] = $Goods['shop_id'];
  982. $goods_data[$key]['goods_id'] = $value['goods_id'];
  983. $goods_data[$key]['item_id'] = $value['item_id'];
  984. $goods_data[$key]['goods_num'] = $value['num'];
  985. $goods_data[$key]['goods_name'] = $Goods['name'];
  986. $goods_data[$key]['goods_price'] = $goodsItemPrice;
  987. $goods_data[$key]['total_price'] = round($goodsItemPrice * $value['num'], 2);
  988. $goods_data[$key]['total_pay_price'] = self::calculateGoodsPrice([$value],$discount);
  989. $goods_data[$key]['spec_value'] = $GoodsItem['spec_value_str'];
  990. $goods_data[$key]['spec_value_ids'] = $GoodsItem['spec_value_ids'];
  991. $goods_data[$key]['image'] = !empty($Goods['image']) ? UrlServer::setFileUrl($Goods['image']) : '';
  992. $goods_data[$key]['weight'] = $GoodsItem['weight'];
  993. $goods_data[$key]['create_time'] = time();
  994. }
  995. $goods_data = self::shareDiscount($goods_data, $order_id);
  996. return $goods_data;
  997. }
  998. public static function shareDiscount($goodsData, $orderId)
  999. {
  1000. // 获取订单优惠价格
  1001. $orderDiscount = Order::where('id', $orderId)->value('discount_amount');
  1002. if ($orderDiscount <= 0) {
  1003. // 未使用优惠
  1004. foreach ($goodsData as $key => &$item) {
  1005. $item['discount_price'] = 0;
  1006. }
  1007. return $goodsData;
  1008. }
  1009. // 累计应付总金额
  1010. $sumPayPrice = array_sum(array_column($goodsData, 'total_pay_price')) ? : 0;
  1011. // 根据比例分摊优惠金额
  1012. $sumDiscount = 0;
  1013. foreach ($goodsData as $key => &$item) {
  1014. if ($key == (count($goodsData) - 1)) {
  1015. // 最后一条记录
  1016. $item['discount_price'] = $orderDiscount - $sumDiscount;
  1017. }
  1018. if ($sumPayPrice <= 0) {
  1019. continue;
  1020. }
  1021. $item['discount_price'] = round($item['total_pay_price'] / $sumPayPrice * $orderDiscount, 2);
  1022. // 优惠超过实付金额 使用实付
  1023. $item['discount_price'] = min($item['discount_price'], $item['total_pay_price']);
  1024. $item['total_pay_price'] -= $item['discount_price'];
  1025. }
  1026. return $goodsData;
  1027. }
  1028. /**
  1029. * @notes 扣除商品库存
  1030. * @param $goods
  1031. * @return bool
  1032. * @author suny
  1033. * @date 2021/7/13 6:21 下午
  1034. */
  1035. public static function subGoodsStock($goods)
  1036. {
  1037. $goods_ids = [];
  1038. foreach ($goods as $key => $value) {
  1039. $goods_item_stock_dec = GoodsItem::where([
  1040. ['id', '=', $value['item_id']],
  1041. ['goods_id', '=', $value['goods_id']],
  1042. ])->dec('stock', $value['num'])
  1043. ->update();
  1044. $goods_stock_dec = Goods::where('id', $value['goods_id'])
  1045. ->dec('stock', $value['num'])
  1046. ->update();
  1047. if (false === $goods_item_stock_dec) {
  1048. return false;
  1049. }
  1050. if (false === $goods_stock_dec) {
  1051. return false;
  1052. }
  1053. $goods_ids[] = $value['goods_id'];
  1054. }
  1055. // 下架总库存为0商品
  1056. //库存为0的商品暂不下架,显示为缺货
  1057. // self::outGoods($goods_ids);
  1058. return true;
  1059. }
  1060. /**
  1061. * @notes 下架总库存为0商品
  1062. * @param $goods_ids
  1063. * @return bool|void
  1064. * @author 段誉
  1065. * @date 2021/12/21 14:46
  1066. */
  1067. public static function outGoods($goods_ids)
  1068. {
  1069. try{
  1070. $goods = Goods::where('id', 'in', $goods_ids)
  1071. ->field('id, stock')
  1072. ->select();
  1073. if (empty($goods)) {
  1074. return true;
  1075. }
  1076. $need_handle_ids = [];
  1077. foreach ($goods as $good) {
  1078. if ($good['stock'] <= 0) {
  1079. $need_handle_ids[] = $good['id'];
  1080. }
  1081. }
  1082. if (empty($need_handle_ids)){
  1083. return true;
  1084. }
  1085. //下架订单商品中 商品总库存已为0的商品
  1086. Goods::where('id', 'in', $need_handle_ids)->update(['status' => 0]);
  1087. // 下架或删除商品更新收藏状态
  1088. event('UpdateCollect', ['goods_id' => $need_handle_ids]);
  1089. } catch (\Exception $e) {}
  1090. }
  1091. /**
  1092. * @notes 添加订单日志表
  1093. * @param $order_id
  1094. * @param $user_id
  1095. * @param $shop_id
  1096. * @return array
  1097. * @author suny
  1098. * @date 2021/7/13 6:21 下午
  1099. */
  1100. public static function getOrderLogData($order_id, $user_id, $shop_id)
  1101. {
  1102. $order_log_data = [];
  1103. $order_log_data['type'] = OrderLogEnum::TYPE_USER;
  1104. $order_log_data['channel'] = OrderLogEnum::USER_ADD_ORDER;
  1105. $order_log_data['order_id'] = $order_id;
  1106. $order_log_data['handle_id'] = $user_id;
  1107. $order_log_data['shop_id'] = $shop_id;
  1108. $order_log_data['content'] = OrderLogEnum::getLogDesc(OrderLogEnum::USER_ADD_ORDER);
  1109. $order_log_data['create_time'] = time();
  1110. return $order_log_data;
  1111. }
  1112. /**
  1113. * @notes 删除购物车
  1114. * @param $cart_id
  1115. * @return bool
  1116. * @author suny
  1117. * @date 2021/7/13 6:21 下午
  1118. */
  1119. public static function delCart($cart_id)
  1120. {
  1121. $delCart = Cart::where([
  1122. ['id', 'in', $cart_id],
  1123. ['selected', '=', 1]
  1124. ])
  1125. ->delete();
  1126. if (false === $delCart) {
  1127. return false;
  1128. }
  1129. return true;
  1130. }
  1131. /**
  1132. * @notes 订单列表
  1133. * @param $user_id
  1134. * @param $type
  1135. * @param $page
  1136. * @param $size
  1137. * @return array
  1138. * @throws \think\db\exception\DataNotFoundException
  1139. * @throws \think\db\exception\DbException
  1140. * @throws \think\db\exception\ModelNotFoundException
  1141. * @author suny
  1142. * @date 2021/7/13 6:21 下午
  1143. */
  1144. public static function getOrderList($user_id, $type, $page, $size)
  1145. {
  1146. $order = new Order();
  1147. $where[] = ['del', '=', 0];
  1148. $where[] = ['user_id', '=', $user_id];
  1149. switch ($type) {
  1150. case 'pay':
  1151. $where[] = ['order_status', '=', OrderEnum::ORDER_STATUS_NO_PAID];
  1152. break;
  1153. case 'delivery':
  1154. $where[] = ['order_status', 'in', [OrderEnum::ORDER_STATUS_DELIVERY, OrderEnum::ORDER_STATUS_GOODS]];
  1155. break;
  1156. case 'finish':
  1157. $where[] = ['order_status', '=', OrderEnum::ORDER_STATUS_COMPLETE];
  1158. break;
  1159. case 'close':
  1160. $where[] = ['order_status', '=', OrderEnum::ORDER_STATUS_DOWN];
  1161. break;
  1162. }
  1163. $count = $order->where(['del' => 0, 'user_id' => $user_id])
  1164. ->where($where)
  1165. ->count();
  1166. $lists = $order->where(['del' => 0, 'user_id' => $user_id])
  1167. ->where($where)
  1168. ->with(['order_goods', 'shop'])
  1169. ->field('id,order_sn,pay_way as pay_way_base,pay_way,order_status,pay_status,order_amount,order_status,order_type,shipping_status,create_time,shop_id,delivery_type')
  1170. ->page($page, $size)
  1171. ->order('id desc')
  1172. ->select();
  1173. $lists->append(['goods_count', 'pay_btn', 'cancel_btn', 'delivery_btn', 'take_btn', 'del_btn', 'comment_btn', 'content_btn','order_cancel_time','is_team_success']);
  1174. foreach ($lists as $list) {
  1175. if ($list['order_type'] == OrderEnum::SECKILL_ORDER) {//如果是秒杀
  1176. foreach ($list['order_goods'] as $item) {
  1177. $seckill_price = GoodsItem::isSeckill($item['item_id']);
  1178. if ($seckill_price != 0) {
  1179. $item['goods_price'] = $seckill_price;
  1180. }
  1181. }
  1182. }
  1183. // 查看提货码按钮
  1184. $list['pickup_btn'] = 0;
  1185. // 订单状态描述
  1186. $list['order_status_desc'] = OrderEnum::getOrderStatus($list['order_status']);
  1187. if ($list['order_status'] == OrderEnum::ORDER_STATUS_DELIVERY
  1188. && $list['delivery_type'] == OrderEnum::DELIVERY_TYPE_SELF
  1189. && $list['pay_status'] == PayEnum::ISPAID
  1190. ) {
  1191. $list['pickup_btn'] = 1;
  1192. $list['order_status_desc'] = '待取货';
  1193. }
  1194. if ($list['order_type'] == OrderEnum::TEAM_ORDER) {
  1195. $team = TeamJoin::field('id,status')
  1196. ->where(['order_id' => $list['id']])
  1197. ->findOrEmpty()->toArray();
  1198. if ($team['status'] != TeamEnum::TEAM_STATUS_SUCCESS) {
  1199. $list['pickup_btn'] = 0;
  1200. }
  1201. }
  1202. }
  1203. $data = [
  1204. 'list' => $lists,
  1205. 'page' => $page,
  1206. 'size' => $size,
  1207. 'count' => $count,
  1208. 'more' => is_more($count, $page, $size)
  1209. ];
  1210. return $data;
  1211. }
  1212. /**
  1213. * @notes 通过规格id查询秒杀价格
  1214. * @param $item_id
  1215. * @return int|mixed
  1216. * @throws \think\db\exception\DataNotFoundException
  1217. * @throws \think\db\exception\DbException
  1218. * @throws \think\db\exception\ModelNotFoundException
  1219. * @author suny
  1220. * @date 2021/7/13 6:21 下午
  1221. */
  1222. public static function getSekillPriceByItemId($item_id)
  1223. {
  1224. $where = [
  1225. 'item_id' => $item_id,
  1226. 'del' => 0,
  1227. 'review_status' => 1
  1228. ];
  1229. $seckill = SeckillGoods::where($where)->find();
  1230. return isset($seckill['price']) ? $seckill['price'] : 0;
  1231. }
  1232. /**
  1233. * @notes 订单详情
  1234. * @param $order_id
  1235. * @return array|\think\Model|null
  1236. * @throws \think\db\exception\DataNotFoundException
  1237. * @throws \think\db\exception\DbException
  1238. * @throws \think\db\exception\ModelNotFoundException
  1239. * @author suny
  1240. * @date 2021/7/13 6:22 下午
  1241. */
  1242. public static function getOrderDetail($order_id)
  1243. {
  1244. $order = Order::with(['order_goods', 'shop'])
  1245. ->field([ '*', 'pay_way as pay_way_base' ])
  1246. ->where(['del' => 0, 'id' => $order_id])
  1247. ->find();
  1248. if (isset($order['shop']['id'])) {
  1249. $order['shop']['run_start_time'] = $order['shop']['run_start_time'] ? date('H:i:s', $order['shop']['run_start_time']) : '';
  1250. $order['shop']['run_end_time'] = $order['shop']['run_end_time'] ? date('H:i:s', $order['shop']['run_end_time']) : '';
  1251. }
  1252. if ($order) {
  1253. $order->append(['delivery_address', 'pay_btn', 'cancel_btn', 'delivery_btn', 'take_btn', 'del_btn','view_invoice_btn','save_invoice_btn', 'order_cancel_time'])
  1254. ->hidden(['user_id', 'order_source',
  1255. 'city', 'district', 'address', 'shipping_status', 'shipping_code',
  1256. 'pay_status', 'transaction_id', 'del', 'province']);
  1257. // $refund_days = ConfigServer::get('after_sale', 'refund_days', 7 * 86400, 0) * 86400;
  1258. foreach ($order->order_goods as $order_good) {
  1259. if ($order['order_type'] == OrderEnum::SECKILL_ORDER) { // 是秒杀商品
  1260. $seckill_price = GoodsItem::isSeckill($order_good['item_id']);
  1261. if ($seckill_price != 0) {
  1262. $order_good['goods_price'] = $seckill_price;
  1263. }
  1264. }$order_good['sum_price'] = $order_good['goods_price'] * $order_good['goods_num'];
  1265. $order_good['comment_btn'] = 0;
  1266. if ($order['pay_status'] == PayEnum::ISPAID && $order['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE && $order_good['is_comment'] == 0) {
  1267. $order_good['comment_btn'] = 1;
  1268. }
  1269. $order_good['refund_btn'] = 0;
  1270. // $confirm_take_time = strtotime($order['confirm_take_time']) ?: 0;
  1271. // $refund_time = $confirm_take_time + $refund_days;
  1272. if ($order['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE && $order_good['refund_status'] == OrderGoodsEnum::REFUND_STATUS_NO) {
  1273. $order_good['refund_btn'] = 1;
  1274. }
  1275. $order_good['sum_price'] = $order_good['goods_price'] * $order_good['goods_num'];
  1276. }
  1277. //订单商品总价
  1278. $order->goods_price = $order->goods_price + $order->member_amount;
  1279. }
  1280. // 如果是拼团的订单
  1281. if ($order['order_type'] == OrderEnum::TEAM_ORDER) {
  1282. $teamJoin = (new TeamJoin())->where(['order_id' => $order['id']])->findOrEmpty()->toArray();
  1283. $teamJoin['team_snap'] = json_decode($teamJoin['team_snap'], true);
  1284. $order['team'] = [
  1285. 'team_activity_id' => $teamJoin['team_activity_id'],
  1286. 'team_id' => $teamJoin['team_id'],
  1287. 'identity' => $teamJoin['identity'] == 1 ? '团长' : '团员',
  1288. 'people_num' => $teamJoin['team_snap']['people_num'],
  1289. 'status' => $teamJoin['status'],
  1290. 'status_text' => TeamEnum::getStatusDesc($teamJoin['status'])
  1291. ];
  1292. }
  1293. $order['order_type'] = Order::getOrderType($order['order_type']);
  1294. $order['pay_way'] = PayEnum::getPayWay($order['pay_way']);
  1295. $order['create_time'] = $order['create_time'] == 0 ? '' : $order['create_time'];
  1296. $order['update_time'] = $order['update_time'] == 0 ? '' : $order['update_time'];
  1297. $order['confirm_take_time'] = $order['confirm_take_time'] == 0 ? '' : date('Y-m-d H:i:s', $order['confirm_take_time']);;
  1298. $order['shipping_time'] = $order['shipping_time'] == 0 ? '' : date('Y-m-d H:i:s', $order['shipping_time']);
  1299. $order['pay_time'] = $order['pay_time'] == 0 ? '' : date('Y-m-d H:i:s', $order['pay_time']);
  1300. $order['cancel_time'] = $order['cancel_time'] == 0 ? '' : date('Y-m-d H:i:s', $order['cancel_time']);
  1301. // 虚拟商品 发货内容
  1302. if ($order['delivery_type'] != OrderEnum::DELIVERY_TYPE_VIRTUAL || $order['shipping_status'] != OrderEnum::SHIPPING_FINISH) {
  1303. $order['delivery_content'] = '';
  1304. }
  1305. // 提货码
  1306. if ($order['pay_status'] == PayEnum::UNPAID){
  1307. $order['pickup_code'] = "";
  1308. }
  1309. // 订单状态描述
  1310. $order['order_status_desc'] = OrderEnum::getOrderStatus($order['order_status']);
  1311. if ($order['order_status'] == OrderEnum::ORDER_STATUS_DELIVERY && $order['delivery_type'] == OrderEnum::DELIVERY_TYPE_SELF) {
  1312. $order['order_status_desc'] = '待取货';
  1313. }
  1314. // 商家地址
  1315. $shop = Shop::where(['id' => $order['shop_id']])->findOrEmpty();
  1316. $region = Db::name('dev_region')
  1317. ->where('id', 'IN', [$shop['province_id'], $shop['city_id'], $shop['district_id']])
  1318. ->order('level asc')
  1319. ->column('name');
  1320. $order['shop_address'] = implode('', $region) . $shop['address'];
  1321. return $order->toArray();
  1322. }
  1323. static function wxReceiveDetail($id, $user_id)
  1324. {
  1325. $result = [
  1326. 'transaction_id' => '',
  1327. ];
  1328. $order = Order::where('id', $id)->where('user_id', $user_id)->findOrEmpty();
  1329. if ($order->isEmpty()) {
  1330. return $result;
  1331. }
  1332. $result['transaction_id'] = $order->transaction_id ? : OrderTrade::where('id', $order->trade_id)->value('transaction_id', '');
  1333. return $result;
  1334. }
  1335. /**
  1336. * @notes 取消订单
  1337. * @param $order_id
  1338. * @param $user_id
  1339. * @return \think\response\Json
  1340. * @throws \think\db\exception\DataNotFoundException
  1341. * @throws \think\db\exception\DbException
  1342. * @throws \think\db\exception\ModelNotFoundException
  1343. * @throws \think\exception\PDOException
  1344. * @author suny
  1345. * @date 2021/7/13 6:22 下午
  1346. */
  1347. public static function cancel($order_id, $user_id)
  1348. {
  1349. $time = time();
  1350. $order = Order::with(['orderGoods'])->where(['del' => 0, 'user_id' => $user_id, 'id' => $order_id])->find();
  1351. if (!$order || (int)$order['order_status'] > OrderEnum::ORDER_STATUS_DELIVERY) {
  1352. return JsonServer::error('很抱歉!订单无法取消');
  1353. }
  1354. $cancel_limit = ConfigServer::get('transaction', 'paid_order_cancel_time', 60);
  1355. $limit_time = $order->getOrigin('pay_time') + $cancel_limit * 60;
  1356. if ($limit_time < time() && $order['order_status'] != OrderEnum::ORDER_STATUS_NO_PAID) {
  1357. return JsonServer::error('很抱歉!订单已不可取消');
  1358. }
  1359. Db::startTrans();
  1360. try {
  1361. // 如果是拼团的订单
  1362. $team_join = (new TeamJoin())->where(['order_id' => $order['id'],'status'=>TeamEnum::TEAM_STATUS_CONDUCT])->findOrEmpty()->toArray();//拼团中
  1363. if ($order['order_type'] == OrderEnum::TEAM_ORDER && !empty($team_join)) {
  1364. $team_id = $team_join['team_id'];
  1365. $teamJoin = (new TeamJoin())->alias('TJ')
  1366. ->field(['TJ.*,O.order_sn,O.order_status,O.pay_status,O.refund_status,O.order_amount'])
  1367. ->where(['team_id' => $team_id])
  1368. ->join('order O', 'O.id=TJ.order_id')
  1369. ->select()->toArray();
  1370. TeamFound::update(['status' => TeamEnum::TEAM_STATUS_FAIL, 'team_end_time' => $time], ['id' => $team_id]);
  1371. foreach ($teamJoin as $item) {
  1372. TeamJoin::update(['status' => TeamEnum::TEAM_STATUS_FAIL, 'update_time' => $time], ['id' => $item['id']]);
  1373. OrderRefundLogic::cancelOrder($item['order_id'], OrderLogEnum::TYPE_USER, $user_id); //取消订单
  1374. if ($item['pay_status'] == PayEnum::ISPAID) {
  1375. $order = (new Order())->findOrEmpty($item['order_id'])->toArray();
  1376. OrderRefundLogic::cancelOrderRefundUpdate($order); //更新订单状态
  1377. OrderRefundLogic::refund($order, $order['order_amount'], $order['order_amount']); //订单退款
  1378. }
  1379. }
  1380. } else {
  1381. //取消订单
  1382. OrderRefundLogic::cancelOrder($order_id, OrderLogEnum::TYPE_USER, $user_id);
  1383. //已支付的订单,取消,退款
  1384. if ($order['pay_status'] == PayEnum::ISPAID) {
  1385. //更新订单状态
  1386. OrderRefundLogic::cancelOrderRefundUpdate($order);
  1387. //订单退款
  1388. OrderRefundLogic::refund($order, $order['order_amount'], $order['order_amount']);
  1389. }
  1390. }
  1391. Db::commit();
  1392. return JsonServer::success('取消成功');
  1393. } catch (Exception $e) {
  1394. Db::rollback();
  1395. self::addErrorRefund($order, $e->getMessage());
  1396. return JsonServer::error($e->getMessage());
  1397. }
  1398. }
  1399. /**
  1400. * @notes 回退商品库存
  1401. * @param $goods
  1402. * @author suny
  1403. * @date 2021/7/13 6:22 下午
  1404. */
  1405. public static function backStock($goods)
  1406. {
  1407. foreach ($goods as $good) {
  1408. //回退库存,回退规格库存,减少商品销量
  1409. Goods::where('id', $good['goods_id'])
  1410. ->update([
  1411. 'stock' => Db::raw('stock+' . $good['goods_num'])
  1412. ]);
  1413. //补充规格表库存
  1414. GoodsItem::where('id', $good['item_id'])
  1415. ->inc('stock', $good['goods_num'])
  1416. ->update();
  1417. }
  1418. }
  1419. /**
  1420. * @notes 增加退款失败记录
  1421. * @param $order
  1422. * @param $err_msg
  1423. * @return int|string
  1424. * @throws \think\db\exception\DataNotFoundException
  1425. * @throws \think\db\exception\DbException
  1426. * @throws \think\db\exception\ModelNotFoundException
  1427. * @throws \think\exception\DbException
  1428. * @author suny
  1429. * @date 2021/7/13 6:22 下午
  1430. */
  1431. public static function addErrorRefund($order, $err_msg)
  1432. {
  1433. $orderRefund = new OrderRefund();
  1434. $refund_data = [
  1435. 'order_id' => $order['id'],
  1436. 'user_id' => $order['user_id'],
  1437. 'refund_sn' => createSn('order_refund', 'refund_sn'),
  1438. 'order_amount' => $order['order_amount'],//订单应付金额
  1439. 'refund_amount' => $order['order_amount'],//订单退款金额
  1440. 'transaction_id' => $order['transaction_id'],
  1441. 'create_time' => time(),
  1442. 'refund_status' => 2,
  1443. 'refund_msg' => json_encode($err_msg, JSON_UNESCAPED_UNICODE),
  1444. ];
  1445. return $orderRefund->insertGetId($refund_data);
  1446. }
  1447. /**
  1448. * @notes 获取退款订单的应付金额
  1449. * @param $order
  1450. * @return mixed
  1451. * @throws \think\db\exception\DataNotFoundException
  1452. * @throws \think\db\exception\DbException
  1453. * @throws \think\db\exception\ModelNotFoundException
  1454. * @author suny
  1455. * @date 2021/7/13 6:22 下午
  1456. */
  1457. public static function getOrderTotalFee($order)
  1458. {
  1459. $OrderTrade = new OrderTrade();
  1460. $trade = $OrderTrade
  1461. ->where('transaction_id', $order['transaction_id'])
  1462. ->find();
  1463. $total_fee = $order['order_amount'];
  1464. if ($trade) {
  1465. $total_fee = $trade['order_amount'];
  1466. }
  1467. return $total_fee;
  1468. }
  1469. /**
  1470. * @notes 确认订单
  1471. * @param $order_id
  1472. * @param $user_id
  1473. * @return \think\response\Json
  1474. * @throws \think\db\exception\DataNotFoundException
  1475. * @throws \think\db\exception\DbException
  1476. * @throws \think\db\exception\ModelNotFoundException
  1477. * @author suny
  1478. * @date 2021/7/13 6:22 下午
  1479. */
  1480. public static function confirm($order_id, $user_id)
  1481. {
  1482. $order = Order::where(['del' => 0, 'id' => $order_id])->find();
  1483. if ($order['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE) {
  1484. return JsonServer::error('订单已完成');
  1485. }
  1486. if ($order['shipping_status'] == 0) {
  1487. return JsonServer::error('订单未发货');
  1488. }
  1489. $order->order_status = OrderEnum::ORDER_STATUS_COMPLETE;
  1490. $order->update_time = time();
  1491. $order->confirm_take_time = time();
  1492. $order->save();
  1493. //订单日志
  1494. OrderLogLogic::record(
  1495. OrderLogEnum::TYPE_USER,
  1496. OrderLogEnum::USER_CONFIRM_ORDER,
  1497. $order_id,
  1498. $user_id,
  1499. OrderLogEnum::USER_CONFIRM_ORDER
  1500. );
  1501. return JsonServer::success('确认成功');
  1502. }
  1503. /**
  1504. * @notes 删除订单
  1505. * @param $order_id
  1506. * @param $user_id
  1507. * @return \think\response\Json
  1508. * @throws \think\db\exception\DataNotFoundException
  1509. * @throws \think\db\exception\DbException
  1510. * @throws \think\db\exception\ModelNotFoundException
  1511. * @author suny
  1512. * @date 2021/7/13 6:23 下午
  1513. */
  1514. public static function del($order_id, $user_id)
  1515. {
  1516. $where = [
  1517. 'order_status' => OrderEnum::ORDER_STATUS_DOWN,
  1518. 'user_id' => $user_id,
  1519. 'id' => $order_id,
  1520. 'del' => 0,
  1521. ];
  1522. $order = Order::where($where)->find();
  1523. if (!$order) {
  1524. return JsonServer::error('订单无法删除');
  1525. }
  1526. // $res = $order->save(['del' => 1, 'update_time' => time()]);
  1527. $data = ['del' => 1, 'update_time' => time(), 'pat_status' => OrderEnum::ORDER_STATUS_DOWN];
  1528. $res = Order::update($data, ['id' => $order['id']]);
  1529. OrderLogLogic::record(
  1530. OrderLogEnum::TYPE_USER,
  1531. OrderLogEnum::USER_DEL_ORDER,
  1532. $order_id,
  1533. $user_id,
  1534. OrderLogEnum::USER_DEL_ORDER
  1535. );
  1536. return JsonServer::success('删除成功', ['res' => $res]);
  1537. }
  1538. /**
  1539. * @notes 获取订单支付结果
  1540. * @param $trade_id
  1541. * @return array|false
  1542. * @author suny
  1543. * @date 2021/7/13 6:23 下午
  1544. */
  1545. public static function pay_result($id,$from)
  1546. {
  1547. switch ($from) {
  1548. case 'trade' : //如果是父订单类型下单,$id为父订单id
  1549. $result = OrderTrade::alias('ot')
  1550. ->where([ 'ot.id' => $id ])
  1551. ->join('order o','o.trade_id = ot.id')
  1552. ->field(['ot.id', 'ot.t_sn as order_sn', 'o.pay_time', 'o.pay_way', 'ot.total_amount' , 'o.pay_status' , 'ot.create_time'])
  1553. ->order('o.pay_status desc')
  1554. ->findOrEmpty()
  1555. ->toArray();
  1556. $result['total_amount'] = '¥' . $result['total_amount'];
  1557. break;
  1558. case 'order' : //如果是子订单类型下单,$id为子订单id
  1559. $result = Order::where(['id' => $id])
  1560. ->field(['id', 'order_sn', 'pay_time', 'pay_way', 'total_amount', 'pay_status' , 'create_time' ])
  1561. ->findOrEmpty()
  1562. ->toArray();
  1563. $result['total_amount'] = '¥' . $result['total_amount'];
  1564. break;
  1565. case 'integral':
  1566. $result = IntegralOrder::where(['id' => $id])
  1567. ->field(['id', 'order_sn', 'pay_time', 'pay_way', 'order_amount', 'order_integral', 'exchange_way', 'pay_status' , 'create_time' ])
  1568. ->findOrEmpty()->toArray();
  1569. $order_integral = $result['order_integral'] > 0 ? $result['order_integral'] . '积分' : '';
  1570. $order_amount = $result['order_amount'] > 0 ? '+ ¥' . $result['order_amount'] . '元' : '';
  1571. $result['total_amount'] = $order_integral . $order_amount;
  1572. break;
  1573. case 'recharge':
  1574. $result = RechargeOrder::where('id', $id)
  1575. ->field([ 'id', 'order_sn', 'pay_time', 'pay_way', 'pay_status as pay_status_origin', 'create_time', 'order_amount' ])
  1576. ->findOrEmpty()->toArray();
  1577. $result['total_amount'] = '¥' . $result['order_amount'];
  1578. $result['pay_status'] = $result['pay_status_origin'];
  1579. break;
  1580. default :
  1581. return false;
  1582. }
  1583. if ($result) {
  1584. $result['pay_time'] = empty($result['pay_time']) ? $result['create_time'] : date('Y-m-d H:i:s', $result['pay_time']);
  1585. $result['pay_way'] = PayEnum::getPayWay($result['pay_way']);
  1586. $result['pay_status'] = $result['pay_status'] ?? 0;
  1587. return $result;
  1588. }
  1589. return false;
  1590. }
  1591. /**
  1592. * @notes 获取支付方式
  1593. * @param $user_id
  1594. * @return array|array[]|\array[][]|\array[][][]
  1595. * @throws \think\db\exception\DataNotFoundException
  1596. * @throws \think\db\exception\DbException
  1597. * @throws \think\db\exception\ModelNotFoundException
  1598. * @author suny
  1599. * @date 2021/7/13 6:23 下午
  1600. */
  1601. public static function getPayWay($user_id, $client, $params)
  1602. {
  1603. $payModel = new Pay();
  1604. $payway = $payModel->where(['status' => 1])->order('sort')->hidden(['config'])->select()->toArray();
  1605. foreach ($payway as $k => &$item) {
  1606. if ($item['code'] == 'wechat') {
  1607. $item['extra'] = '微信快捷支付';
  1608. $item['pay_way'] = PayEnum::WECHAT_PAY;
  1609. }
  1610. if ($item['code'] == 'balance') {
  1611. $user_money = Db::name('user')->where(['id' => $user_id])->value('user_money');
  1612. $item['extra'] = '可用余额:' . $user_money;
  1613. $item['pay_way'] = PayEnum::BALANCE_PAY;
  1614. if($params['from'] == 'recharge') {
  1615. unset($payway[$k]);
  1616. }
  1617. }
  1618. if ($item['code'] == 'alipay') {
  1619. $item['extra'] = '';
  1620. $item['pay_way'] = PayEnum::ALI_PAY;
  1621. }
  1622. if ($item['code'] == 'hfdg_wechat') {
  1623. $item['extra'] = '';
  1624. $item['pay_way'] = PayEnum::HFDG_WECHAT;
  1625. if (! in_array($client, [ ClientEnum::mnp, ClientEnum::oa ])) {
  1626. unset($payway[$k]);
  1627. }
  1628. }
  1629. if ($item['code'] == 'hfdg_alipay') {
  1630. $item['extra'] = '';
  1631. $item['pay_way'] = PayEnum::HFDG_ALIPAY;
  1632. if (in_array($client, [ ClientEnum::mnp, ClientEnum::oa ])) {
  1633. unset($payway[$k]);
  1634. }
  1635. }
  1636. if ($item['code'] == 'offline') {
  1637. $item['extra'] = '';
  1638. $item['pay_way'] = PayEnum::OFFLINE_PAY;
  1639. if($params['from'] == 'recharge') {
  1640. unset($payway[$k]);
  1641. }
  1642. }
  1643. }
  1644. if($params['from'] == 'order') {
  1645. $order = Order::findOrEmpty($params['order_id']);
  1646. } else if($params['from'] == 'trade') {
  1647. $order = OrderTrade::findOrEmpty($params['order_id']);
  1648. } else if($params['from'] == 'integral') {
  1649. $order = IntegralOrder::findOrEmpty($params['order_id']);
  1650. } else {
  1651. $order = RechargeOrder::findOrEmpty($params['order_id']);
  1652. }
  1653. $cancelTime = ConfigServer::get('transaction', 'unpaid_order_cancel_time', 60);
  1654. if(empty($cancelTime) || $params['from'] == 'integral') {
  1655. $cancelTime = 0;
  1656. } else {
  1657. $cancelTime = strtotime($order['create_time']) + intval($cancelTime) * 60;
  1658. }
  1659. return [
  1660. 'pay_way' => array_values($payway),
  1661. 'order_amount' => $order['order_amount'],
  1662. 'cancel_time' => $cancelTime,
  1663. ];
  1664. }
  1665. /**
  1666. * @notes 查询物流
  1667. * @param $id
  1668. * @param $user_id
  1669. * @return array|false
  1670. * @throws \think\db\exception\DataNotFoundException
  1671. * @throws \think\db\exception\DbException
  1672. * @throws \think\db\exception\ModelNotFoundException
  1673. * @throws \think\exception\DbException
  1674. * @author suny
  1675. * @date 2021/7/13 6:23 下午
  1676. */
  1677. public static function orderTraces($id, $user_id)
  1678. {
  1679. $order = new Order();
  1680. $order = $order->alias('o')
  1681. ->join('order_goods og', 'o.id = og.order_id')
  1682. ->join('goods g', 'g.id = og.goods_id')
  1683. ->where(['o.id' => $id, 'o.user_id' => $user_id, 'pay_status' => OrderEnum::ORDER_STATUS_DELIVERY, 'o.del' => 0])
  1684. ->field('o.id,o.delivery_id,order_status,total_num,og.image,o.consignee,o.mobile,o.province,o.city,o.district,o.address,pay_time,confirm_take_time,o.shipping_status,shipping_time')
  1685. ->append(['delivery_address'])
  1686. ->find();
  1687. if (!self::checkDelivery($order['delivery_id'])) {
  1688. return false;
  1689. }
  1690. //初始化数据
  1691. $order_tips = '已下单';
  1692. $order_traces = [];
  1693. $traces = [];//物流轨迹
  1694. $shipment = [//发货
  1695. 'title' => '已发货',
  1696. 'tips' => '',
  1697. 'time' => '',
  1698. ];
  1699. $finish = [//交易完成
  1700. 'title' => '交易完成',
  1701. 'tips' => '',
  1702. 'time' => '',
  1703. ];
  1704. if ($order) {
  1705. $order_delivery = Delivery::where(['order_id' => $id])->field('invoice_no,shipping_name,shipping_id,mobile')->find();
  1706. $express = ConfigServer::get('express', 'way', '', '');
  1707. //已发货
  1708. if ($express && $order['shipping_status']) {
  1709. $key = ConfigServer::get($express, 'appkey');
  1710. $app = ConfigServer::get($express, 'appsecret');
  1711. //获取物流配置
  1712. if ($app && $key) {
  1713. //快递配置设置为快递鸟时
  1714. if ($express === 'kdniao') {
  1715. $expressage = (new Kdniao($app, $key, Env::get('app.app_debug', 'true')));
  1716. $shipping_field = 'codebird';
  1717. } else {
  1718. $expressage = (new Kd100($app, $key, Env::get('app.app_debug', 'true')));
  1719. $shipping_field = 'code100';
  1720. }
  1721. //快递编码
  1722. $shipping_code = Db::name('express')->where(['id' => $order_delivery['shipping_id']])->value($shipping_field);
  1723. //获取物流轨迹
  1724. if (in_array(strtolower($shipping_code ), [ 'sf', 'shunfeng' ])) {
  1725. if ($express === 'kdniao') {
  1726. $expressage->logistics($shipping_code, $order_delivery['invoice_no'], substr($order_delivery['mobile'],-4));
  1727. } else {
  1728. $expressage->logistics($shipping_code, $order_delivery['invoice_no'], $order_delivery['mobile']);
  1729. }
  1730. }else {
  1731. $expressage->logistics($shipping_code, $order_delivery['invoice_no']);
  1732. }
  1733. $traces = $expressage->logisticsFormat();
  1734. //获取不到物流轨迹时
  1735. if ($traces == false) {
  1736. $error = $expressage->getError();
  1737. $error = json_decode($error,true);
  1738. if ($express === 'kdniao') {
  1739. if($error['Success'] == false){
  1740. $traces[] = [$error['Reason']];
  1741. }
  1742. } else {
  1743. if($error['result'] == false){
  1744. $traces[] = [$error['message']];
  1745. }
  1746. }
  1747. } else {
  1748. foreach ($traces as &$item) {
  1749. $item = array_values(array_unique($item));
  1750. }
  1751. }
  1752. }
  1753. }
  1754. //待收货
  1755. if ($order['order_status'] == 2) {
  1756. $shipment['tips'] = '商品已出库';
  1757. $shipment['time'] = date('Y-m-d H:i:s', $order['shipping_time']);
  1758. }
  1759. //确认收货
  1760. if ($order['order_status'] == 3) {
  1761. $order_tips = '交易完成';
  1762. $finish['tips'] = '订单交易完成';
  1763. $finish['time'] = $order['confirm_take_time'] ? date('Y-m-d H:i:s', $order['confirm_take_time']) : $order['confirm_take_time'];
  1764. }
  1765. //数据合并
  1766. $order_traces = [
  1767. 'order' => [
  1768. 'tips' => $order_tips,
  1769. 'image' => UrlServer::getFileUrl($order['image']),
  1770. 'count' => $order['total_num'],
  1771. 'invoice_no' => $order_delivery['invoice_no'],
  1772. 'shipping_name' => empty($order_delivery['shipping_name']) ? '-' : $order_delivery['shipping_name'],
  1773. ],
  1774. 'take' => [
  1775. 'contacts' => $order['consignee'],
  1776. 'mobile' => $order['mobile'],
  1777. 'address' => $order['delivery_address'],
  1778. ],
  1779. 'finish' => $finish,
  1780. 'delivery' => [
  1781. 'title' => '运输中',
  1782. 'traces' => $traces
  1783. ],
  1784. 'shipment' => $shipment,
  1785. 'buy' => [
  1786. 'title' => '已下单',
  1787. 'tips' => '订单提交成功',
  1788. 'time' => date('Y-m-d H:i:s', $order['pay_time'])
  1789. ],
  1790. ];
  1791. return $order_traces;
  1792. }
  1793. return $order_traces;
  1794. }
  1795. /**
  1796. * @notes 配送方式无需快递的
  1797. * @param $delivery_id
  1798. * @return bool
  1799. * @throws \think\db\exception\DataNotFoundException
  1800. * @throws \think\db\exception\DbException
  1801. * @throws \think\db\exception\ModelNotFoundException
  1802. * @author suny
  1803. * @date 2021/7/13 6:23 下午
  1804. */
  1805. public static function checkDelivery($delivery_id)
  1806. {
  1807. $delivery = Delivery::where(['id' => $delivery_id])
  1808. ->find();
  1809. if ($delivery['send_type'] == 2) {
  1810. return false;
  1811. }
  1812. return true;
  1813. }
  1814. /**
  1815. * @notes 判断商家营业状态
  1816. * @param $value
  1817. * @return bool|string
  1818. * @author suny
  1819. * @date 2021/7/13 6:23 下午
  1820. */
  1821. public static function checkShop($value)
  1822. {
  1823. $shop = Shop::where('id', $value['shop_id'])->field('is_run, is_pay')->findOrEmpty();
  1824. if (!$shop['is_run']) {
  1825. return '该商家已暂停营业';
  1826. }
  1827. if (!$shop['is_pay']) {
  1828. return '该商家支付功能已关闭';
  1829. }
  1830. return true;
  1831. }
  1832. /**
  1833. * @notes 修改优惠券状态
  1834. * @param $coupon_id
  1835. * @param $order_id
  1836. * @return CouponList|false
  1837. * @throws \think\db\exception\DataNotFoundException
  1838. * @throws \think\db\exception\DbException
  1839. * @throws \think\db\exception\ModelNotFoundException
  1840. * @author suny
  1841. * @date 2021/7/13 6:23 下午
  1842. */
  1843. public static function editCoupon($coupon_id, $order_id)
  1844. {
  1845. $status = CouponList::where(['id' => $coupon_id, 'status' => 0])->find();
  1846. if (!$status) {
  1847. return false;
  1848. }
  1849. $time = time();
  1850. $data = [
  1851. 'status' => 1,
  1852. 'use_time' => $time,
  1853. 'update_time' => $time,
  1854. 'order_id' => $order_id
  1855. ];
  1856. $res = CouponList::where('id', $status->id)
  1857. ->update($data);
  1858. return $res;
  1859. }
  1860. /**
  1861. * @notes 设置秒杀商品销量
  1862. * @param $item_id
  1863. * @param $num
  1864. * @return bool
  1865. * @author suny
  1866. * @date 2021/7/13 6:23 下午
  1867. */
  1868. public static function setSeckillSaleSum($item_id, $num)
  1869. {
  1870. $result = SeckillGoods::where('item_id', $item_id)
  1871. ->inc('sales_sum', $num)
  1872. ->save();
  1873. if ($result) {
  1874. return true;
  1875. } else {
  1876. return false;
  1877. }
  1878. }
  1879. public static function getPayStatus($id,$from)
  1880. {
  1881. switch ($from){
  1882. case 'trade' : //如果是父订单类型下单,$id为父订单id
  1883. $order_trade = OrderTrade::alias('ot')
  1884. ->where(['ot.id' => $id])
  1885. ->join('order o','o.trade_id = ot.id')
  1886. ->field(['ot.id','o.id as order_id', 'ot.t_sn as order_sn', 'o.pay_time', 'o.pay_way', 'ot.total_amount','o.pay_status','o.order_status'])
  1887. ->select()
  1888. ->toArray();
  1889. foreach ($order_trade as $item) {
  1890. if($item['pay_time']){
  1891. $item['pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  1892. }
  1893. $create_time = Db::name('order')->where(['id' => $item['order_id']])->value('create_time');
  1894. $unpaid_order_cancel_time = ConfigServer::get('transaction','unpaid_order_cancel_time',60);
  1895. $cancel_time = $create_time + $unpaid_order_cancel_time * 60;
  1896. $item['pay_status_text'] = PayEnum::getPayStatus($item['pay_status']);
  1897. $result = $item;
  1898. $goods_lists = Db::name('order_goods')
  1899. ->where(['order_id' => $item['order_id']])
  1900. ->field('id,goods_name')
  1901. ->order('shop_id desc')
  1902. ->select()
  1903. ->toArray();
  1904. foreach ($goods_lists as $goods_list) {
  1905. $order_goods[] = $goods_list;
  1906. }
  1907. $order = Order::where(['id' => $item['order_id']])
  1908. ->find()
  1909. ->append(['pc_address'])
  1910. ->toArray();
  1911. $contact = $order['consignee'];
  1912. $mobile = $order['mobile'];
  1913. $address = [
  1914. 'contact' => $contact,
  1915. 'mobile' => $mobile,
  1916. 'delivery_address' => $order['pc_address'],
  1917. ];
  1918. $pay_way_text = PayEnum::getPayWay($item['pay_way']);
  1919. $result['pay_way'] = $pay_way_text;
  1920. $result['cancel_time'] = $cancel_time;
  1921. }
  1922. $result['order_goods'] = $order_goods;
  1923. $result['address'] = $address;
  1924. break;
  1925. case 'order' : //如果是子订单类型下单,$id为子订单id
  1926. $result = Order::where(['id' => $id])
  1927. ->field(['id', 'order_sn', 'pay_time', 'pay_way', 'total_amount','consignee','mobile','province','city','district','address','pay_status','order_status'])
  1928. ->findOrEmpty()
  1929. ->append(['pc_address'])
  1930. ->toArray();
  1931. $create_time = Db::name('order')->where(['id' => $id])->value('create_time');
  1932. $unpaid_order_cancel_time = ConfigServer::get('transaction','unpaid_order_cancel_time',60);
  1933. $cancel_time = $create_time + $unpaid_order_cancel_time * 60;
  1934. $order_goods = Db::name('order_goods')
  1935. ->where(['order_id' => $result['id']])
  1936. ->field('id,goods_name')
  1937. ->order('shop_id desc')
  1938. ->select()
  1939. ->toArray();
  1940. $contact = $result['consignee'];
  1941. $mobile = $result['mobile'];
  1942. $address = [
  1943. 'contact' => $contact,
  1944. 'mobile' => $mobile,
  1945. 'delivery_address' => $result['pc_address'],
  1946. ];
  1947. $result['order_goods'] = $order_goods;
  1948. $result['address'] = $address;
  1949. $result['cancel_time'] = $cancel_time;
  1950. $result['pay_time'] = empty($result['pay_time']) ? '-' : date('Y-m-d H:i:s', $result['pay_time']);
  1951. break;
  1952. default :
  1953. return false;
  1954. }
  1955. return $result;
  1956. }
  1957. }