MSupplierOrderDetails.Class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace JinDouYun\Model\Order;
  3. use JinDouYun\Dao\Order\DSupplierOrderDetails;
  4. use JinDouYun\Dao\Order\DSupplierOrderIndex;
  5. use JinDouYun\Model\Customer\MCustomer;
  6. use Mall\Framework\Core\ErrorCode;
  7. use Mall\Framework\Core\ResultWrapper;
  8. use Mall\Framework\Core\StatusCode;
  9. /**
  10. * @copyright Copyright (c) https://www.qianniaovip.com All rights reserved
  11. * Description:
  12. * Class MSupplierOrderDetails
  13. * @package JinDouYun\Model\Order
  14. */
  15. class MSupplierOrderDetails
  16. {
  17. private $enterpriseId;
  18. private $userCenterId;
  19. private $objDSupplierOrderDetails;
  20. public static $cutTable = 50000;
  21. public $objDSupplierOrderIndex;
  22. /**
  23. * MSupplierOrderDetails constructor.
  24. * @param $enterpriseId
  25. * @param $userCenterId
  26. * @throws \Exception
  27. */
  28. public function __construct($enterpriseId, $userCenterId)
  29. {
  30. $this->enterpriseId = $enterpriseId;
  31. $this->userCenterId = $userCenterId;
  32. $this->objDSupplierOrderDetails = new DSupplierOrderDetails();
  33. $this->objDSupplierOrderIndex = new DSupplierOrderIndex();
  34. $this->objDSupplierOrderIndex->setTable('qianniao_supplier_order_index_'.$this->enterpriseId);
  35. }
  36. /**
  37. * Doc: (des="")
  38. * User: XMing
  39. * Date: 2020/12/22
  40. * Time: 5:22 下午
  41. * @param array $selectParams
  42. */
  43. private function buildSqlBySelectParams(array $selectParams): array
  44. {
  45. $fields = ' o.id as orderId,s.*';
  46. $countFields = ' COUNT(o.id) as total ';
  47. $whereSql = '';
  48. if (isset($selectParams['supplierId']) && !empty($selectParams['supplierId'])){
  49. $whereSql .= ' AND s.supplierId = '.$selectParams['supplierId'];
  50. }
  51. if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])){
  52. $whereSql .= ' AND o.auditStatus = '.$selectParams['auditStatus'];
  53. }
  54. if (isset($selectParams['startTime']) && !empty($selectParams['startTime'])){
  55. $whereSql .= ' AND o.createTime BETWEEN '.$selectParams['startTime'].' AND '.$selectParams['endTime'];
  56. }
  57. $countSql = 'SElECT '.$countFields.' FROM qianniao_order_index_'.$this->enterpriseId.' AS o
  58. LEFT JOIN qianniao_supplier_order_index_'.$this->enterpriseId.' as s
  59. ON s.no = o.no WHERE o.deleteStatus = '.StatusCode::$standard.$whereSql;
  60. $whereSql .= ' GROUP BY s.no ';
  61. $whereSql .= ' ORDER BY o.createTime DESC ';
  62. if (isset($selectParams['limit']) && !empty($selectParams['limit'])){
  63. $whereSql .= ' LIMIT '.$selectParams['offset'].','.$selectParams['limit'];
  64. }
  65. $sql = 'SElECT '.$fields.' FROM qianniao_order_index_'.$this->enterpriseId.' AS o
  66. LEFT JOIN qianniao_supplier_order_index_'.$this->enterpriseId.' as s
  67. ON s.no = o.no WHERE o.deleteStatus = '.StatusCode::$standard.$whereSql;
  68. return [
  69. 'sql' => $sql,
  70. 'countSql' => $countSql
  71. ];
  72. }
  73. /**
  74. * Doc: (des="")
  75. * User: XMing
  76. * Date: 2020/12/22
  77. * Time: 5:21 下午
  78. * @param array $selectParams
  79. * @return ResultWrapper
  80. */
  81. public function getAll(array $selectParams): ResultWrapper
  82. {
  83. $buildSql = self::buildSqlBySelectParams($selectParams);
  84. $indexLists = $this->objDSupplierOrderIndex->query($buildSql['sql']);
  85. if ($indexLists === false){
  86. return ResultWrapper::fail($this->objDSupplierOrderIndex->error(),ErrorCode::$dberror);
  87. }
  88. $count = $this->objDSupplierOrderIndex->query($buildSql['countSql']);
  89. $customerIds = [];
  90. foreach ($indexLists as $list){
  91. $customerIds[] = $list['customerId'];
  92. }
  93. $objMCustomer = new MCustomer($this->enterpriseId,$this->userCenterId);
  94. $reservoirMapResult = $objMCustomer->getReservoirByCustomerIds($customerIds);
  95. if (!$reservoirMapResult->isSuccess()){
  96. return ResultWrapper::fail($reservoirMapResult->getData(),$reservoirMapResult->getErrorCode());
  97. }
  98. $reservoirMap = $reservoirMapResult->getData();
  99. foreach ($indexLists as &$list){
  100. $list['reservoir'] = !empty($list['reservoir']) ? json_decode($list['reservoir'],true) : [];
  101. if (empty($list['reservoir'])){
  102. $list['reservoir'] = getArrayItem($reservoirMap,$list['customerId'],[]);
  103. }
  104. }
  105. $ret = [
  106. 'data' => $indexLists,
  107. 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
  108. ];
  109. return ResultWrapper::success($ret);
  110. }
  111. /**
  112. * Doc: (des="")
  113. * User: XMing
  114. * Date: 2020/12/22
  115. * Time: 6:16 下午
  116. * @param int $id
  117. * @return ResultWrapper
  118. * @throws \Exception
  119. */
  120. public function get(int $id): ResultWrapper
  121. {
  122. $index = $this->objDSupplierOrderIndex->get($id);
  123. if ($index === false){
  124. return ResultWrapper::fail($this->objDSupplierOrderIndex->error(),ErrorCode::$dberror);
  125. }
  126. if (empty($index)){
  127. return ResultWrapper::fail('未获取到指定订单',ErrorCode::$paramError);
  128. }
  129. $fix = ceil($index['orderId'] / self::$cutTable);
  130. $this->objDSupplierOrderDetails->setTable('qianniao_supplier_order_details_'.$this->enterpriseId.'_'.$fix);
  131. $details = $this->objDSupplierOrderDetails->select(['no' => $index['no']]);
  132. if ($details === false){
  133. return ResultWrapper::fail($this->objDSupplierOrderDetails->error(),ErrorCode::$dberror);
  134. }
  135. $customerIds = [];
  136. foreach ($details as $list){
  137. if (!in_array($list['customerId'],$customerIds)){
  138. $customerIds[] = $list['customerId'];
  139. }
  140. }
  141. $objMCustomer = new MCustomer($this->enterpriseId,$this->userCenterId);
  142. $reservoirMapResult = $objMCustomer->getReservoirByCustomerIds($customerIds);
  143. if (!$reservoirMapResult->isSuccess()){
  144. return ResultWrapper::fail($reservoirMapResult->getData(),$reservoirMapResult->getErrorCode());
  145. }
  146. $reservoirMap = $reservoirMapResult->getData();
  147. $index['reservoir'] = !empty($index['reservoir']) ? json_decode($index['reservoir'],true) : [];
  148. foreach ($details as &$val){
  149. $val['specGroup'] = !empty($val['specGroup']) ? json_decode($val['specGroup'],true) : [];
  150. unset($val['reservoir']);
  151. if (empty($index['reservoir'])){
  152. $index['reservoir'] = getArrayItem($reservoirMap,$val['customerId'],[]);
  153. }
  154. }
  155. $index['details'] = $details;
  156. return ResultWrapper::success($index);
  157. }
  158. /**
  159. * Doc: (des="修改-采购单生成状态")
  160. * User: XMing
  161. * Date: 2020/12/24
  162. * Time: 9:23 上午
  163. * @param $ids
  164. * @return ResultWrapper
  165. */
  166. public function updatePurchaseStatus($ids): ResultWrapper
  167. {
  168. $result = $this->objDSupplierOrderIndex->update(['purchaseStatus' => StatusCode::$standard,'updateTime' => time()],['id' => $ids]);
  169. if ($result === false){
  170. return ResultWrapper::fail($this->objDSupplierOrderIndex->error(),ErrorCode::$dberror);
  171. }
  172. return ResultWrapper::success(true);
  173. }
  174. /**
  175. * Doc: (des="获取供应商订单数量")
  176. * User: XMing
  177. * Date: 2020/12/26
  178. * Time: 4:46 下午
  179. * @param $supplierId
  180. * @return ResultWrapper
  181. */
  182. public function getTotalBySupplierId($supplierId): ResultWrapper
  183. {
  184. $selectParams = [
  185. 'supplierId' => $supplierId,
  186. 'auditStatus' => StatusCode::$auditStatus['auditPass'],
  187. ];
  188. $buildSql = self::buildSqlBySelectParams($selectParams);
  189. $count = $this->objDSupplierOrderIndex->query($buildSql['countSql']);
  190. if ($count === false){
  191. return ResultWrapper::fail($this->objDSupplierOrderIndex->error(),ErrorCode::$dberror);
  192. }
  193. $total = isset($count[0]['total']) ? $count[0]['total'] : 0;
  194. return ResultWrapper::success($total);
  195. }
  196. }