StoreOrderServices.php 102 KB

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