MCommissionOrder.Class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <?php
  2. namespace JinDouYun\Model\Commission;
  3. use JinDouYun\Controller\Common\Logger;
  4. use JinDouYun\Dao\Commission\DCommissionOrderIndex;
  5. use JinDouYun\Dao\Customer\DCustomer;
  6. use JinDouYun\Dao\Order\DOrderGoods;
  7. use JinDouYun\Dao\Order\DOrderIndex;
  8. use Mall\Framework\Core\StatusCode;
  9. use JinDouYun\Dao\Commission\DCommissionOrder;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. /**
  13. * Description:
  14. * Class MCommissionOrder
  15. * @package JinDouYun\Model\Commission
  16. */
  17. class MCommissionOrder
  18. {
  19. /**
  20. * @var DCommissionOrder
  21. */
  22. private $objDCommissionOrder;
  23. /**
  24. * @var int
  25. */
  26. private $onlineEnterpriseId;
  27. /**
  28. * @var int
  29. */
  30. private $onlineUserId;
  31. private static $retLevel = [
  32. 0 => '',
  33. 1 => '一级返佣',
  34. 2 => '二级返佣',
  35. 3 => '三级返佣'
  36. ];
  37. /**
  38. * @var array
  39. */
  40. private static $isAccount = [
  41. 3 => '已取消',
  42. 4 => '等待入账',
  43. 5 => '已入账'
  44. ];
  45. /**
  46. * @var DCustomer
  47. */
  48. private $objDCustomer;
  49. /**
  50. * @var DCommissionOrderIndex
  51. */
  52. private $objDCommissionOrderIndex;
  53. /**
  54. * @var DOrderGoods
  55. */
  56. private $objDOrderGoods;
  57. /**
  58. * @var int 单表数据量
  59. */
  60. private $cutTable = 200000;
  61. /**
  62. * @var DOrderIndex
  63. */
  64. private $objDOrderIndex;
  65. /**
  66. * MCommissionOrder constructor.
  67. * @param $onlineEnterpriseId
  68. * @param $onlineUserId
  69. * @throws \Exception
  70. */
  71. public function __construct($onlineEnterpriseId, $onlineUserId)
  72. {
  73. $this->onlineEnterpriseId = $onlineEnterpriseId;
  74. $this->onlineUserId = $onlineUserId;
  75. $this->objDCommissionOrder = new DCommissionOrder();
  76. $this->objDCommissionOrder->setTable('qianniao_commission_order_' . $this->onlineEnterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  77. $this->objDCustomer = new DCustomer();
  78. $this->objDCustomer->setTable('qianniao_customer_'.$this->onlineEnterpriseId);
  79. $this->objDCommissionOrderIndex = new DCommissionOrderIndex();
  80. $this->objDCommissionOrderIndex->setTable('qianniao_commission_order_index_'.$this->onlineEnterpriseId);
  81. $this->objDOrderIndex = new DOrderIndex();
  82. $this->objDOrderIndex->setTable('qianniao_order_index_'.$this->onlineEnterpriseId);
  83. $this->objDOrderGoods = new DOrderGoods();
  84. }
  85. public function __destruct()
  86. {
  87. // TODO: Implement __destruct() method.
  88. }
  89. /**
  90. * Doc: (des="分销商品记录")
  91. * User: XMing
  92. * Date: 2020/7/24
  93. * Time: 4:23 下午
  94. * @param array $selectParams
  95. * @return ResultWrapper
  96. */
  97. public function getAll(array $selectParams)
  98. {
  99. $fields = 'c.*,o.auditStatus,o.orderStatus,i.isAccount';
  100. $commissionOrderTableName = 'qianniao_commission_order_' . $this->onlineEnterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3);
  101. $sql = 'SELECT '.$fields.' FROM '.$commissionOrderTableName.' as c
  102. LEFT JOIN qianniao_order_index_'.$this->onlineEnterpriseId. ' as o
  103. ON o.id = c.orderId
  104. LEFT JOIN qianniao_commission_order_index_'.$this->onlineEnterpriseId.' as i
  105. ON i.orderId = c.orderId
  106. WHERE c.id IS NOT NULL';
  107. if(isset($selectParams['state']) && !empty($selectParams['state'])){
  108. $stateSql = self::stateSql($selectParams['state']);
  109. $sql .= empty($stateSql) ? '' : ' and '.$stateSql;
  110. }
  111. if (isset($selectParams['customerId']) && !empty($selectParams['customerId'])){
  112. $sql .= ' AND c.customerId = '.$selectParams['customerId'];
  113. }
  114. if (isset($selectParams['no']) && !empty($selectParams['no'])){
  115. $sql .= ' AND c.orderNo = '.$selectParams['no'];
  116. }
  117. if (isset($selectParams['goodsName']) && !empty($selectParams['goodsName'])){
  118. $sql .= ' AND c.goodsName = "'.$selectParams['goodsName'].'"';
  119. }
  120. if (isset($selectParams['orderStatus']) && !empty($selectParams['orderStatus'])){
  121. $sql .= ' AND o.orderStatus = '.$selectParams['orderStatus'];
  122. }
  123. if (isset($selectParams['isAccount']) && !empty($selectParams['isAccount'])){
  124. $sql .= ' AND i.isAccount = '.$selectParams['isAccount'];
  125. }
  126. if (isset($selectParams['startTime']) && !empty($selectParams['startTime']) && isset($selectParams['endTime']) && !empty($selectParams['endTime'])){
  127. $sql .= ' AND c.createTime BETWEEN '.$selectParams['startTime'].' and '.$selectParams['endTime'];
  128. }
  129. $total = count((array) $this->objDCommissionOrder->query($sql));
  130. //排序规则
  131. if (isset($selectParams['orderBy']) && empty($selectParams['orderBy'])){
  132. $sql .= ' GROUP BY c.id order by '.$selectParams['orderBy'];
  133. }else{
  134. $sql .= ' GROUP BY c.id order by c.createTime DESC';
  135. }
  136. //导出不需要分页
  137. if( isset($selectParams['export']) && !empty($selectParams['export']) ){ //导出
  138. $commissionList = $this->objDCommissionOrder->query($sql);
  139. if ($commissionList === false){
  140. return ResultWrapper::fail($this->objDCommissionOrder->error(),ErrorCode::$dberror);
  141. }
  142. if(empty($commissionList)){
  143. return ResultWrapper::success('导出数据为空');
  144. }
  145. self::exportCommissionOrder($commissionList);
  146. }
  147. $sql .= ' limit '.$selectParams['offset'].','.$selectParams['limit'];//分页
  148. $commissionList = $this->objDCommissionOrder->query($sql);
  149. if ($commissionList === false){
  150. return ResultWrapper::fail($this->objDCommissionOrder->error(),ErrorCode::$dberror);
  151. }
  152. $formatResult = self::formatList((array)$commissionList);
  153. if (!$formatResult->isSuccess()){
  154. return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
  155. }
  156. $return = [
  157. 'total' => $total,
  158. 'data' => $formatResult->getData()
  159. ];
  160. return ResultWrapper::success($return);
  161. }
  162. /**
  163. * Doc: (des="state标示对应sql")
  164. * User: XMing
  165. * Date: 2020/7/13
  166. * Time: 9:43 上午
  167. * @param string $state
  168. * @return string
  169. */
  170. private static function stateSql(string $state)
  171. {
  172. $sql = '';
  173. switch ($state) {
  174. case 'waitAudit'://待审核
  175. $sql .= ' (o.auditStatus = ' . StatusCode::$auditStatus['auditing'] . ' and o.orderStatus != ' . StatusCode::$orderStatus['close'] . ')';
  176. break;
  177. case 'waitOutStock'://待出库
  178. $sql .= ' (o.auditStatus = ' . StatusCode::$auditStatus['auditPass'] . ' and (o.orderStatus = ' . StatusCode::$orderStatus['waitPay'] . ' or o.orderStatus = ' . StatusCode::$orderStatus['waitDelivery'] . ')) ';
  179. break;
  180. case 'hasOutStock'://已出库
  181. $sql .= ' (o.auditStatus = ' . StatusCode::$auditStatus['auditPass'] . ' and o.orderStatus = ' . StatusCode::$orderStatus['waitReceive'] . ')';
  182. break;
  183. case 'finish'://已完成
  184. $sql .= ' (o.auditStatus = ' . StatusCode::$auditStatus['auditPass'] . ' and o.orderStatus = ' . StatusCode::$orderStatus['finish'] . ')';
  185. break;
  186. case 'close'://已关闭
  187. $sql .= ' (o.orderStatus = ' . StatusCode::$orderStatus['close'] . ')';
  188. break;
  189. case 'all'://全部订单
  190. break;
  191. }
  192. return $sql;
  193. }
  194. /**
  195. * Doc: (des="格式化")
  196. * User: XMing
  197. * Date: 2020/7/29
  198. * Time: 4:06 下午
  199. * @param array $data
  200. * @return ResultWrapper
  201. */
  202. public function formatList(array $data)
  203. {
  204. if (empty($data)){
  205. return ResultWrapper::success([]);
  206. }
  207. $customerIds = [];
  208. $retCustomerIds = [];
  209. foreach ($data as $datum){
  210. $customerIds[] = $datum['customerId'];
  211. $retCustomerIds[] = $datum['retCustomerId'];
  212. }
  213. $allCustomerIds = array_merge($customerIds,$retCustomerIds);//全部客户id
  214. //获取客户的名称
  215. $customerLists = $this->objDCustomer->select(['id'=>$allCustomerIds],'id,name');
  216. if ($customerIds === false){
  217. return ResultWrapper::fail($this->objDCustomer->error(),ErrorCode::$dberror);
  218. }
  219. $customerRelNameMap = [];
  220. foreach ($customerLists as $list){
  221. $customerRelNameMap[$list['id']] = $list['name'];
  222. }
  223. $data = self::formatOrderStatus($data);
  224. foreach ($data as $key => &$value){
  225. is_string($value['specGroup']) && $data[$key]['specGroup'] = json_decode($value['specGroup'],true);
  226. $value['selfMsg'] = $value['isSelf'] == StatusCode::$standard ? '自购返佣' : '';
  227. $value['retLevelMsg'] = self::$retLevel[$value['retLevel']];
  228. $value['customerName'] = isset($customerRelNameMap[$value['customerId']]) ? $customerRelNameMap[$value['customerId']] : '';
  229. $value['retCustomerName'] = isset($customerRelNameMap[$value['retCustomerId']]) ? $customerRelNameMap[$value['retCustomerId']] : '';
  230. $value['retMsg'] = isset(self::$isAccount[$value['isAccount']]) ? self::$isAccount[$value['isAccount']] : '';
  231. }
  232. return ResultWrapper::success($data);
  233. }
  234. /**
  235. * 格式化订单状态
  236. * @param $data
  237. * @return mixed
  238. * @return ResultWrapper
  239. */
  240. private static function formatOrderStatus($data)
  241. {
  242. if (empty($data)) return $data;
  243. foreach ($data as $key => $value) {
  244. if ($value['auditStatus'] == StatusCode::$auditStatus['auditing']) {
  245. $data[$key]['orderMsg'] = '待审核';
  246. }
  247. if ($value['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
  248. $data[$key]['orderMsg'] = '待出库';
  249. if ($value['orderStatus'] == StatusCode::$orderStatus['waitReceive']) {
  250. $data[$key]['orderMsg'] = '已出库';
  251. }
  252. if ($value['orderStatus'] == StatusCode::$orderStatus['finish']) {
  253. $data[$key]['orderMsg'] = '已完成';
  254. }
  255. }
  256. if ($value['orderStatus'] == StatusCode::$orderStatus['close']) {
  257. $data[$key]['orderMsg'] = '已关闭';
  258. }
  259. }
  260. return $data;
  261. }
  262. /**
  263. * Doc: (des="分销中心订单")
  264. * User: XMing
  265. * Date: 2020/7/30
  266. * Time: 10:51 上午
  267. * @param array $selectParams
  268. * @return ResultWrapper
  269. *
  270. * @throws \Exception
  271. */
  272. public function getCommissionOrder(array $selectParams)
  273. {
  274. $fields = 'c.id,o.id as orderId,o.orderStatus,o.payStatus,o.createTime,o.payType,o.deliveryType,
  275. c.payAmount,c.orderNo,c.retMoney,c.retLevel,c.retUserCenterId,c.retCustomerId,c.userCenterId,c.customerId';
  276. $sql = 'SELECT '.$fields.' FROM qianniao_commission_order_index_'.$this->onlineEnterpriseId.' as c
  277. LEFT JOIN qianniao_order_index_'.$this->onlineEnterpriseId.' as o
  278. ON o.id = c.orderId
  279. WHERE c.retUserCenterId = '.$this->onlineUserId;
  280. if (isset($selectParams['orderStatus']) && !empty($selectParams['orderStatus'])){
  281. if (is_array($selectParams['orderStatus'])){
  282. $orderStatusStr = implode(',',$selectParams['orderStatus']);
  283. $sql .= ' AND o.orderStatus IN('.$orderStatusStr.') ';
  284. }else{
  285. $sql .= ' AND o.orderStatus = '.$selectParams['orderStatus'];
  286. }
  287. }
  288. $sql .= ' order by c.createTime desc';
  289. $count = count((array)$this->objDCommissionOrderIndex->query($sql));
  290. //分页
  291. if (isset($selectParams['limit']) && isset($selectParams['offset'])) {
  292. $sql .= ' limit ' . $selectParams['offset'] . ',' . $selectParams['limit'];
  293. }
  294. $list = $this->objDCommissionOrderIndex->query($sql);
  295. if ($list === false){
  296. return ResultWrapper::fail($this->objDCommissionOrderIndex->error(),ErrorCode::$dberror);
  297. }
  298. if (empty($list)){
  299. return ResultWrapper::success([
  300. 'data' => [],
  301. 'total' => 0
  302. ]);
  303. }
  304. $formatResult = self::formatApiOrderList((array)$list);
  305. if (!$formatResult->isSuccess()){
  306. return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
  307. }
  308. $ret = [
  309. 'data' => $formatResult->getData(),
  310. 'total' => $count
  311. ];
  312. return ResultWrapper::success($ret);
  313. }
  314. /**
  315. * Doc: (des="")
  316. * User: XMing
  317. * Date: 2020/7/30
  318. * Time: 3:41 下午
  319. * @param array $data
  320. * @return ResultWrapper
  321. * @throws \Exception
  322. */
  323. private function formatApiOrderList(array $data)
  324. {
  325. $allGroupData = [];
  326. $allCustomerIds = [];
  327. foreach ($data as $key => $val) {
  328. $tableNum = ceil($val['userCenterId'] / $this->cutTable);
  329. $allGroupData[$tableNum][] = $val['orderId'];
  330. $allCustomerIds[] = $val['customerId'];
  331. $allCustomerIds[] = $val['retCustomerId'];
  332. }
  333. //查询这些订单的商品数据
  334. $allOrderGoodsData = [];
  335. foreach ($allGroupData as $k => $orderId) {
  336. $this->objDOrderGoods->setTable('qianniao_order_goods_' . $this->onlineEnterpriseId . '_' . $k);
  337. $dbResult = $this->objDOrderGoods->select(
  338. ['orderId' => $orderId],
  339. 'orderId,goodsName,goodsId,goodsCode,skuId,shopName,price,buyNum,preferential,totalMoney,specGroup,goodsImages,unitName',
  340. 'createTime desc'
  341. );//查询订单
  342. if ($dbResult === false) {
  343. Logger::logs(E_USER_ERROR, 'sql错误', __CLASS__, __LINE__, $this->objDOrderGoods->error());
  344. return ResultWrapper::fail($this->objDOrderGoods->error(), ErrorCode::$dberror);
  345. }
  346. $allOrderGoodsData = array_merge($allOrderGoodsData,$dbResult);
  347. }
  348. $orderGoodsMap = [];//订单商品数据集合
  349. foreach ($allOrderGoodsData as $value){
  350. $value['specGroup'] = empty($value['specGroup']) ? [] : json_decode($value['specGroup'],true);
  351. $orderGoodsMap[$value['orderId']][] = $value;
  352. }
  353. $customer = $this->objDCustomer->select(['id' => $allCustomerIds],'id,name');
  354. if ($customer === false){
  355. return ResultWrapper::fail($this->objDCustomer->error(),ErrorCode::$dberror);
  356. }
  357. $customerMap = [];
  358. foreach ($customer as $value){
  359. $customerMap[$value['id']] = $value['name'];
  360. }
  361. foreach ($data as &$datum){
  362. $datum['selfMsg'] = '';
  363. if ($datum['userCenterId'] == $datum['retUserCenterId']){
  364. $datum['selfMsg'] = '自购返佣';
  365. }
  366. $datum['retLevelMsg'] = isset(self::$retLevel[$datum['retLevel']]) ? self::$retLevel[$datum['retLevel']] : '';
  367. $datum['orderGoods'] = isset($orderGoodsMap[$datum['orderId']]) ? $orderGoodsMap[$datum['orderId']] : [];
  368. $datum['customerName'] = isset($customerMap[$datum['customerId']]) ? $customerMap[$datum['customerId']] : '';
  369. $datum['retCustomerName'] = isset($customerMap[$datum['retCustomerId']]) ? $customerMap[$datum['retCustomerId']] : '';
  370. }
  371. return ResultWrapper::success($data);
  372. }
  373. /**
  374. * Doc: (des="分销订单详情")
  375. * User: XMing
  376. * Date: 2020/7/30
  377. * Time: 4:07 下午
  378. * @param int $id
  379. *
  380. * @throws \Exception
  381. * @return ResultWrapper
  382. */
  383. public function getCommissionOrderInfo(int $id)
  384. {
  385. $fields = 'c.id,o.id as orderId,o.orderStatus,o.payStatus,o.createTime,o.payType,o.deliveryType,
  386. c.payAmount,c.orderNo,c.retMoney,c.retLevel,c.retUserCenterId,c.retCustomerId,c.userCenterId,c.customerId';
  387. $sql = 'SELECT '.$fields.' FROM qianniao_commission_order_index_'.$this->onlineEnterpriseId.' as c
  388. LEFT JOIN qianniao_order_index_'.$this->onlineEnterpriseId.' as o
  389. ON o.id = c.orderId
  390. WHERE c.id = '.$id;
  391. $info = $this->objDCommissionOrderIndex->query($sql);
  392. if ($info === false){
  393. return ResultWrapper::fail($this->objDCommissionOrderIndex->error(),ErrorCode::$dberror);
  394. }
  395. $info = array_shift($info);
  396. if (empty($info)){
  397. return ResultWrapper::fail('未找到订单信息',ErrorCode::$paramError);
  398. }
  399. $formatResult = self::formatDetails($info);
  400. if (!$formatResult->isSuccess()){
  401. return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
  402. }
  403. return ResultWrapper::success($formatResult->getData());
  404. }
  405. /**
  406. * Doc: (des="")
  407. * User: XMing
  408. * Date: 2020/7/30
  409. * Time: 4:15 下午
  410. * @param array $data
  411. * @throws \Exception
  412. * @return ResultWrapper
  413. */
  414. private function formatDetails(array $data)
  415. {
  416. $fix = ceil($data['userCenterId'] / $this->cutTable);
  417. $this->objDOrderGoods->setTable('qianniao_order_goods_' . $this->onlineEnterpriseId . '_' . $fix);
  418. $orderGoods = $this->objDOrderGoods->select(['orderId' => $data['orderId']],'orderId,goodsName,goodsId,goodsCode,skuId,shopName,price,buyNum,preferential,totalMoney,specGroup,goodsImages,unitName');
  419. if ($orderGoods === false) {
  420. Logger::logs(E_USER_ERROR, 'sql错误', __CLASS__, __LINE__, $this->objDOrderGoods->error());
  421. return ResultWrapper::fail($this->objDOrderGoods->error(), ErrorCode::$dberror);
  422. }
  423. foreach ($orderGoods as &$value){
  424. $value['specGroup'] = empty($value['specGroup']) ? [] : json_decode($value['specGroup'],true);
  425. }
  426. unset($value);
  427. $data['orderGoods'] = $orderGoods;
  428. //获取客户名称
  429. $allCustomerIds = [
  430. $data['customerId'],
  431. $data['retCustomerId']
  432. ];
  433. $customer = $this->objDCustomer->select(['id' => $allCustomerIds],'id,name,avatar');
  434. if ($customer === false){
  435. return ResultWrapper::fail($this->objDCustomer->error(),ErrorCode::$dberror);
  436. }
  437. $customerMap = [];
  438. foreach ($customer as $value){
  439. $customerMap[$value['id']] = $value;
  440. }
  441. $data['customerName'] = isset($customerMap[$data['customerId']]) ? $customerMap[$data['customerId']]['name'] : '';
  442. $data['customerAvatar'] = isset($customerMap[$data['customerId']]) ? $customerMap[$data['customerId']]['avatar'] : '';
  443. $data['retCustomerName'] = isset($customerMap[$data['retCustomerId']]) ? $customerMap[$data['retCustomerId']]['name'] : '';
  444. $data['retCustomerAvatar'] = isset($customerMap[$data['retCustomerId']]) ? $customerMap[$data['retCustomerId']]['avatar'] : '';
  445. $data['deliveryMsg'] = isset(StatusCode::$deliveryType[$data['deliveryType']]) ? StatusCode::$deliveryType[$data['deliveryType']] : '';
  446. $data['selfMsg'] = '';
  447. if ($data['userCenterId'] == $data['retCustomerId']){
  448. $data['selfMsg'] = '自购返佣';
  449. }
  450. $data['payTypeMsg'] = StatusCode::$payType[$data['payType']];
  451. $data['retLevelMsg'] = self::$retLevel[$data['retLevel']];
  452. return ResultWrapper::success($data);
  453. }
  454. /**
  455. * Doc: (des="获取分销订单数据")
  456. * User: XMing
  457. * Date: 2020/8/5
  458. * Time: 10:38 上午
  459. * @param array $userCenterIds
  460. * @return ResultWrapper
  461. */
  462. public function getCommissionOrderData(array $userCenterIds)
  463. {
  464. $CommissionOrder = $this->objDCommissionOrderIndex->select(['userCenterId' => $userCenterIds],'userCenterId,payAmount');
  465. if ($CommissionOrder === false){
  466. return ResultWrapper::fail($this->objDCommissionOrderIndex->error(),ErrorCode::$dberror);
  467. }
  468. $retMap = [];
  469. foreach ($CommissionOrder as $value){
  470. $retMap[$value['userCenterId']][] = $value['payAmount'];
  471. }
  472. $map = [];
  473. foreach ($retMap as $userCenterId => $item){
  474. $map[$userCenterId] = [
  475. 'totalOrderAmount' => array_sum($item),
  476. 'total' => count($item),
  477. ];
  478. }
  479. return ResultWrapper::success($map);
  480. }
  481. public function exportCommissionOrder($date)
  482. {
  483. //导出到本地
  484. header("Content-type:application/vnd.ms-excel");
  485. header("Content-Disposition:filename=分销订单.csv");
  486. header('Cache-Control: max-age=0');
  487. $fp = fopen('php://output', 'a');
  488. $head = ['商品信息', '单价','数量','实付款', '订单来源', '买家','订单状态','时间', '分销商', '佣金', '佣金状态']; //定义标题
  489. foreach ($head as $i => $v) {
  490. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  491. }
  492. fputcsv($fp, $head);
  493. $limit = 10000;
  494. $num = 0;//计数器
  495. foreach ($date as $value) {//循环数据
  496. $num++;
  497. if ($num == $limit) {
  498. ob_flush();//释放内存
  499. flush();
  500. }
  501. $rows['goodsName'] = isset($value['goodsName']) ? $value['goodsName'] : null; // 商品信息
  502. $rows['goodsPrice'] = isset($value['goodsPrice']) ? $value['goodsPrice'] : null; // 单价
  503. $rows['buyNum'] = isset($value['buyNum']) ? $value['createTime'] : 0; // 数量
  504. $rows['payAmount'] = bcmul($rows['goodsPrice'],$rows['buyNum'],2); // 实付款
  505. $rows['customerName'] = isset($value['customerName']) ? $value['customerName'] : null; // 订单来源
  506. $rows['customerMobile'] = isset($value['customerMobile']) ? $value['customerMobile'] : null; // 买家
  507. $rows['sourceNo'] = isset($value['sourceNo']) ? $value['sourceNo'] : null; // 订单状态
  508. $rows['createTime'] = isset($value['createTime']) ? date("Y-m-d H:i:s",$value['createTime']) : null; // 时间
  509. $rows['typeName'] = isset($value['typeName']) ? $value['typeName'] : null; // 分销商
  510. $rows['retMoney'] = isset($value['retMoney']) ? $value['retMoney'] : null; // 佣金
  511. $rows['isSettled'] = ($value['isSettled'] == StatusCode::$standard) ? '已结算': '未结算';// 佣金状态
  512. foreach ($rows as $kk => $vv) {
  513. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  514. }
  515. fputcsv($fp, $rs);
  516. $rows = [];
  517. }
  518. }
  519. }