QueueAuxiliaryServices.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\other\queue;
  12. use app\dao\other\queue\QueueAuxiliaryDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreOrderServices;
  15. use think\exception\ValidateException;
  16. /**
  17. * 队列辅助
  18. * Class QueueAuxiliaryServices
  19. * @package app\services\other\queue
  20. * @mixin QueueAuxiliaryDao
  21. */
  22. class QueueAuxiliaryServices extends BaseServices
  23. {
  24. public static $_status = [
  25. 0 => "未执行",
  26. 1 => "成功",
  27. 2 => "失败",
  28. 3 => '删除'
  29. ];
  30. /**
  31. * QueueAuxiliaryServices constructor.
  32. * @param QueueAuxiliaryDao $dao
  33. */
  34. public function __construct(QueueAuxiliaryDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 添加队列数据
  40. * @param $queueId
  41. * @param $ids
  42. * @param $data
  43. * @param $type
  44. * @param $rediskey
  45. * @return bool
  46. */
  47. public function saveQueueOrderData($queueId, $ids, $data, $type, $rediskey)
  48. {
  49. if (!$ids) {
  50. throw new ValidateException('缺少数据');
  51. }
  52. $data_all = [];
  53. $save = ['binding_id' => $queueId, 'type' => $rediskey, 'status' => 0, 'add_time' => time()];
  54. if ($type == 7) {//批量发货读取数据表格
  55. foreach ($data as $k => $v) {
  56. if ($v[0] && $v[1] && $v[2] && $v[3] && $v[4] && in_array($v[0], $ids)) {
  57. $save['relation_id'] = $v[0];
  58. $save['other'] = json_encode(['id' => $v[0], 'order_id' => $v[1], 'delivery_name' => $v[2], 'delivery_code' => $v[3], 'delivery_id' => $v[4], 'delivery_status' => 0, 'wx_message' => 0, 'phone_message' => 0, 'error_info' => ""]);
  59. $data_all[] = $save;
  60. }
  61. }
  62. } else {
  63. foreach ($ids as $k => $v) {
  64. $save['relation_id'] = $v;
  65. $save['other'] = json_encode(['id' => $v, 'delivery_status' => 0, 'wx_message' => 0, 'phone_message' => 0, 'error_info' => ""]);
  66. $data_all[] = $save;
  67. }
  68. }
  69. if ($data_all) {
  70. if (!$this->dao->saveAll($data_all)) {
  71. throw new ValidateException('添加队列数据失败');
  72. }
  73. }
  74. return true;
  75. }
  76. /**
  77. * 获取发货缓存数据列表
  78. * @param array $where
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function getOrderExpreList(array $where, int $limit = 0)
  85. {
  86. if (!$where) throw new ValidateException("获取发货缓存数据条件缺失");
  87. if ($limit) {
  88. [$page] = $this->getPageValue();
  89. } else {
  90. [$page, $limit] = $this->getPageValue();
  91. }
  92. return $this->dao->getOrderExpreList($where, $page, $limit);
  93. }
  94. /**
  95. * 修改订单缓存数据
  96. * @param array $where
  97. * @param array $data
  98. * @return mixed
  99. */
  100. public function updateOrderStatus(array $where, array $data)
  101. {
  102. if (!$where) throw new ValidateException("数据条件缺失");
  103. return $this->dao->update($where, $data);
  104. }
  105. /**
  106. * 根据条件统计缓存数据
  107. * @param array $where
  108. * @return int
  109. */
  110. public function getCountOrder(array $where)
  111. {
  112. return $this->dao->count($where);
  113. }
  114. /**
  115. * 查看订单缓存数据的各种状态
  116. * @param $orderId
  117. * @param $queueId
  118. * @return bool|mixed
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. public function getOrderOtherSataus($orderId, $queueId, $cacheType)
  124. {
  125. if (!$orderId || !$queueId) return false;
  126. $where['relation_id'] = $orderId;
  127. $where['binding_id'] = $queueId;
  128. $where['type'] = $cacheType;
  129. $where['status'] = 1;
  130. $getOne = $this->dao->getOrderCacheOne($where);
  131. if ($getOne) return true;
  132. return false;
  133. }
  134. /**
  135. * 获取发货记录
  136. * @param array $where
  137. * @return array
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. */
  142. public function deliveryLogList(array $where = [])
  143. {
  144. [$page, $limit] = $this->getPageValue();
  145. $list = $this->dao->deliveryLogList($where, $page, $limit);
  146. $list = $this->doBatchDeliveryData($list);
  147. $count = $this->dao->count($where);
  148. return compact('list', 'count');
  149. }
  150. /**
  151. * 下载发货记录
  152. * @param array $where
  153. * @return array
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function getExportData(array $where, int $limit = 0)
  159. {
  160. if (!$where) throw new ValidateException("数据条件缺失");
  161. $list = $this->getOrderExpreList($where, $limit);
  162. $list = $this->doBatchDeliveryData($list);
  163. return $list;
  164. }
  165. /**
  166. * 批量发货记录数据
  167. * @param $list
  168. * @return mixed
  169. */
  170. public function doBatchDeliveryData($list)
  171. {
  172. if ($list) {
  173. /** @var StoreOrderServices $storeOrderService */
  174. $storeOrderService = app()->make(StoreOrderServices::class);
  175. /** @var QueueServices $queueService */
  176. $queueService = app()->make(QueueServices::class);
  177. $type = array_flip($queueService->queue_redis_key);
  178. foreach ($list as &$v) {
  179. $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  180. $v['status_cn'] = self::$_status[$v['status']];
  181. $v['error'] = $v['status'] == 1 ? '无' : '队列异常';
  182. $orderInfo = $storeOrderService->getOne(['id' => $v['relation_id']]);
  183. if (!$orderInfo) {
  184. continue;
  185. }
  186. $v['order_id'] = $orderInfo['order_id'];
  187. if (in_array($type[$v['type']], [7, 8, 9])) {
  188. $v['delivery_name'] = $orderInfo ? $orderInfo['delivery_name'] : "";
  189. $v['delivery_id'] = $orderInfo ? $orderInfo['delivery_id'] : "";
  190. }
  191. if ($type[$v['type']] == 10) {
  192. $v['fictitious_content'] = $orderInfo ? $orderInfo['fictitious_content'] : "";
  193. }
  194. }
  195. }
  196. return $list;
  197. }
  198. /**
  199. * 获取某个队列的数据缓存
  200. * @param $bindingId
  201. * @param $type
  202. * @return array
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\DbException
  205. * @throws \think\db\exception\ModelNotFoundException
  206. */
  207. public function getCacheOidList($bindingId, $type)
  208. {
  209. return $this->dao->getCacheOidList($bindingId, $type);
  210. }
  211. }