StoreOrderServices.php 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\services\activity\combination\StorePinkServices;
  14. use app\services\activity\seckill\StoreSeckillServices;
  15. use app\services\BaseServices;
  16. use app\services\pay\PayServices;
  17. use app\services\product\product\StoreProductLogServices;
  18. use app\services\system\store\SystemStoreServices;
  19. use app\services\user\UserInvoiceServices;
  20. use app\services\user\UserServices;
  21. use app\services\product\product\StoreProductReplyServices;
  22. use app\services\user\UserAddressServices;
  23. use app\services\user\UserBillServices;
  24. use app\services\user\UserLevelServices;
  25. use app\services\wechat\WechatUserServices;
  26. use crmeb\services\CacheService;
  27. use crmeb\services\FormBuilder as Form;
  28. use crmeb\services\printer\Printer;
  29. use crmeb\services\SystemConfigService;
  30. use crmeb\traits\ServicesTrait;
  31. use crmeb\utils\Arr;
  32. use think\exception\ValidateException;
  33. use think\facade\Log;
  34. /**
  35. * Class StoreOrderServices
  36. * @package app\services\order
  37. * @method getOrderIdsCount(array $ids) 获取订单id下没有删除的订单数量
  38. * @method StoreOrderDao getUserOrderDetail(string $key, int $uid, array $with) 获取订单详情
  39. * @method chartTimePrice($start, $stop) 获取当前时间到指定时间的支付金额 管理员
  40. * @method chartTimeNumber($start, $stop) 获取当前时间到指定时间的支付订单数 管理员
  41. * @method together(array $where, string $field, string $together = 'sum') 聚合查询
  42. * @method getBuyCount($uid, $type, $typeId) 获取用户已购买此活动商品的个数
  43. * @method getDistinctCount(array $where, $field, ?bool $search = true)
  44. * @method getTrendData($time, $type, $timeType, $str) 用户趋势
  45. * @method getRegion($time, $channelType) 地域统计
  46. * @method getProductTrend($time, $timeType, $field, $str) 商品趋势
  47. */
  48. class StoreOrderServices extends BaseServices
  49. {
  50. use ServicesTrait;
  51. /**
  52. * 发货类型
  53. * @var string[]
  54. */
  55. public $deliveryType = ['send' => '商家配送', 'express' => '快递配送', 'fictitious' => '虚拟发货', 'delivery_part_split' => '拆分部分发货', 'delivery_split' => '拆分发货完成'];
  56. /**
  57. * StoreOrderProductServices constructor.
  58. * @param StoreOrderDao $dao
  59. */
  60. public function __construct(StoreOrderDao $dao)
  61. {
  62. $this->dao = $dao;
  63. }
  64. /**
  65. * 获取列表
  66. * @param array $where
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getOrderList(array $where, array $field = ['*'], array $with = [])
  73. {
  74. [$page, $limit] = $this->getPageValue();
  75. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  76. $count = $this->dao->count($where);
  77. $data = $this->tidyOrderList($data);
  78. foreach ($data as &$item) {
  79. $refund_num = array_sum(array_column($item['refund'], 'refund_num'));
  80. $cart_num = 0;
  81. foreach ($item['_info'] as $items) {
  82. $cart_num += $items['cart_info']['cart_num'];
  83. }
  84. $item['is_all_refund'] = $refund_num == $cart_num ? true : false;
  85. }
  86. return compact('data', 'count');
  87. }
  88. /**
  89. * 前端订单列表
  90. * @param array $where
  91. * @param array|string[] $field
  92. * @param array $with
  93. * @return array
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function getOrderApiList(array $where, array $field = ['*'], array $with = [])
  99. {
  100. [$page, $limit] = $this->getPageValue();
  101. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  102. foreach ($data as &$item) {
  103. $item = $this->tidyOrder($item, true);
  104. foreach ($item['cartInfo'] ?: [] as $key => $product) {
  105. if ($item['_status']['_type'] == 3) {
  106. $item['cartInfo'][$key]['add_time'] = isset($product['add_time']) ? date('Y-m-d H:i', (int)$product['add_time']) : '时间错误';
  107. }
  108. $item['cartInfo'][$key]['productInfo']['price'] = $product['truePrice'] ?? 0;
  109. }
  110. if (count($item['refund'])) {
  111. $refund_num = array_sum(array_column($item['refund'], 'refund_num'));
  112. $cart_num = array_sum(array_column($item['cartInfo'], 'cart_num'));
  113. $item['is_all_refund'] = $refund_num == $cart_num ? true : false;
  114. } else {
  115. $item['is_all_refund'] = false;
  116. }
  117. }
  118. return $data;
  119. }
  120. /**
  121. * 获取订单数量
  122. * @param int $uid
  123. * @return mixed
  124. */
  125. public function getOrderData(int $uid = 0, $del = true, $isPid = false)
  126. {
  127. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => [0, 3], 'pid' => 0];
  128. if (!$del) $where = $where + ['is_del' => 0, 'is_system_del' => 0];
  129. $data['order_count'] = (string)$this->dao->count($where);
  130. $data['sum_price'] = (string)$this->dao->sum([
  131. ['uid', '=', $uid],
  132. ['paid', '=', 1],
  133. ['refund_status', '=', 0],
  134. ['pid', '>=', 0]
  135. ], 'pay_price', false);
  136. $countWhere = ['is_del' => 0, 'is_system_del' => 0];
  137. if ($uid) {
  138. $countWhere['uid'] = $uid;
  139. }
  140. $data['unpaid_count'] = (string)$this->dao->count(['status' => 0] + $countWhere);
  141. $data['unshipped_count'] = (string)$this->dao->count(['status' => 1] + $countWhere + ['pid' => 0]);
  142. $data['received_count'] = (string)$this->dao->count(['status' => 2] + $countWhere + ['pid' => 0]);
  143. $data['evaluated_count'] = (string)$this->dao->count(['status' => 3] + $countWhere + ['pid' => 0]);
  144. $data['complete_count'] = (string)$this->dao->count(['status' => 4] + $countWhere + ['pid' => 0]);
  145. if (!$isPid) $countWhere = $countWhere + ['pid' => 0];
  146. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  147. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  148. $refund_where = ['is_cancel' => 0];
  149. if ($uid) $refund_where['uid'] = $uid;
  150. $data['refunding_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => [1, 2, 4, 5]]);
  151. $data['refunded_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => 6]);
  152. $data['refund_count'] = (string)bcadd($data['refunding_count'], $data['refunded_count'], 0);
  153. $data['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  154. $data['pay_weixin_open'] = (int)sys_config('pay_weixin_open') ?? 0;//微信支付 1 开启 0 关闭
  155. $data['ali_pay_status'] = (bool)sys_config('ali_pay_status');//支付包支付 1 开启 0 关闭
  156. return $data;
  157. }
  158. /**
  159. * 订单详情数据格式化
  160. * @param $order
  161. * @param bool $detail 是否需要订单商品详情
  162. * @param bool $isPic 是否需要订单状态图片
  163. * @return mixed
  164. */
  165. public function tidyOrder($order, bool $detail = false, $isPic = false)
  166. {
  167. if ($detail == true && isset($order['id'])) {
  168. /** @var StoreOrderCartInfoServices $cartServices */
  169. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  170. $cartInfos = $cartServices->getCartColunm(['oid' => $order['id']], 'cart_num,surplus_num,cart_info,refund_num', 'unique');
  171. $info = [];
  172. /** @var StoreProductReplyServices $replyServices */
  173. $replyServices = app()->make(StoreProductReplyServices::class);
  174. foreach ($cartInfos as $k => $cartInfo) {
  175. $cart = json_decode($cartInfo['cart_info'], true);
  176. $cart['cart_num'] = $cartInfo['cart_num'];
  177. $cart['surplus_num'] = $cartInfo['surplus_num'];
  178. $cart['refund_num'] = $cartInfo['refund_num'];
  179. $cart['surplus_refund_num'] = $cartInfo['surplus_num'] - $cartInfo['refund_num'];
  180. $cart['unique'] = $k;
  181. //新增是否评价字段
  182. $cart['is_reply'] = $replyServices->count(['unique' => $k]);
  183. if (isset($cart['productInfo']['attrInfo'])) {
  184. $cart['productInfo']['attrInfo'] = get_thumb_water($cart['productInfo']['attrInfo']);
  185. }
  186. $cart['productInfo'] = get_thumb_water($cart['productInfo']);
  187. //一种商品买多件 计算总优惠
  188. $cart['vip_sum_truePrice'] = bcmul($cart['vip_truePrice'], $cart['cart_num'] ? $cart['cart_num'] : 1, 2);
  189. $cart['is_valid'] = 1;
  190. array_push($info, $cart);
  191. unset($cart);
  192. }
  193. $order['cartInfo'] = $info;
  194. }
  195. /** @var StoreOrderStatusServices $statusServices */
  196. $statusServices = app()->make(StoreOrderStatusServices::class);
  197. $status = [];
  198. if (!$order['paid'] && $order['pay_type'] == 'offline' && !$order['status'] >= 2) {
  199. $status['_type'] = 9;
  200. $status['_title'] = '线下付款,未支付';
  201. $status['_msg'] = '等待商家处理,请耐心等待';
  202. $status['_class'] = 'nobuy';
  203. } else if (!$order['paid']) {
  204. $status['_type'] = 0;
  205. $status['_title'] = '未支付';
  206. //系统预设取消订单时间段
  207. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  208. //获取配置
  209. $systemValue = SystemConfigService::more($keyValue);
  210. //格式化数据
  211. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  212. if ($order['pink_id'] || $order['combination_id']) {
  213. $order_pink_time = $systemValue['order_pink_time'] ?: $systemValue['order_activity_time'];
  214. $time = $order['add_time'] + $order_pink_time * 3600;
  215. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  216. } else if ($order['seckill_id']) {
  217. $order_seckill_time = $systemValue['order_seckill_time'] ?: $systemValue['order_activity_time'];
  218. $time = $order['add_time'] + $order_seckill_time * 3600;
  219. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  220. } else if ($order['bargain_id']) {
  221. $order_bargain_time = $systemValue['order_bargain_time'] ?: $systemValue['order_activity_time'];
  222. $time = $order['add_time'] + $order_bargain_time * 3600;
  223. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  224. } else {
  225. $time = $order['add_time'] + $systemValue['order_cancel_time'] * 3600;
  226. $status['_msg'] = '请在' . date('m-d H:i:s', (int)$time) . '前完成支付!';
  227. }
  228. $status['_class'] = 'nobuy';
  229. } else if ($order['status'] == 4) {
  230. if ($order['delivery_type'] == 'send') {//TODO 送货
  231. $status['_type'] = 1;
  232. $status['_title'] = '待收货';
  233. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery'], 'change_time')) . '服务商已送货';
  234. $status['_class'] = 'state-ysh';
  235. } elseif ($order['delivery_type'] == 'express') {//TODO 发货
  236. $status['_type'] = 1;
  237. $status['_title'] = '待收货';
  238. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_goods'], 'change_time')) . '服务商已发货';
  239. $status['_class'] = 'state-ysh';
  240. } elseif ($order['delivery_type'] == 'split') {//拆分发货
  241. $status['_type'] = 1;
  242. $status['_title'] = '待收货';
  243. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_part_split'], 'change_time')) . '服务商已拆分多个包裹发货';
  244. $status['_class'] = 'state-ysh';
  245. } else {
  246. $status['_type'] = 1;
  247. $status['_title'] = '待收货';
  248. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_fictitious'], 'change_time')) . '服务商已虚拟发货';
  249. $status['_class'] = 'state-ysh';
  250. }
  251. } else if ($order['refund_status'] == 1) {
  252. if (in_array($order['refund_type'], [0, 1, 2])) {
  253. $status['_type'] = -1;
  254. $status['_title'] = '申请退款中';
  255. $status['_msg'] = '商家审核中,请耐心等待';
  256. $status['_class'] = 'state-sqtk';
  257. } elseif ($order['refund_type'] == 4) {
  258. $status['_type'] = -1;
  259. $status['_title'] = '申请退款中';
  260. $status['_msg'] = '商家同意退款,请填写退货订单号';
  261. $status['_class'] = 'state-sqtk';
  262. $status['refund_name'] = sys_config('refund_name', '');
  263. $status['refund_phone'] = sys_config('refund_phone', '');
  264. $status['refund_address'] = sys_config('refund_address', '');
  265. } elseif ($order['refund_type'] == 5) {
  266. $status['_type'] = -1;
  267. $status['_title'] = '申请退款中';
  268. $status['_msg'] = '等待商家收货';
  269. $status['_class'] = 'state-sqtk';
  270. $status['refund_name'] = sys_config('refund_name', '');
  271. $status['refund_phone'] = sys_config('refund_phone', '');
  272. $status['refund_address'] = sys_config('refund_address', '');
  273. }
  274. } else if ($order['refund_status'] == 2 || $order['refund_type'] == 6) {
  275. $status['_type'] = -2;
  276. $status['_title'] = '已退款';
  277. $status['_msg'] = '已为您退款,感谢您的支持';
  278. $status['_class'] = 'state-sqtk';
  279. } else if ($order['refund_status'] == 3) {
  280. $status['_type'] = -1;
  281. $status['_title'] = '部分退款(子订单)';
  282. $status['_msg'] = '拆分发货,部分退款';
  283. $status['_class'] = 'state-sqtk';
  284. } else if ($order['refund_status'] == 4) {
  285. $status['_type'] = -1;
  286. $status['_title'] = '子订单已全部申请退款中';
  287. $status['_msg'] = '拆分发货,全部退款';
  288. $status['_class'] = 'state-sqtk';
  289. } else if (!$order['status']) {
  290. if ($order['pink_id']) {
  291. /** @var StorePinkServices $pinkServices */
  292. $pinkServices = app()->make(StorePinkServices::class);
  293. if ($pinkServices->getCount(['id' => $order['pink_id'], 'status' => 1])) {
  294. $status['_type'] = 1;
  295. $status['_title'] = '拼团中';
  296. $status['_msg'] = '等待其他人参加拼团';
  297. $status['_class'] = 'state-nfh';
  298. } else {
  299. $status['_type'] = 1;
  300. $status['_title'] = '未发货';
  301. $status['_msg'] = '商家未发货,请耐心等待';
  302. $status['_class'] = 'state-nfh';
  303. }
  304. } else {
  305. if ($order['shipping_type'] === 1) {
  306. $status['_type'] = 1;
  307. $status['_title'] = '未发货';
  308. if ($order['advance_id']) {
  309. $status['_msg'] = date('Y-m-d', $order['cartInfo'][0]['productInfo']['presale_end_time']) . '预售结束后' . $order['cartInfo'][0]['productInfo']['presale_day'] . '天内发货,请耐心等待';
  310. } else {
  311. $status['_msg'] = '商家未发货,请耐心等待';
  312. }
  313. $status['_class'] = 'state-nfh';
  314. } else {
  315. $status['_type'] = 1;
  316. $status['_title'] = '待核销';
  317. $status['_msg'] = '待核销,请到核销点进行核销';
  318. $status['_class'] = 'state-nfh';
  319. }
  320. }
  321. } else if ($order['status'] == 1) {
  322. if ($order['delivery_type'] == 'send') {//TODO 送货
  323. $status['_type'] = 2;
  324. $status['_title'] = '待收货';
  325. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery'], 'change_time')) . '服务商已送货';
  326. $status['_class'] = 'state-ysh';
  327. } elseif ($order['delivery_type'] == 'express') {//TODO 发货
  328. $status['_type'] = 2;
  329. $status['_title'] = '待收货';
  330. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_goods'], 'change_time')) . '服务商已发货';
  331. $status['_class'] = 'state-ysh';
  332. } elseif ($order['delivery_type'] == 'split') {//拆分发货
  333. $status['_type'] = 2;
  334. $status['_title'] = '待收货';
  335. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_split'], 'change_time')) . '服务商已拆分多个包裹发货';
  336. $status['_class'] = 'state-ysh';
  337. } else {
  338. $status['_type'] = 2;
  339. $status['_title'] = '待收货';
  340. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_fictitious'], 'change_time')) . '服务商已虚拟发货';
  341. $status['_class'] = 'state-ysh';
  342. }
  343. } else if ($order['status'] == 2) {
  344. $status['_type'] = 3;
  345. $status['_title'] = '待评价';
  346. $status['_msg'] = '已收货,快去评价一下吧';
  347. $status['_class'] = 'state-ypj';
  348. } else if ($order['status'] == 3) {
  349. $status['_type'] = 4;
  350. $status['_title'] = '交易完成';
  351. $status['_msg'] = '交易完成,感谢您的支持';
  352. $status['_class'] = 'state-ytk';
  353. }
  354. if (isset($order['pay_type']))
  355. $status['_payType'] = $status['_type'] == 0 ? '' : PayServices::PAY_TYPE[$order['pay_type']] ?? '其他方式';
  356. if (isset($order['delivery_type']))
  357. $status['_deliveryType'] = $this->deliveryType[$order['delivery_type']] ?? '其他方式';
  358. $order['_status'] = $status;
  359. $order['_pay_time'] = isset($order['pay_time']) && $order['pay_time'] != null ? date('Y-m-d H:i:s', $order['pay_time']) : '';
  360. $order['_add_time'] = isset($order['add_time']) ? (strstr((string)$order['add_time'], '-') === false ? date('Y-m-d H:i:s', $order['add_time']) : $order['add_time']) : '';
  361. //系统预设取消订单时间段
  362. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  363. //获取配置
  364. $systemValue = SystemConfigService::more($keyValue);
  365. //格式化数据
  366. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  367. if ($order['seckill_id']) {
  368. $secs = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  369. } elseif ($order['bargain_id']) {
  370. $secs = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  371. } elseif ($order['combination_id']) {
  372. $secs = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  373. } else {
  374. $secs = $systemValue['order_cancel_time'];
  375. }
  376. $order['stop_time'] = $secs * 3600 + $order['add_time'];
  377. $order['status_pic'] = '';
  378. //获取商品状态图片
  379. if ($isPic) {
  380. $order_details_images = sys_data('order_details_images') ?: [];
  381. foreach ($order_details_images as $image) {
  382. if (isset($image['order_status']) && $image['order_status'] == $order['_status']['_type']) {
  383. $order['status_pic'] = $image['pic'];
  384. break;
  385. }
  386. }
  387. }
  388. if ($order['seckill_id'] || $order['bargain_id'] || $order['combination_id'] || $order['advance_id']) {
  389. if ($order['seckill_id']) $order['type'] = 1;
  390. if ($order['bargain_id']) $order['type'] = 2;
  391. if ($order['combination_id']) $order['type'] = 3;
  392. if ($order['advance_id']) $order['type'] = 4;
  393. }
  394. $order['offlinePayStatus'] = (int)sys_config('offline_pay_status') ?? (int)2;
  395. $log = $statusServices->getColumn(['oid' => $order['id']], 'change_time', 'change_type');
  396. if (isset($log['delivery'])) {
  397. $delivery = date('Y-m-d', $log['delivery']);
  398. } elseif (isset($log['delivery_goods'])) {
  399. $delivery = date('Y-m-d', $log['delivery_goods']);
  400. } elseif (isset($log['delivery_fictitious'])) {
  401. $delivery = date('Y-m-d', $log['delivery_fictitious']);
  402. } else {
  403. $delivery = '';
  404. }
  405. $order['order_log'] = [
  406. 'create' => isset($log['cache_key_create_order']) ? date('Y-m-d', $log['cache_key_create_order']) : '',
  407. 'pay' => isset($log['pay_success']) ? date('Y-m-d', $log['pay_success']) : '',
  408. 'delivery' => $delivery,
  409. 'take' => isset($log['take_delivery']) ? date('Y-m-d', $log['take_delivery']) : '',
  410. 'complete' => isset($log['check_order_over']) ? date('Y-m-d', $log['check_order_over']) : '',
  411. ];
  412. return $order;
  413. }
  414. /**
  415. * 数据转换
  416. * @param array $data
  417. * @return array
  418. */
  419. public function tidyOrderList(array $data)
  420. {
  421. /** @var StoreOrderCartInfoServices $services */
  422. $services = app()->make(StoreOrderCartInfoServices::class);
  423. foreach ($data as &$item) {
  424. $item['_info'] = $services->getOrderCartInfo((int)$item['id']);
  425. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  426. $item['_refund_time'] = isset($item['refund_reason_time']) && $item['refund_reason_time'] ? date('Y-m-d H:i:s', $item['refund_reason_time']) : '';
  427. $item['_pay_time'] = isset($item['pay_time']) && $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  428. if (($item['pink_id'] || $item['combination_id']) && isset($item['pinkStatus'])) {
  429. switch ($item['pinkStatus']) {
  430. case 1:
  431. $item['pink_name'] = '[拼团订单]正在进行中';
  432. $item['color'] = '#f00';
  433. break;
  434. case 2:
  435. $item['pink_name'] = '[拼团订单]已完成';
  436. $item['color'] = '#00f';
  437. break;
  438. case 3:
  439. $item['pink_name'] = '[拼团订单]未完成';
  440. $item['color'] = '#f0f';
  441. break;
  442. default:
  443. $item['pink_name'] = '[拼团订单]历史订单';
  444. $item['color'] = '#457856';
  445. break;
  446. }
  447. } elseif ($item['seckill_id']) {
  448. $item['pink_name'] = '[秒杀订单]';
  449. $item['color'] = '#32c5e9';
  450. } elseif ($item['bargain_id']) {
  451. $item['pink_name'] = '[砍价订单]';
  452. $item['color'] = '#12c5e9';
  453. } elseif ($item['advance_id']) {
  454. $item['pink_name'] = '[预售订单]';
  455. $item['color'] = '#12c5e9';
  456. } else {
  457. if ($item['shipping_type'] == 1) {
  458. $item['pink_name'] = '[普通订单]';
  459. $item['color'] = '#895612';
  460. } else if ($item['shipping_type'] == 2) {
  461. $item['pink_name'] = '[核销订单]';
  462. $item['color'] = '#8956E8';
  463. }
  464. }
  465. if ($item['paid'] == 1) {
  466. switch ($item['pay_type']) {
  467. case PayServices::WEIXIN_PAY:
  468. $item['pay_type_name'] = '微信支付';
  469. break;
  470. case PayServices::YUE_PAY:
  471. $item['pay_type_name'] = '余额支付';
  472. break;
  473. case PayServices::OFFLINE_PAY:
  474. $item['pay_type_name'] = '线下支付';
  475. break;
  476. case PayServices::ALIAPY_PAY:
  477. $item['pay_type_name'] = '支付宝支付';
  478. break;
  479. default:
  480. $item['pay_type_name'] = '其他支付';
  481. break;
  482. }
  483. } else {
  484. switch ($item['pay_type']) {
  485. case PayServices::WEIXIN_PAY:
  486. $item['pay_type_name'] = '微信未支付';
  487. break;
  488. case PayServices::ALIAPY_PAY:
  489. $item['pay_type_name'] = '支付宝未支付';
  490. break;
  491. case 'offline':
  492. $item['pay_type_name'] = '线下支付';
  493. $item['pay_type_info'] = 1;
  494. break;
  495. default:
  496. $item['pay_type_name'] = '未支付';
  497. break;
  498. }
  499. }
  500. $status_name = ['status_name' => '', 'pics' => []];
  501. if ($item['paid'] == 0 && $item['status'] == 0) {
  502. $status_name['status_name'] = '未支付';
  503. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  504. $status_name['status_name'] = '未发货';
  505. } else if ($item['paid'] == 1 && $item['status'] == 4 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  506. $status_name['status_name'] = '部分发货';
  507. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  508. $status_name['status_name'] = '未核销';
  509. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  510. $status_name['status_name'] = '待收货';
  511. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  512. $status_name['status_name'] = '未核销';
  513. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  514. $status_name['status_name'] = '待评价';
  515. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  516. $status_name['status_name'] = '已完成';
  517. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  518. $refundReasonTime = date('Y-m-d H:i', $item['refund_reason_time']);
  519. $refundReasonWapImg = json_decode($item['refund_reason_wap_img'], true);
  520. $refundReasonWapImg = $refundReasonWapImg ?: [];
  521. $img = [];
  522. if (count($refundReasonWapImg)) {
  523. foreach ($refundReasonWapImg as $itemImg) {
  524. if (strlen(trim($itemImg)))
  525. $img[] = $itemImg;
  526. }
  527. }
  528. $status_name['status_name'] = <<<HTML
  529. <b style="color:#f124c7">申请退款</b><br/>
  530. <span>退款原因:{$item['refund_reason_wap']}</span><br/>
  531. <span>备注说明:{$item['refund_reason_wap_explain']}</span><br/>
  532. <span>退款时间:{$refundReasonTime}</span><br/>
  533. <span>退款凭证:</span>
  534. HTML;
  535. $status_name['pics'] = $img;
  536. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  537. $status_name['status_name'] = '已退款';
  538. } else if ($item['paid'] == 1 && $item['refund_status'] == 3) {
  539. $status_name['status_name'] = <<<HTML
  540. <b style="color:#f124c7">部分退款</b><br/>
  541. HTML;
  542. } else if ($item['paid'] == 1 && $item['refund_status'] == 4) {
  543. $status_name['status_name'] = <<<HTML
  544. <b style="color:#f124c7">退款中</b><br/>
  545. HTML;
  546. }
  547. $item['status_name'] = $status_name;
  548. if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
  549. $item['_status'] = 1;//未支付
  550. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  551. $item['_status'] = 2;//已支付 未发货
  552. } else if ($item['paid'] == 1 && $item['status'] == 4 && $item['refund_status'] == 0) {
  553. $item['_status'] = 8;//已支付 部分发货
  554. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  555. $item['_status'] = 3;//已支付 申请退款中
  556. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0) {
  557. $item['_status'] = 4;//已支付 待收货
  558. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  559. $item['_status'] = 5;//已支付 待评价
  560. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  561. $item['_status'] = 6;//已支付 已完成
  562. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  563. $item['_status'] = 7;//已支付 已退款
  564. } else if ($item['paid'] == 1 && $item['refund_status'] == 3 && $item['status'] == 4) {
  565. $item['_status'] = 9;//拆单发货 部分申请退款
  566. } else if ($item['paid'] == 1 && $item['refund_status'] == 4) {
  567. $item['_status'] = 10;//拆单发货 已全部申请退款
  568. } else if ($item['paid'] == 1 && $item['refund_status'] == 3 && $item['status'] == 0) {
  569. $item['_status'] = 11;//拆单退款 未发货
  570. }
  571. if ($item['clerk_id'] == 0 && !isset($item['clerk_name'])) {
  572. $item['clerk_name'] = '总平台';
  573. }
  574. //根据核销员更改store_name
  575. if ($item['clerk_id'] && isset($item['staff_store_id']) && $item['staff_store_id']) {
  576. /** @var SystemStoreServices $store */
  577. $store = app()->make(SystemStoreServices::class);
  578. $storeOne = $store->value(['id' => $item['staff_store_id']], 'name');
  579. if ($storeOne) $item['store_name'] = $storeOne;
  580. }
  581. }
  582. return $data;
  583. }
  584. /**
  585. * 处理订单金额
  586. * @param $where
  587. * @return array
  588. */
  589. public function getOrderPrice($where)
  590. {
  591. if (isset($where['refund_type']) && $where['refund_type']) unset($where['refund_type']);
  592. $where['is_del'] = 0;//删除订单不统计
  593. $price['today_pay_price'] = 0;//今日支付金额
  594. $price['pay_price'] = 0;//支付金额
  595. $price['refund_price'] = 0;//退款金额
  596. $price['pay_price_wx'] = 0;//微信支付金额
  597. $price['pay_price_yue'] = 0;//余额支付金额
  598. $price['pay_price_offline'] = 0;//线下支付金额
  599. $price['pay_price_other'] = 0;//其他支付金额
  600. $price['use_integral'] = 0;//用户使用积分
  601. $price['back_integral'] = 0;//退积分总数
  602. $price['deduction_price'] = 0;//抵扣金额
  603. $price['total_num'] = 0; //商品总数
  604. $price['today_count_sum'] = 0; //今日订单总数
  605. $price['count_sum'] = 0; //订单总数
  606. $price['brokerage'] = 0;
  607. $price['pay_postage'] = 0;
  608. $whereData = ['is_del' => 0];
  609. if ($where['status'] == '' && $where['pay_type'] != 3) {
  610. $whereData['paid'] = 1;
  611. }
  612. $ids = $this->dao->column($where + $whereData, 'id');
  613. if (count($ids)) {
  614. /** @var UserBillServices $services */
  615. $services = app()->make(UserBillServices::class);
  616. $price['brokerage'] = $services->getBrokerageNumSum($ids);
  617. }
  618. $price['refund_price'] = $this->dao->together($where + ['is_del' => 0, 'paid' => 1, 'refund_status' => 2], 'refund_price');
  619. $sumNumber = $this->dao->search($where + $whereData)->field([
  620. 'sum(total_num) as sum_total_num',
  621. 'count(id) as count_sum',
  622. 'sum(pay_price) as sum_pay_price',
  623. 'sum(pay_postage) as sum_pay_postage',
  624. 'sum(use_integral) as sum_use_integral',
  625. 'sum(back_integral) as sum_back_integral',
  626. 'sum(deduction_price) as sum_deduction_price'
  627. ])->find();
  628. if ($sumNumber) {
  629. $price['count_sum'] = $sumNumber['count_sum'];
  630. $price['total_num'] = $sumNumber['sum_total_num'];
  631. $price['pay_price'] = $sumNumber['sum_pay_price'];
  632. $price['pay_postage'] = $sumNumber['sum_pay_postage'];
  633. $price['use_integral'] = $sumNumber['sum_use_integral'];
  634. $price['back_integral'] = $sumNumber['sum_back_integral'];
  635. $price['deduction_price'] = $sumNumber['sum_deduction_price'];
  636. }
  637. $list = $this->dao->column($where + $whereData, 'sum(pay_price) as sum_pay_price,pay_type', 'id', 'pay_type');
  638. foreach ($list as $v) {
  639. if ($v['pay_type'] == 'weixin') {
  640. $price['pay_price_wx'] = $v['sum_pay_price'];
  641. } elseif ($v['pay_type'] == 'yue') {
  642. $price['pay_price_yue'] = $v['sum_pay_price'];
  643. } elseif ($v['pay_type'] == 'offline') {
  644. $price['pay_price_offline'] = $v['sum_pay_price'];
  645. } else {
  646. $price['pay_price_other'] = $v['sum_pay_price'];
  647. }
  648. }
  649. $where['time'] = 'today';
  650. $sumNumber = $this->dao->search($where + $whereData)->field([
  651. 'count(id) as today_count_sum',
  652. 'sum(pay_price) as today_pay_price',
  653. ])->find();
  654. if ($sumNumber) {
  655. $price['today_count_sum'] = $sumNumber['today_count_sum'];
  656. $price['today_pay_price'] = $where['status'] !== 0 ? $sumNumber['today_pay_price'] : 0;
  657. }
  658. return $price;
  659. }
  660. /**
  661. * 获取订单列表页面统计数据
  662. * @param $where
  663. * @return array
  664. */
  665. public function getBadge($where)
  666. {
  667. $price = $this->getOrderPrice($where);
  668. return [
  669. [
  670. 'name' => '订单数量',
  671. 'field' => '件',
  672. 'count' => $price['count_sum'],
  673. 'className' => 'md-basket',
  674. 'col' => 6
  675. ],
  676. [
  677. 'name' => '订单金额',
  678. 'field' => '元',
  679. 'count' => $price['pay_price'],
  680. 'className' => 'md-pricetags',
  681. 'col' => 6
  682. ],
  683. [
  684. 'name' => '今日订单数量',
  685. 'field' => '件',
  686. 'count' => $price['today_count_sum'],
  687. 'className' => 'ios-chatbubbles',
  688. 'col' => 6
  689. ],
  690. [
  691. 'name' => '今日支付金额',
  692. 'field' => '元',
  693. 'count' => $price['today_pay_price'],
  694. 'className' => 'ios-cash',
  695. 'col' => 6
  696. ],
  697. ];
  698. }
  699. /**
  700. *
  701. * @param array $where
  702. * @return mixed
  703. */
  704. public function orderCount(array $where)
  705. {
  706. $where_one = ['time' => $where['time'], 'is_system_del' => 0, 'pid' => 0, 'status' => 1, 'shipping_type' => 1];
  707. $data['all'] = (string)$this->dao->count($where_one);
  708. $data['general'] = (string)$this->dao->count($where_one + ['type' => 1]);
  709. $data['pink'] = (string)$this->dao->count($where_one + ['type' => 2]);
  710. $data['seckill'] = (string)$this->dao->count($where_one + ['type' => 3]);
  711. $data['bargain'] = (string)$this->dao->count($where_one + ['type' => 4]);
  712. $data['advance'] = (string)$this->dao->count($where_one + ['type' => 5]);
  713. return $data;
  714. }
  715. /**
  716. * 创建修改订单表单
  717. * @param int $id
  718. * @return array
  719. * @throws \FormBuilder\Exception\FormBuilderException
  720. */
  721. public function updateForm(int $id)
  722. {
  723. $product = $this->dao->get($id);
  724. if (!$product) {
  725. throw new ValidateException('Data does not exist!');
  726. }
  727. $f = [];
  728. $f[] = Form::input('order_id', '订单编号', $product->getData('order_id'))->disabled(true);
  729. $f[] = Form::number('total_price', '商品总价', (float)$product->getData('total_price'))->min(0)->disabled(true);
  730. $f[] = Form::number('pay_postage', '支付邮费', (float)$product->getData('pay_postage') ?: 0)->disabled(true);
  731. $f[] = Form::number('pay_price', '实际支付金额', (float)$product->getData('pay_price'))->min(0);
  732. $f[] = Form::number('gain_integral', '赠送积分', (float)$product->getData('gain_integral') ?: 0);
  733. return create_form('修改订单', $f, $this->url('/order/update/' . $id), 'PUT');
  734. }
  735. /**
  736. * 修改订单
  737. * @param int $id
  738. * @param array $data
  739. * @return mixed
  740. * @throws \Exception
  741. */
  742. public function updateOrder(int $id, array $data)
  743. {
  744. $order = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  745. if (!$order) {
  746. throw new ValidateException('订单不存在或已删除');
  747. }
  748. /** @var StoreOrderCreateServices $createServices */
  749. $createServices = app()->make(StoreOrderCreateServices::class);
  750. $data['order_id'] = $createServices->getNewOrderId();
  751. /** @var StoreOrderStatusServices $services */
  752. $services = app()->make(StoreOrderStatusServices::class);
  753. return $this->transaction(function () use ($id, $data, $services) {
  754. $res = $this->dao->update($id, $data);
  755. $res = $res && $services->save([
  756. 'oid' => $id,
  757. 'change_type' => 'order_edit',
  758. 'change_time' => time(),
  759. 'change_message' => '修改商品总价为:' . $data['total_price'] . ' 实际支付金额' . $data['pay_price']
  760. ]);
  761. if ($res) {
  762. $order = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  763. //改价短信提醒
  764. event('notice.notice', [['order' => $order, 'pay_price' => $data['pay_price']], 'price_revision']);
  765. return true;
  766. } else {
  767. throw new ValidateException('Modification failed');
  768. }
  769. });
  770. }
  771. /**
  772. * 订单图表
  773. * @param $cycle
  774. * @return array
  775. */
  776. public function orderCharts($cycle)
  777. {
  778. $datalist = [];
  779. switch ($cycle) {
  780. case 'thirtyday':
  781. $datebefor = date('Y-m-d', strtotime('-30 day'));
  782. $dateafter = date('Y-m-d 23:59:59');
  783. //上期
  784. $pre_datebefor = date('Y-m-d', strtotime('-60 day'));
  785. $pre_dateafter = date('Y-m-d', strtotime('-30 day'));
  786. for ($i = -29; $i <= 0; $i++) {
  787. $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day'));
  788. }
  789. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, '30');
  790. if (empty($order_list)) return ['yAxis' => [], 'legend' => [], 'xAxis' => [], 'serise' => [], 'pre_cycle' => [], 'cycle' => []];
  791. foreach ($order_list as $k => &$v) {
  792. $order_list[$v['day']] = $v;
  793. }
  794. $cycle_list = [];
  795. foreach ($datalist as $dk => $dd) {
  796. if (!empty($order_list[$dd])) {
  797. $cycle_list[$dd] = $order_list[$dd];
  798. } else {
  799. $cycle_list[$dd] = ['count' => 0, 'day' => $dd, 'price' => ''];
  800. }
  801. }
  802. $chartdata = [];
  803. $data = [];//临时
  804. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  805. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  806. foreach ($cycle_list as $k => $v) {
  807. $data['day'][] = $v['day'];
  808. $data['count'][] = $v['count'];
  809. $data['price'][] = round($v['price'], 2);
  810. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  811. $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数
  812. if ($chartdata['yAxis']['maxprice'] < $v['price'])
  813. $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额
  814. }
  815. $chartdata['legend'] = ['订单金额', '订单数'];//分类
  816. $chartdata['xAxis'] = $data['day'];//X轴值
  817. $series1 = ['normal' => ['color' => [
  818. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  819. 'colorStops' => [
  820. [
  821. 'offset' => 0,
  822. 'color' => '#69cdff'
  823. ],
  824. [
  825. 'offset' => 0.5,
  826. 'color' => '#3eb3f7'
  827. ],
  828. [
  829. 'offset' => 1,
  830. 'color' => '#1495eb'
  831. ]
  832. ]
  833. ]]
  834. ];
  835. $series2 = ['normal' => ['color' => [
  836. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  837. 'colorStops' => [
  838. [
  839. 'offset' => 0,
  840. 'color' => '#6fdeab'
  841. ],
  842. [
  843. 'offset' => 0.5,
  844. 'color' => '#44d693'
  845. ],
  846. [
  847. 'offset' => 1,
  848. 'color' => '#2cc981'
  849. ]
  850. ]
  851. ]]
  852. ];
  853. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['price']];//分类1值
  854. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['count'], 'yAxisIndex' => 1];//分类2值
  855. //统计总数上期
  856. $pre_total = $this->dao->preTotalFind($pre_datebefor, $pre_dateafter);
  857. if ($pre_total) {
  858. $chartdata['pre_cycle']['count'] = [
  859. 'data' => $pre_total['count'] ?: 0
  860. ];
  861. $chartdata['pre_cycle']['price'] = [
  862. 'data' => $pre_total['price'] ?: 0
  863. ];
  864. }
  865. //统计总数
  866. $total = $this->dao->preTotalFind($datebefor, $dateafter);
  867. if ($total) {
  868. $cha_count = intval($pre_total['count']) - intval($total['count']);
  869. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  870. $chartdata['cycle']['count'] = [
  871. 'data' => $total['count'] ?: 0,
  872. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  873. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  874. ];
  875. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  876. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  877. $chartdata['cycle']['price'] = [
  878. 'data' => $total['price'] ?: 0,
  879. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  880. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  881. ];
  882. }
  883. return $chartdata;
  884. case 'week':
  885. $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']);
  886. $datebefor = date('Y-m-d', strtotime('-1 week Monday'));
  887. $dateafter = date('Y-m-d', strtotime('-1 week Sunday'));
  888. // $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, 'week');
  889. //数据查询重新处理
  890. $new_order_list = [];
  891. // foreach ($order_list as $k => $v) {
  892. // $new_order_list[$v['day']] = $v;
  893. // }
  894. $now_datebefor = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  895. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  896. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, 'week');
  897. //数据查询重新处理 key 变为当前值
  898. $new_now_order_list = [];
  899. foreach ($now_order_list as $k => $v) {
  900. $new_now_order_list[$v['day']] = $v;
  901. }
  902. foreach ($weekarray as $dk => $dd) {
  903. if (!empty($new_order_list[$dk])) {
  904. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  905. } else {
  906. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  907. }
  908. if (!empty($new_now_order_list[$dk])) {
  909. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  910. } else {
  911. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  912. }
  913. }
  914. $chartdata = [];
  915. $data = [];//临时
  916. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  917. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  918. foreach ($weekarray as $k => $v) {
  919. $data['day'][] = $v[0];
  920. $data['pre']['count'][] = $v['pre']['count'];
  921. $data['pre']['price'][] = round($v['pre']['price'], 2);
  922. $data['now']['count'][] = $v['now']['count'];
  923. $data['now']['price'][] = round($v['now']['price'], 2);
  924. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  925. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  926. }
  927. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  928. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  929. }
  930. }
  931. $chartdata['legend'] = ['上周金额', '本周金额', '上周订单数', '本周订单数'];//分类
  932. $chartdata['xAxis'] = $data['day'];//X轴值
  933. $series1 = ['normal' => ['color' => [
  934. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  935. 'colorStops' => [
  936. [
  937. 'offset' => 0,
  938. 'color' => '#69cdff'
  939. ],
  940. [
  941. 'offset' => 0.5,
  942. 'color' => '#3eb3f7'
  943. ],
  944. [
  945. 'offset' => 1,
  946. 'color' => '#1495eb'
  947. ]
  948. ]
  949. ]]
  950. ];
  951. $series2 = ['normal' => ['color' => [
  952. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  953. 'colorStops' => [
  954. [
  955. 'offset' => 0,
  956. 'color' => '#6fdeab'
  957. ],
  958. [
  959. 'offset' => 0.5,
  960. 'color' => '#44d693'
  961. ],
  962. [
  963. 'offset' => 1,
  964. 'color' => '#2cc981'
  965. ]
  966. ]
  967. ]]
  968. ];
  969. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  970. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值
  971. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  972. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  973. //统计总数上期
  974. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  975. if ($pre_total) {
  976. $chartdata['pre_cycle']['count'] = [
  977. 'data' => $pre_total['count'] ?: 0
  978. ];
  979. $chartdata['pre_cycle']['price'] = [
  980. 'data' => $pre_total['price'] ?: 0
  981. ];
  982. }
  983. //统计总数
  984. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  985. if ($total) {
  986. $cha_count = intval($pre_total['count']) - intval($total['count']);
  987. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  988. $chartdata['cycle']['count'] = [
  989. 'data' => $total['count'] ?: 0,
  990. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  991. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  992. ];
  993. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  994. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  995. $chartdata['cycle']['price'] = [
  996. 'data' => $total['price'] ?: 0,
  997. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  998. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  999. ];
  1000. }
  1001. return $chartdata;
  1002. case 'month':
  1003. $weekarray = array('01' => ['1'], '02' => ['2'], '03' => ['3'], '04' => ['4'], '05' => ['5'], '06' => ['6'], '07' => ['7'], '08' => ['8'], '09' => ['9'], '10' => ['10'], '11' => ['11'], '12' => ['12'], '13' => ['13'], '14' => ['14'], '15' => ['15'], '16' => ['16'], '17' => ['17'], '18' => ['18'], '19' => ['19'], '20' => ['20'], '21' => ['21'], '22' => ['22'], '23' => ['23'], '24' => ['24'], '25' => ['25'], '26' => ['26'], '27' => ['27'], '28' => ['28'], '29' => ['29'], '30' => ['30'], '31' => ['31']);
  1004. $datebefor = date('Y-m-01', strtotime('-1 month'));
  1005. $dateafter = date('Y-m-d', strtotime(date('Y-m-01')));
  1006. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, "month");
  1007. //数据查询重新处理
  1008. $new_order_list = [];
  1009. foreach ($order_list as $k => $v) {
  1010. $new_order_list[$v['day']] = $v;
  1011. }
  1012. $now_datebefor = date('Y-m-01');
  1013. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  1014. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, "month");
  1015. //数据查询重新处理 key 变为当前值
  1016. $new_now_order_list = [];
  1017. foreach ($now_order_list as $k => $v) {
  1018. $new_now_order_list[$v['day']] = $v;
  1019. }
  1020. foreach ($weekarray as $dk => $dd) {
  1021. if (!empty($new_order_list[$dk])) {
  1022. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  1023. } else {
  1024. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1025. }
  1026. if (!empty($new_now_order_list[$dk])) {
  1027. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  1028. } else {
  1029. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1030. }
  1031. }
  1032. $chartdata = [];
  1033. $data = [];//临时
  1034. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1035. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  1036. foreach ($weekarray as $k => $v) {
  1037. $data['day'][] = $v[0];
  1038. $data['pre']['count'][] = $v['pre']['count'];
  1039. $data['pre']['price'][] = round($v['pre']['price'], 2);
  1040. $data['now']['count'][] = $v['now']['count'];
  1041. $data['now']['price'][] = round($v['now']['price'], 2);
  1042. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  1043. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  1044. }
  1045. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  1046. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  1047. }
  1048. }
  1049. $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类
  1050. $chartdata['xAxis'] = $data['day'];//X轴值
  1051. $series1 = ['normal' => ['color' => [
  1052. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1053. 'colorStops' => [
  1054. [
  1055. 'offset' => 0,
  1056. 'color' => '#69cdff'
  1057. ],
  1058. [
  1059. 'offset' => 0.5,
  1060. 'color' => '#3eb3f7'
  1061. ],
  1062. [
  1063. 'offset' => 1,
  1064. 'color' => '#1495eb'
  1065. ]
  1066. ]
  1067. ]]
  1068. ];
  1069. $series2 = ['normal' => ['color' => [
  1070. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1071. 'colorStops' => [
  1072. [
  1073. 'offset' => 0,
  1074. 'color' => '#6fdeab'
  1075. ],
  1076. [
  1077. 'offset' => 0.5,
  1078. 'color' => '#44d693'
  1079. ],
  1080. [
  1081. 'offset' => 1,
  1082. 'color' => '#2cc981'
  1083. ]
  1084. ]
  1085. ]]
  1086. ];
  1087. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  1088. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值
  1089. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  1090. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  1091. //统计总数上期
  1092. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  1093. if ($pre_total) {
  1094. $chartdata['pre_cycle']['count'] = [
  1095. 'data' => $pre_total['count'] ?: 0
  1096. ];
  1097. $chartdata['pre_cycle']['price'] = [
  1098. 'data' => $pre_total['price'] ?: 0
  1099. ];
  1100. }
  1101. //统计总数
  1102. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  1103. if ($total) {
  1104. $cha_count = intval($pre_total['count']) - intval($total['count']);
  1105. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  1106. $chartdata['cycle']['count'] = [
  1107. 'data' => $total['count'] ?: 0,
  1108. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  1109. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  1110. ];
  1111. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  1112. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  1113. $chartdata['cycle']['price'] = [
  1114. 'data' => $total['price'] ?: 0,
  1115. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  1116. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  1117. ];
  1118. }
  1119. return $chartdata;
  1120. case 'year':
  1121. $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']);
  1122. $datebefor = date('Y-01-01', strtotime('-1 year'));
  1123. $dateafter = date('Y-12-31', strtotime('-1 year'));
  1124. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, 'year');
  1125. //数据查询重新处理
  1126. $new_order_list = [];
  1127. foreach ($order_list as $k => $v) {
  1128. $new_order_list[$v['day']] = $v;
  1129. }
  1130. $now_datebefor = date('Y-01-01');
  1131. $now_dateafter = date('Y-12-31 23:59:59');
  1132. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, 'year');
  1133. //数据查询重新处理 key 变为当前值
  1134. $new_now_order_list = [];
  1135. foreach ($now_order_list as $k => $v) {
  1136. $new_now_order_list[$v['day']] = $v;
  1137. }
  1138. foreach ($weekarray as $dk => $dd) {
  1139. if (!empty($new_order_list[$dk])) {
  1140. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  1141. } else {
  1142. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1143. }
  1144. if (!empty($new_now_order_list[$dk])) {
  1145. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  1146. } else {
  1147. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1148. }
  1149. }
  1150. $chartdata = [];
  1151. $data = [];//临时
  1152. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1153. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  1154. foreach ($weekarray as $k => $v) {
  1155. $data['day'][] = $v[0];
  1156. $data['pre']['count'][] = $v['pre']['count'];
  1157. $data['pre']['price'][] = round($v['pre']['price'], 2);
  1158. $data['now']['count'][] = $v['now']['count'];
  1159. $data['now']['price'][] = round($v['now']['price'], 2);
  1160. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  1161. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  1162. }
  1163. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  1164. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  1165. }
  1166. }
  1167. $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类
  1168. $chartdata['xAxis'] = $data['day'];//X轴值
  1169. $series1 = ['normal' => ['color' => [
  1170. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1171. 'colorStops' => [
  1172. [
  1173. 'offset' => 0,
  1174. 'color' => '#69cdff'
  1175. ],
  1176. [
  1177. 'offset' => 0.5,
  1178. 'color' => '#3eb3f7'
  1179. ],
  1180. [
  1181. 'offset' => 1,
  1182. 'color' => '#1495eb'
  1183. ]
  1184. ]
  1185. ]]
  1186. ];
  1187. $series2 = ['normal' => ['color' => [
  1188. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1189. 'colorStops' => [
  1190. [
  1191. 'offset' => 0,
  1192. 'color' => '#6fdeab'
  1193. ],
  1194. [
  1195. 'offset' => 0.5,
  1196. 'color' => '#44d693'
  1197. ],
  1198. [
  1199. 'offset' => 1,
  1200. 'color' => '#2cc981'
  1201. ]
  1202. ]
  1203. ]]
  1204. ];
  1205. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  1206. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['now']['price']];//分类1值
  1207. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  1208. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  1209. //统计总数上期
  1210. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  1211. if ($pre_total) {
  1212. $chartdata['pre_cycle']['count'] = [
  1213. 'data' => $pre_total['count'] ?: 0
  1214. ];
  1215. $chartdata['pre_cycle']['price'] = [
  1216. 'data' => $pre_total['price'] ?: 0
  1217. ];
  1218. }
  1219. //统计总数
  1220. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  1221. if ($total) {
  1222. $cha_count = intval($pre_total['count']) - intval($total['count']);
  1223. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  1224. $chartdata['cycle']['count'] = [
  1225. 'data' => $total['count'] ?: 0,
  1226. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  1227. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  1228. ];
  1229. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  1230. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  1231. $chartdata['cycle']['price'] = [
  1232. 'data' => $total['price'] ?: 0,
  1233. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  1234. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  1235. ];
  1236. }
  1237. return $chartdata;
  1238. default:
  1239. break;
  1240. }
  1241. }
  1242. /**
  1243. * 获取订单数量
  1244. * @return int
  1245. */
  1246. public function storeOrderCount()
  1247. {
  1248. return $this->dao->storeOrderCount();
  1249. }
  1250. /**
  1251. * 新订单ID
  1252. * @param $status
  1253. * @return array
  1254. */
  1255. public function newOrderId($status)
  1256. {
  1257. return $this->dao->search(['status' => $status, 'is_remind' => 0])->column('order_id', 'id');
  1258. }
  1259. /**
  1260. * 新订单修改
  1261. * @param $newOrderId
  1262. * @return \crmeb\basic\BaseModel
  1263. */
  1264. public function newOrderUpdate($newOrderId)
  1265. {
  1266. return $this->dao->newOrderUpdates($newOrderId);
  1267. }
  1268. /**
  1269. * 增长率
  1270. * @param $left
  1271. * @param $right
  1272. * @return int|string
  1273. */
  1274. public function growth($nowValue, $lastValue)
  1275. {
  1276. if ($lastValue == 0 && $nowValue == 0) return 0;
  1277. if ($lastValue == 0) return bcmul((string)$nowValue, '100', 2);
  1278. if ($nowValue == 0) return bcdiv(bcsub($nowValue, $lastValue, 2), $lastValue, 4) * 100;
  1279. return bcmul(bcdiv((bcsub($nowValue, $lastValue, 2)), $lastValue, 4), 100, 2);
  1280. }
  1281. public function homeStatics()
  1282. {
  1283. /** @var UserServices $uSercice */
  1284. $uSercice = app()->make(UserServices::class);
  1285. /** @var StoreProductLogServices $productLogServices */
  1286. $productLogServices = app()->make(StoreProductLogServices::class);
  1287. //TODO 销售额
  1288. //今日销售额
  1289. $today_sales = $this->dao->todaySales('today');
  1290. //昨日销售额
  1291. $yesterday_sales = $this->dao->todaySales('yesterday');
  1292. //日同比
  1293. $sales_today_ratio = $this->growth($today_sales, $yesterday_sales);
  1294. //周销售额
  1295. //本周
  1296. $this_week_sales = $this->dao->thisWeekSales('week');
  1297. //上周
  1298. $last_week_sales = $this->dao->thisWeekSales('last week');
  1299. //周同比
  1300. $sales_week_ratio = $this->growth($this_week_sales, $last_week_sales);
  1301. //总销售额
  1302. $total_sales = $this->dao->totalSales('month');
  1303. $sales = [
  1304. 'today' => $today_sales,
  1305. 'yesterday' => $yesterday_sales,
  1306. 'today_ratio' => $sales_today_ratio,
  1307. 'week' => $this_week_sales,
  1308. 'last_week' => $last_week_sales,
  1309. 'week_ratio' => $sales_week_ratio,
  1310. 'total' => $total_sales . '元',
  1311. 'date' => '昨日'
  1312. ];
  1313. //TODO:用户访问量
  1314. //今日访问量
  1315. $today_visits = $productLogServices->count(['time' => 'today', 'type' => 'visit']);
  1316. //昨日访问量
  1317. $yesterday_visits = $productLogServices->count(['time' => 'yesterday', 'type' => 'visit']);
  1318. //日同比
  1319. $visits_today_ratio = $this->growth($today_visits, $yesterday_visits);
  1320. //本周访问量
  1321. $this_week_visits = $productLogServices->count(['time' => 'week', 'type' => 'visit']);
  1322. //上周访问量
  1323. $last_week_visits = $productLogServices->count(['time' => 'last week', 'type' => 'visit']);
  1324. //周同比
  1325. $visits_week_ratio = $this->growth($this_week_visits, $last_week_visits);
  1326. //总访问量
  1327. $total_visits = $productLogServices->count(['time' => 'month', 'type' => 'visit']);
  1328. $visits = [
  1329. 'today' => $today_visits,
  1330. 'yesterday' => $yesterday_visits,
  1331. 'today_ratio' => $visits_today_ratio,
  1332. 'week' => $this_week_visits,
  1333. 'last_week' => $last_week_visits,
  1334. 'week_ratio' => $visits_week_ratio,
  1335. 'total' => $total_visits . 'Pv',
  1336. 'date' => '昨日'
  1337. ];
  1338. //TODO 订单量
  1339. //今日订单量
  1340. $today_order = $this->dao->todayOrderVisit('today', 1);
  1341. //昨日订单量
  1342. $yesterday_order = $this->dao->todayOrderVisit('yesterday', 1);
  1343. //订单日同比
  1344. $order_today_ratio = $this->growth($today_order, $yesterday_order);
  1345. //本周订单量
  1346. $this_week_order = $this->dao->todayOrderVisit('week', 2);
  1347. //上周订单量
  1348. $last_week_order = $this->dao->todayOrderVisit('last week', 2);
  1349. //订单周同比
  1350. $order_week_ratio = $this->growth($this_week_order, $last_week_order);
  1351. //总订单量
  1352. $total_order = $this->dao->count(['time' => 'month', 'paid' => 1, 'refund_status' => 0, 'pid' => 0]);
  1353. $order = [
  1354. 'today' => $today_order,
  1355. 'yesterday' => $yesterday_order,
  1356. 'today_ratio' => $order_today_ratio,
  1357. 'week' => $this_week_order,
  1358. 'last_week' => $last_week_order,
  1359. 'week_ratio' => $order_week_ratio,
  1360. 'total' => $total_order . '单',
  1361. 'date' => '昨日'
  1362. ];
  1363. //TODO 用户
  1364. //今日新增用户
  1365. $today_user = $uSercice->todayAddVisits('today', 1);
  1366. //昨日新增用户
  1367. $yesterday_user = $uSercice->todayAddVisits('yesterday', 1);
  1368. //新增用户日同比
  1369. $user_today_ratio = $this->growth($today_user, $yesterday_user);
  1370. //本周新增用户
  1371. $this_week_user = $uSercice->todayAddVisits('week', 2);
  1372. //上周新增用户
  1373. $last_week_user = $uSercice->todayAddVisits('last week', 2);
  1374. //新增用户周同比
  1375. $user_week_ratio = $this->growth($this_week_user, $last_week_user);
  1376. //所有用户
  1377. $total_user = $uSercice->count(['time' => 'month']);
  1378. $user = [
  1379. 'today' => $today_user,
  1380. 'yesterday' => $yesterday_user,
  1381. 'today_ratio' => $user_today_ratio,
  1382. 'week' => $this_week_user,
  1383. 'last_week' => $last_week_user,
  1384. 'week_ratio' => $user_week_ratio,
  1385. 'total' => $total_user . '人',
  1386. 'date' => '昨日'
  1387. ];
  1388. $info = array_values(compact('sales', 'visits', 'order', 'user'));
  1389. $info[0]['title'] = '销售额';
  1390. $info[1]['title'] = '用户访问量';
  1391. $info[2]['title'] = '订单量';
  1392. $info[3]['title'] = '新增用户';
  1393. $info[0]['total_name'] = '本月销售额';
  1394. $info[1]['total_name'] = '本月访问量';
  1395. $info[2]['total_name'] = '本月订单量';
  1396. $info[3]['total_name'] = '本月新增用户';
  1397. return $info;
  1398. }
  1399. /**
  1400. * 打印订单
  1401. * @param $order
  1402. * @param array $cartId
  1403. * @throws \think\db\exception\DataNotFoundException
  1404. * @throws \think\db\exception\DbException
  1405. * @throws \think\db\exception\ModelNotFoundException
  1406. */
  1407. public function orderPrint($order, array $cartId)
  1408. {
  1409. /** @var StoreOrderCartInfoServices $cartServices */
  1410. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  1411. $product = $cartServices->getCartInfoPrintProduct($cartId);
  1412. if (!$product) {
  1413. throw new ValidateException('订单商品获取失败,无法打印!');
  1414. }
  1415. $data = [
  1416. 'clientId' => sys_config('printing_client_id', ''),
  1417. 'apiKey' => sys_config('printing_api_key', ''),
  1418. 'partner' => sys_config('develop_id', ''),
  1419. 'terminal' => sys_config('terminal_number', '')
  1420. ];
  1421. if (!$data['clientId'] || !$data['apiKey'] || !$data['partner'] || !$data['terminal']) {
  1422. throw new ValidateException('请先配置小票打印开发者');
  1423. }
  1424. $printer = new Printer('yi_lian_yun', $data);
  1425. $res = $printer->setPrinterContent([
  1426. 'name' => sys_config('site_name'),
  1427. 'orderInfo' => is_object($order) ? $order->toArray() : $order,
  1428. 'product' => $product
  1429. ])->startPrinter();
  1430. if (!$res) {
  1431. throw new ValidateException($printer->getError());
  1432. }
  1433. return $res;
  1434. }
  1435. /**
  1436. * 获取订单确认数据
  1437. * @param array $user
  1438. * @param $cartId
  1439. * @return mixed
  1440. */
  1441. public function getOrderConfirmData(array $user, $cartId, bool $new, int $addressId, int $shipping_type = 1)
  1442. {
  1443. $addr = [];
  1444. /** @var UserAddressServices $addressServices */
  1445. $addressServices = app()->make(UserAddressServices::class);
  1446. if ($addressId) {
  1447. $addr = $addressServices->getAddress($addressId);
  1448. }
  1449. //没传地址id或地址已删除未找到 ||获取默认地址
  1450. if (!$addr) {
  1451. $addr = $addressServices->getUserDefaultAddress((int)$user['uid']);
  1452. }
  1453. if ($addr) {
  1454. $addr = $addr->toArray();
  1455. } else {
  1456. $addr = [];
  1457. }
  1458. /** @var StoreCartServices $cartServices */
  1459. $cartServices = app()->make(StoreCartServices::class);
  1460. $cartGroup = $cartServices->getUserProductCartListV1($user['uid'], $cartId, $new, $addr, $shipping_type);
  1461. $data = [];
  1462. $data['storeFreePostage'] = $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮金额
  1463. $validCartInfo = $cartGroup['valid'];
  1464. if (count($validCartInfo)) {
  1465. if (isset($validCartInfo[0]['productInfo']['is_virtual']) && $validCartInfo[0]['productInfo']['is_virtual']) {
  1466. $data['virtual_type'] = 1;
  1467. $data['deduction'] = true;
  1468. } else {
  1469. if ($validCartInfo[0]['productInfo']['virtual_type'] == 3) {
  1470. $data['virtual_type'] = 1;
  1471. $data['deduction'] = true;
  1472. } else {
  1473. $data['virtual_type'] = 0;
  1474. }
  1475. }
  1476. }
  1477. /** @var StoreOrderComputedServices $computedServices */
  1478. $computedServices = app()->make(StoreOrderComputedServices::class);
  1479. $priceGroup = $computedServices->getOrderPriceGroup($storeFreePostage, $validCartInfo, $addr, $user);
  1480. $validCartInfo = $priceGroup['cartInfo'] ?? $validCartInfo;
  1481. $other = [
  1482. 'offlinePostage' => sys_config('offline_postage'),
  1483. 'integralRatio' => sys_config('integral_ratio')
  1484. ];
  1485. $cartIdA = explode(',', $cartId);
  1486. $seckill_id = 0;
  1487. $combination_id = 0;
  1488. $bargain_id = 0;
  1489. $advance_id = 0;
  1490. if (count($cartIdA) == 1) {
  1491. $seckill_id = $cartGroup['deduction']['seckill_id'] ?? 0;
  1492. $combination_id = $cartGroup['deduction']['combination_id'] ?? 0;
  1493. $bargain_id = $cartGroup['deduction']['bargain_id'] ?? 0;
  1494. $advance_id = $cartGroup['deduction']['advance_id'] ?? 0;
  1495. }
  1496. $data['valid_count'] = count($validCartInfo);
  1497. $data['deduction'] = $seckill_id || $combination_id || $bargain_id || $advance_id;
  1498. $data['addressInfo'] = $addr;
  1499. $data['seckill_id'] = $seckill_id;
  1500. $data['combination_id'] = $combination_id;
  1501. $data['bargain_id'] = $bargain_id;
  1502. $data['advance_id'] = $advance_id;
  1503. $data['cartInfo'] = $cartGroup['cartInfo'];
  1504. $data['custom_form'] = json_decode($cartGroup['cartInfo'][0]['productInfo']['custom_form'], true) ?? [];
  1505. $data['priceGroup'] = $priceGroup;
  1506. $data['orderKey'] = $this->cacheOrderInfo($user['uid'], $validCartInfo, $priceGroup, $other);
  1507. $data['offlinePostage'] = $other['offlinePostage'];
  1508. /** @var UserLevelServices $levelServices */
  1509. $levelServices = app()->make(UserLevelServices::class);
  1510. $userLevel = $levelServices->getUerLevelInfoByUid($user['uid']);
  1511. if (isset($user['pwd'])) unset($user['pwd']);
  1512. $user['vip'] = $userLevel !== false;
  1513. if ($user['vip']) {
  1514. $user['vip_id'] = $userLevel['id'] ?? 0;
  1515. $user['discount'] = $userLevel['discount'] ?? 0;
  1516. }
  1517. $data['userInfo'] = $user;
  1518. $data['integralRatio'] = $other['integralRatio'];
  1519. $data['offline_pay_status'] = (int)sys_config('offline_pay_status') ?? (int)2;
  1520. $data['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  1521. $data['pay_weixin_open'] = (int)sys_config('pay_weixin_open') ?? 0;//微信支付 1 开启 0 关闭
  1522. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  1523. /** @var SystemStoreServices $systemStoreServices */
  1524. $systemStoreServices = app()->make(SystemStoreServices::class);
  1525. $store_count = $systemStoreServices->count(['type' => 0]);
  1526. $data['store_self_mention'] = $data['store_self_mention'] && $store_count;
  1527. $data['ali_pay_status'] = (bool)sys_config('ali_pay_status');//支付包支付 1 开启 0 关闭
  1528. $data['system_store'] = [];//门店信息
  1529. /** @var UserInvoiceServices $userInvoice */
  1530. $userInvoice = app()->make(UserInvoiceServices::class);
  1531. $invoice_func = $userInvoice->invoiceFuncStatus();
  1532. $data['invoice_func'] = $invoice_func['invoice_func'];
  1533. $data['special_invoice'] = $invoice_func['special_invoice'];
  1534. /** @var UserBillServices $userBillServices */
  1535. $userBillServices = app()->make(UserBillServices::class);
  1536. $data['usable_integral'] = bcsub((string)$user['integral'], (string)$userBillServices->getBillSum(['uid' => $user['uid'], 'is_frozen' => 1]), 0);
  1537. return $data;
  1538. }
  1539. /**
  1540. * 缓存订单信息
  1541. * @param $uid
  1542. * @param $cartInfo
  1543. * @param $priceGroup
  1544. * @param array $other
  1545. * @param int $cacheTime
  1546. * @return string
  1547. * @throws \Psr\SimpleCache\InvalidArgumentException
  1548. */
  1549. public function cacheOrderInfo($uid, $cartInfo, $priceGroup, $other = [], $cacheTime = 600)
  1550. {
  1551. $key = $this->getCacheKey();
  1552. CacheService::redisHandler()->set('user_order_' . $uid . $key, compact('cartInfo', 'priceGroup', 'other'), $cacheTime);
  1553. return $key;
  1554. }
  1555. /**
  1556. * 使用雪花算法生成订单ID
  1557. * @return string
  1558. * @throws \Exception
  1559. */
  1560. public function getCacheKey(string $prefix = '')
  1561. {
  1562. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  1563. //32位
  1564. if (PHP_INT_SIZE == 4) {
  1565. $id = abs($snowflake->id());
  1566. } else {
  1567. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  1568. }
  1569. return $prefix . $id;
  1570. }
  1571. /**获取用户购买活动产品的次数
  1572. * @param $uid
  1573. * @param $seckill_id
  1574. * @return int
  1575. */
  1576. public function activityProductCount(array $where)
  1577. {
  1578. return $this->dao->count($where);
  1579. }
  1580. /**
  1581. * 获取订单缓存信息
  1582. * @param int $uid
  1583. * @param string $key
  1584. * @return |null
  1585. */
  1586. public function getCacheOrderInfo(int $uid, string $key)
  1587. {
  1588. $cacheName = 'user_order_' . $uid . $key;
  1589. if (!CacheService::redisHandler()->has($cacheName)) return null;
  1590. return CacheService::redisHandler()->get($cacheName);
  1591. }
  1592. /**
  1593. * 获取拼团的订单id
  1594. * @param int $pid
  1595. * @param int $uid
  1596. * @return mixed
  1597. */
  1598. public function getStoreIdPink(int $pid, int $uid)
  1599. {
  1600. return $this->dao->value(['uid' => $uid, 'pink_id' => $pid, 'is_del' => 0], 'order_id');
  1601. }
  1602. /**
  1603. * 判断当前订单中是否有拼团
  1604. * @param int $pid
  1605. * @param int $uid
  1606. * @return int
  1607. */
  1608. public function getIsOrderPink($pid = 0, $uid = 0)
  1609. {
  1610. return $this->dao->count(['uid' => $uid, 'pink_id' => $pid, 'refund_status' => 0, 'is_del' => 0]);
  1611. }
  1612. /**
  1613. * 判断支付方式是否开启
  1614. * @param $payType
  1615. * @return bool
  1616. */
  1617. public function checkPaytype(string $payType)
  1618. {
  1619. $res = false;
  1620. switch ($payType) {
  1621. case PayServices::WEIXIN_PAY:
  1622. $res = (bool)sys_config('pay_weixin_open');
  1623. break;
  1624. case PayServices::YUE_PAY:
  1625. $res = sys_config('balance_func_status') && sys_config('yue_pay_status') == 1;
  1626. break;
  1627. case 'offline':
  1628. $res = sys_config('offline_pay_status') == 1;
  1629. break;
  1630. case PayServices::ALIAPY_PAY:
  1631. $res = sys_config('ali_pay_status') == 1;
  1632. break;
  1633. case PayServices::FRIEND:
  1634. $res = sys_config('friend_pay_status', 1) == 1;
  1635. break;
  1636. }
  1637. return $res;
  1638. }
  1639. /**
  1640. * 修改支付方式为线下支付
  1641. * @param string $orderId
  1642. * @return bool
  1643. */
  1644. public function setOrderTypePayOffline(string $orderId)
  1645. {
  1646. return $this->dao->update($orderId, ['pay_type' => 'offline'], 'order_id');
  1647. }
  1648. /**
  1649. * 删除订单
  1650. * @param $uni
  1651. * @param $uid
  1652. * @return bool
  1653. */
  1654. public function removeOrder(string $uni, int $uid)
  1655. {
  1656. $order = $this->getUserOrderDetail($uni, $uid);
  1657. if (!$order) {
  1658. throw new ValidateException('订单不存在!');
  1659. }
  1660. $order = $this->tidyOrder($order);
  1661. if ($order['_status']['_type'] != 0 && $order['_status']['_type'] != -2 && $order['_status']['_type'] != 4)
  1662. throw new ValidateException('该订单无法删除!');
  1663. $order->is_del = 1;
  1664. /** @var StoreOrderStatusServices $statusService */
  1665. $statusService = app()->make(StoreOrderStatusServices::class);
  1666. $res = $statusService->save([
  1667. 'oid' => $order['id'],
  1668. 'change_type' => 'remove_order',
  1669. 'change_message' => '删除订单',
  1670. 'change_time' => time()
  1671. ]);
  1672. if ($order->save() && $res) {
  1673. //未支付和已退款的状态下才可以退积分退库存退优惠券
  1674. if ($order['_status']['_type'] == 0 || $order['_status']['_type'] == -2) {
  1675. /** @var StoreOrderRefundServices $refundServices */
  1676. $refundServices = app()->make(StoreOrderRefundServices::class);
  1677. $this->transaction(function () use ($order, $refundServices) {
  1678. //回退积分和优惠卷
  1679. $res = $refundServices->integralAndCouponBack($order);
  1680. //回退库存
  1681. $res = $res && $refundServices->regressionStock($order);
  1682. if (!$res) {
  1683. throw new ValidateException('取消订单失败!');
  1684. }
  1685. });
  1686. }
  1687. return true;
  1688. } else
  1689. throw new ValidateException('订单删除失败!');
  1690. }
  1691. /**
  1692. * 取消订单
  1693. * @param $order_id
  1694. * @param $uid
  1695. * @return bool|void
  1696. * @throws \think\db\exception\DataNotFoundException
  1697. * @throws \think\db\exception\DbException
  1698. * @throws \think\db\exception\ModelNotFoundException
  1699. */
  1700. public function cancelOrder($order_id, int $uid)
  1701. {
  1702. $order = $this->dao->getOne(['order_id' => $order_id, 'uid' => $uid, 'is_del' => 0]);
  1703. if (!$order) {
  1704. throw new ValidateException('没有查到此订单');
  1705. }
  1706. if ($order->paid) {
  1707. throw new ValidateException('订单已经支付无法取消');
  1708. }
  1709. /** @var StoreOrderCartInfoServices $cartServices */
  1710. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  1711. $cartInfo = $cartServices->getOrderCartInfo($order['id']);
  1712. /** @var StoreOrderRefundServices $refundServices */
  1713. $refundServices = app()->make(StoreOrderRefundServices::class);
  1714. $this->transaction(function () use ($refundServices, $order) {
  1715. $res = $refundServices->integralAndCouponBack($order) && $refundServices->regressionStock($order);
  1716. $order->is_del = 1;
  1717. if (!($res && $order->save())) {
  1718. throw new ValidateException('取消订单失败');
  1719. }
  1720. });
  1721. /** @var StoreSeckillServices $seckiiServices */
  1722. $seckiiServices = app()->make(StoreSeckillServices::class);
  1723. $seckiiServices->cancelOccupySeckillStock($cartInfo, $order['unique']);
  1724. $seckiiServices->rollBackStock($cartInfo);
  1725. return true;
  1726. }
  1727. /**
  1728. * 判断订单完成
  1729. * @param StoreProductReplyServices $replyServices
  1730. * @param array $uniqueList
  1731. * @param $oid
  1732. * @return mixed
  1733. */
  1734. public function checkOrderOver($replyServices, array $uniqueList, $oid)
  1735. {
  1736. //订单商品全部评价完成
  1737. $replyServices->count(['unique' => $uniqueList, 'oid' => $oid]);
  1738. if ($replyServices->count(['unique' => $uniqueList, 'oid' => $oid]) == count($uniqueList)) {
  1739. event('StoreProductOrderOver', [$oid]);
  1740. $res = $this->dao->update($oid, ['status' => '3']);
  1741. if (!$res) throw new ValidateException('评价后置操作失败!');
  1742. /** @var StoreOrderStatusServices $statusService */
  1743. $statusService = app()->make(StoreOrderStatusServices::class);
  1744. $statusService->save([
  1745. 'oid' => $oid,
  1746. 'change_type' => 'check_order_over',
  1747. 'change_message' => '用户评价',
  1748. 'change_time' => time()
  1749. ]);
  1750. $order = $this->dao->get((int)$oid, ['id,pid,status']);
  1751. if ($order && $order['pid'] > 0) {
  1752. $p_order = $this->dao->get((int)$order['pid'], ['id,pid,status']);
  1753. //主订单全部收货 且子订单没有待评价 有已完成
  1754. if ($p_order['status'] == 2 && !$this->dao->count(['pid' => $order['pid'], 'status' => 3]) && $this->dao->count(['pid' => $order['pid'], 'status' => 4])) {
  1755. $this->dao->update($p_order['id'], ['status' => 3]);
  1756. $statusService->save([
  1757. 'oid' => $p_order['id'],
  1758. 'change_type' => 'check_order_over',
  1759. 'change_message' => '用户评价',
  1760. 'change_time' => time()
  1761. ]);
  1762. }
  1763. }
  1764. }
  1765. }
  1766. /**
  1767. * 某个用户订单
  1768. * @param int $uid
  1769. * @param UserServices $userServices
  1770. * @return array
  1771. * @throws \think\db\exception\DataNotFoundException
  1772. * @throws \think\db\exception\DbException
  1773. * @throws \think\db\exception\ModelNotFoundException
  1774. */
  1775. public function getUserOrderList(int $uid)
  1776. {
  1777. /** @var UserServices $userServices */
  1778. $userServices = app()->make(UserServices::class);
  1779. $user = $userServices->getUserInfo($uid);
  1780. if (!$user) {
  1781. throw new ValidateException('数据不存在');
  1782. }
  1783. [$page, $limit] = $this->getPageValue();
  1784. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  1785. $list = $this->dao->getStairOrderList($where, 'order_id,real_name,total_num,total_price,pay_price,FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time,paid,pay_type,pink_id,seckill_id,bargain_id', $page, $limit);
  1786. $count = $this->dao->count($where);
  1787. return compact('list', 'count');
  1788. }
  1789. /**
  1790. * 获取推广订单列表
  1791. * @param int $uid
  1792. * @param $where
  1793. * @return array
  1794. * @throws \think\db\exception\DataNotFoundException
  1795. * @throws \think\db\exception\DbException
  1796. * @throws \think\db\exception\ModelNotFoundException
  1797. */
  1798. public function getUserStairOrderList(int $uid, $where)
  1799. {
  1800. $where_data = [];
  1801. if (isset($where['type'])) {
  1802. switch ((int)$where['type']) {
  1803. case 1:
  1804. $where_data['spread_uid'] = $uid;
  1805. break;
  1806. case 2:
  1807. $where_data['spread_two_uid'] = $uid;
  1808. break;
  1809. default:
  1810. $where_data['spread_or_uid'] = $uid;
  1811. break;
  1812. }
  1813. }
  1814. if (isset($where['data']) && $where['data']) {
  1815. $where_data['time'] = $where['data'];
  1816. }
  1817. if (isset($where['order_id']) && $where['order_id']) {
  1818. $where_data['order_id'] = $where['order_id'];
  1819. }
  1820. //推广订单只显示支付过并且未退款的订单
  1821. $where_data['paid'] = 1;
  1822. $where_data['refund_status'] = 0;
  1823. [$page, $limit] = $this->getPageValue();
  1824. $list = $this->dao->getStairOrderList($where_data, '*', $page, $limit);
  1825. $count = $this->dao->count($where_data);
  1826. return compact('list', 'count');
  1827. }
  1828. /**
  1829. * 订单导出
  1830. * @param array $where
  1831. * @return array
  1832. * @throws \think\db\exception\DataNotFoundException
  1833. * @throws \think\db\exception\DbException
  1834. * @throws \think\db\exception\ModelNotFoundException
  1835. */
  1836. public function getExportList(array $where)
  1837. {
  1838. $list = $this->dao->search($where)->order('id desc')->select()->toArray();
  1839. foreach ($list as &$item) {
  1840. /** @var StoreOrderCartInfoServices $orderCart */
  1841. $orderCart = app()->make(StoreOrderCartInfoServices::class);
  1842. $_info = $orderCart->getCartColunm(['oid' => $item['id']], 'cart_info', 'unique');
  1843. foreach ($_info as $k => $v) {
  1844. $cart_info = is_string($v) ? json_decode($v, true) : $v;
  1845. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  1846. $_info[$k] = $cart_info;
  1847. unset($cart_info);
  1848. }
  1849. $item['_info'] = $_info;
  1850. /** @var WechatUserServices $wechatUserService */
  1851. $wechatUserService = app()->make(WechatUserServices::class);
  1852. $item['sex'] = $wechatUserService->value(['uid' => $item['uid']], 'sex');
  1853. if ($item['pink_id'] || $item['combination_id']) {
  1854. /** @var StorePinkServices $pinkService */
  1855. $pinkService = app()->make(StorePinkServices::class);
  1856. $pinkStatus = $pinkService->value(['order_id_key' => $item['id']], 'status');
  1857. switch ($pinkStatus) {
  1858. case 1:
  1859. $item['pink_name'] = '[拼团订单]正在进行中';
  1860. $item['color'] = '#f00';
  1861. break;
  1862. case 2:
  1863. $item['pink_name'] = '[拼团订单]已完成';
  1864. $item['color'] = '#00f';
  1865. break;
  1866. case 3:
  1867. $item['pink_name'] = '[拼团订单]未完成';
  1868. $item['color'] = '#f0f';
  1869. break;
  1870. default:
  1871. $item['pink_name'] = '[拼团订单]历史订单';
  1872. $item['color'] = '#457856';
  1873. break;
  1874. }
  1875. } elseif ($item['seckill_id']) {
  1876. $item['pink_name'] = '[秒杀订单]';
  1877. $item['color'] = '#32c5e9';
  1878. } elseif ($item['bargain_id']) {
  1879. $item['pink_name'] = '[砍价订单]';
  1880. $item['color'] = '#12c5e9';
  1881. } else {
  1882. if ($item['shipping_type'] == 1) {
  1883. $item['pink_name'] = '[普通订单]';
  1884. $item['color'] = '#895612';
  1885. } else if ($item['shipping_type'] == 2) {
  1886. $item['pink_name'] = '[核销订单]';
  1887. $item['color'] = '#8956E8';
  1888. }
  1889. }
  1890. }
  1891. return $list;
  1892. }
  1893. /**
  1894. * 自动取消订单
  1895. * @return bool
  1896. * @throws \think\db\exception\DataNotFoundException
  1897. * @throws \think\db\exception\DbException
  1898. * @throws \think\db\exception\ModelNotFoundException
  1899. */
  1900. public function orderUnpaidCancel()
  1901. {
  1902. //系统预设取消订单时间段
  1903. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  1904. //获取配置
  1905. $systemValue = SystemConfigService::more($keyValue);
  1906. //格式化数据
  1907. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  1908. $list = $this->dao->getOrderUnPaidList();
  1909. /** @var StoreOrderRefundServices $refundServices */
  1910. $refundServices = app()->make(StoreOrderRefundServices::class);
  1911. foreach ($list as $order) {
  1912. if ($order['pink_id'] || $order['combination_id']) {
  1913. $secs = $systemValue['order_pink_time'] ?: $systemValue['order_activity_time'];
  1914. } elseif ($order['seckill_id']) {
  1915. $secs = $systemValue['order_seckill_time'] ?: $systemValue['order_activity_time'];
  1916. } elseif ($order['bargain_id']) {
  1917. $secs = $systemValue['order_bargain_time'] ?: $systemValue['order_activity_time'];
  1918. } else {
  1919. $secs = $systemValue['order_cancel_time'];
  1920. }
  1921. if ($secs == 0) return true;
  1922. if (($order['add_time'] + bcmul($secs, '3600', 0)) < time()) {
  1923. try {
  1924. $this->transaction(function () use ($order, $refundServices) {
  1925. //回退积分和优惠卷
  1926. $res = $refundServices->integralAndCouponBack($order);
  1927. //回退库存和销量
  1928. $res = $res && $refundServices->regressionStock($order);
  1929. //修改订单状态
  1930. $res = $res && $this->dao->update($order['id'], ['is_del' => 1, 'mark' => '订单未支付已超过系统预设时间']);
  1931. if (!$res) {
  1932. throw new ValidateException('订单号' . $order['order_id'] . '自动取消订单失败');
  1933. }
  1934. return true;
  1935. });
  1936. /** @var StoreOrderCartInfoServices $cartServices */
  1937. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  1938. $cartInfo = $cartServices->getOrderCartInfo((int)$order['id']);
  1939. /** @var StoreSeckillServices $seckiiServices */
  1940. $seckiiServices = app()->make(StoreSeckillServices::class);
  1941. $seckiiServices->cancelOccupySeckillStock($cartInfo, $order['unique']);
  1942. $seckiiServices->rollBackStock($cartInfo);
  1943. } catch (\Throwable $e) {
  1944. Log::error('自动取消订单失败,失败原因:' . $e->getMessage(), $e->getTrace());
  1945. }
  1946. }
  1947. }
  1948. }
  1949. /**根据时间获取当天或昨天订单营业额
  1950. * @param array $where
  1951. * @return float|int
  1952. */
  1953. public function getOrderMoneyByWhere(array $where, string $sum_field, string $selectType, string $group = "")
  1954. {
  1955. switch ($selectType) {
  1956. case "sum" :
  1957. return $this->dao->getDayTotalMoney($where, $sum_field);
  1958. case "group" :
  1959. return $this->dao->getDayGroupMoney($where, $sum_field, $group);
  1960. }
  1961. }
  1962. /**统计时间段订单数
  1963. * @param array $where
  1964. * @param string $sum_field
  1965. */
  1966. public function getOrderCountByWhere(array $where)
  1967. {
  1968. return $this->dao->getDayOrderCount($where);
  1969. }
  1970. /**分组统计时间段订单数
  1971. * @param $where
  1972. * @return mixed
  1973. */
  1974. public function getOrderGroupCountByWhere($where)
  1975. {
  1976. return $this->dao->getOrderGroupCount($where);
  1977. }
  1978. /** 时间段支付订单人数
  1979. * @param $where
  1980. * @return mixed
  1981. */
  1982. public function getPayOrderPeopleByWhere($where)
  1983. {
  1984. return $this->dao->getPayOrderPeople($where);
  1985. }
  1986. /**时间段分组统计支付订单人数
  1987. * @param $where
  1988. * @return mixed
  1989. */
  1990. public function getPayOrderGroupPeopleByWhere($where)
  1991. {
  1992. return $this->dao->getPayOrderGroupPeople($where);
  1993. }
  1994. /**
  1995. * 退款订单列表
  1996. * @param array $where
  1997. * @return array
  1998. * @throws \think\db\exception\DataNotFoundException
  1999. * @throws \think\db\exception\DbException
  2000. * @throws \think\db\exception\ModelNotFoundException
  2001. */
  2002. public function refundList(array $where)
  2003. {
  2004. [$page, $limit] = $this->getPageValue();
  2005. if ($where['refund_reason_time'] != '') $where['refund_reason_time'] = explode('-', $where['refund_reason_time']);
  2006. $data = $this->dao->getRefundList($where, $page, $limit);
  2007. if ($data['list']) $data['list'] = $this->tidyOrderList($data['list']);
  2008. $data['num'] = [
  2009. 0 => ['name' => '全部', 'num' => $this->dao->count(['refund_type' => 0, 'is_system_del' => 0])],
  2010. 1 => ['name' => '仅退款', 'num' => $this->dao->count(['refund_type' => 1, 'is_system_del' => 0])],
  2011. 2 => ['name' => '退货退款', 'num' => $this->dao->count(['refund_type' => 2, 'is_system_del' => 0])],
  2012. 3 => ['name' => '拒绝退款', 'num' => $this->dao->count(['refund_type' => 3, 'is_system_del' => 0])],
  2013. 4 => ['name' => '商品待退货', 'num' => $this->dao->count(['refund_type' => 4, 'is_system_del' => 0])],
  2014. 5 => ['name' => '退货待收货', 'num' => $this->dao->count(['refund_type' => 5, 'is_system_del' => 0])],
  2015. 6 => ['name' => '已退款', 'num' => $this->dao->count(['refund_type' => 6, 'is_system_del' => 0])]
  2016. ];
  2017. return $data;
  2018. }
  2019. /**
  2020. * 商家同意退款,等待客户退货
  2021. * @param $order_id
  2022. * @return bool
  2023. */
  2024. public function agreeRefund($order_id)
  2025. {
  2026. $res = $this->dao->update(['id' => $order_id], ['refund_type' => 4]);
  2027. /** @var StoreOrderStatusServices $statusService */
  2028. $statusService = app()->make(StoreOrderStatusServices::class);
  2029. $statusService->save([
  2030. 'oid' => $order_id,
  2031. 'change_type' => 'refund_express',
  2032. 'change_message' => '等待用户退货',
  2033. 'change_time' => time()
  2034. ]);
  2035. if ($res) return true;
  2036. throw new ValidateException('操作失败');
  2037. }
  2038. /**
  2039. * @param array $where
  2040. * @param array|string[] $field
  2041. * @param array $with
  2042. * @param int $page
  2043. * @param int $limit
  2044. * @param string $order
  2045. * @return array
  2046. * @throws \think\db\exception\DataNotFoundException
  2047. * @throws \think\db\exception\DbException
  2048. * @throws \think\db\exception\ModelNotFoundException
  2049. */
  2050. public function getSplitOrderList(array $where, array $field = ['*'], array $with = [], $page = 0, $limit = 0, $order = 'pay_time DESC,id DESC')
  2051. {
  2052. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with, $order);
  2053. if ($data) {
  2054. $data = $this->tidyOrderList($data);
  2055. }
  2056. return $data;
  2057. }
  2058. /**
  2059. * 代付详情
  2060. * @param $orderId
  2061. * @param $uid
  2062. * @return array
  2063. * @throws \think\db\exception\DataNotFoundException
  2064. * @throws \think\db\exception\DbException
  2065. * @throws \think\db\exception\ModelNotFoundException
  2066. */
  2067. public function getFriendDetail($orderId, $uid)
  2068. {
  2069. $orderInfo = $this->dao->getOne(['order_id' => $orderId]);
  2070. if ($orderInfo) {
  2071. $orderInfo = $orderInfo->toArray();
  2072. } else {
  2073. throw new ValidateException('订单不存在');
  2074. }
  2075. $orderInfo = $this->tidyOrder($orderInfo, true);
  2076. /** @var UserServices $userServices */
  2077. $userServices = app()->make(UserServices::class);
  2078. $userInfo = $userServices->get($orderInfo['uid']);
  2079. $friendInfo = $userServices->get($orderInfo['pay_uid']);
  2080. $info = [
  2081. 'id' => $orderInfo['id'],
  2082. 'order_id' => $orderInfo['order_id'],
  2083. 'uid' => $orderInfo['uid'],
  2084. 'avatar' => $userInfo['avatar'],
  2085. 'nickname' => $userInfo['nickname'],
  2086. 'cartInfo' => $orderInfo['cartInfo'],
  2087. 'paid' => $orderInfo['paid'],
  2088. 'total_num' => $orderInfo['total_num'],
  2089. 'pay_price' => $orderInfo['pay_price'],
  2090. 'type' => $uid == $orderInfo['uid'] ? 0 : 1,
  2091. 'pay_uid' => isset($friendInfo) ? $friendInfo['uid'] : 0,
  2092. 'pay_nickname' => isset($friendInfo) ? $friendInfo['nickname'] : '',
  2093. 'pay_avatar' => isset($friendInfo) ? $friendInfo['avatar'] : '',
  2094. ];
  2095. return $info;
  2096. }
  2097. /**
  2098. * 获取退货商品列表
  2099. * @param array $cart_ids
  2100. * @param int $id
  2101. * @return array
  2102. * @throws \think\db\exception\DataNotFoundException
  2103. * @throws \think\db\exception\DbException
  2104. * @throws \think\db\exception\ModelNotFoundException
  2105. */
  2106. public function refundCartInfoList(array $cart_ids = [], int $id = 0)
  2107. {
  2108. $orderInfo = $this->dao->get($id);
  2109. if (!$orderInfo) {
  2110. throw new ValidateException('订单不存在');
  2111. }
  2112. $orderInfo = $this->tidyOrder($orderInfo, true);
  2113. $cartInfo = $orderInfo['cartInfo'] ?? [];
  2114. $data = [];
  2115. if ($cart_ids) {
  2116. foreach ($cart_ids as $cart) {
  2117. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num'] || $cart['cart_num'] <= 0) {
  2118. throw new ValidateException('请重新选择退款商品,或件数');
  2119. }
  2120. }
  2121. $cart_ids = array_combine(array_column($cart_ids, 'cart_id'), $cart_ids);
  2122. $i = 0;
  2123. foreach ($cartInfo as $item) {
  2124. if (isset($cart_ids[$item['id']])) {
  2125. $data['cartInfo'][$i] = $item;
  2126. if (isset($cart_ids[$item['id']]['cart_num'])) $data['cartInfo'][$i]['cart_num'] = $cart_ids[$item['id']]['cart_num'];
  2127. $i++;
  2128. }
  2129. }
  2130. }
  2131. $data['_status'] = $orderInfo['_status'] ?? [];
  2132. $data['cartInfo'] = $data['cartInfo'] ?? $cartInfo;
  2133. return $data;
  2134. }
  2135. }