MCharts.Class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. namespace JinDouYun\Model\Charts;
  3. use JinDouYun\Controller\Common\Logger;
  4. use JinDouYun\Dao\Order\DOrder;
  5. use JinDouYun\Dao\Order\DOrderGoods;
  6. use JinDouYun\Dao\Order\DOrderReceive;
  7. use JinDouYun\Dao\SysAreaChina\DSysAreaChina;
  8. use JinDouYun\Dao\System\DCustomerSource;
  9. use JinDouYun\Model\GoodsManage\MGoodsBasic;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Core\StatusCode;
  13. /**
  14. * @copyright Copyright (c) https://www.qianniaovip.com All rights reserved
  15. * Description: 报表
  16. * Class MCharts
  17. * @package JinDouYun\Model\Cart
  18. */
  19. class MCharts
  20. {
  21. private $onlineUserId;
  22. private $onlineEnterpriseId;
  23. private $objDOrderGoods;
  24. private $objDOrder;
  25. private $objDOrderReceive;
  26. /**
  27. * MCharts constructor.
  28. * @param $onlineUserId
  29. * @param $onlineEnterpriseId
  30. * @throws \Exception
  31. */
  32. public function __construct($onlineUserId, $onlineEnterpriseId)
  33. {
  34. $this->onlineEnterpriseId = $onlineEnterpriseId;
  35. $this->onlineUserId = $onlineUserId;
  36. $this->objDOrderGoods = new DOrderGoods();
  37. $this->objDOrderGoods->setTable('qianniao_order_goods_'.$this->onlineEnterpriseId);
  38. $this->objDOrder = new DOrder();
  39. $this->objDOrder->setTable('qianniao_order_'.$this->onlineEnterpriseId);
  40. $this->objDOrderReceive = new DOrderReceive();
  41. $this->objDOrderReceive->setTable('qianniao_order_receive_'.$this->onlineEnterpriseId);
  42. }
  43. /**
  44. * Doc: (des="")
  45. * User: XMing
  46. * Date: 2021/3/29
  47. * Time: 10:59 上午
  48. * @param array $params
  49. * @return ResultWrapper
  50. * @throws \Exception
  51. */
  52. public function goods(array $params): ResultWrapper
  53. {
  54. //todo(后期要改成根据最大userId计算最大fix使用UNION ALL联合查询)
  55. $this->objDOrderGoods->setTable('qianniao_order_goods_'.$this->onlineEnterpriseId.'_1');
  56. $fields = '';
  57. switch ($params['type']){
  58. case 1:
  59. //商品
  60. $fields = ' goodsBasicId,goodsName as title,SUM(buyNum) as goodsNum,SUM(totalMoney) as goodsAmount ,SUM(JSON_EXTRACT(extends,"$.u_1_buy")) AS otherNum ';
  61. $group = ' GROUP BY goodsId ';
  62. break;
  63. case 2:
  64. //商品分类
  65. $fields = ' goodsBasicId,categoryName as title,SUM(buyNum) as goodsNum,SUM(totalMoney) as goodsAmount ,SUM(JSON_EXTRACT(extends,"$.u_1_buy")) AS otherNum ';
  66. $group = ' GROUP BY categoryName ';
  67. break;
  68. default:
  69. return ResultWrapper::fail('统计方式异常',ErrorCode::$paramError);
  70. break;
  71. }
  72. $where = '';
  73. if(isset($params['startTime']) && !empty($params['startTime']) && isset($params['endTime']) && !empty($params['endTime'])){
  74. $where .= ' AND createTime BETWEEN '.$params['startTime'].' AND '.$params['endTime'];
  75. }
  76. if(isset($params['categoryId']) && !empty($params['categoryId'])){
  77. $where .= ' AND FIND_IN_SET('.$params['categoryId'].',`categoryPath`)';
  78. }
  79. if(isset($params['brandId']) && !empty($params['brandId'])){
  80. $where .= ' AND brandId = '.$params['brandId'];
  81. }
  82. if(isset($params['shopId']) && !empty($params['shopId'])){
  83. $where .= ' AND shopId = '.$params['shopId'];
  84. }
  85. $sql = 'SELECT '.$fields.' FROM '.$this->objDOrderGoods->get_Table().' WHERE deleteStatus = '.StatusCode::$standard.$where.$group;
  86. $sql .= ' ORDER BY goodsNum DESC ';
  87. $count = $this->objDOrderGoods->query($sql);
  88. if ($count === false){
  89. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  90. }
  91. if (isset($params['limit']) && !empty($params['limit'])){
  92. $sql .= ' LIMIT '.$params['offset'].','.$params['limit'];
  93. }
  94. $lists = $this->objDOrderGoods->query($sql);
  95. if ($lists === false){
  96. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  97. }
  98. $goodsBasicDate = [];
  99. if (!empty($lists)){
  100. //获取商品资料
  101. $goodsBasicIds = array_column($lists,'goodsBasicId');
  102. $objGoodsDate = new MGoodsBasic($this->onlineUserId,$this->onlineEnterpriseId);
  103. $result = $objGoodsDate->getBasicGoodsFieldByIds($goodsBasicIds);
  104. if ( !$result->isSuccess() ){
  105. return ResultWrapper::fail($result->getData(),$result->getErrorCode());
  106. }
  107. $goodsBasicDate = $result->getData();
  108. }
  109. foreach ($lists as &$list){
  110. $list['isEq'] = isset($goodsBasicDate[$list['goodsBasicId']]['isEq']) ? $goodsBasicDate[$list['goodsBasicId']]['isEq'] : 4;
  111. $list['goodsNum'] = sprintf("%.2f", $list['goodsNum']);
  112. $list['goodsAmount'] = sprintf("%.2f", $list['goodsAmount']);
  113. }
  114. return ResultWrapper::success(['data' => $lists,'total' => count($count)]);
  115. }
  116. /**
  117. * Doc: (des="")
  118. * User: XMing
  119. * Date: 2021/3/29
  120. * Time: 2:23 下午
  121. * @param array $params
  122. * @return ResultWrapper
  123. * @throws \Exception
  124. */
  125. public function customerGoods(array $params): ResultWrapper
  126. {
  127. //todo(后期要改成根据最大userId计算最大fix使用UNION ALL联合查询)
  128. $this->objDOrderGoods->setTable('qianniao_order_goods_'.$this->onlineEnterpriseId.'_1');
  129. $this->objDOrder->setTable('qianniao_order_'.$this->onlineEnterpriseId.'_1');
  130. $where = '';
  131. switch ($params['type']){
  132. case 1:
  133. //客户
  134. $fields = ' g.goodsBasicId, o.customerName as title,SUM(g.buyNum) as goodsNum,SUM(g.totalMoney) as goodsAmount ,SUM(JSON_EXTRACT(g.extends,"$.u_1_buy")) AS otherNum ';
  135. $group = ' GROUP BY o.customerName ';
  136. break;
  137. case 2:
  138. //客户类型
  139. $fields = ' g.goodsBasicId, o.customerType as customerType,SUM(g.buyNum) as goodsNum,SUM(g.totalMoney) as goodsAmount ,SUM(JSON_EXTRACT(g.extends,"$.u_1_buy")) AS otherNum ';
  140. $group = ' GROUP BY o.customerType ';
  141. $where .= ' AND o.customerType != 0 ';
  142. break;
  143. default:
  144. return ResultWrapper::fail('统计方式异常',ErrorCode::$paramError);
  145. break;
  146. }
  147. if (isset($params['categoryId']) && !empty($params['categoryId'])){
  148. $where .= ' AND FIND_IN_SET('.$params['categoryId'].',g.categoryPath)';
  149. }
  150. if(isset($params['salesManId']) && !empty($params['salesManId'])){
  151. $where .= ' AND o.salesManId = '.$params['salesManId'];
  152. }
  153. if(isset($params['customerType']) && !empty($params['customerType'])){
  154. $where .= ' AND o.customerType = '.$params['salesManId'];
  155. }
  156. if(isset($params['startTime']) && !empty($params['startTime']) && isset($params['endTime']) && !empty($params['endTime'])){
  157. $where .= ' AND o.createTime BETWEEN '.$params['startTime'].' AND '.$params['endTime'];
  158. }
  159. if(isset($params['auditStatus']) && !empty($params['auditStatus'])){
  160. $where .= ' AND o.auditStatus = '.$params['auditStatus'];
  161. }
  162. if(isset($params['orderStatus']) && !empty($params['orderStatus'])){
  163. $where .= ' AND o.orderStatus = '.$params['orderStatus'];
  164. }
  165. $sql = 'SELECT '.$fields.' FROM '.$this->objDOrder->get_Table().' AS o
  166. LEFT JOIN '.$this->objDOrderGoods->get_Table().' as g
  167. ON o.id = g.orderId
  168. WHERE o.deleteStatus = '.StatusCode::$standard.'
  169. AND g.deleteStatus = '.StatusCode::$standard.$where.$group;
  170. $sql .= ' ORDER BY goodsNum DESC ';
  171. $count = $this->objDOrderGoods->query($sql);
  172. if ($count === false){
  173. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  174. }
  175. if (isset($params['limit']) && !empty($params['limit'])){
  176. $sql .= ' LIMIT '.$params['offset'].','.$params['limit'];
  177. }
  178. $lists = $this->objDOrderGoods->query($sql);
  179. if ($lists === false){
  180. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  181. }
  182. $map = [];
  183. if ($params['type'] == 2){
  184. $customerTypeIds = [];
  185. foreach ($lists as $val){
  186. if (!empty($val['customerType']) && !in_array($val['customerType'].$customerTypeIds)){
  187. $customerTypeIds[] = $val['customerType'];
  188. }
  189. }
  190. $objDCustomerSource = new DCustomerSource();
  191. if (!empty($customerTypeIds)){
  192. $result = $objDCustomerSource->select(['id' => $customerTypeIds]);
  193. if ($result === false){
  194. return ResultWrapper::fail($objDCustomerSource->error(),ErrorCode::$dberror);
  195. }
  196. foreach ($result as $value){
  197. $map[$value['id']] = $value['name'];
  198. }
  199. }
  200. }
  201. $goodsBasicDate = [];
  202. //获取商品资料
  203. $goodsBasicIds = array_column($lists,'goodsBasicId');
  204. $objGoodsDate = new MGoodsBasic($this->onlineUserId,$this->onlineEnterpriseId);
  205. $result = $objGoodsDate->getBasicGoodsFieldByIds($goodsBasicIds);
  206. if ( !$result->isSuccess() ){
  207. return ResultWrapper::fail($result->getData(),$result->getErrorCode());
  208. }
  209. $goodsBasicDate = $result->getData();
  210. foreach ($lists as &$list){
  211. $list['isEq'] = isset($goodsBasicDate[$list['goodsBasicId']]['isEq']) ? $goodsBasicDate[$list['goodsBasicId']]['isEq'] : 4;
  212. $list['goodsNum'] = sprintf("%.2f", $list['goodsNum']);
  213. $list['goodsAmount'] = sprintf("%.2f", $list['goodsAmount']);
  214. if ($params['type'] == 2){
  215. $list['title'] = getArrayItem($map,$list['customerType'],'');
  216. }
  217. }
  218. return ResultWrapper::success(['data' => $lists,'total' => count($count)]);
  219. }
  220. /**
  221. * Doc: (des="")
  222. * User: XMing
  223. * Date: 2021/3/29
  224. * Time: 3:13 下午
  225. * @param array $params
  226. * @return ResultWrapper
  227. * @throws \Exception
  228. */
  229. public function order(array $params): ResultWrapper
  230. {
  231. //todo(后期要改成根据最大userId计算最大fix使用UNION ALL联合查询)
  232. $this->objDOrderGoods->setTable('qianniao_order_goods_'.$this->onlineEnterpriseId.'_1');
  233. $this->objDOrder->setTable('qianniao_order_'.$this->onlineEnterpriseId.'_1');
  234. $where = '';
  235. switch ($params['type']){
  236. case 1:
  237. //日
  238. $fields = ' FROM_UNIXTIME(createTime,"%Y-%m-%d") as day,count(id) as num,sum(payAmount) as amount ';
  239. $group = ' GROUP BY day ';
  240. break;
  241. case 2:
  242. //月
  243. $fields = ' FROM_UNIXTIME(createTime,"%Y-%m") as month,count(id) as num,sum(payAmount) as amount ';
  244. $group = ' GROUP BY month ';
  245. break;
  246. default:
  247. return ResultWrapper::fail('统计方式异常',ErrorCode::$paramError);
  248. break;
  249. }
  250. if(isset($params['customerId']) && !empty($params['customerId'])){
  251. $where .= ' AND customerId = '.$params['customerId'];
  252. }
  253. if(isset($params['customerType']) && !empty($params['customerType'])){
  254. $where .= ' AND customerType = '.$params['salesManId'];
  255. }
  256. if(isset($params['auditStatus']) && !empty($params['auditStatus'])){
  257. $where .= ' AND auditStatus = '.$params['auditStatus'];
  258. }
  259. if(isset($params['orderStatus']) && !empty($params['orderStatus'])){
  260. $where .= ' AND orderStatus = '.$params['orderStatus'];
  261. }
  262. if(isset($params['startTime']) && !empty($params['startTime']) && isset($params['endTime']) && !empty($params['endTime'])){
  263. $where .= ' AND createTime BETWEEN '.$params['startTime'].' AND '.$params['endTime'];
  264. }
  265. $sql = 'SELECT '.$fields.' FROM '.$this->objDOrder->get_Table().' WHERE deleteStatus = '.StatusCode::$standard.$where.$group;
  266. $sql .= ' ORDER BY createTime DESC ';
  267. $count = $this->objDOrderGoods->query($sql);
  268. if ($count === false){
  269. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  270. }
  271. if (isset($params['limit']) && !empty($params['limit'])){
  272. $sql .= ' LIMIT '.$params['offset'].','.$params['limit'];
  273. }
  274. $lists = $this->objDOrderGoods->query($sql);
  275. if ($lists === false){
  276. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  277. }
  278. foreach ($lists as &$list){
  279. $list['num'] = sprintf("%.2f", $list['num']);
  280. $list['amount'] = sprintf("%.2f", $list['amount']);
  281. }
  282. return ResultWrapper::success(['data' => $lists,'total' => count($count)]);
  283. }
  284. /**
  285. * Doc: (des="")
  286. * User: XMing
  287. * Date: 2021/3/29
  288. * Time: 4:02 下午
  289. * @param array $params
  290. * @return ResultWrapper
  291. * @throws \Exception
  292. */
  293. public function customerOrder(array $params): ResultWrapper
  294. {
  295. //todo(后期要改成根据最大userId计算最大fix使用UNION ALL联合查询)
  296. $this->objDOrder->setTable('qianniao_order_'.$this->onlineEnterpriseId.'_1');
  297. $where = '';
  298. $fields = ' customerName,customerMobile,count(id) as num,sum(payAmount) as amount ';
  299. $group = ' GROUP BY customerName ';
  300. if(isset($params['startTime']) && !empty($params['startTime']) && isset($params['endTime']) && !empty($params['endTime'])){
  301. $where .= ' AND createTime BETWEEN '.$params['startTime'].' AND '.$params['endTime'];
  302. }
  303. if(isset($params['customerType']) && !empty($params['customerType'])){
  304. $where .= ' AND customerType = '.$params['customerType'];
  305. }
  306. if(isset($params['auditStatus']) && !empty($params['auditStatus'])){
  307. $where .= ' AND auditStatus = '.$params['auditStatus'];
  308. }
  309. if(isset($params['orderStatus']) && !empty($params['orderStatus'])){
  310. $where .= ' AND orderStatus = '.$params['orderStatus'];
  311. }
  312. $where .= ' AND customerName != "" ';
  313. $sql = 'SELECT '.$fields.' FROM '.$this->objDOrder->get_Table().' WHERE deleteStatus = '.StatusCode::$standard.$where.$group;
  314. $sql .= ' ORDER BY num DESC ';
  315. $count = $this->objDOrderGoods->query($sql);
  316. if ($count === false){
  317. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  318. }
  319. if (isset($params['limit']) && !empty($params['limit'])){
  320. $sql .= ' LIMIT '.$params['offset'].','.$params['limit'];
  321. }
  322. $lists = $this->objDOrderGoods->query($sql);
  323. if ($lists === false){
  324. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  325. }
  326. foreach ($lists as &$list){
  327. $list['num'] = sprintf("%.2f", $list['num']);
  328. $list['amount'] = sprintf("%.2f", $list['amount']);
  329. }
  330. return ResultWrapper::success(['data' => $lists,'total' => count($count)]);
  331. }
  332. /**
  333. * Doc: (des="")
  334. * User: XMing
  335. * Date: 2021/3/29
  336. * Time: 4:11 下午
  337. * @param array $params
  338. * @return ResultWrapper\
  339. * @throws \Exception
  340. */
  341. public function salesManOrder(array $params): ResultWrapper
  342. {
  343. //todo(后期要改成根据最大userId计算最大fix使用UNION ALL联合查询)
  344. $this->objDOrder->setTable('qianniao_order_'.$this->onlineEnterpriseId.'_1');
  345. $where = '';
  346. $fields = ' salesManName,count(id) as num,COUNT(DISTINCT customerId) as dealCustomerNum,sum(payAmount) as amount ';
  347. $group = ' GROUP BY salesManName ';
  348. if(isset($params['startTime']) && !empty($params['startTime']) && isset($params['endTime']) && !empty($params['endTime'])){
  349. $where .= ' AND createTime BETWEEN '.$params['startTime'].' AND '.$params['endTime'];
  350. }
  351. if(isset($params['auditStatus']) && !empty($params['auditStatus'])){
  352. $where .= ' AND auditStatus = '.$params['auditStatus'];
  353. }
  354. $where .= ' AND salesManName != "" ';
  355. $sql = 'SELECT '.$fields.' FROM '.$this->objDOrder->get_Table().' WHERE deleteStatus = '.StatusCode::$standard.' AND orderStatus = '.StatusCode::$standard.$where.$group;
  356. $sql .= ' ORDER BY amount DESC ';
  357. $count = $this->objDOrderGoods->query($sql);
  358. if ($count === false){
  359. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  360. }
  361. if (isset($params['limit']) && !empty($params['limit'])){
  362. $sql .= ' LIMIT '.$params['offset'].','.$params['limit'];
  363. }
  364. Logger::logs(E_USER_ERROR,'salesManOrder sql',__CLASS__,__LINE__,$sql);
  365. $lists = $this->objDOrderGoods->query($sql);
  366. if ($lists === false){
  367. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  368. }
  369. foreach ($lists as &$list){
  370. $list['num'] = sprintf("%.2f", $list['num']);
  371. $list['amount'] = sprintf("%.2f", $list['amount']);
  372. }
  373. return ResultWrapper::success(['data' => $lists,'total' => count($count)]);
  374. }
  375. /**
  376. * Doc: (des="地区订单报表")
  377. * User: XMing
  378. * Date: 2021/3/30
  379. * Time: 10:09 上午
  380. * @param array $params
  381. * @return ResultWrapper
  382. * @throws \Exception
  383. */
  384. public function areaOrder(array $params): ResultWrapper
  385. {
  386. //todo(后期要改成根据最大userId计算最大fix使用UNION ALL联合查询)
  387. $this->objDOrder->setTable('qianniao_order_'.$this->onlineEnterpriseId.'_1');
  388. $this->objDOrderReceive->setTable('qianniao_order_receive_'.$this->onlineEnterpriseId.'_1');
  389. $fields = ' r.provinceCode,COUNT(DISTINCT o.id) as num,COUNT(DISTINCT o.customerId) as dealCustomerNum,sum(o.payAmount) as amount ';
  390. $where = '';
  391. $group = ' GROUP BY r.provinceCode ';
  392. if(isset($params['startTime']) && !empty($params['startTime']) && isset($params['endTime']) && !empty($params['endTime'])){
  393. $where .= ' AND o.createTime BETWEEN '.$params['startTime'].' AND '.$params['endTime'];
  394. }
  395. if(isset($params['customerType']) && !empty($params['customerType'])){
  396. $where .= ' AND o.customerType = '.$params['customerType'];
  397. }
  398. if(isset($params['provinceCode']) && !empty($params['provinceCode'])){
  399. $where .= ' AND r.provinceCode = '.$params['provinceCode'];
  400. }
  401. if(isset($params['auditStatus']) && !empty($params['auditStatus'])){
  402. $where .= ' AND o.auditStatus = '.$params['auditStatus'];
  403. }
  404. if(isset($params['orderStatus']) && !empty($params['orderStatus'])){
  405. $where .= ' AND o.orderStatus = '.$params['orderStatus'];
  406. }
  407. $where .= ' AND r.provinceCode IS NOT NULL AND r.provinceCode != 0 ';
  408. $sql = 'SELECT '.$fields.' FROM '.$this->objDOrder->get_Table().' AS o
  409. LEFT JOIN '.$this->objDOrderReceive->get_Table().' AS r
  410. ON o.id = r.orderId
  411. WHERE o.deleteStatus = '.StatusCode::$standard.$where.$group;
  412. $sql .= ' ORDER BY dealCustomerNum DESC ';
  413. $count = $this->objDOrderGoods->query($sql);
  414. if ($count === false){
  415. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  416. }
  417. if (isset($params['limit']) && !empty($params['limit'])){
  418. $sql .= ' LIMIT '.$params['offset'].','.$params['limit'];
  419. }
  420. $lists = $this->objDOrderGoods->query($sql);
  421. if ($lists === false){
  422. return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror);
  423. }
  424. $provinceCodes = [];
  425. foreach ($lists as $item){
  426. if (!in_array($item['provinceCode'],$provinceCodes)){
  427. $provinceCodes[] = $item['provinceCode'];
  428. }
  429. }
  430. $map = [];
  431. if (!empty($provinceCodes)){
  432. $objDSysAreaChina = new DSysAreaChina();
  433. $result = $objDSysAreaChina->select(['code'=>$provinceCodes],'code,name');
  434. if ($result === false){
  435. return ResultWrapper::fail($objDSysAreaChina->error(),ErrorCode::$dberror);
  436. }
  437. foreach ($result as $value){
  438. $map[$value['code']] = $value['name'];
  439. }
  440. }
  441. foreach ($lists as &$list){
  442. $list['num'] = sprintf("%.2f", $list['num']);
  443. $list['amount'] = sprintf("%.2f", $list['amount']);
  444. $list['title'] = getArrayItem($map,$list['provinceCode'],'');
  445. }
  446. return ResultWrapper::success(['data' => $lists,'total' => count($count)]);
  447. }
  448. }