StoreOrderServices.php 102 KB

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