StoreOrderServices.php 88 KB

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