MDelivery.Class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /**
  3. * 配送Model
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2021/04/20
  7. * Time: 16:00
  8. */
  9. namespace JinDouYun\Model\Delivery;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Dao\Delivery\DDeliveryRoute;
  14. use JinDouYun\Dao\Delivery\DDeliveryRouteCustomer;
  15. use JinDouYun\Model\Customer\MCustomer;
  16. use JinDouYun\Model\System\MDriver;
  17. use JinDouYun\Model\MBaseModel;
  18. class MDelivery extends MBaseModel
  19. {
  20. private $objDDeliveryRoute;
  21. private $objDDeliveryRouteCustomer;
  22. private $objMDriver;
  23. private $userCenterId;
  24. private $enterpriseId;
  25. public function __construct($enterpriseId, $userCenterId = false)
  26. {
  27. $this->enterpriseId = $enterpriseId;
  28. $this->userCenterId = $userCenterId;
  29. $this->objDDeliveryRoute = new DDeliveryRoute('default');
  30. $this->objDDeliveryRouteCustomer = new DDeliveryRouteCustomer('default');
  31. $this->objMDriver = new MDriver($this->enterpriseId, $this->userCenterId);
  32. $this->objDDeliveryRoute->setTable('qianniao_delivery_route_'.$enterpriseId);
  33. $this->objDDeliveryRouteCustomer->setTable('qianniao_delivery_route_customer_'.$enterpriseId);
  34. }
  35. /**
  36. * 配送线路添加
  37. * @param $params
  38. * @return ResultWrapper
  39. * @throws Exception
  40. */
  41. public function addDeliveryRoute($params)
  42. {
  43. $params['createTime'] = time();
  44. $params['updateTime'] = time();
  45. $customerData = isset($params['customerId']) ? $params['customerId'] : [];
  46. $driverIds = isset($params['driverId']) ? trim($params['driverId'], ',') : '';
  47. $driverIds = strpos($driverIds, ',') ? explode(',', $driverIds) : $driverIds;
  48. unset($params['customerId'], $params['driverId']);
  49. $beginStatus = $this->objDDeliveryRoute->beginTransaction();
  50. $dbResult = $this->objDDeliveryRoute->insert($params);
  51. if($dbResult === false){
  52. $this->objDDeliveryRoute->rollBack();
  53. return ResultWrapper::fail($this->objDDeliveryRoute->error(), ErrorCode::$dberror);
  54. }
  55. $routeId = $dbResult;
  56. unset($dbResult);
  57. //增加客户绑定
  58. if(!empty($customerData)){
  59. $dbResult = $this->objDDeliveryRouteCustomer->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['customerId' => $customerData]);
  60. if($dbResult === false){
  61. $this->objDDeliveryRoute->rollBack();
  62. return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror);
  63. }
  64. $insert = [];
  65. foreach($customerData as $value){
  66. $insert[] = [
  67. 'routeId' => $routeId,
  68. 'customerId' => $value,
  69. 'createTime' => $params['createTime'],
  70. 'updateTime' => $params['updateTime'],
  71. ];
  72. }
  73. $dbResult = $this->objDDeliveryRouteCustomer->insert($insert, true);
  74. if($dbResult === false){
  75. $this->objDDeliveryRoute->rollBack();
  76. return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror);
  77. }
  78. }
  79. //增加司机绑定
  80. if(!empty($driverIds)){
  81. $update = [
  82. 'routeId' => $routeId,
  83. 'updateTime' => time()
  84. ];
  85. $modelResult = $this->objMDriver->updateDriverData($update, ['id' => $routeId]);
  86. if(!$modelResult->isSuccess()){
  87. $this->objDDeliveryRoute->rollBack();
  88. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  89. }
  90. }
  91. $beginStatus && $this->objDDeliveryRoute->commit();
  92. return ResultWrapper::success($routeId);
  93. }
  94. /**
  95. * 配送线路修改
  96. * @param $update
  97. * @param $where
  98. * @return ResultWrapper
  99. */
  100. public function updateDeliveryRoute($update, $where)
  101. {
  102. $update['updateTime'] = time();
  103. $customerData = isset($update['customerId']) ? array_unique($update['customerId']) : [];
  104. $driverIds = isset($update['driverId']) ? $update['driverId'] : '';
  105. $driverIds = strpos($driverIds, ',') ? explode(',', $driverIds) : $driverIds;
  106. unset($update['customerId'], $update['driverId']);
  107. $beginStatus = $this->objDDeliveryRoute->beginTransaction();
  108. //处理客户数据
  109. if(!empty($customerData)){
  110. $dbResult = $this->objDDeliveryRouteCustomer->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()],['customerId' => $customerData]);
  111. if($dbResult === false){
  112. return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror);
  113. }
  114. foreach($customerData as $value){
  115. $insert[] = [
  116. 'routeId' => $where['id'],
  117. 'customerId' => $value,
  118. 'createTime' => $update['updateTime'],
  119. 'updateTime' => $update['updateTime'],
  120. ];
  121. }
  122. if(!empty($insert)){
  123. $dbResult = $this->objDDeliveryRouteCustomer->insert($insert, true);
  124. if($dbResult === false){
  125. $this->objDDeliveryRoute->rollBack();
  126. return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror);
  127. }
  128. }
  129. }else{
  130. $dbResult = $this->objDDeliveryRouteCustomer->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['routeId' => $where['id']]);
  131. if($dbResult === false){
  132. $this->objDDeliveryRoute->rollBack();
  133. return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror);
  134. }
  135. }
  136. //处理司机数据
  137. if(!empty($driverIds)){
  138. $modelResult = $this->objMDriver->updateDriverData(['routeId' => 0, 'updateTime' => time()], ['routeId' => $where['id']]);
  139. if(!$modelResult->isSuccess()){
  140. $this->objDDeliveryRoute->rollBack();
  141. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  142. }
  143. $modelResult = $this->objMDriver->updateDriverData(['routeId' => $where['id'], 'updateTime' => time()], ['id' => $driverIds]);
  144. if(!$modelResult->isSuccess()){
  145. $this->objDDeliveryRoute->rollBack();
  146. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  147. }
  148. }else{
  149. $modelResult = $this->objMDriver->updateDriverData(['routeId' => 0, 'updateTime' => time()], ['routeId' => $where['id']]);
  150. if(!$modelResult->isSuccess()){
  151. $this->objDDeliveryRoute->rollBack();
  152. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  153. }
  154. }
  155. $dbResult = $this->objDDeliveryRoute->update($update,$where);
  156. if($dbResult === false){
  157. $this->objDDeliveryRoute->rollBack();
  158. return ResultWrapper::fail($this->objDDeliveryRoute->error(), ErrorCode::$dberror);
  159. }
  160. $beginStatus && $this->objDDeliveryRoute->commit();
  161. return ResultWrapper::success($dbResult);
  162. }
  163. /**
  164. * 配送线路列表
  165. * @param $selectParams
  166. * @return ResultWrapper
  167. */
  168. public function getAllDeliveryRoute($selectParams)
  169. {
  170. $limit = $selectParams['limit'];
  171. unset($selectParams['limit']);
  172. $offset = $selectParams['offset'];
  173. unset($selectParams['offset']);
  174. $selectParams['deleteStatus'] = StatusCode::$standard;
  175. $whereSql = self::formatSqlWhere($selectParams);
  176. $dbResult = $this->objDDeliveryRoute->select($whereSql, '*', 'createTime desc', $limit, $offset);
  177. if ($dbResult === false) {
  178. return ResultWrapper::fail($this->objDDeliveryRoute->error(), ErrorCode::$dberror);
  179. }
  180. $total = $this->objDDeliveryRoute->count($whereSql);
  181. $return = [
  182. 'data' => self::formatDeliveryRoute($dbResult),
  183. 'total' => $total ? intval($total) : 0,
  184. ];
  185. return ResultWrapper::success($return);
  186. }
  187. /**
  188. * 配送线路详情
  189. * @param $where
  190. * @return ResultWrapper
  191. */
  192. public function getDeliveryRouteInfo($where)
  193. {
  194. $dbResult = $this->objDDeliveryRoute->get($where);
  195. if($dbResult === false){
  196. return ResultWrapper::fail($this->objDDeliveryRoute->error(), ErrorCode::$dberror);
  197. }
  198. return ResultWrapper::success(self::formatDeliveryRoute($dbResult));
  199. }
  200. /**
  201. * 配送线路客户
  202. * @param $selectParams
  203. * @return ResultWrapper
  204. */
  205. public function getDeliveryRouteCustomer($selectParams)
  206. {
  207. $limit = $selectParams['limit'];
  208. unset($selectParams['limit']);
  209. $offset = $selectParams['offset'];
  210. unset($selectParams['offset']);
  211. $selectParams['deleteStatus'] = StatusCode::$standard;
  212. $dbResult = $this->objDDeliveryRouteCustomer->select($selectParams, '*', 'sort desc', $limit, $offset);
  213. if($dbResult === false){
  214. return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror);
  215. }
  216. $total = $this->objDDeliveryRouteCustomer->count($selectParams);
  217. $return = [
  218. 'data' => self::formatDeliveryRouteCustomer($dbResult),
  219. 'total' => $total ? $total : 0
  220. ];
  221. return ResultWrapper::success($return);
  222. }
  223. /**
  224. * 配送线路客户排序
  225. * @param $update
  226. * @param $where
  227. * @return ResultWrapper
  228. */
  229. public function updateDeliveryRouteCustomer($update, $where)
  230. {
  231. $update['updateTime'] = time();
  232. $dbResult = $this->objDDeliveryRouteCustomer->update($update, $where);
  233. if($dbResult === false){
  234. return ResultWrapper::fail($this->objDDeliveryRouteCustomer->error(), ErrorCode::$dberror);
  235. }
  236. return ResultWrapper::success($dbResult);
  237. }
  238. /**
  239. * 格式化线路客户
  240. * @param $params
  241. * @return mixed
  242. */
  243. public function formatDeliveryRouteCustomer($params)
  244. {
  245. $customerIds = array_column($params, 'customerId');
  246. //客户数据
  247. $customerData = [];
  248. if(!empty($customerIds)){
  249. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  250. $modelResult = $objMCustomer->getCustomerData(['id' => $customerIds], '*', false, true);
  251. if($modelResult->isSuccess()){
  252. $customerResult = $modelResult->getData();
  253. foreach($customerResult as $customer){
  254. $customerData[$customer['id']] = $customer;
  255. }
  256. }
  257. }
  258. foreach($params as &$value){
  259. $value['customerName'] = '';
  260. $value['customerType'] = '';
  261. $value['customerMobile'] = '';
  262. $value['customerArea'] = [];
  263. if(isset($customerData[$value['customerId']])){
  264. $value['customerName'] = $customerData[$value['customerId']]['name'];
  265. $value['customerType'] = $customerData[$value['customerId']]['customerType'];
  266. $value['customerMobile'] = $customerData[$value['customerId']]['mobile'];
  267. $value['customerArea'] = $customerData[$value['customerId']]['area'];
  268. }
  269. }
  270. return $params;
  271. }
  272. /**
  273. * 格式化线路客户订单数据
  274. * @param $customerData
  275. * @return mixed
  276. */
  277. public function formatDeliveryRouteCustomerOrder($customerData)
  278. {
  279. $customerIds = array_column($customerData, 'customerId');
  280. //客户数据
  281. $customerOrderData = [];
  282. $params['orderCustomerNum'] = 0;
  283. $params['orderTotal'] = 0;
  284. $params['orderAmount'] = 0;
  285. $params['goodsTotal'] = 0;
  286. $params['otherTotal'] = 0;
  287. $params['outOrderTotal'] = 0;
  288. $params['outOrderAmount'] = 0;
  289. if(!empty($customerIds)){
  290. //查询没有删除 并且订单状态是3代发货 4待收货 5已完成
  291. $whereSql = ' where customerId in('.implode(',',array_unique($customerIds)).') and deleteStatus = '.StatusCode::$standard.' and orderStatus in(3,4) and deliveryType = '.StatusCode::$deliveryType['logistics'].' and auditStatus = '.StatusCode::$auditStatus['auditPass'];
  292. $whereSqlAs = ' where o.customerId in('.implode(',',array_unique($customerIds)).') and o.deleteStatus = '.StatusCode::$standard.' and o.orderStatus in(3,4) and o.deliveryType = '.StatusCode::$deliveryType['logistics'].' and o.auditStatus = '.StatusCode::$auditStatus['auditPass'].' and g.deleteStatus = '.StatusCode::$standard;
  293. //下单客户数
  294. $sql = 'select count(a.customerId) as total from (select customerId from qianniao_order_'.$this->enterpriseId.'_1 '.$whereSql.' group by customerId) as a';
  295. $dbResult = $this->objDDeliveryRoute->query($sql);
  296. $customerOrderData['orderCustomerNum'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0;
  297. //总单数 总金额
  298. $sql = 'select count(*) as total, sum(totalMoney) as amount from qianniao_order_'.$this->enterpriseId.'_1 '.$whereSql;
  299. $dbResult = $this->objDDeliveryRoute->query($sql);
  300. $customerOrderData['orderTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0;
  301. $customerOrderData['orderAmount'] = isset($dbResult[0]['amount']) ? $dbResult[0]['amount'] : 0;
  302. //商品数量
  303. $sql = 'select count(a.skuId) as total from (select count(g.skuId) as skuId from qianniao_order_goods_'.$this->enterpriseId.'_1 g left join qianniao_order_'.$this->enterpriseId.'_1 o on o.id = g.orderId '.$whereSqlAs.' group by g.skuId) as a';
  304. $dbResult = $this->objDDeliveryRoute->query($sql);
  305. $customerOrderData['goodsTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0;
  306. //商品吨数 (只统计抄码商品)
  307. $sql = 'select sum(g.buyNum) as total from qianniao_order_goods_'.$this->enterpriseId.'_1 g left join qianniao_order_'.$this->enterpriseId.'_1 o on o.id = g.orderId '.$whereSqlAs;
  308. $dbResult = $this->objDDeliveryRoute->query($sql);
  309. $customerOrderData['otherTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0;
  310. $whereSql .= ' and outStatus <> '.StatusCode::$delete;
  311. //已出库商品吨数 (只统计抄码商品)
  312. $sql = 'select sum(g.outNum) as total from qianniao_order_goods_'.$this->enterpriseId.'_1 g left join qianniao_order_'.$this->enterpriseId.'_1 o on o.id = g.orderId '.$whereSqlAs;
  313. $dbResult = $this->objDDeliveryRoute->query($sql);
  314. $customerOrderData['outOtherTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0;
  315. //已出库 订单金额 订单数
  316. $sql = 'select count(*) as total, sum(totalMoney) as amount from qianniao_order_'.$this->enterpriseId.'_1 '.$whereSql;
  317. $dbResult = $this->objDDeliveryRoute->query($sql);
  318. $customerOrderData['outOrderTotal'] = isset($dbResult[0]['total']) ? $dbResult[0]['total'] : 0;
  319. $customerOrderData['outOrderAmount'] = isset($dbResult[0]['amount']) ? $dbResult[0]['amount'] : 0;
  320. }
  321. $params['orderCustomerNum'] = $customerOrderData['orderCustomerNum'];
  322. $params['orderTotal'] = $customerOrderData['orderTotal'];
  323. $params['orderAmount'] = $customerOrderData['orderAmount'];
  324. $params['goodsTotal'] = $customerOrderData['goodsTotal'];
  325. $params['otherTotal'] = $customerOrderData['otherTotal'];
  326. $params['outOtherTotal'] = $customerOrderData['outOtherTotal'];
  327. $params['outOrderTotal'] = $customerOrderData['outOrderTotal'];
  328. $params['outOrderAmount'] = $customerOrderData['outOrderAmount'];
  329. return $params;
  330. }
  331. /**
  332. * 格式化线路
  333. * @param $params
  334. * @return array|mixed
  335. */
  336. public function formatDeliveryRoute($params)
  337. {
  338. if(isset($params['id'])){
  339. $data = [$params];
  340. }else{
  341. $data = $params;
  342. }
  343. $routeIds = [];
  344. foreach($data as $value){
  345. $routeIds[] = $value['id'];
  346. }
  347. $routeCustomerData = [];
  348. $routeCustomerOrderData = [];
  349. $driverData = [];
  350. if(!empty($routeIds)){
  351. //查询线路绑定客户
  352. $dbResult = $this->objDDeliveryRouteCustomer->select(['routeId' => $routeIds, 'deleteStatus' => StatusCode::$standard], '*', 'sort desc');
  353. if($dbResult){
  354. foreach($dbResult as $value){
  355. $routeCustomerData[$value['routeId']][] = $value;
  356. }
  357. foreach($routeCustomerData as $key => &$value){
  358. $value = self::formatDeliveryRouteCustomer($value);//根据线路绑定的客户查找客户数据
  359. $routeCustomerOrderData[$key] = self::formatDeliveryRouteCustomerOrder($value);//根据客户查找订单数据
  360. }
  361. unset($value);
  362. }
  363. //查询线路绑定司机
  364. $modelResult = $this->objMDriver->getDriverData(['routeId' => $routeIds, 'state' => StatusCode::$standard]);
  365. if($modelResult->isSuccess()){
  366. $driverResult = $modelResult->getData();
  367. foreach($driverResult as $value){
  368. $driverData[$value['routeId']][] = $value;
  369. }
  370. }
  371. }
  372. foreach($data as &$value){
  373. $value['driverData'] = isset($driverData[$value['id']]) ? $driverData[$value['id']] : [];
  374. $value['customerData'] = [];
  375. if(isset($routeCustomerData[$value['id']])){
  376. $value['customerData'] = $routeCustomerData[$value['id']];
  377. }
  378. $value['orderCustomerNum'] = 0;
  379. $value['orderTotal'] = 0;
  380. $value['orderAmount'] = 0;
  381. $value['goodsNum'] = 0;
  382. $value['otherNum'] = 0;
  383. $value['outOtherTotal'] = 0;
  384. $value['outGoodsTotal'] = 0;
  385. $value['outAmount'] = 0;
  386. if(isset($routeCustomerOrderData[$value['id']])){
  387. $value['orderCustomerNum'] = $routeCustomerOrderData[$value['id']]['orderCustomerNum'];
  388. $value['orderTotal'] = $routeCustomerOrderData[$value['id']]['orderTotal'];
  389. $value['orderAmount'] = $routeCustomerOrderData[$value['id']]['orderAmount'];
  390. $value['goodsNum'] = $routeCustomerOrderData[$value['id']]['goodsTotal'];
  391. $value['outOtherTotal'] = $routeCustomerOrderData[$value['id']]['outOtherTotal'] > 0 ? bcdiv($routeCustomerOrderData[$value['id']]['outOtherTotal'], 1000) : $routeCustomerOrderData[$value['id']]['outOtherTotal'];
  392. $value['otherNum'] = $routeCustomerOrderData[$value['id']]['otherTotal'] > 0 ? bcdiv($routeCustomerOrderData[$value['id']]['otherTotal'], 1000) : $routeCustomerOrderData[$value['id']]['otherTotal'];
  393. $value['outGoodsTotal'] = $routeCustomerOrderData[$value['id']]['outOrderTotal'];
  394. $value['outAmount'] = $routeCustomerOrderData[$value['id']]['outOrderAmount'];
  395. }
  396. }
  397. if(isset($params['id'])){
  398. $return = array_shift($data);
  399. }else{
  400. $return = $data;
  401. }
  402. return $return;
  403. }
  404. }