StoreOrder.php 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/20
  6. */
  7. namespace app\models\store;
  8. use app\admin\model\system\ShippingTemplatesFree;
  9. use app\admin\model\system\ShippingTemplatesRegion;
  10. use app\models\tree\TreeRecommend;
  11. use crmeb\basic\BaseModel;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. use think\Exception;
  16. use think\facade\Cache;
  17. use crmeb\traits\ModelTrait;
  18. use think\facade\Log;
  19. use app\models\system\SystemStore;
  20. use app\models\routine\RoutineTemplate;
  21. use app\models\user\{User, UserAddress, UserBill, UserSpread, WechatUser};
  22. use crmeb\services\{MiniProgramService,
  23. SystemConfigService,
  24. WechatService,
  25. WechatTemplateService,
  26. workerman\ChannelService
  27. };
  28. use crmeb\repositories\{
  29. GoodsRepository, PaymentRepositories, OrderRepository, ShortLetterRepositories, UserRepository
  30. };
  31. use app\admin\model\system\ShippingTemplates;
  32. /**
  33. * TODO 订单Model
  34. * Class StoreOrder
  35. * @package app\models\store
  36. */
  37. class StoreOrder extends BaseModel
  38. {
  39. /**
  40. * 数据表主键
  41. * @var string
  42. */
  43. protected $pk = 'id';
  44. /**
  45. * 模型名称
  46. * @var string
  47. */
  48. protected $name = 'store_order';
  49. use ModelTrait;
  50. protected $insert = ['add_time'];
  51. protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'pink_integral' => '拼团积分支付'];
  52. protected static $deliveryType = ['send' => '商家配送', 'express' => '快递配送'];
  53. protected function setAddTimeAttr()
  54. {
  55. return time();
  56. }
  57. protected function setCartIdAttr($value)
  58. {
  59. return is_array($value) ? json_encode($value) : $value;
  60. }
  61. protected function getCartIdAttr($value)
  62. {
  63. return json_decode($value, true);
  64. }
  65. /**获取订单组信息
  66. * @param $cartInfo
  67. * @return array
  68. */
  69. public static function getOrderPriceGroup($cartInfo, $addr)
  70. {
  71. $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮
  72. $totalPrice = self::getOrderSumPrice($cartInfo, 'truePrice');//获取订单总金额
  73. $costPrice = self::getOrderSumPrice($cartInfo, 'costPrice');//获取订单成本价
  74. $vipPrice = self::getOrderSumPrice($cartInfo, 'vip_truePrice');//获取订单会员优惠金额
  75. //如果满额包邮等于0
  76. if (!$storeFreePostage) {
  77. $storePostage = 0;
  78. } else {
  79. if ($addr) {
  80. //按照运费模板计算每个运费模板下商品的件数/重量/体积以及总金额 按照首重倒序排列
  81. $temp_num = [];
  82. foreach ($cartInfo as $cart) {
  83. $temp = ShippingTemplates::get($cart['productInfo']['temp_id']);
  84. if (!$temp) $temp = ShippingTemplates::get(1);
  85. if ($temp->getData('type') == 1) {
  86. $num = $cart['cart_num'];
  87. } elseif ($temp->getData('type') == 2) {
  88. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['weight'];
  89. } else {
  90. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['volume'];
  91. }
  92. $region = ShippingTemplatesRegion::where('temp_id', $cart['productInfo']['temp_id'])->where('city_id', $addr['city_id'])->find();
  93. if (!$region) $region = ShippingTemplatesRegion::where('temp_id', $cart['productInfo']['temp_id'])->where('city_id', 0)->find();
  94. if (!$region) $region = ShippingTemplatesRegion::where('temp_id', 1)->where('city_id', 0)->find();
  95. if (!$region) {
  96. return self::setErrorInfo('运费模板不存在');
  97. }
  98. if (!isset($temp_num[$cart['productInfo']['temp_id']])) {
  99. $temp_num[$cart['productInfo']['temp_id']]['number'] = $num;
  100. $temp_num[$cart['productInfo']['temp_id']]['price'] = bcmul($cart['cart_num'], $cart['truePrice'], 2);
  101. $temp_num[$cart['productInfo']['temp_id']]['first'] = $region['first'];
  102. $temp_num[$cart['productInfo']['temp_id']]['first_price'] = $region['first_price'];
  103. $temp_num[$cart['productInfo']['temp_id']]['continue'] = $region['continue'];
  104. $temp_num[$cart['productInfo']['temp_id']]['continue_price'] = $region['continue_price'];
  105. $temp_num[$cart['productInfo']['temp_id']]['temp_id'] = $cart['productInfo']['temp_id'];
  106. $temp_num[$cart['productInfo']['temp_id']]['city_id'] = $addr['city_id'];
  107. } else {
  108. $temp_num[$cart['productInfo']['temp_id']]['number'] += $num;
  109. $temp_num[$cart['productInfo']['temp_id']]['price'] += bcmul($cart['cart_num'], $cart['truePrice'], 2);
  110. }
  111. }
  112. array_multisort(array_column($temp_num, 'first_price'), SORT_DESC, $temp_num);
  113. $type = $storePostage = 0;
  114. foreach ($temp_num as $k => $v) {
  115. if (ShippingTemplatesFree::where('temp_id', $v['temp_id'])->where('city_id', $v['city_id'])->where('number', '<=', $v['number'])->where('price', '<=', $v['price'])->find()) {
  116. unset($temp_num[$k]);
  117. }
  118. }
  119. foreach ($temp_num as $v) {
  120. if ($type == 0) {
  121. if ($v['number'] <= $v['first']) {
  122. $storePostage = bcadd($storePostage, $v['first_price'], 2);
  123. } else {
  124. if ($v['continue'] <= 0) {
  125. $storePostage = $storePostage;
  126. } else {
  127. $storePostage = bcadd(bcadd($storePostage, $v['first_price'], 2), bcmul(ceil(bcdiv(bcsub($v['number'], $v['first']), $v['continue'] ?? 0, 2)), $v['continue_price']), 2);
  128. }
  129. }
  130. $type = 1;
  131. } else {
  132. if ($v['continue'] <= 0) {
  133. $storePostage = $storePostage;
  134. } else {
  135. $storePostage = bcadd($storePostage, bcmul(ceil(bcdiv($v['number'], $v['continue'] ?? 0, 2)), $v['continue_price']), 2);
  136. }
  137. }
  138. }
  139. } else {
  140. $storePostage = 0;
  141. }
  142. if ($storeFreePostage <= $totalPrice) $storePostage = 0;//如果总价大于等于满额包邮 邮费等于0
  143. }
  144. return compact('storePostage', 'storeFreePostage', 'totalPrice', 'costPrice', 'vipPrice');
  145. }
  146. /**获取某个字段总金额
  147. * @param $cartInfo
  148. * @param $key 键名
  149. * @return int|string
  150. */
  151. public static function getOrderSumPrice($cartInfo, $key = 'truePrice')
  152. {
  153. $SumPrice = 0;
  154. foreach ($cartInfo as $cart) {
  155. $SumPrice = bcadd($SumPrice, bcmul($cart['cart_num'], $cart[$key], 2), 2);
  156. }
  157. return $SumPrice;
  158. }
  159. public static function refundPink($id)
  160. {
  161. $product = StoreOrder::get($id);
  162. if (!$product) return self::setErrorInfo('数据不存在');
  163. if ($product['pay_price'] == $product['refund_price']) return self::setErrorInfo('已退完支付金额!不能再退款了');
  164. $refund_price = bcsub($product['pay_price'], $product['refund_price'], 2);
  165. $data['refund_price'] = $product['pay_price'];
  166. $data['refund_status'] = 2;
  167. $refund_data['pay_price'] = $product['pay_price'];
  168. $refund_data['refund_price'] = $refund_price;
  169. $usermoney = User::where('uid', $product['uid'])->value('pink_integral');
  170. $res1 = User::bcInc($product['uid'], 'pink_integral', $refund_price, 'uid');
  171. $res2 = UserBill::income('拼团退款', $product['uid'], 'pink_integral', 'pink_fail', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '拼团退款到拼团积分' . floatval($refund_price) . '元');
  172. try {
  173. OrderRepository::storeOrderYueRefund($product, $refund_data);
  174. } catch (Exception $e) {
  175. return self::setErrorInfo($e->getMessage());
  176. }
  177. $res = $res1 && $res2;
  178. if (!$res) return self::setErrorInfo('退款失败!');
  179. $resEdit = StoreOrder::edit($data, $id);
  180. if ($resEdit) {
  181. \app\admin\model\order\StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元');
  182. return true;
  183. } else {
  184. \app\admin\model\order\StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元失败');
  185. return self::setErrorInfo('修改失败!');
  186. }
  187. }
  188. /**
  189. * 拼团
  190. * @param $cartInfo
  191. * @return array
  192. */
  193. public static function getCombinationOrderPriceGroup($cartInfo)
  194. {
  195. $storePostage = floatval(sys_config('store_postage')) ?: 0;
  196. $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;
  197. $totalPrice = self::getCombinationOrderTotalPrice($cartInfo);
  198. $costPrice = self::getCombinationOrderTotalPrice($cartInfo);
  199. if (!$storeFreePostage) {
  200. $storePostage = 0;
  201. } else {
  202. foreach ($cartInfo as $cart) {
  203. if (!StoreCombination::where('id', $cart['combination_id'])->value('is_postage'))
  204. $storePostage = bcadd($storePostage, StoreCombination::where('id', $cart['combination_id'])->value('postage'), 2);
  205. }
  206. if ($storeFreePostage <= $totalPrice) $storePostage = 0;
  207. }
  208. return compact('storePostage', 'storeFreePostage', 'totalPrice', 'costPrice');
  209. }
  210. /**
  211. * 拼团价格
  212. * @param $cartInfo
  213. * @return float
  214. */
  215. public static function getCombinationOrderTotalPrice($cartInfo)
  216. {
  217. $totalPrice = 0;
  218. foreach ($cartInfo as $cart) {
  219. if ($cart['combination_id']) {
  220. $totalPrice = bcadd($totalPrice, bcmul($cart['cart_num'], StoreCombination::where('id', $cart['combination_id'])->value('price'), 2), 2);
  221. }
  222. }
  223. return (float)$totalPrice;
  224. }
  225. /**
  226. * 缓存订单信息
  227. * @param $uid
  228. * @param $cartInfo
  229. * @param $priceGroup
  230. * @param array $other
  231. * @param int $cacheTime
  232. * @return string
  233. * @throws \Psr\SimpleCache\InvalidArgumentException
  234. */
  235. public static function cacheOrderInfo($uid, $cartInfo, $priceGroup, $other = [], $cacheTime = 600)
  236. {
  237. do {
  238. $key = md5(time() . rand(100000, 999999));
  239. } while (Cache::has('user_order_' . $uid . $key));
  240. Cache::set('user_order_' . $uid . $key, compact('cartInfo', 'priceGroup', 'other'), $cacheTime);
  241. return $key;
  242. }
  243. /**
  244. * 获取订单缓存信息
  245. * @param $uid
  246. * @param $key
  247. * @return mixed|null
  248. * @throws \Psr\SimpleCache\InvalidArgumentException
  249. */
  250. public static function getCacheOrderInfo($uid, $key)
  251. {
  252. $cacheName = 'user_order_' . $uid . $key;
  253. if (!Cache::has($cacheName)) return null;
  254. return Cache::get($cacheName);
  255. }
  256. /**
  257. * 删除订单缓存
  258. * @param $uid
  259. * @param $key
  260. */
  261. public static function clearCacheOrderInfo($uid, $key)
  262. {
  263. Cache::delete('user_order_' . $uid . $key);
  264. }
  265. /**
  266. * 生成订单
  267. * @param $uid
  268. * @param $key
  269. * @param $addressId
  270. * @param $payType
  271. * @param bool $useIntegral
  272. * @param int $couponId
  273. * @param string $mark
  274. * @param int $combinationId
  275. * @param int $pinkId
  276. * @param int $seckill_id
  277. * @param int $bargain_id
  278. * @param bool $test
  279. * @param int $isChannel
  280. * @param int $shipping_type
  281. * @param string $real_name
  282. * @param string $phone
  283. * @return StoreOrder|bool|\think\Model
  284. * @throws \think\Exception
  285. * @throws DataNotFoundException
  286. * @throws ModelNotFoundException
  287. * @throws \think\exception\DbException
  288. */
  289. public static function cacheKeyCreateOrder($uid, $key, $addressId, $payType, $useIntegral = false, $couponId = 0, $mark = '', $combinationId = 0, $pinkId = 0, $seckill_id = 0, $bargain_id = 0, $test = false, $isChannel = 0, $shipping_type = 1, $real_name = '', $phone = '', $storeId = 0)
  290. {
  291. self::beginTrans();
  292. try {
  293. $shipping_type = (int)$shipping_type;
  294. $offlinePayStatus = (int)sys_config('offline_pay_status') ?? (int)2;
  295. if ($offlinePayStatus == 2) unset(self::$payType['offline']);
  296. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!', true);
  297. if (self::be(['unique' => $key, 'uid' => $uid])) return self::setErrorInfo('请勿重复提交订单', true);
  298. $userInfo = User::getUserInfo($uid);
  299. if (!$userInfo) return self::setErrorInfo('用户不存在!', true);
  300. $cartGroup = self::getCacheOrderInfo($uid, $key);
  301. if (!$cartGroup) return self::setErrorInfo('订单已过期,请刷新当前页面!', true);
  302. $cartInfo = $cartGroup['cartInfo'];
  303. $priceGroup = $cartGroup['priceGroup'];
  304. $other = $cartGroup['other'];
  305. $payPrice = (float)$priceGroup['totalPrice'];
  306. $addr = UserAddress::where('uid', $uid)->where('id', $addressId)->find();
  307. if ($payType == 'offline' && sys_config('offline_postage') == 1) {
  308. $payPostage = 0;
  309. } else {
  310. $payPostage = self::getOrderPriceGroup($cartInfo, $addr)['storePostage'];
  311. }
  312. if ($shipping_type === 1) {
  313. if (!$test && !$addressId) return self::setErrorInfo('请选择收货地址!', true);
  314. if (!$test && (!UserAddress::be(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]) || !($addressInfo = UserAddress::find($addressId))))
  315. return self::setErrorInfo('地址选择有误!', true);
  316. } else {
  317. if ((!$real_name || !$phone) && !$test) return self::setErrorInfo('请填写姓名和电话', true);
  318. $addressInfo['real_name'] = $real_name;
  319. $addressInfo['phone'] = $phone;
  320. $addressInfo['province'] = '';
  321. $addressInfo['city'] = '';
  322. $addressInfo['district'] = '';
  323. $addressInfo['detail'] = '';
  324. }
  325. $cartIds = [];
  326. $totalNum = 0;
  327. $gainIntegral = 0;
  328. foreach ($cartInfo as $cart) {
  329. $cartIds[] = $cart['id'];
  330. $totalNum += $cart['cart_num'];
  331. if (!$seckill_id) $seckill_id = $cart['seckill_id'];
  332. if (!$bargain_id) $bargain_id = $cart['bargain_id'];
  333. if (!$combinationId) $combinationId = $cart['combination_id'];
  334. $cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul($cart['cart_num'], $cart['productInfo']['give_integral'], 2) : 0;
  335. $gainIntegral = bcadd($gainIntegral, $cartInfoGainIntegral, 2);
  336. }
  337. $deduction = $seckill_id || $bargain_id || $combinationId;
  338. if ($deduction) {
  339. $couponId = 0;
  340. $useIntegral = false;
  341. if (!$test) {
  342. unset(self::$payType['offline']);
  343. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('营销产品不能使用线下支付!', true);
  344. }
  345. }
  346. //使用优惠劵
  347. $res1 = true;
  348. if ($couponId) {
  349. $couponInfo = StoreCouponUser::validAddressWhere()->where('id', $couponId)->where('uid', $uid)->find();
  350. if (!$couponInfo) return self::setErrorInfo('选择的优惠劵无效!', true);
  351. $coupons = StoreCouponUser::getUsableCouponList($uid, ['valid' => $cartInfo], $payPrice);
  352. $flag = false;
  353. foreach ($coupons as $coupon) {
  354. if ($coupon['id'] == $couponId) {
  355. $flag = true;
  356. continue;
  357. }
  358. }
  359. if (!$flag)
  360. return self::setErrorInfo('不满足优惠劵的使用条件!', true);
  361. $payPrice = (float)bcsub($payPrice, $couponInfo['coupon_price'], 2);
  362. $res1 = StoreCouponUser::useCoupon($couponId);
  363. $couponPrice = $couponInfo['coupon_price'];
  364. } else {
  365. $couponId = 0;
  366. $couponPrice = 0;
  367. }
  368. if (!$res1) return self::setErrorInfo('使用优惠劵失败!', true);
  369. //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
  370. $store_self_mention = sys_config('store_self_mention') ?? 0;
  371. if (!$store_self_mention) $shipping_type = 1;
  372. if ($shipping_type === 1) {
  373. //是否包邮
  374. if ((isset($other['offlinePostage']) && $other['offlinePostage'] && $payType == 'offline')) $payPostage = 0;
  375. $payPrice = (float)bcadd($payPrice, $payPostage, 2);
  376. } else if ($shipping_type === 2) {
  377. //门店自提没有邮费支付
  378. $priceGroup['storePostage'] = 0;
  379. $payPostage = 0;
  380. if (!$storeId && !$test) {
  381. return self::setErrorInfo('请选择门店', true);
  382. }
  383. }
  384. //积分抵扣
  385. $res2 = true;
  386. $SurplusIntegral = 0;
  387. if ($useIntegral && $userInfo['integral'] > 0) {
  388. $deductionPrice = (float)bcmul($userInfo['integral'], $other['integralRatio'], 2);
  389. if ($deductionPrice < $payPrice) {
  390. $payPrice = bcsub($payPrice, $deductionPrice, 2);
  391. $usedIntegral = $userInfo['integral'];
  392. $SurplusIntegral = 0;
  393. $res2 = false !== User::edit(['integral' => 0], $userInfo['uid'], 'uid');
  394. } else {
  395. $deductionPrice = $payPrice;
  396. $usedIntegral = (float)bcdiv($payPrice, $other['integralRatio'], 2);
  397. $SurplusIntegral = bcsub($userInfo['integral'], $usedIntegral, 2);
  398. $res2 = false !== User::bcDec($userInfo['uid'], 'integral', $usedIntegral, 'uid');
  399. $payPrice = 0;
  400. }
  401. $res2 = $res2 && false != UserBill::expend('积分抵扣', $uid, 'integral', 'deduction', $usedIntegral, $key, $userInfo['integral'], '购买商品使用' . floatval($usedIntegral) . '积分抵扣' . floatval($deductionPrice) . '元');
  402. } else {
  403. $deductionPrice = 0;
  404. $usedIntegral = 0;
  405. }
  406. if (!$res2) return self::setErrorInfo('使用积分抵扣失败!', true);
  407. if ($payPrice <= 0) $payPrice = 0;
  408. if ($test) {
  409. self::rollbackTrans();
  410. return [
  411. 'total_price' => $priceGroup['totalPrice'],
  412. 'pay_price' => $payPrice,
  413. 'pay_postage' => $payPostage,
  414. 'coupon_price' => $couponPrice,
  415. 'deduction_price' => $deductionPrice,
  416. 'SurplusIntegral' => $SurplusIntegral,
  417. ];
  418. }
  419. $orderInfo = [
  420. 'uid' => $uid,
  421. 'order_id' => $test ? 0 : self::getNewOrderId(),
  422. 'real_name' => $addressInfo['real_name'],
  423. 'user_phone' => $addressInfo['phone'],
  424. 'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
  425. 'cart_id' => $cartIds,
  426. 'total_num' => $totalNum,
  427. 'total_price' => $priceGroup['totalPrice'],
  428. 'total_postage' => $priceGroup['storePostage'],
  429. 'coupon_id' => $couponId,
  430. 'coupon_price' => $couponPrice,
  431. 'pay_price' => $payPrice,
  432. 'pay_postage' => $payPostage,
  433. 'deduction_price' => $deductionPrice,
  434. 'paid' => 0,
  435. 'pay_type' => $payType,
  436. 'use_integral' => $usedIntegral,
  437. 'gain_integral' => $gainIntegral,
  438. 'mark' => htmlspecialchars($mark),
  439. 'combination_id' => $combinationId,
  440. 'pink_id' => $pinkId,
  441. 'seckill_id' => $seckill_id,
  442. 'bargain_id' => $bargain_id,
  443. 'cost' => $priceGroup['costPrice'],
  444. 'is_channel' => $isChannel,
  445. 'add_time' => time(),
  446. 'unique' => $key,
  447. 'is_gift' => $other['is_gift'],
  448. 'shipping_type' => $shipping_type,
  449. 'province' => $addressInfo['province'],
  450. 'city' => $addressInfo['city'],
  451. 'district' => $addressInfo['district'],
  452. ];
  453. if ($shipping_type === 2) {
  454. $orderInfo['verify_code'] = self::getStoreCode();
  455. $orderInfo['store_id'] = SystemStore::getStoreDispose($storeId, 'id');
  456. if (!$orderInfo['store_id']) return self::setErrorInfo('暂无门店无法选择门店自提!', true);
  457. }
  458. $order = self::create($orderInfo);
  459. if (!$order) return self::setErrorInfo('订单生成失败!', true);
  460. $res5 = true;
  461. foreach ($cartInfo as $cart) {
  462. //减库存加销量
  463. if ($combinationId) $res5 = $res5 && StoreCombination::decCombinationStock($cart['cart_num'], $combinationId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  464. else if ($seckill_id) $res5 = $res5 && StoreSeckill::decSeckillStock($cart['cart_num'], $seckill_id, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  465. else if ($bargain_id) $res5 = $res5 && StoreBargain::decBargainStock($cart['cart_num'], $bargain_id, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  466. else $res5 = $res5 && StoreProduct::decProductStock($cart['cart_num'], $cart['productInfo']['id'], isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  467. }
  468. //保存购物车商品信息
  469. $res4 = false !== StoreOrderCartInfo::setCartInfo($order['id'], $cartInfo);
  470. //购物车状态修改
  471. $res6 = false !== StoreCart::where('id', 'IN', $cartIds)->update(['is_pay' => 1]);
  472. if (!$res4 || !$res5 || !$res6) return self::setErrorInfo('订单生成失败!', true);
  473. //自动设置默认地址
  474. UserRepository::storeProductOrderCreateEbApi($order, compact('cartInfo', 'addressId'));
  475. self::clearCacheOrderInfo($uid, $key);
  476. self::commitTrans();
  477. StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成');
  478. return $order;
  479. } catch (\PDOException $e) {
  480. self::rollbackTrans();
  481. return self::setErrorInfo('生成订单时SQL执行错误错误原因:' . $e->getMessage());
  482. } catch (\Exception $e) {
  483. self::rollbackTrans();
  484. return self::setErrorInfo('生成订单时系统错误错误原因:' . $e->getMessage());
  485. }
  486. }
  487. /**
  488. * 回退积分
  489. * @param $order 订单信息
  490. * @return bool
  491. */
  492. public static function RegressionIntegral($order)
  493. {
  494. if ($order['paid'] || $order['status'] == -2 || $order['is_del']) return true;
  495. if ($order['use_integral'] <= 0) return true;
  496. if ((int)$order['status'] != -2 && (int)$order['refund_status'] != 2 && $order['back_integral'] >= $order['use_integral']) return true;
  497. $res = User::bcInc($order['uid'], 'integral', $order['use_integral']);
  498. if (!$res) return self::setErrorInfo('回退积分增加失败');
  499. UserBill::income('积分回退', $order['uid'], 'integral', 'deduction', $order['use_integral'], $order['unique'], User::where('uid', $order['uid'])->value('integral'), '购买商品失败,回退积分' . floatval($order['use_integral']));
  500. return false !== self::where('order_id', $order['order_id'])->update(['back_integral' => $order['use_integral']]);
  501. }
  502. /**
  503. * 回退库存和销量
  504. * @param $order 订单信息
  505. * @return bool
  506. * @throws DataNotFoundException
  507. * @throws ModelNotFoundException
  508. * @throws \think\exception\DbException
  509. */
  510. public static function RegressionStock($order)
  511. {
  512. if ($order['paid'] || $order['status'] == -2 || $order['is_del']) return true;
  513. $combinationId = $order['combination_id'];
  514. $seckill_id = $order['seckill_id'];
  515. $bargain_id = $order['bargain_id'];
  516. $res5 = true;
  517. $cartInfo = StoreOrderCartInfo::where('cart_id', 'in', $order['cart_id'])->select();
  518. foreach ($cartInfo as $cart) {
  519. //增库存减销量
  520. if ($combinationId) $res5 = $res5 && StoreCombination::incCombinationStock($cart['cart_info']['cart_num'], $combinationId);
  521. else if ($seckill_id) $res5 = $res5 && StoreSeckill::incSeckillStock($cart['cart_info']['cart_num'], $seckill_id);
  522. else if ($bargain_id) $res5 = $res5 && StoreBargain::incBargainStock($cart['cart_info']['cart_num'], $bargain_id);
  523. else $res5 = $res5 && StoreProduct::incProductStock($cart['cart_info']['cart_num'], $cart['cart_info']['productInfo']['id'], isset($cart['cart_info']['productInfo']['attrInfo']) ? $cart['cart_info']['productInfo']['attrInfo']['unique'] : '');
  524. }
  525. return $res5;
  526. }
  527. /**
  528. * 回退优惠卷
  529. * @param $order 订单信息
  530. * @return bool
  531. */
  532. public static function RegressionCoupon($order)
  533. {
  534. if ($order['paid'] || $order['status'] == -2 || $order['is_del']) return true;
  535. $res = true;
  536. if ($order['coupon_id'] && StoreCouponUser::be(['id' => $order['coupon_id'], 'uid' => $order['uid'], 'status' => 1])) {
  537. $res = $res && false !== StoreCouponUser::where('id', $order['coupon_id'])->where('uid', $order['uid'])->update(['status' => 0, 'use_time' => 0]);
  538. }
  539. return $res;
  540. }
  541. /**
  542. * 取消订单
  543. * @param string order_id 订单id
  544. * @param $uid
  545. * @return bool
  546. * @throws DataNotFoundException
  547. * @throws ModelNotFoundException
  548. * @throws \think\exception\DbException
  549. */
  550. public static function cancelOrder($order_id, $uid)
  551. {
  552. $order = self::where('order_id', $order_id)->where('uid', $uid)->find();
  553. if (!$order) return self::setErrorInfo('没有查到此订单');
  554. self::beginTrans();
  555. try {
  556. $res = self::RegressionIntegral($order) && self::RegressionStock($order) && self::RegressionCoupon($order);
  557. $order->is_del = 1;
  558. if ($res && $order->save()) {
  559. self::commitTrans();
  560. return true;
  561. } else
  562. return false;
  563. } catch (\Exception $e) {
  564. self::rollbackTrans();
  565. return self::setErrorInfo(['line' => $e->getLine(), 'message' => $e->getMessage()]);
  566. }
  567. }
  568. /**
  569. * 生成订单唯一id
  570. * @param $uid 用户uid
  571. * @return string
  572. */
  573. public static function getNewOrderId()
  574. {
  575. do {
  576. list($msec, $sec) = explode(' ', microtime());
  577. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  578. $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  579. } while (self::be(['order_id' => $orderId]));// $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
  580. return $orderId;
  581. }
  582. /**
  583. * 修改订单号
  584. * @param $orderId
  585. * @return string
  586. */
  587. public static function changeOrderId($orderId)
  588. {
  589. $ymd = substr($orderId, 2, 8);
  590. $key = substr($orderId, 16);
  591. return 'wx' . $ymd . date('His') . $key;
  592. }
  593. /**
  594. * 查找购物车里的所有产品标题
  595. * @param $cartId 购物车id
  596. * @return bool|string
  597. */
  598. public static function getProductTitle($cartId)
  599. {
  600. $title = '';
  601. try {
  602. $orderCart = StoreOrderCartInfo::where('cart_id', 'in', $cartId)->field('cart_info')->select();
  603. foreach ($orderCart as $item) {
  604. if (isset($item['cart_info']['productInfo']['store_name'])) {
  605. $title .= $item['cart_info']['productInfo']['store_name'] . '|';
  606. }
  607. }
  608. unset($item);
  609. if (!$title) {
  610. $productIds = StoreCart::where('id', 'in', $cartId)->column('product_id');
  611. $productlist = ($productlist = StoreProduct::getProductField($productIds, 'store_name')) ? $productlist->toArray() : [];
  612. foreach ($productlist as $item) {
  613. if (isset($item['store_name'])) $title .= $item['store_name'] . '|';
  614. }
  615. }
  616. if ($title) $title = substr($title, 0, strlen($title) - 1);
  617. unset($item);
  618. } catch (\Exception $e) {
  619. }
  620. return $title;
  621. }
  622. /**
  623. * 获取门店自提唯一核销码
  624. * @return bool|string
  625. */
  626. public static function getStoreCode()
  627. {
  628. list($msec, $sec) = explode(' ', microtime());
  629. $num = bcadd(time(), mt_rand(10, 999999), 0) . '' . substr($msec, 2, 3);//生成随机数
  630. if (strlen($num) < 12)
  631. $num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT);
  632. else
  633. $num = substr($num, 0, 12);
  634. if (self::be(['verify_code' => $num])) return self::getStoreCode();
  635. return $num;
  636. }
  637. /**
  638. * 余额支付
  639. * @param $order_id
  640. * @param $uid
  641. * @param string $formId
  642. * @return bool
  643. * @throws \think\Exception
  644. * @throws DataNotFoundException
  645. * @throws ModelNotFoundException
  646. * @throws \think\exception\DbException
  647. */
  648. public static function yuePay($order_id, $uid, $formId = '')
  649. {
  650. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  651. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  652. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  653. // if($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  654. $userInfo = User::getUserInfo($uid);
  655. if ($userInfo['now_money'] < $orderInfo['pay_price'])
  656. return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_price'])]);
  657. self::beginTrans();
  658. $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
  659. $res2 = UserBill::expend('购买商品', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  660. $res3 = self::paySuccess($order_id, 'yue', $formId);//余额支付成功
  661. try {
  662. PaymentRepositories::yuePayProduct($userInfo, $orderInfo);
  663. } catch (\Exception $e) {
  664. self::rollbackTrans();
  665. return self::setErrorInfo($e->getMessage());
  666. }
  667. $res = $res1 && $res2 && $res3;
  668. self::checkTrans($res);
  669. return $res;
  670. }
  671. public static function pinkPay($order_id, $uid, $formId = '')
  672. {
  673. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  674. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  675. if (!$orderInfo['combination_id']) return self::setErrorInfo('拼团积分只可支付拼团订单!');
  676. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  677. $userInfo = User::getUserInfo($uid);
  678. if ($userInfo['pink_integral'] < $orderInfo['pay_price'])
  679. return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '拼团积分不足' . floatval($orderInfo['pay_price'])]);
  680. self::beginTrans();
  681. $res1 = false !== User::bcDec($uid, 'pink_integral', $orderInfo['pay_price'], 'uid');
  682. $res2 = UserBill::expend('购买商品', $uid, 'pink_integral', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['pink_integral'], '拼团积分支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  683. $res3 = self::paySuccess($order_id, 'pink_integral', $formId);//余额支付成功
  684. try {
  685. PaymentRepositories::yuePayProduct($userInfo, $orderInfo);
  686. } catch (\Exception $e) {
  687. self::rollbackTrans();
  688. return self::setErrorInfo($e->getMessage());
  689. }
  690. $res = $res1 && $res2 && $res3;
  691. self::checkTrans($res);
  692. return $res;
  693. }
  694. /**
  695. * 微信支付 为 0元时
  696. * @param $order_id
  697. * @param $uid
  698. * @param string $formId
  699. * @return bool
  700. * @throws \think\Exception
  701. * @throws DataNotFoundException
  702. * @throws ModelNotFoundException
  703. * @throws \think\exception\DbException
  704. */
  705. public static function jsPayPrice($order_id, $uid, $formId = '')
  706. {
  707. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  708. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  709. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  710. $userInfo = User::getUserInfo($uid);
  711. self::beginTrans();
  712. $res1 = UserBill::expend('购买商品', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  713. $res2 = self::paySuccess($order_id, 'weixin', $formId);//微信支付为0时
  714. $res = $res1 && $res2;
  715. self::checkTrans($res);
  716. return $res;
  717. }
  718. /**
  719. * 用户申请退款
  720. * @param $uni
  721. * @param $uid
  722. * @param string $refundReasonWap
  723. * @return bool
  724. */
  725. public static function orderApplyRefund($uni, $uid, $refundReasonWap = '', $refundReasonWapExplain = '', $refundReasonWapImg = [])
  726. {
  727. $order = self::getUserOrderDetail($uid, $uni);
  728. if (!$order) return self::setErrorInfo('支付订单不存在!');
  729. if ($order['refund_status'] == 2) return self::setErrorInfo('订单已退款!');
  730. if ($order['refund_status'] == 1) return self::setErrorInfo('正在申请退款中!');
  731. if ($order['is_gift']) return self::setErrorInfo('礼包订单不可退款!');
  732. if ($order['status'] == 1) return self::setErrorInfo('订单当前无法退款!');
  733. self::beginTrans();
  734. $res1 = false !== StoreOrderStatus::status($order['id'], 'apply_refund', '用户申请退款,原因:' . $refundReasonWap);
  735. $res2 = false !== self::edit(['refund_status' => 1, 'refund_reason_time' => time(), 'refund_reason_wap' => $refundReasonWap, 'refund_reason_wap_explain' => $refundReasonWapExplain, 'refund_reason_wap_img' => json_encode($refundReasonWapImg)], $order['id'], 'id');
  736. $res = $res1 && $res2;
  737. self::checkTrans($res);
  738. if (!$res)
  739. return self::setErrorInfo('申请退款失败!');
  740. else {
  741. try {
  742. if (in_array($order['is_channel'], [0, 2])) {
  743. //公众号发送模板消息通知客服
  744. WechatTemplateService::sendAdminNoticeTemplate([
  745. 'first' => "亲,有个订单申请退款 \n订单号:{$order['order_id']}",
  746. 'keyword1' => '退款申请',
  747. 'keyword2' => '已支付',
  748. 'keyword3' => date('Y/m/d H:i', time()),
  749. 'remark' => '请及时处理'
  750. ]);
  751. }
  752. if (in_array($order['is_channel'], [1, 2])) {
  753. //小程序 发送模板消息
  754. RoutineTemplate::sendOrderRefundStatus($order, $refundReasonWap);
  755. }
  756. //通知后台消息提醒
  757. ChannelService::instance()->send('NEW_REFUND_ORDER', ['order_id' => $order['order_id']]);
  758. } catch (\Exception $e) {
  759. }
  760. //发送短信
  761. event('ShortMssageSend', [$order['order_id'], 'AdminRefund']);
  762. return true;
  763. }
  764. }
  765. /**
  766. * //TODO 支付成功后
  767. * @param $orderId
  768. * @param string $paytype
  769. * @param string $formId
  770. * @return bool
  771. * @throws DataNotFoundException
  772. * @throws DbException
  773. * @throws ModelNotFoundException
  774. */
  775. public static function paySuccess($orderId, $paytype = 'weixin', $formId = '')
  776. {
  777. $order = self::where('order_id', $orderId)->find();
  778. $resPink = true;
  779. $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()]);//订单改为支付
  780. $res1 = UserSpread::setSpreadSure($order['uid']) && $res1;
  781. if ($order->combination_id && $res1 && !$order->refund_status) $resPink = StorePink::createPink($order);//创建拼团
  782. $oid = self::where('order_id', $orderId)->value('id');
  783. StoreOrderStatus::status($oid, 'pay_success', '用户付款成功');
  784. $now_money = User::where('uid', $order['uid'])->value('now_money');
  785. UserBill::expend('购买商品', $order['uid'], 'now_money', 'pay_money', $order['pay_price'], $order['id'], $now_money, '支付' . floatval($order['pay_price']) . '元购买商品');
  786. //支付成功后
  787. event('OrderPaySuccess', [$order, $formId]);
  788. if ($order['is_gift']) {
  789. TreeRecommend::insertTree($order['uid'], User::where('uid', $order['uid'])->value('spread_uid'));
  790. StorePink::pinkRecommend($order['id']);
  791. }
  792. $res = $res1 && $resPink && User::backOrderBrokerage($order) && self::areaAward($order);
  793. return false !== $res;
  794. }
  795. public static function areaAward($order)
  796. {
  797. if (isset($order['combination_id']) && $order['combination_id']) return true;
  798. if (isset($order['seckill_id']) && $order['seckill_id']) return true;
  799. if (isset($order['bargain_id']) && $order['bargain_id']) return true;
  800. $userInfo = User::getUserInfo($order['uid']);
  801. $cartId = is_string($order['cart_id']) ? json_decode($order['cart_id'], true) : $order['cart_id'];
  802. list($realPrice, $virtualPrice) = StoreProduct::getProductPrice($cartId);
  803. if ($realPrice <= 0 && $virtualPrice <= 0) return true;
  804. $province_admin_ratio = sys_config('province_admin_ratio', 0);
  805. $city_admin_ratio = sys_config('city_admin_ratio', 0);
  806. $district_admin_ratio = sys_config('district_admin_ratio', 0);
  807. $area_recommend_ratio = sys_config('area_recommend_ratio', 0);
  808. $res = true;
  809. $district = User::where('area_admin', 1)->where('area_province', $order['province'])->where('area_city', $order['city'])->where('area_district', $order['district'])->select();
  810. if (count($district) > 0) {
  811. $money_real = bcmul(bcdiv($district_admin_ratio, 100, 4), $realPrice, 2);
  812. $money_virtual = bcmul(bcdiv($district_admin_ratio, 100, 4), $virtualPrice, 2);
  813. $every_money_real = bcdiv($money_real, count($district), 2);
  814. $every_money_virtual = bcdiv($money_virtual, count($district), 2);
  815. if ($every_money_real <= 0 && $every_money_virtual <= 0) return true;
  816. $res = true;
  817. foreach ($district as $v) {
  818. if ($every_money_real > 0) {
  819. $balance = bcadd($v['brokerage_price'], $every_money_real, 2);
  820. $mark = $userInfo['nickname'] . '成功消费[实物产品]' . floatval($order['pay_price']) . '元,区代理分红' . floatval($every_money_real);
  821. $res1 = UserBill::income('区代理分红', $v['uid'], 'now_money', 'brokerage', $every_money_real, $order['id'], $balance, $mark, 0);
  822. $res = $res && $res1;
  823. $sp_money = bcmul($every_money_real, bcdiv($area_recommend_ratio, 100, 4), 2);
  824. if ($v['spread_uid'] && $sp_money > 0) {
  825. $sp = User::where('uid', $v['spread_uid']);
  826. $balance = bcadd($sp['brokerage_price'], $sp_money, 2);
  827. $mark = $userInfo['nickname'] . '成功消费[实物产品]' . floatval($order['pay_price']) . '元,推荐代理分红' . floatval($sp_money);
  828. $res1 = UserBill::income('推荐代理分红', $sp['uid'], 'now_money', 'brokerage', $sp_money, $order['id'], $balance, $mark, 0);
  829. $res = $res && $res1;
  830. }
  831. }
  832. if ($every_money_virtual > 0) {
  833. $balance = bcadd($v['brokerage_price'], $every_money_virtual, 2);
  834. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($order['pay_price']) . '元,区代理分红' . floatval($every_money_virtual);
  835. $res1 = UserBill::income('区代理分红', $v['uid'], 'now_money', 'brokerage', $every_money_virtual, $order['id'], $balance, $mark);
  836. $res2 = self::bcInc($v['uid'], 'brokerage_price', $every_money_virtual, 'uid');
  837. $res = $res && $res1 && $res2;
  838. $sp_money = bcmul($every_money_virtual, bcdiv($area_recommend_ratio, 100, 4), 2);
  839. if ($v['spread_uid'] && $sp_money > 0) {
  840. $sp = User::where('uid', $v['spread_uid']);
  841. $balance = bcadd($sp['brokerage_price'], $sp_money, 2);
  842. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($order['pay_price']) . '元,推荐代理分红' . floatval($sp_money);
  843. $res1 = UserBill::income('推荐代理分红', $sp['uid'], 'now_money', 'brokerage', $sp_money, $order['id'], $balance, $mark, 0);
  844. $res2 = self::bcInc($sp['uid'], 'brokerage_price', $sp_money, 'uid');
  845. $res = $res && $res1 && $res2;
  846. }
  847. }
  848. }
  849. }
  850. $city = User::where('area_admin', 2)->where('area_province', $order['province'])->where('area_city', $order['city'])->select();
  851. if (count($city) > 0) {
  852. $money_real = bcmul(bcdiv($city_admin_ratio, 100, 4), $realPrice, 2);
  853. $money_virtual = bcmul(bcdiv($city_admin_ratio, 100, 4), $virtualPrice, 2);
  854. $every_money_real = bcdiv($money_real, count($city), 2);
  855. $every_money_virtual = bcdiv($money_virtual, count($city), 2);
  856. if ($every_money_real <= 0 && $every_money_virtual <= 0) return true;
  857. $res = true;
  858. foreach ($city as $v) {
  859. if ($every_money_real > 0) {
  860. $balance = bcadd($v['brokerage_price'], $every_money_real, 2);
  861. $mark = $userInfo['nickname'] . '成功消费[实物产品]' . floatval($order['pay_price']) . '元,市代理分红' . floatval($every_money_real);
  862. $res1 = UserBill::income('市代理分红', $v['uid'], 'now_money', 'brokerage', $every_money_real, $order['id'], $balance, $mark, 0);
  863. $res = $res && $res1;
  864. $sp_money = bcmul($every_money_real, bcdiv($area_recommend_ratio, 100, 4), 2);
  865. if ($v['spread_uid'] && $sp_money > 0) {
  866. $sp = User::where('uid', $v['spread_uid']);
  867. $balance = bcadd($sp['brokerage_price'], $sp_money, 2);
  868. $mark = $userInfo['nickname'] . '成功消费[实物产品]' . floatval($order['pay_price']) . '元,推荐代理分红' . floatval($sp_money);
  869. $res1 = UserBill::income('推荐代理分红', $sp['uid'], 'now_money', 'brokerage', $sp_money, $order['id'], $balance, $mark, 0);
  870. $res = $res && $res1;
  871. }
  872. }
  873. if ($every_money_virtual > 0) {
  874. $balance = bcadd($v['brokerage_price'], $every_money_virtual, 2);
  875. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($order['pay_price']) . '元,市代理分红' . floatval($every_money_virtual);
  876. $res1 = UserBill::income('市代理分红', $v['uid'], 'now_money', 'brokerage', $every_money_virtual, $order['id'], $balance, $mark);
  877. $res2 = self::bcInc($v['uid'], 'brokerage_price', $every_money_virtual, 'uid');
  878. $res = $res && $res1 && $res2;
  879. $sp_money = bcmul($every_money_virtual, bcdiv($area_recommend_ratio, 100, 4), 2);
  880. if ($v['spread_uid'] && $sp_money > 0) {
  881. $sp = User::where('uid', $v['spread_uid']);
  882. $balance = bcadd($sp['brokerage_price'], $sp_money, 2);
  883. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($order['pay_price']) . '元,推荐代理分红' . floatval($sp_money);
  884. $res1 = UserBill::income('推荐代理分红', $sp['uid'], 'now_money', 'brokerage', $sp_money, $order['id'], $balance, $mark, 0);
  885. $res2 = self::bcInc($sp['uid'], 'brokerage_price', $sp_money, 'uid');
  886. $res = $res && $res1 && $res2;
  887. }
  888. }
  889. }
  890. }
  891. $province = User::where('area_admin', 3)->where('area_province', $order['province'])->select();
  892. if (count($province) > 0) {
  893. $money_real = bcmul(bcdiv($province_admin_ratio, 100, 4), $realPrice, 2);
  894. $money_virtual = bcmul(bcdiv($province_admin_ratio, 100, 4), $virtualPrice, 2);
  895. $every_money_real = bcdiv($money_real, count($province), 2);
  896. $every_money_virtual = bcdiv($money_virtual, count($province), 2);
  897. if ($every_money_real <= 0 && $every_money_virtual <= 0) return true;
  898. $res = true;
  899. foreach ($province as $v) {
  900. if ($every_money_real > 0) {
  901. $balance = bcadd($v['brokerage_price'], $every_money_real, 2);
  902. $mark = $userInfo['nickname'] . '成功消费[实物产品]' . floatval($order['pay_price']) . '元,省代理分红' . floatval($every_money_real);
  903. $res1 = UserBill::income('省代理分红', $v['uid'], 'now_money', 'brokerage', $every_money_real, $order['id'], $balance, $mark, 0);
  904. $res = $res && $res1;
  905. $sp_money = bcmul($every_money_real, bcdiv($area_recommend_ratio, 100, 4), 2);
  906. if ($v['spread_uid'] && $sp_money > 0) {
  907. $sp = User::where('uid', $v['spread_uid']);
  908. $balance = bcadd($sp['brokerage_price'], $sp_money, 2);
  909. $mark = $userInfo['nickname'] . '成功消费[实物产品]' . floatval($order['pay_price']) . '元,推荐代理分红' . floatval($sp_money);
  910. $res1 = UserBill::income('推荐代理分红', $sp['uid'], 'now_money', 'brokerage', $sp_money, $order['id'], $balance, $mark, 0);
  911. $res = $res && $res1;
  912. }
  913. }
  914. if ($every_money_virtual > 0) {
  915. $balance = bcadd($v['brokerage_price'], $every_money_virtual, 2);
  916. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($order['pay_price']) . '元,省代理分红' . floatval($every_money_virtual);
  917. $res1 = UserBill::income('省代理分红', $v['uid'], 'now_money', 'brokerage', $every_money_virtual, $order['id'], $balance, $mark);
  918. $res2 = self::bcInc($v['uid'], 'brokerage_price', $every_money_virtual, 'uid');
  919. $res = $res && $res1 && $res2;
  920. $sp_money = bcmul($every_money_virtual, bcdiv($area_recommend_ratio, 100, 4), 2);
  921. if ($v['spread_uid'] && $sp_money > 0) {
  922. $sp = User::where('uid', $v['spread_uid']);
  923. $balance = bcadd($sp['brokerage_price'], $sp_money, 2);
  924. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($order['pay_price']) . '元,推荐代理分红' . floatval($sp_money);
  925. $res1 = UserBill::income('推荐代理分红', $sp['uid'], 'now_money', 'brokerage', $sp_money, $order['id'], $balance, $mark, 0);
  926. $res2 = self::bcInc($sp['uid'], 'brokerage_price', $sp_money, 'uid');
  927. $res = $res && $res1 && $res2;
  928. }
  929. }
  930. }
  931. }
  932. return $res;
  933. }
  934. /*
  935. * 线下支付消息通知
  936. * 待完善
  937. *
  938. * */
  939. public static function createOrderTemplate($order)
  940. {
  941. //$goodsName = StoreOrderCartInfo::getProductNameList($order['id']);
  942. // RoutineTemplateService::sendTemplate(WechatUser::getOpenId($order['uid']),RoutineTemplateService::ORDER_CREATE, [
  943. // 'first'=>'亲,您购买的商品已支付成功',
  944. // 'keyword1'=>date('Y/m/d H:i',$order['add_time']),
  945. // 'keyword2'=>implode(',',$goodsName),
  946. // 'keyword3'=>$order['order_id'],
  947. // 'remark'=>'点击查看订单详情'
  948. // ],Url::build('/wap/My/order',['uni'=>$order['order_id']],true,true));
  949. // RoutineTemplateService::sendAdminNoticeTemplate([
  950. // 'first'=>"亲,您有一个新订单 \n订单号:{$order['order_id']}",
  951. // 'keyword1'=>'新订单',
  952. // 'keyword2'=>'线下支付',
  953. // 'keyword3'=>date('Y/m/d H:i',time()),
  954. // 'remark'=>'请及时处理'
  955. // ]);
  956. }
  957. /**
  958. * 获取订单详情
  959. * @param $uid
  960. * @param $key
  961. * @return array|\think\Model|null
  962. * @throws DataNotFoundException
  963. * @throws ModelNotFoundException
  964. * @throws \think\exception\DbException
  965. */
  966. public static function getUserOrderDetail($uid, $key)
  967. {
  968. return self::where('order_id|unique', $key)->where('uid', $uid)->where('is_del', 0)->find();
  969. }
  970. /**
  971. * TODO 订单发货
  972. * @param array $postageData 发货信息
  973. * @param string $oid orderID
  974. */
  975. public static function orderPostageAfter($postageData, $oid)
  976. {
  977. $order = self::where('id', $oid)->find();
  978. if ($postageData['delivery_type'] == 'send') {//送货
  979. RoutineTemplate::sendOrderPostage($order);
  980. } else if ($postageData['delivery_type'] == 'express') {//发货
  981. RoutineTemplate::sendOrderPostage($order, 1);
  982. }
  983. }
  984. /** 收货后发送模版消息
  985. * @param $order
  986. */
  987. public static function orderTakeAfter($order)
  988. {
  989. $title = self::getProductTitle($order['cart_id']);
  990. if ($order['is_channel'] == 1) {//小程序
  991. RoutineTemplate::sendOrderTakeOver($order, $title);
  992. } else {
  993. $openid = WechatUser::where('uid', $order['uid'])->value('openid');
  994. \crmeb\services\WechatTemplateService::sendTemplate($openid, \crmeb\services\WechatTemplateService::ORDER_TAKE_SUCCESS, [
  995. 'first' => '亲,您的订单已收货',
  996. 'keyword1' => $order['order_id'],
  997. 'keyword2' => '已收货',
  998. 'keyword3' => date('Y-m-d H:i:s', time()),
  999. 'keyword4' => $title,
  1000. 'remark' => '感谢您的光临!'
  1001. ]);
  1002. }
  1003. }
  1004. /**
  1005. * 删除订单
  1006. * @param $uni
  1007. * @param $uid
  1008. * @return bool
  1009. */
  1010. public static function removeOrder($uni, $uid)
  1011. {
  1012. $order = self::getUserOrderDetail($uid, $uni);
  1013. if (!$order) return self::setErrorInfo('订单不存在!');
  1014. $order = self::tidyOrder($order);
  1015. if ($order['_status']['_type'] != 0 && $order['_status']['_type'] != -2 && $order['_status']['_type'] != 4)
  1016. return self::setErrorInfo('该订单无法删除!');
  1017. if (false !== self::edit(['is_del' => 1], $order['id'], 'id') && false !== StoreOrderStatus::status($order['id'], 'remove_order', '删除订单')) {
  1018. //未支付和已退款的状态下才可以退积分退库存退优惠券
  1019. if ($order['_status']['_type'] == 0 || $order['_status']['_type'] == -2) {
  1020. event('StoreOrderRegressionAllAfter', [$order]);
  1021. }
  1022. event('UserOrderRemoved', $uni);
  1023. return true;
  1024. } else
  1025. return self::setErrorInfo('订单删除失败!');
  1026. }
  1027. /**
  1028. * //TODO 用户确认收货
  1029. * @param $uni
  1030. * @param $uid
  1031. */
  1032. public static function takeOrder($uni, $uid)
  1033. {
  1034. $order = self::getUserOrderDetail($uid, $uni);
  1035. if (!$order) return self::setErrorInfo('订单不存在!');
  1036. $order = self::tidyOrder($order);
  1037. if ($order['_status']['_type'] != 2) return self::setErrorInfo('订单状态错误!');
  1038. self::beginTrans();
  1039. if (false !== self::edit(['status' => 2], $order['id'], 'id') &&
  1040. false !== StoreOrderStatus::status($order['id'], 'user_take_delivery', '用户已收货')) {
  1041. try {
  1042. OrderRepository::storeProductOrderUserTakeDelivery($order, $uid);
  1043. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  1044. } catch (\Exception $e) {
  1045. self::rollbackTrans();
  1046. return self::setErrorInfo($e->getMessage());
  1047. }
  1048. self::commitTrans();
  1049. event('UserLevelAfter', [User::get($uni)]);
  1050. event('UserOrderTake', $uni);
  1051. //短信通知
  1052. event('ShortMssageSend', [$order['order_id'], ['Receiving', 'AdminConfirmTakeOver']]);
  1053. return true;
  1054. } else {
  1055. self::rollbackTrans();
  1056. return false;
  1057. }
  1058. }
  1059. /**
  1060. * 获取订单状态购物车等信息
  1061. * @param $order
  1062. * @param bool $detail 是否获取订单购物车详情
  1063. * @param bool $isPic 是否获取订单状态图片
  1064. * @return mixed
  1065. * @throws DataNotFoundException
  1066. * @throws ModelNotFoundException
  1067. * @throws \think\exception\DbException
  1068. */
  1069. public static function tidyOrder($order, $detail = false, $isPic = false)
  1070. {
  1071. if ($detail == true && isset($order['id'])) {
  1072. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('cart_info', 'unique') ?: [];
  1073. $info = [];
  1074. foreach ($cartInfo as $k => $cart) {
  1075. $cart = json_decode($cart, true);
  1076. $cart['unique'] = $k;
  1077. //新增是否评价字段
  1078. $cart['is_reply'] = StoreProductReply::where('unique', $k)->count();
  1079. array_push($info, $cart);
  1080. unset($cart);
  1081. }
  1082. $order['cartInfo'] = $info;
  1083. }
  1084. $status = [];
  1085. if (!$order['paid'] && $order['pay_type'] == 'offline' && !$order['status'] >= 2) {
  1086. $status['_type'] = 9;
  1087. $status['_title'] = '线下付款';
  1088. $status['_msg'] = '商家处理中,请耐心等待';
  1089. $status['_class'] = 'nobuy';
  1090. } else if (!$order['paid']) {
  1091. $status['_type'] = 0;
  1092. $status['_title'] = '未支付';
  1093. //系统预设取消订单时间段
  1094. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  1095. //获取配置
  1096. $systemValue = SystemConfigService::more($keyValue);
  1097. //格式化数据
  1098. $systemValue = self::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  1099. if ($order['pink_id'] || $order['combination_id']) {
  1100. $order_pink_time = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  1101. $time = bcadd($order['add_time'], $order_pink_time * 3600, 0);
  1102. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  1103. } else if ($order['seckill_id']) {
  1104. $order_seckill_time = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  1105. $time = bcadd($order['add_time'], $order_seckill_time * 3600, 0);
  1106. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  1107. } else if ($order['bargain_id']) {
  1108. $order_bargain_time = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  1109. $time = bcadd($order['add_time'], $order_bargain_time * 3600, 0);
  1110. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  1111. } else {
  1112. $time = bcadd($order['add_time'], $systemValue['order_cancel_time'] * 3600, 0);
  1113. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  1114. }
  1115. $status['_class'] = 'nobuy';
  1116. } else if ($order['refund_status'] == 1) {
  1117. $status['_type'] = -1;
  1118. $status['_title'] = '申请退款中';
  1119. $status['_msg'] = '商家审核中,请耐心等待';
  1120. $status['_class'] = 'state-sqtk';
  1121. } else if ($order['refund_status'] == 2) {
  1122. $status['_type'] = -2;
  1123. $status['_title'] = '已退款';
  1124. $status['_msg'] = '已为您退款,感谢您的支持';
  1125. $status['_class'] = 'state-sqtk';
  1126. } else if (!$order['status']) {
  1127. if ($order['pink_id']) {
  1128. if (StorePink::where('id', $order['pink_id'])->where('status', 1)->count()) {
  1129. $status['_type'] = 1;
  1130. $status['_title'] = '拼团中';
  1131. $status['_msg'] = '等待其他人参加拼团';
  1132. $status['_class'] = 'state-nfh';
  1133. } else {
  1134. $status['_type'] = 1;
  1135. $status['_title'] = '未发货';
  1136. $status['_msg'] = '商家未发货,请耐心等待';
  1137. $status['_class'] = 'state-nfh';
  1138. }
  1139. } else {
  1140. if ($order['shipping_type'] === 1) {
  1141. $status['_type'] = 1;
  1142. $status['_title'] = '未发货';
  1143. $status['_msg'] = '商家未发货,请耐心等待';
  1144. $status['_class'] = 'state-nfh';
  1145. } else {
  1146. $status['_type'] = 1;
  1147. $status['_title'] = '待核销';
  1148. $status['_msg'] = '待核销,请到核销点进行核销';
  1149. $status['_class'] = 'state-nfh';
  1150. }
  1151. }
  1152. } else if ($order['status'] == 1) {
  1153. if ($order['delivery_type'] == 'send') {//TODO 送货
  1154. $status['_type'] = 2;
  1155. $status['_title'] = '待收货';
  1156. $status['_msg'] = date('m月d日H时i分', StoreOrderStatus::getTime($order['id'], 'delivery')) . '服务商已送货';
  1157. $status['_class'] = 'state-ysh';
  1158. } else {//TODO 发货
  1159. $status['_type'] = 2;
  1160. $status['_title'] = '待收货';
  1161. if ($order['delivery_type'] == 'fictitious')
  1162. $_time = StoreOrderStatus::getTime($order['id'], 'delivery_fictitious');
  1163. else
  1164. $_time = StoreOrderStatus::getTime($order['id'], 'delivery_goods');
  1165. $status['_msg'] = date('m月d日H时i分', $_time) . '服务商已发货';
  1166. $status['_class'] = 'state-ysh';
  1167. }
  1168. } else if ($order['status'] == 2) {
  1169. $status['_type'] = 3;
  1170. $status['_title'] = '待评价';
  1171. $status['_msg'] = '已收货,快去评价一下吧';
  1172. $status['_class'] = 'state-ypj';
  1173. } else if ($order['status'] == 3) {
  1174. $status['_type'] = 4;
  1175. $status['_title'] = '交易完成';
  1176. $status['_msg'] = '交易完成,感谢您的支持';
  1177. $status['_class'] = 'state-ytk';
  1178. }
  1179. if (isset($order['pay_type']))
  1180. $status['_payType'] = isset(self::$payType[$order['pay_type']]) ? self::$payType[$order['pay_type']] : '其他方式';
  1181. if (isset($order['delivery_type']))
  1182. $status['_deliveryType'] = isset(self::$deliveryType[$order['delivery_type']]) ? self::$deliveryType[$order['delivery_type']] : '其他方式';
  1183. $order['_status'] = $status;
  1184. $order['_pay_time'] = isset($order['pay_time']) && $order['pay_time'] != null ? date('Y-m-d H:i:s', $order['pay_time']) : date('Y-m-d H:i:s', $order['add_time']);
  1185. $order['_add_time'] = isset($order['add_time']) ? (strstr($order['add_time'], '-') === false ? date('Y-m-d H:i:s', $order['add_time']) : $order['add_time']) : '';
  1186. $order['status_pic'] = '';
  1187. //获取产品状态图片
  1188. if ($isPic) {
  1189. $order_details_images = sys_data('order_details_images') ?: [];
  1190. foreach ($order_details_images as $image) {
  1191. if (isset($image['order_status']) && $image['order_status'] == $order['_status']['_type']) {
  1192. $order['status_pic'] = $image['pic'];
  1193. break;
  1194. }
  1195. }
  1196. }
  1197. $order['offlinePayStatus'] = (int)sys_config('offline_pay_status') ?? (int)2;
  1198. return $order;
  1199. }
  1200. /**
  1201. * 设置订单查询状态
  1202. * @param $status
  1203. * @param int $uid
  1204. * @param null $model
  1205. * @return StoreOrder|null
  1206. */
  1207. public static function statusByWhere($status, $uid = 0, $model = null)
  1208. {
  1209. // $orderId = StorePink::where('uid',$uid)->where('status',1)->column('order_id','id');//获取正在拼团的订单编号
  1210. if ($model == null) $model = new self;
  1211. if ('' === $status)
  1212. return $model;
  1213. else if ($status == 0)//未支付
  1214. return $model->where('paid', 0)->where('status', 0)->where('refund_status', 0);
  1215. else if ($status == 1)//待发货
  1216. return $model->where('paid', 1)->where('status', 0)->where('refund_status', 0);
  1217. else if ($status == 2)//待收货
  1218. return $model->where('paid', 1)->where('status', 1)->where('refund_status', 0);
  1219. else if ($status == 3)//待评价
  1220. return $model->where('paid', 1)->where('status', 2)->where('refund_status', 0);
  1221. else if ($status == 4)//已完成
  1222. return $model->where('paid', 1)->where('status', 3)->where('refund_status', 0);
  1223. else if ($status == -1)//退款中
  1224. return $model->where('paid', 1)->where('refund_status', 1);
  1225. else if ($status == -2)//已退款
  1226. return $model->where('paid', 1)->where('refund_status', 2);
  1227. else if ($status == -3)//退款
  1228. return $model->where('paid', 1)->where('refund_status', 'IN', '1,2');
  1229. // else if($status == 11){
  1230. // return $model->where('order_id','IN',implode(',',$orderId));
  1231. // }
  1232. else
  1233. return $model;
  1234. }
  1235. /**
  1236. * 获取订单并分页
  1237. * @param $uid
  1238. * @param string $status
  1239. * @param int $page
  1240. * @param int $limit
  1241. * @return mixed
  1242. * @throws DataNotFoundException
  1243. * @throws ModelNotFoundException
  1244. * @throws \think\exception\DbException
  1245. */
  1246. public static function getUserOrderList($uid, $status = '', $page = 0, $limit = 8)
  1247. {
  1248. if ($page) $list = self::statusByWhere($status, $uid)->where('is_del', 0)->where('uid', $uid)
  1249. ->field('add_time,seckill_id,bargain_id,combination_id,id,order_id,pay_price,total_num,total_price,pay_postage,total_postage,paid,status,refund_status,pay_type,coupon_price,deduction_price,pink_id,delivery_type,is_del,shipping_type')
  1250. ->order('add_time DESC')->page((int)$page, (int)$limit)->select()->toArray();
  1251. else $list = self::statusByWhere($status, $uid)->where('is_del', 0)->where('uid', $uid)
  1252. ->field('add_time,seckill_id,bargain_id,combination_id,id,order_id,pay_price,total_num,total_price,pay_postage,total_postage,paid,status,refund_status,pay_type,coupon_price,deduction_price,pink_id,delivery_type,is_del,shipping_type')
  1253. ->order('add_time DESC')->page((int)$page, (int)$limit)->select()->toArray();
  1254. foreach ($list as $k => $order) {
  1255. $list[$k] = self::tidyOrder($order, true);
  1256. }
  1257. return $list;
  1258. }
  1259. /**
  1260. * 获取推广人地下用户的订单金额
  1261. * @param string $uid
  1262. * @param string $status
  1263. * @return array
  1264. */
  1265. public static function getUserOrderCount($uid = '', $status = '')
  1266. {
  1267. $res = self::statusByWhere($status, $uid)->where('uid', 'IN', $uid)->column('pay_price');
  1268. return $res;
  1269. }
  1270. /**
  1271. * 搜索某个订单详细信息
  1272. * @param $uid
  1273. * @param $order_id
  1274. * @return bool|mixed
  1275. * @throws DataNotFoundException
  1276. * @throws ModelNotFoundException
  1277. * @throws \think\exception\DbException
  1278. */
  1279. public static function searchUserOrder($uid, $order_id)
  1280. {
  1281. $order = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->field('seckill_id,bargain_id,combination_id,id,order_id,pay_price,total_num,total_price,pay_postage,total_postage,paid,status,refund_status,pay_type,coupon_price,deduction_price,delivery_type,shipping_type')
  1282. ->order('add_time DESC')->find();
  1283. if (!$order)
  1284. return false;
  1285. else
  1286. return self::tidyOrder($order->toArray(), true);
  1287. }
  1288. /**
  1289. * 订单评价信息记录
  1290. * @param $oid
  1291. * @return StoreOrderStatus|\think\Model
  1292. * @throws \Exception
  1293. */
  1294. public static function orderOver($oid)
  1295. {
  1296. $res = self::edit(['status' => '3'], $oid, 'id');
  1297. if (!$res) exception('评价后置操作失败!');
  1298. return StoreOrderStatus::status($oid, 'check_order_over', '用户评价');
  1299. }
  1300. /**
  1301. * 设置订单产品评价完毕事件
  1302. * @param $oid
  1303. * @return StoreOrderStatus|\think\Model
  1304. * @throws \Exception
  1305. */
  1306. public static function checkOrderOver($oid)
  1307. {
  1308. $uniqueList = StoreOrderCartInfo::where('oid', $oid)->column('unique', 'unique');
  1309. //订单产品全部评价完成
  1310. if (StoreProductReply::where('unique', 'IN', $uniqueList)->where('oid', $oid)->count() == count($uniqueList)) {
  1311. event('StoreProductOrderOver', [$oid]);
  1312. return self::orderOver($oid);
  1313. }
  1314. }
  1315. public static function getOrderStatusNum($uid)
  1316. {
  1317. $noBuy = (int)self::where('uid', $uid)->where('paid', 0)->where('is_del', 0)->where('pay_type', '<>', 'offline')->count();
  1318. $noPostageNoPink = (int)self::where('o.uid', $uid)->alias('o')->where('o.paid', 1)->where('o.pink_id', 0)->where('o.is_del', 0)->where('o.status', 0)->where('o.pay_type', '<>', 'offline')->count();
  1319. $noPostageYesPink = (int)self::where('o.uid', $uid)->alias('o')->join('StorePink p', 'o.pink_id = p.id')->where('p.status', 2)->where('o.paid', 1)->where('o.is_del', 0)->where('o.status', 0)->where('o.pay_type', '<>', 'offline')->count();
  1320. $noPostage = (int)bcadd($noPostageNoPink, $noPostageYesPink, 0);
  1321. $noTake = (int)self::where('uid', $uid)->where('paid', 1)->where('is_del', 0)->where('status', 1)->where('pay_type', '<>', 'offline')->count();
  1322. $noReply = (int)self::where('uid', $uid)->where('paid', 1)->where('is_del', 0)->where('status', 2)->count();
  1323. $noPink = (int)self::where('o.uid', $uid)->alias('o')->join('StorePink p', 'o.pink_id = p.id')->where('p.status', 1)->where('o.paid', 1)->where('o.is_del', 0)->where('o.status', 0)->where('o.pay_type', '<>', 'offline')->count();
  1324. $noRefund = (int)self::where('uid', $uid)->where('paid', 1)->where('is_del', 0)->where('refund_status', 'IN', '1,2')->count();
  1325. return compact('noBuy', 'noPostage', 'noTake', 'noReply', 'noPink', 'noRefund');
  1326. }
  1327. /**
  1328. * 购买商品赠送积分
  1329. * @param $order
  1330. * @return bool
  1331. * @throws \think\Exception
  1332. * @throws DataNotFoundException
  1333. * @throws ModelNotFoundException
  1334. * @throws \think\exception\DbException
  1335. */
  1336. public static function gainUserIntegral($order)
  1337. {
  1338. if ($order['gain_integral'] > 0) {
  1339. $userInfo = User::getUserInfo($order['uid']);
  1340. BaseModel::beginTrans();
  1341. $res1 = false != User::where('uid', $userInfo['uid'])->update(['integral' => bcadd($userInfo['integral'], $order['gain_integral'], 2)]);
  1342. $res2 = false != UserBill::income('购买商品赠送积分', $order['uid'], 'integral', 'gain', $order['gain_integral'], $order['id'], $userInfo['integral'], '购买商品赠送' . floatval($order['gain_integral']) . '积分');
  1343. $res = $res1 && $res2;
  1344. BaseModel::checkTrans($res);
  1345. return $res;
  1346. }
  1347. return true;
  1348. }
  1349. /**
  1350. * 获取当前订单中有没有拼团存在
  1351. * @param $pid
  1352. * @return int|string
  1353. */
  1354. public static function getIsOrderPink($pid = 0, $uid = 0)
  1355. {
  1356. return self::where('uid', $uid)->where('pink_id', $pid)->where('refund_status', 0)->where('is_del', 0)->count();
  1357. }
  1358. /**
  1359. * 获取order_id
  1360. * @param $pid
  1361. * @return mixed
  1362. */
  1363. public static function getStoreIdPink($pid = 0, $uid = 0)
  1364. {
  1365. return self::where('uid', $uid)->where('pink_id', $pid)->where('is_del', 0)->value('order_id');
  1366. }
  1367. /**
  1368. * 删除当前用户拼团未支付的订单
  1369. */
  1370. public static function delCombination()
  1371. {
  1372. self::where('combination', '>', 0)->where('paid', 0)->where('uid', User::getActiveUid())->delete();
  1373. }
  1374. public static function getUserPrice($uid = 0)
  1375. {
  1376. if (!$uid) return 0;
  1377. $price = self::where('paid', 1)->where('uid', $uid)->where('status', 2)->where('refund_status', 0)->column('pay_price', 'id');
  1378. $count = 0;
  1379. if ($price) {
  1380. foreach ($price as $v) {
  1381. $count = bcadd($count, $v, 2);
  1382. }
  1383. }
  1384. return $count;
  1385. }
  1386. /**
  1387. * 个人中心获取个人订单列表和订单搜索
  1388. * @param int $uid 用户uid
  1389. * @param int | string 查找订单类型
  1390. * @param int $first 分页
  1391. * @param int 每页显示多少条
  1392. * @param string $search 订单号
  1393. * @return array
  1394. * */
  1395. public static function getUserOrderSearchList($uid, $type, $page, $limit, $search)
  1396. {
  1397. if ($search) {
  1398. $order = self::searchUserOrder($uid, $search) ?: [];
  1399. $list = $order == false ? [] : [$order];
  1400. } else {
  1401. $list = self::getUserOrderList($uid, $type, $page, $limit);
  1402. }
  1403. foreach ($list as $k => $order) {
  1404. $list[$k] = self::tidyOrder($order, true);
  1405. if ($list[$k]['_status']['_type'] == 3) {
  1406. foreach ($order['cartInfo'] ?: [] as $key => $product) {
  1407. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'], 'product');
  1408. $list[$k]['cartInfo'][$key]['add_time'] = isset($product['add_time']) ? date('Y-m-d H:i', $product['add_time']) : '时间错误';
  1409. }
  1410. }
  1411. }
  1412. return $list;
  1413. }
  1414. /**
  1415. * 获取用户下级的订单
  1416. * @param int $xuid 下级用户用户uid
  1417. * @param int $uid 用户uid
  1418. * @param int $type 订单类型
  1419. * @param int $first 截取行数
  1420. * @param int $limit 展示条数
  1421. * @return array
  1422. * */
  1423. public static function getSubordinateOrderlist($xUid, $uid, $type, $first, $limit)
  1424. {
  1425. $list = [];
  1426. if (!$xUid) {
  1427. $arr = User::getOneSpreadUid($uid);
  1428. foreach ($arr as $v) $list = StoreOrder::getUserOrderList($v, $type, $first, $limit);
  1429. } else $list = self::getUserOrderList($xUid, $type, $first, $limit);
  1430. foreach ($list as $k => $order) {
  1431. $list[$k] = self::tidyOrder($order, true);
  1432. if ($list[$k]['_status']['_type'] == 3) {
  1433. foreach ($order['cartInfo'] ?: [] as $key => $product) {
  1434. $list[$k]['cartInfo'][$key]['is_reply'] = StoreProductReply::isReply($product['unique'], 'product');
  1435. }
  1436. }
  1437. }
  1438. return $list;
  1439. }
  1440. /**
  1441. * 获取 今日 昨日 本月 订单金额
  1442. * @return mixed
  1443. */
  1444. public static function getOrderTimeData()
  1445. {
  1446. $to_day = strtotime(date('Y-m-d'));//今日
  1447. $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨日
  1448. $now_month = strtotime(date('Y-m'));//本月
  1449. //今日成交额
  1450. // $data['todayPrice'] = (float)number_format(self::where('is_del', 0)->where('pay_time', '>=', $to_day)->where('paid', 1)->where('refund_status', 0)->value('sum(pay_price)'), 2) ?? 0;
  1451. $data['todayPrice'] = number_format(self::where('is_del', 0)->where('pay_time', '>=', $to_day)->where('paid', 1)->where('refund_status', 0)->value('sum(pay_price)'), 2) ?? 0;
  1452. //今日订单数
  1453. $data['todayCount'] = self::where('is_del', 0)->where('pay_time', '>=', $to_day)->where('paid', 1)->where('refund_status', 0)->count();
  1454. //昨日成交额
  1455. $data['proPrice'] = number_format(self::where('is_del', 0)->where('pay_time', '<', $to_day)->where('pay_time', '>=', $pre_day)->where('paid', 1)->where('refund_status', 0)->value('sum(pay_price)'), 2) ?? 0;
  1456. //昨日订单数
  1457. $data['proCount'] = self::where('is_del', 0)->where('pay_time', '<', $to_day)->where('pay_time', '>=', $pre_day)->where('paid', 1)->where('refund_status', 0)->count();
  1458. //本月成交额
  1459. $data['monthPrice'] = number_format(self::where('is_del', 0)->where('pay_time', '>=', $now_month)->where('paid', 1)->where('refund_status', 0)->value('sum(pay_price)'), 2) ?? 0;
  1460. //本月订单数
  1461. $data['monthCount'] = self::where('is_del', 0)->where('pay_time', '>=', $now_month)->where('paid', 1)->where('refund_status', 0)->count();
  1462. return $data;
  1463. }
  1464. /**
  1465. * 获取某个用户的订单统计数据
  1466. * @param $uid
  1467. * @return mixed
  1468. */
  1469. public static function getOrderData($uid)
  1470. {
  1471. //订单支付没有退款 数量
  1472. $data['order_count'] = self::where('is_del', 0)->where('paid', 1)->where('uid', $uid)->where('refund_status', 0)->count();
  1473. //订单支付没有退款 支付总金额
  1474. $data['sum_price'] = self::where('is_del', 0)->where('paid', 1)->where('uid', $uid)->where('refund_status', 0)->sum('pay_price');
  1475. //订单待支付 数量
  1476. $data['unpaid_count'] = self::statusByWhere(0, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1477. //订单待发货 数量
  1478. $data['unshipped_count'] = self::statusByWhere(1, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1479. //订单待收货 数量
  1480. $data['received_count'] = self::statusByWhere(2, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1481. //订单待评价 数量
  1482. $data['evaluated_count'] = self::statusByWhere(3, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1483. //订单已完成 数量
  1484. $data['complete_count'] = self::statusByWhere(4, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1485. //订单退款
  1486. $data['refund_count'] = self::statusByWhere(-1, $uid)->where('is_del', 0)->where('uid', $uid)->count();
  1487. return $data;
  1488. }
  1489. /**
  1490. * 获取订单统计数据
  1491. * @param $uid
  1492. * @return mixed
  1493. */
  1494. public static function getOrderDataAdmin()
  1495. {
  1496. //订单支付没有退款 数量
  1497. $data['order_count'] = self::where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->count();
  1498. //订单支付没有退款 支付总金额
  1499. $data['sum_price'] = self::where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
  1500. //订单待支付 数量
  1501. $data['unpaid_count'] = self::statusByWhere(0, 0)->where('is_del', 0)->count();
  1502. //订单待发货 数量
  1503. $data['unshipped_count'] = self::statusByWhere(1, 0)->where('is_del', 0)->count();
  1504. //订单待收货 数量
  1505. $data['received_count'] = self::statusByWhere(2, 0)->where('is_del', 0)->count();
  1506. //订单待评价 数量
  1507. $data['evaluated_count'] = self::statusByWhere(3, 0)->where('is_del', 0)->count();
  1508. //订单已完成 数量
  1509. $data['complete_count'] = self::statusByWhere(4, 0)->where('is_del', 0)->count();
  1510. //订单退款 数量
  1511. $data['refund_count'] = self::statusByWhere(-3, 0)->where('is_del', 0)->count();
  1512. return $data;
  1513. }
  1514. /**
  1515. * 累计消费
  1516. * @param $uid
  1517. * @return float
  1518. */
  1519. public static function getOrderStatusSum($uid)
  1520. {
  1521. return self::where('uid', $uid)->where('is_del', 0)->where('paid', 1)->sum('pay_price');
  1522. }
  1523. public static function getPinkOrderId($id)
  1524. {
  1525. return self::where('id', $id)->value('order_id');
  1526. }
  1527. /**
  1528. * 未支付订单自动取消
  1529. * @param int $limit 分页截取条数
  1530. * @param string $prefid 缓存名称
  1531. * @param int $expire 缓存时间
  1532. * @return string|null
  1533. * @throws \Psr\SimpleCache\InvalidArgumentException
  1534. */
  1535. public static function orderUnpaidCancel()
  1536. {
  1537. //系统预设取消订单时间段
  1538. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  1539. //获取配置
  1540. $systemValue = SystemConfigService::more($keyValue);
  1541. //格式化数据
  1542. $systemValue = self::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  1543. //检查是否有未支付的订单 未支付查询条件
  1544. $unPidCount = self::where('paid', 0)->where('pay_type', '<>', 'offline')->where('is_del', 0)->where('status', 0)->where('refund_status', 0)->count();
  1545. if (!$unPidCount) return null;
  1546. try {
  1547. $res = true;
  1548. // 未支付查询条件
  1549. $orderList = self::where('paid', 0)->where('pay_type', '<>', 'offline')->where('is_del', 0)->where('status', 0)->where('refund_status', 0)->field('add_time,pink_id,order_id,seckill_id,bargain_id,combination_id,status,cart_id,use_integral,refund_status,uid,unique,back_integral,coupon_id,paid,is_del')->select();
  1550. foreach ($orderList as $order) {
  1551. if ($order['seckill_id']) {
  1552. //优先使用单独配置的过期时间
  1553. $order_seckill_time = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  1554. $res = $res && self::RegressionAll($order_seckill_time, $order);
  1555. unset($order_seckill_time);
  1556. } else if ($order['bargain_id']) {
  1557. $order_bargain_time = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  1558. $res = $res && self::RegressionAll($order_bargain_time, $order);
  1559. unset($order_bargain_time);
  1560. } else if ($order['pink_id'] || $order['combination_id']) {
  1561. $order_pink_time = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  1562. $res = $res && self::RegressionAll($order_pink_time, $order);
  1563. unset($order_pink_time);
  1564. } else {
  1565. $res = $res && self::RegressionAll($systemValue['order_cancel_time'], $order);
  1566. }
  1567. }
  1568. if (!$res) throw new \Exception('更新错误');
  1569. unset($orderList, $res, $pages);
  1570. return null;
  1571. } catch (PDOException $e) {
  1572. Log::error('未支付自动取消时发生数据库查询错误,错误原因为:' . $e->getMessage());
  1573. throw new \Exception($e->getMessage());
  1574. } catch (\think\Exception $e) {
  1575. Log::error('未支付自动取消时发生系统错误,错误原因为:' . $e->getMessage());
  1576. throw new \Exception($e->getMessage());
  1577. }
  1578. }
  1579. /**
  1580. * 未支付订单超过预设时间回退所有,如果不设置未支付过期时间,将不取消订单
  1581. * @param $time 预设时间
  1582. * @param $order 订单详情
  1583. * @return bool
  1584. * @throws DataNotFoundException
  1585. * @throws ModelNotFoundException
  1586. * @throws \think\exception\DbException
  1587. */
  1588. protected static function RegressionAll($time, $order)
  1589. {
  1590. if ($time == 0) return true;
  1591. if (($order['add_time'] + bcmul($time, 3600, 0)) < time()) {
  1592. $res1 = self::RegressionStock($order);
  1593. $res2 = self::RegressionIntegral($order);
  1594. $res3 = self::RegressionCoupon($order);
  1595. $res = $res1 && $res2 && $res3;
  1596. if ($res) $res = false !== self::where('order_id', $order['order_id'])->update(['is_del' => 1, 'mark' => '订单未支付已超过系统预设时间']);
  1597. unset($res1, $res2, $res3);
  1598. return $res;
  1599. } else
  1600. return true;
  1601. }
  1602. /**
  1603. * 格式化数据
  1604. * @param array $array 原本数据键
  1605. * @param $value 需要格式化的数据
  1606. * @param int $default 默认值
  1607. * @return mixed
  1608. */
  1609. protected static function setValeTime(array $array, $value, $default = 0)
  1610. {
  1611. foreach ($array as $item) {
  1612. if (!isset($value[$item]))
  1613. $value[$item] = $default;
  1614. else if (is_string($value[$item]))
  1615. $value[$item] = (float)$value[$item];
  1616. }
  1617. return $value;
  1618. }
  1619. public static function getOrderTotalPrice($cartInfo)
  1620. {
  1621. $totalPrice = 0;
  1622. foreach ($cartInfo as $cart) {
  1623. $totalPrice = bcadd($totalPrice, bcmul($cart['cart_num'], $cart['truePrice'], 2), 2);
  1624. }
  1625. return $totalPrice;
  1626. }
  1627. public static function getOrderCostPrice($cartInfo)
  1628. {
  1629. $costPrice = 0;
  1630. foreach ($cartInfo as $cart) {
  1631. $costPrice = bcadd($costPrice, bcmul($cart['cart_num'], $cart['costPrice'], 2), 2);
  1632. }
  1633. return $costPrice;
  1634. }
  1635. public static function getCombinationOrderCostPrice($cartInfo)
  1636. {
  1637. $costPrice = 0;
  1638. foreach ($cartInfo as $cart) {
  1639. if ($cart['combination_id']) {
  1640. $costPrice = bcadd($costPrice, bcmul($cart['cart_num'], StoreCombination::where('id', $cart['combination_id'])->value('price'), 2), 2);
  1641. }
  1642. }
  1643. return (float)$costPrice;
  1644. }
  1645. public static function yueRefundAfter($order)
  1646. {
  1647. }
  1648. /**
  1649. * 获取余额支付的金额
  1650. * @param $uid
  1651. * @return float|int
  1652. */
  1653. public static function getOrderStatusYueSum($uid)
  1654. {
  1655. return self::where('uid', $uid)->where('is_del', 0)->where('is_del', 0)->where('pay_type', 'yue')->where('paid', 1)->sum('pay_price');
  1656. }
  1657. /**
  1658. * 砍价支付成功订单数量
  1659. * @param $bargain
  1660. * @return int
  1661. */
  1662. public static function getBargainPayCount($bargain)
  1663. {
  1664. return self::where('bargain_id', $bargain)->where(['paid' => 1, 'refund_status' => 0])->count();
  1665. }
  1666. /**
  1667. * 7天自动收货
  1668. * @return bool
  1669. */
  1670. public static function startTakeOrder()
  1671. {
  1672. //7天前时间戳
  1673. $systemDeliveryTime = SystemConfigService::get('system_delivery_time') ?? 0;
  1674. //0为取消自动收货功能
  1675. if ($systemDeliveryTime == 0) return true;
  1676. $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemDeliveryTime . ' day')));
  1677. $model = new self;
  1678. $model = $model->alias('o');
  1679. $model = $model->join('StoreOrderStatus s', 's.oid=o.id');
  1680. $model = $model->where('o.paid', 1);
  1681. $model = $model->where('s.change_type', 'delivery_goods');
  1682. $model = $model->where('s.change_time', '<', $sevenDay);
  1683. $model = $model->where('o.status', 1);
  1684. $model = $model->where('o.refund_status', 0);
  1685. $model = $model->where('o.is_del', 0);
  1686. $orderInfo = $model->column('id', 'id');
  1687. if (!count($orderInfo)) return true;
  1688. $res = true;
  1689. foreach ($orderInfo as $key => &$item) {
  1690. $order = self::get($item);
  1691. if ($order['status'] == 2) continue;
  1692. if ($order['paid'] == 1 && $order['status'] == 1) $data['status'] = 2;
  1693. else if ($order['pay_type'] == 'offline') $data['status'] = 2;
  1694. else continue;
  1695. if (!self::edit($data, $item, 'id')) continue;
  1696. try {
  1697. OrderRepository::storeProductOrderTakeDeliveryAdmin($order, $item);
  1698. $res = $res && true;
  1699. } catch (\Exception $e) {
  1700. $res = $res && false;
  1701. }
  1702. $res = $res && StoreOrderStatus::status($item, 'take_delivery', '已收货[自动收货]');
  1703. }
  1704. if (!$res) {
  1705. throw new \Exception('');
  1706. }
  1707. }
  1708. /**
  1709. * 获取订单信息
  1710. * @param $id
  1711. * @param string $field
  1712. * @return array|null|\think\Model
  1713. * @throws DataNotFoundException
  1714. * @throws ModelNotFoundException
  1715. * @throws \think\exception\DbException
  1716. */
  1717. public static function getOrderInfo($id, $field = 'order_id')
  1718. {
  1719. return self::where('id', $id)->field($field)->find();
  1720. }
  1721. /**
  1722. * 订单每月统计数据
  1723. * @param $page
  1724. * @param $limit
  1725. * @return array
  1726. */
  1727. public static function getOrderDataPriceCount($page, $limit, $start, $stop)
  1728. {
  1729. if (!$limit) return [];
  1730. $model = new self;
  1731. if ($start != '' && $stop != '') $model = $model->where('pay_time', '>', $start)->where('pay_time', '<=', $stop);
  1732. $model = $model->field('sum(pay_price) as price,count(id) as count,FROM_UNIXTIME(pay_time, \'%m-%d\') as time');
  1733. $model = $model->where('is_del', 0);
  1734. $model = $model->where('paid', 1);
  1735. $model = $model->where('refund_status', 0);
  1736. $model = $model->group("FROM_UNIXTIME(pay_time, '%Y-%m-%d')");
  1737. $model = $model->order('pay_time DESC');
  1738. if ($page) $model = $model->page($page, $limit);
  1739. return $model->select();
  1740. }
  1741. /**
  1742. * 前台订单管理订单列表获取
  1743. * @param $where
  1744. * @return mixed
  1745. */
  1746. public static function orderList($where)
  1747. {
  1748. $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.id,a.order_id,a.add_time,a.status,a.total_num,a.total_price,a.total_postage,a.pay_price,a.pay_postage,a.paid,a.refund_status,a.remark,a.pay_type');
  1749. if ($where['order'] != '') {
  1750. $model = $model->order(self::setOrder($where['order']));
  1751. } else {
  1752. $model = $model->order('a.id desc');
  1753. }
  1754. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  1755. return self::tidyAdminOrder($data);
  1756. }
  1757. /**
  1758. * 前台订单管理 订单信息设置
  1759. * @param $data
  1760. * @param bool $status
  1761. * @return mixed
  1762. * @throws DataNotFoundException
  1763. * @throws ModelNotFoundException
  1764. * @throws \think\exception\DbException
  1765. */
  1766. public static function tidyAdminOrder($data, $status = false)
  1767. {
  1768. foreach ($data as &$item) {
  1769. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select()->toArray();
  1770. foreach ($_info as $k => $v) {
  1771. if (!is_array($v['cart_info']))
  1772. $_info[$k]['cart_info'] = json_decode($v['cart_info'], true);
  1773. }
  1774. foreach ($_info as $k => $v) {
  1775. unset($_info[$k]['cart_info']['type'], $_info[$k]['cart_info']['product_id'], $_info[$k]['cart_info']['combination_id'], $_info[$k]['cart_info']['seckill_id'], $_info[$k]['cart_info']['bargain_id'], $_info[$k]['cart_info']['bargain_id'], $_info[$k]['cart_info']['truePrice'], $_info[$k]['cart_info']['vip_truePrice'], $_info[$k]['cart_info']['trueStock'], $_info[$k]['cart_info']['costPrice'], $_info[$k]['cart_info']['productInfo']['id'], $_info[$k]['cart_info']['productInfo']['vip_price'], $_info[$k]['cart_info']['productInfo']['postage'], $_info[$k]['cart_info']['productInfo']['give_integral'], $_info[$k]['cart_info']['productInfo']['sales'], $_info[$k]['cart_info']['productInfo']['stock'], $_info[$k]['cart_info']['productInfo']['unit_name'], $_info[$k]['cart_info']['productInfo']['is_postage'], $_info[$k]['cart_info']['productInfo']['slider_image'], $_info[$k]['cart_info']['productInfo']['cost'], $_info[$k]['cart_info']['productInfo']['mer_id'], $_info[$k]['cart_info']['productInfo']['cate_id'], $_info[$k]['cart_info']['productInfo']['is_show'], $_info[$k]['cart_info']['productInfo']['store_info'], $_info[$k]['cart_info']['productInfo']['is_del'], $_info[$k]['cart_info']['is_pay'], $_info[$k]['cart_info']['is_del'], $_info[$k]['cart_info']['is_new'], $_info[$k]['cart_info']['add_time'], $_info[$k]['cart_info']['id'], $_info[$k]['cart_info']['uid'], $_info[$k]['cart_info']['product_attr_unique']);
  1776. $_info[$k]['cart_info']['productInfo']['suk'] = '';
  1777. if (isset($v['cart_info']['productInfo']['attrInfo'])) {
  1778. $_info[$k]['cart_info']['productInfo']['image'] = $_info[$k]['cart_info']['productInfo']['attrInfo']['image'];
  1779. $_info[$k]['cart_info']['productInfo']['price'] = $_info[$k]['cart_info']['productInfo']['attrInfo']['price'];
  1780. $_info[$k]['cart_info']['productInfo']['suk'] = $_info[$k]['cart_info']['productInfo']['attrInfo']['suk'];
  1781. unset($_info[$k]['cart_info']['productInfo']['attrInfo']);
  1782. }
  1783. if (!isset($v['cart_info']['productInfo']['ot_price'])) {
  1784. $_info[$k]['cart_info']['productInfo']['ot_price'] = $v['cart_info']['productInfo']['price'];
  1785. }
  1786. }
  1787. $item['_info'] = $_info;
  1788. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  1789. // if($item['pink_id'] || $item['combination_id']){
  1790. // $pinkStatus = StorePink::where('order_id_key',$item['id'])->value('status');
  1791. // switch ($pinkStatus){
  1792. // case 1:
  1793. // $item['pink_name'] = '[拼团订单]正在进行中';
  1794. // $item['color'] = '#f00';
  1795. // break;
  1796. // case 2:
  1797. // $item['pink_name'] = '[拼团订单]已完成';
  1798. // $item['color'] = '#00f';
  1799. // break;
  1800. // case 3:
  1801. // $item['pink_name'] = '[拼团订单]未完成';
  1802. // $item['color'] = '#f0f';
  1803. // break;
  1804. // default:
  1805. // $item['pink_name'] = '[拼团订单]历史订单';
  1806. // $item['color'] = '#457856';
  1807. // break;
  1808. // }
  1809. // }elseif ($item['seckill_id']){
  1810. // $item['pink_name'] = '[秒杀订单]';
  1811. // $item['color'] = '#32c5e9';
  1812. // }elseif ($item['bargain_id']){
  1813. // $item['pink_name'] = '[砍价订单]';
  1814. // $item['color'] = '#12c5e9';
  1815. // }else{
  1816. // $item['pink_name'] = '[普通订单]';
  1817. // $item['color'] = '#895612';
  1818. // }
  1819. // if($item['paid']==1){
  1820. // switch ($item['pay_type']){
  1821. // case 'weixin':
  1822. // $item['pay_type_name']='微信支付';
  1823. // break;
  1824. // case 'yue':
  1825. // $item['pay_type_name']='余额支付';
  1826. // break;
  1827. // case 'offline':
  1828. // $item['pay_type_name']='线下支付';
  1829. // break;
  1830. // default:
  1831. // $item['pay_type_name']='其他支付';
  1832. // break;
  1833. // }
  1834. // }else{
  1835. // switch ($item['pay_type']){
  1836. // default:
  1837. // $item['pay_type_name']='未支付';
  1838. // break;
  1839. // case 'offline':
  1840. // $item['pay_type_name']='线下支付';
  1841. // $item['pay_type_info']=1;
  1842. // break;
  1843. // }
  1844. // }
  1845. if ($status) {
  1846. $status = [];
  1847. if (!$item['paid'] && $item['pay_type'] == 'offline' && !$item['status'] >= 2) {
  1848. $status['_type'] = 9;
  1849. $status['_title'] = '线下付款';
  1850. $status['_msg'] = '商家处理中,请耐心等待';
  1851. $status['_class'] = 'nobuy';
  1852. } else if (!$item['paid']) {
  1853. $status['_type'] = 0;
  1854. $status['_title'] = '未支付';
  1855. //系统预设取消订单时间段
  1856. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  1857. //获取配置
  1858. $systemValue = SystemConfigService::more($keyValue);
  1859. //格式化数据
  1860. $systemValue = self::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  1861. if ($item['pink_id'] || $item['combination_id']) {
  1862. $order_pink_time = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  1863. $time = bcadd($item['add_time'], $order_pink_time * 3600, 0);
  1864. $status['_msg'] = '请在' . date('Y-m-d H:i:s', $time) . '前完成支付!';
  1865. } else if ($item['seckill_id']) {
  1866. $order_seckill_time = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  1867. $time = bcadd($item['add_time'], $order_seckill_time * 3600, 0);
  1868. $status['_msg'] = '请在' . date('Y-m-d H:i:s', $time) . '前完成支付!';
  1869. } else if ($item['bargain_id']) {
  1870. $order_bargain_time = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  1871. $time = bcadd($item['add_time'], $order_bargain_time * 3600, 0);
  1872. $status['_msg'] = '请在' . date('Y-m-d H:i:s', $time) . '前完成支付!';
  1873. } else {
  1874. $time = bcadd($item['add_time'], $systemValue['order_cancel_time'] * 3600, 0);
  1875. $status['_msg'] = '请在' . date('Y-m-d H:i:s', $time) . '前完成支付!';
  1876. }
  1877. $status['_class'] = 'nobuy';
  1878. } else if ($item['refund_status'] == 1) {
  1879. $status['_type'] = -1;
  1880. $status['_title'] = '申请退款中';
  1881. $status['_msg'] = '商家审核中,请耐心等待';
  1882. $status['_class'] = 'state-sqtk';
  1883. } else if ($item['refund_status'] == 2) {
  1884. $status['_type'] = -2;
  1885. $status['_title'] = '已退款';
  1886. $status['_msg'] = '已为您退款,感谢您的支持';
  1887. $status['_class'] = 'state-sqtk';
  1888. } else if (!$item['status']) {
  1889. if ($item['pink_id']) {
  1890. if (StorePink::where('id', $item['pink_id'])->where('status', 1)->count()) {
  1891. $status['_type'] = 11;
  1892. $status['_title'] = '拼团中';
  1893. $status['_msg'] = '等待其他人参加拼团';
  1894. $status['_class'] = 'state-nfh';
  1895. } else {
  1896. $status['_type'] = 1;
  1897. $status['_title'] = '未发货';
  1898. $status['_msg'] = '商家未发货,请耐心等待';
  1899. $status['_class'] = 'state-nfh';
  1900. }
  1901. } else {
  1902. $status['_type'] = 1;
  1903. $status['_title'] = '未发货';
  1904. $status['_msg'] = '商家未发货,请耐心等待';
  1905. $status['_class'] = 'state-nfh';
  1906. }
  1907. } else if ($item['status'] == 1) {
  1908. if ($item['delivery_type'] == 'send') {//TODO 送货
  1909. $status['_type'] = 2;
  1910. $status['_title'] = '待收货';
  1911. $status['_msg'] = date('m月d日H时i分', StoreOrderStatus::getTime($item['id'], 'delivery')) . '服务商已送货';
  1912. $status['_class'] = 'state-ysh';
  1913. } else {//TODO 发货
  1914. $status['_type'] = 2;
  1915. $status['_title'] = '待收货';
  1916. $status['_msg'] = date('m月d日H时i分', StoreOrderStatus::getTime($item['id'], 'delivery_goods')) . '服务商已发货';
  1917. $status['_class'] = 'state-ysh';
  1918. }
  1919. } else if ($item['status'] == 2) {
  1920. $status['_type'] = 3;
  1921. $status['_title'] = '待评价';
  1922. $status['_msg'] = '已收货,快去评价一下吧';
  1923. $status['_class'] = 'state-ypj';
  1924. } else if ($item['status'] == 3) {
  1925. $status['_type'] = 4;
  1926. $status['_title'] = '交易完成';
  1927. $status['_msg'] = '交易完成,感谢您的支持';
  1928. $status['_class'] = 'state-ytk';
  1929. }
  1930. if (isset($item['pay_type']))
  1931. $status['_payType'] = isset(self::$payType[$item['pay_type']]) ? self::$payType[$item['pay_type']] : '其他方式';
  1932. if (isset($item['delivery_type']))
  1933. $status['_deliveryType'] = isset(self::$deliveryType[$item['delivery_type']]) ? self::$deliveryType[$item['delivery_type']] : '其他方式';
  1934. $item['_status'] = $status;
  1935. } else {
  1936. if ($item['paid'] == 0 && $item['status'] == 0) {
  1937. $item['status_name'] = '未支付';
  1938. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  1939. $item['status_name'] = '未发货';
  1940. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0) {
  1941. $item['status_name'] = '待收货';
  1942. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  1943. $item['status_name'] = '待评价';
  1944. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  1945. $item['status_name'] = '已完成';
  1946. }
  1947. }
  1948. // unset($item['refund_status']);
  1949. // else if($item['paid']==1 && $item['refund_status']==1){
  1950. // $item['status_name']=<<<HTML
  1951. //<b style="color:#f124c7">申请退款</b><br/>
  1952. //<span>退款原因:{$item['refund_reason_wap']}</span>
  1953. //HTML;
  1954. // }else if($item['paid']==1 && $item['refund_status']==2){
  1955. // $item['status_name']='已退款';
  1956. // }
  1957. // if($item['paid']==0 && $item['status']==0 && $item['refund_status']==0){
  1958. // $item['_status']=1;
  1959. // }else if($item['paid']==1 && $item['status']==0 && $item['refund_status']==0){
  1960. // $item['_status']=2;
  1961. // }else if($item['paid']==1 && $item['refund_status']==1){
  1962. // $item['_status']=3;
  1963. // }else if($item['paid']==1 && $item['status']==1 && $item['refund_status']==0){
  1964. // $item['_status']=4;
  1965. // }else if($item['paid']==1 && $item['status']==2 && $item['refund_status']==0){
  1966. // $item['_status']=5;
  1967. // }else if($item['paid']==1 && $item['status']==3 && $item['refund_status']==0){
  1968. // $item['_status']=6;
  1969. // }else if($item['paid']==1 && $item['refund_status']==2){
  1970. // $item['_status']=7;
  1971. // }
  1972. }
  1973. return $data;
  1974. }
  1975. /**
  1976. * 处理where条件
  1977. * @param $where
  1978. * @param $model
  1979. * @param string $aler
  1980. * @param string $join
  1981. * @return StoreOrder|null
  1982. */
  1983. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  1984. {
  1985. if (isset($where['status']) && $where['status'] != '') $model = self::statusWhere($where['status'], $model, $aler);
  1986. if (isset($where['is_del']) && $where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where($aler . 'is_del', $where['is_del']);
  1987. if (isset($where['combination_id'])) {
  1988. if ($where['combination_id'] == '普通订单') {
  1989. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
  1990. }
  1991. if ($where['combination_id'] == '拼团订单') {
  1992. $model = $model->where($aler . 'combination_id', ">", 0)->where($aler . 'pink_id', ">", 0);
  1993. }
  1994. if ($where['combination_id'] == '秒杀订单') {
  1995. $model = $model->where($aler . 'seckill_id', ">", 0);
  1996. }
  1997. if ($where['combination_id'] == '砍价订单') {
  1998. $model = $model->where($aler . 'bargain_id', ">", 0);
  1999. }
  2000. }
  2001. if (isset($where['type'])) {
  2002. switch ($where['type']) {
  2003. case 1:
  2004. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
  2005. break;
  2006. case 2:
  2007. $model = $model->where($aler . 'combination_id', ">", 0);
  2008. break;
  2009. case 3:
  2010. $model = $model->where($aler . 'seckill_id', ">", 0);
  2011. break;
  2012. case 4:
  2013. $model = $model->where($aler . 'bargain_id', ">", 0);
  2014. break;
  2015. }
  2016. }
  2017. if (isset($where['real_name']) && $where['real_name'] != '')
  2018. $model = $model->where($aler . 'order_id|' . $aler . 'real_name|' . $aler . 'user_phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid' : ''), 'LIKE', "%$where[real_name]%");
  2019. if (isset($where['data']) && $where['data'] !== '')
  2020. $model = self::getModelTime($where, $model, $aler . 'add_time');
  2021. return $model;
  2022. }
  2023. /**
  2024. * 设置where条件
  2025. * @param $status
  2026. * @param null $model
  2027. * @param string $alert
  2028. * @return StoreOrder|null
  2029. */
  2030. public static function statusWhere($status, $model = null, $alert = '')
  2031. {
  2032. if ($model == null) $model = new self;
  2033. if ('' === $status)
  2034. return $model;
  2035. else if ($status == 0)//未支付
  2036. return $model->where($alert . 'paid', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  2037. else if ($status == 1)//已支付 未发货
  2038. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  2039. else if ($status == 2)//已支付 待收货
  2040. return $model->where($alert . 'paid', 1)->where($alert . 'status', 1)->where($alert . 'refund_status', 0);
  2041. else if ($status == 3)// 已支付 已收货 待评价
  2042. return $model->where($alert . 'paid', 1)->where($alert . 'status', 2)->where($alert . 'refund_status', 0);
  2043. else if ($status == 4)// 交易完成
  2044. return $model->where($alert . 'paid', 1)->where($alert . 'status', 3)->where($alert . 'refund_status', 0);
  2045. else if ($status == -1)//退款中
  2046. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 1);
  2047. else if ($status == -2)//已退款
  2048. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 2);
  2049. else if ($status == -3)//退款
  2050. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 'in', '1,2');
  2051. else
  2052. return $model;
  2053. }
  2054. /**
  2055. * 订单详情 管理员
  2056. * @param $orderId
  2057. * @param string $field
  2058. * @return array|null|\think\Model
  2059. * @throws DataNotFoundException
  2060. * @throws ModelNotFoundException
  2061. * @throws \think\exception\DbException
  2062. */
  2063. public static function getAdminOrderDetail($orderId, $field = '*')
  2064. {
  2065. return self::where('order_id', $orderId)->field($field)->find();
  2066. }
  2067. /**
  2068. * 获取指定时间区间的支付金额 管理员
  2069. * @param $start
  2070. * @param $stop
  2071. * @return float
  2072. */
  2073. public static function getOrderTimeBusinessVolumePrice($start, $stop)
  2074. {
  2075. return self::where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->where('add_time', '>=', $start)->where('add_time', '<', $stop)->sum('pay_price');
  2076. }
  2077. /**
  2078. * 获取指定时间区间的支付订单数量 管理员
  2079. * @param $start
  2080. * @param $stop
  2081. * @return float
  2082. */
  2083. public static function getOrderTimeBusinessVolumeNumber($start, $stop)
  2084. {
  2085. return self::where('is_del', 0)->where('paid', 1)->where('refund_status', 0)->where('add_time', '>=', $start)->where('add_time', '<', $stop)->count();
  2086. }
  2087. /**
  2088. * 获取当前时间到指定时间的支付金额 管理员
  2089. * @param $start 开始时间
  2090. * @param $stop 结束时间
  2091. * @return mixed
  2092. */
  2093. public static function chartTimePrice($start, $stop)
  2094. {
  2095. $model = new self;
  2096. $model = $model->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time');
  2097. $model = $model->where('is_del', 0);
  2098. $model = $model->where('paid', 1);
  2099. $model = $model->where('refund_status', 0);
  2100. $model = $model->where('add_time', '>=', $start);
  2101. $model = $model->where('add_time', '<', $stop);
  2102. $model = $model->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')");
  2103. $model = $model->order('add_time ASC');
  2104. return $model->select();
  2105. }
  2106. /**
  2107. * 获取当前时间到指定时间的支付订单数 管理员
  2108. * @param $start 开始时间
  2109. * @param $stop 结束时间
  2110. * @return mixed
  2111. */
  2112. public static function chartTimeNumber($start, $stop)
  2113. {
  2114. $model = new self;
  2115. $model = $model->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time');
  2116. $model = $model->where('is_del', 0);
  2117. $model = $model->where('paid', 1);
  2118. $model = $model->where('refund_status', 0);
  2119. $model = $model->where('add_time', '>=', $start);
  2120. $model = $model->where('add_time', '<', $stop);
  2121. $model = $model->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')");
  2122. $model = $model->order('add_time ASC');
  2123. return $model->select();
  2124. }
  2125. /**
  2126. * 修改支付方式为线下支付
  2127. * @param $orderId
  2128. * @return bool
  2129. */
  2130. public static function setOrderTypePayOffline($orderId)
  2131. {
  2132. return self::edit(['pay_type' => 'offline'], $orderId, 'order_id');
  2133. }
  2134. /**
  2135. * 线下付款
  2136. * @param $id
  2137. * @return $this
  2138. */
  2139. public static function updateOffline($id)
  2140. {
  2141. $count = self::where('id', $id)->count();
  2142. if (!$count) return self::setErrorInfo('订单不存在');
  2143. $count = self::where('id', $id)->where('paid', 0)->count();
  2144. if (!$count) return self::setErrorInfo('订单已支付');
  2145. $res = self::where('id', $id)->update(['paid' => 1, 'pay_time' => time()]);
  2146. return $res;
  2147. }
  2148. /**
  2149. * 向创建订单10分钟未付款的用户发送短信
  2150. */
  2151. public static function sendTen()
  2152. {
  2153. $list = self::where('paid', 0)->where('is_del', 0)->where('is_system_del', 0)->where('add_time', '>', time() - 900)->where('add_time', '<', time() - 600)->column('user_phone');
  2154. foreach ($list as $phone) {
  2155. ShortLetterRepositories::send(true, $phone, [], 'ORDER_PAY_FALSE');
  2156. }
  2157. }
  2158. public function productInfo()
  2159. {
  2160. return $this->hasMany(StoreProductReply::class, 'oid', 'id');
  2161. }
  2162. public static function setOrderProductReplyWhere($where)
  2163. {
  2164. $model = self::where('status', 3)->order('add_time desc')->whereIn('id', function ($query) {
  2165. $query->name('store_order')->alias('o')->join('store_product_reply a', 'a.oid = o.id')->group('o.id')->field('o.id')->select();
  2166. })->with('productInfo', function ($query) use ($where) {
  2167. $alias = '';
  2168. if (isset($where['title']) && $where['title'] != '')
  2169. $query->where("{$alias}comment", 'LIKE', "%$where[title]%");
  2170. if (isset($where['is_reply']) && $where['is_reply'] != '') {
  2171. if ($where['is_reply'] >= 0) {
  2172. $query->where("{$alias}is_reply", $where['is_reply']);
  2173. } else {
  2174. $query->where("{$alias}is_reply", '>', 0);
  2175. }
  2176. }
  2177. if (isset($where['producr_id']) && $where['producr_id'] != 0)
  2178. $query->where($alias . 'product_id', $where['producr_id']);
  2179. $query->where("{$alias}is_del", 0);
  2180. });
  2181. return $model;
  2182. }
  2183. public static function getOrderProductReplyList($where)
  2184. {
  2185. $list = self::setOrderProductReplyWhere($where)->page((int)$where['message_page'], (int)$where['limit'])->select();
  2186. $list = count($list) ? $list->toArray() : [];
  2187. foreach ($list as $key => $item) {
  2188. if (isset($item['productInfo']) && is_array($item['productInfo']) && count($item['productInfo'])) {
  2189. foreach ($item['productInfo'] as $k => $v) {
  2190. if (!$v['nickname'] && $v['uid']) {
  2191. $v['nickname'] = User::where('uid', $v['uid'])->value('nickname');
  2192. $v['avatar'] = User::where('uid', $v['uid'])->value('avatar');
  2193. }
  2194. $v['image'] = '';
  2195. $v['store_name'] = '';
  2196. if ($v['product_id']) {
  2197. $product = StoreProduct::where('id', $v['product_id'])->field(['image', 'store_name'])->find();
  2198. if ($product) {
  2199. $v['image'] = $product['image'];
  2200. $v['store_name'] = $product['store_name'];
  2201. }
  2202. }
  2203. $list[$key]['productInfo'][$k] = $v;
  2204. }
  2205. }
  2206. }
  2207. $count = self::setOrderProductReplyWhere($where)->count();
  2208. return compact('list', 'count');
  2209. }
  2210. }