StoreOrderServices.php 98 KB

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