MDistributionCentre.Class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: kang
  5. * Date: 2021/5/26
  6. * Time: 9:55
  7. */
  8. namespace JinDouYun\Model\Delivery;
  9. use Mall\Framework\Core\ErrorCode;
  10. use Mall\Framework\Core\StatusCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use JinDouYun\Dao\Delivery\DDistributionCentre;
  13. use JinDouYun\Dao\Delivery\DDeliveryTask;
  14. use JinDouYun\Dao\Delivery\DDeliveryTaskIndex;
  15. use JinDouYun\Dao\Delivery\DDeliveryDetails;
  16. use JinDouYun\Model\MBaseModel;
  17. class MDistributionCentre extends MBaseModel
  18. {
  19. private $objDDistributionCentre;
  20. private $objDDeliveryTask;
  21. private $objDDeliveryTaskIndex;
  22. private $objDDeliveryDetails;
  23. private $userCenterId;
  24. private $enterpriseId;
  25. public function __construct($enterpriseId, $userCenterId)
  26. {
  27. $this->enterpriseId = $enterpriseId;
  28. $this->userCenterId = $userCenterId;
  29. $this->objDDistributionCentre = new DDistributionCentre('stock'); // 待司机配送主单据表
  30. $this->objDDistributionCentre->setTable('qinniao_delivery_centre_' . $enterpriseId . '_' . date('Y'));
  31. $this->objDDeliveryTaskIndex = new DDeliveryTaskIndex('stock'); // 配送任务索引表
  32. $this->objDDeliveryTaskIndex->setTable('qianniao_delivery_task_index_1' . $enterpriseId);
  33. $this->objDDeliveryTask = new DDeliveryTask('stock'); // 配送任务表
  34. $this->objDDeliveryTask->setTable('qianniao_delivery_task_' . $enterpriseId . '_' . date('Y'));
  35. $this->objDDeliveryDetails = new DDeliveryDetails('stock'); // 配送任务明细
  36. $this->objDDeliveryDetails->setTable('qianniao_delivery_details_' . $enterpriseId . '_' . date('Y'));
  37. }
  38. /**
  39. * 添加待司机配送
  40. * @param $params
  41. * @return ResultWrapper
  42. * @throws Exception
  43. */
  44. public function addDistribution($params)
  45. {
  46. $distributionId = $this->objDDistributionCentre->insert($params);
  47. if($distributionId === false){
  48. return ResultWrapper::fail($this->objDDistributionCentre->error(), ErrorCode::$dberror);
  49. }
  50. return ResultWrapper::success($distributionId);
  51. }
  52. /**
  53. * 删除待司机配送
  54. * @param array $params 要删除待司机配送单
  55. * @return ResultWrapper
  56. */
  57. public function delDistribution($distributionId)
  58. {
  59. $updateData = [
  60. 'updateTime' => time(),
  61. 'deleteStatus' => StatusCode::$delete,
  62. ];
  63. $dbResult = $this->objDDistributionCentre->update($updateData, $distributionId);
  64. if($dbResult === false){
  65. return ResultWrapper::fail($this->objDDistributionCentre->error(), ErrorCode::$dberror);
  66. }else{
  67. return ResultWrapper::success($dbResult);
  68. }
  69. }
  70. /**
  71. * 获取所有的待司机配送
  72. */
  73. public function getAllDistribution($selectParams)
  74. {
  75. $limit = $selectParams['limit'];
  76. unset($selectParams['limit']);
  77. $offset = $selectParams['offset'];
  78. unset($selectParams['offset']);
  79. $returnData = [
  80. 'data' => [],
  81. 'total' => 0,
  82. ];
  83. $dbResult = $this->objDDistributionCentre->select($selectParams,'*','',$limit,$offset);
  84. if ($dbResult === false) {
  85. return ResultWrapper::fail($this->objDDistributionCentre->error(), ErrorCode::$dberror);
  86. }
  87. if(empty($dbResult)){
  88. return ResultWrapper::success($returnData);
  89. }
  90. $dbTotalResult = $this->objDDistributionCentre->count($selectParams);
  91. if ($dbTotalResult === false) {
  92. return ResultWrapper::fail($this->objDDistributionCentre->error(), ErrorCode::$dberror);
  93. }
  94. $return = [
  95. 'data' => $dbResult,
  96. 'total' => $dbTotalResult
  97. ];
  98. return ResultWrapper::success($return);
  99. }
  100. /**
  101. * 添加配送任务
  102. * @param $params
  103. * @return ResultWrapper
  104. */
  105. public function addTask($params)
  106. {
  107. $deliveryTaskData = $params['deliveryTaskData'];
  108. unset($params['deliveryTaskData']);
  109. // 生成编号
  110. $dbResult = $this->objDDeliveryTaskIndex->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'TaskNo', 'createTime desc');
  111. if ($dbResult === false) {
  112. return ResultWrapper::fail($this->objDDeliveryTaskIndex->error(), ErrorCode::$dberror);
  113. }
  114. if(empty($dbResult)){
  115. $params['TaskNo'] = createSerialNumberByDate('');
  116. }else{
  117. $params['TaskNo'] = createSerialNumberByDate($dbResult['TaskNo']);
  118. }
  119. //开启事务
  120. $beginTransactionStatus = $this->objDDeliveryTaskIndex->beginTransaction();
  121. // 组装index数据
  122. $indexDate = [
  123. 'deliveryStatus' => getArrayItem($params,'deliveryStatus',1), //配送状态(1:待配送 2:已配送3:配送中)
  124. 'TaskNo' => getArrayItem($params,'TaskNo',''), //配送任务单号
  125. 'driverId' => getArrayItem($params,'driverId',''), //配送司机id
  126. 'carId' => getArrayItem($params,'carId',''), //配送车辆id
  127. 'deliveryBoyId' => getArrayItem($params,'deliveryBoyId',''), //送货员id
  128. 'deleteStatus' => StatusCode::$standard,
  129. 'enterpriseId' => $this->enterpriseId,
  130. 'createTime' => time(),//创建时间
  131. 'deliveryStartTime'=> time(), // 开始配送时间
  132. ];
  133. //索引表插入数据
  134. $deliveryTaskIndexId = $this->objDDeliveryTaskIndex->insert($indexDate);
  135. if ($deliveryTaskIndexId === false) {
  136. $this->objDDeliveryTaskIndex->rollBack();
  137. return ResultWrapper::fail($this->objDDeliveryTaskIndex->error(), ErrorCode::$dberror);
  138. }
  139. $params['id'] = $deliveryTaskIndexId;
  140. // 主单据插入数据
  141. $deliveryTaskId = $this->objDDeliveryTask->insert($params);
  142. if ($deliveryTaskId === false) {
  143. $this->objDDeliveryTaskIndex->rollBack();
  144. return ResultWrapper::fail($this->objDDeliveryTask->error(), ErrorCode::$dberror);
  145. }
  146. foreach ($deliveryTaskData as $key => $value){
  147. // 主单据表的id赋值给”linkeId“
  148. $deliveryTaskData[$key]['linkeId'] = $deliveryTaskId;
  149. // 配送任务单号
  150. $deliveryTaskData[$key]['TaskNo'] = getArrayItem($params,'TaskNo','');
  151. }
  152. $deliveryDetailsId = $this->objDDeliveryDetails->insert($deliveryTaskData,true);
  153. if ($deliveryDetailsId === false) {
  154. $this->objDDeliveryTaskIndex->rollBack();
  155. return ResultWrapper::fail($this->objDDeliveryDetails->error(), ErrorCode::$dberror);
  156. }
  157. if($beginTransactionStatus){
  158. $this->objDDeliveryTaskIndex->commit();
  159. }
  160. return ResultWrapper::success($deliveryTaskId);
  161. }
  162. /**
  163. * 配送任务完成配送
  164. * @param array $params 配送任务
  165. * @return ResultWrapper
  166. */
  167. public function taskComplete($taskCompleteId)
  168. {
  169. $updateData = [
  170. 'deliveryStatus' => 2, // (1:待配送,2已配送)
  171. 'deliveryEndTime'=> time(), //配送完成时间
  172. ];
  173. $dbResult = $this->objDDeliveryTask->update($updateData, $taskCompleteId);
  174. if($dbResult === false){
  175. return ResultWrapper::fail($this->objDDeliveryTask->error(), ErrorCode::$dberror);
  176. }else{
  177. return ResultWrapper::success($dbResult);
  178. }
  179. }
  180. }