ShippingTemplatesNoDeliveryServices.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\product\shipping;
  12. use app\dao\product\shipping\ShippingTemplatesNoDeliveryDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\utils\Arr;
  16. /**
  17. * 不送达
  18. * Class ShippingTemplatesNoDeliveryServices
  19. * @package app\services\product\shipping
  20. * @mixin ShippingTemplatesNoDeliveryDao
  21. */
  22. class ShippingTemplatesNoDeliveryServices extends BaseServices
  23. {
  24. /**
  25. * 构造方法
  26. * ShippingTemplatesNoDeliveryServices constructor.
  27. * @param ShippingTemplatesNoDeliveryDao $dao
  28. */
  29. public function __construct(ShippingTemplatesNoDeliveryDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 添加不送达信息
  35. * @param array $noDeliveryInfo
  36. * @param int $tempId
  37. * @return bool|mixed
  38. */
  39. public function saveNoDeliveryV1(array $noDeliveryInfo, int $tempId = 0)
  40. {
  41. $res = true;
  42. if ($tempId) {
  43. if ($this->dao->count(['temp_id' => $tempId])) {
  44. $res = $this->dao->delete($tempId, 'temp_id');
  45. }
  46. }
  47. $placeList = [];
  48. mt_srand();
  49. foreach ($noDeliveryInfo as $item) {
  50. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  51. foreach ($item['city_ids'] as $cityId) {
  52. $placeList [] = [
  53. 'temp_id' => $tempId,
  54. 'city_id' => $cityId[count($cityId) - 1],
  55. 'value' => json_encode($cityId),
  56. 'uniqid' => $uniqid,
  57. ];
  58. }
  59. }
  60. if (count($placeList)) {
  61. return $res && $this->dao->saveAll($placeList);
  62. } else {
  63. return $res;
  64. }
  65. }
  66. /**
  67. * 添加不送达信息
  68. * @param array $noDeliveryInfo
  69. * @param int $tempId
  70. * @return bool|mixed
  71. */
  72. public function saveNoDelivery(array $noDeliveryInfo, int $tempId = 0)
  73. {
  74. $res = true;
  75. if ($tempId) {
  76. if ($this->dao->count(['temp_id' => $tempId])) {
  77. $res = $this->dao->delete($tempId, 'temp_id');
  78. }
  79. }
  80. $placeList = [];
  81. mt_srand();
  82. foreach ($noDeliveryInfo as $item) {
  83. if (isset($item['place']) && is_array($item['place'])) {
  84. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  85. foreach ($item['place'] as $value) {
  86. if (isset($value['children']) && is_array($value['children'])) {
  87. foreach ($value['children'] as $vv) {
  88. if (!isset($vv['city_id'])) {
  89. throw new AdminException('缺少城市id无法保存');
  90. }
  91. $placeList [] = [
  92. 'temp_id' => $tempId,
  93. 'province_id' => $value['city_id'] ?? 0,
  94. 'city_id' => $vv['city_id'] ?? 0,
  95. 'uniqid' => $uniqid,
  96. ];
  97. }
  98. }
  99. }
  100. }
  101. }
  102. if (count($placeList)) {
  103. return $res && $this->dao->saveAll($placeList);
  104. } else {
  105. return $res;
  106. }
  107. }
  108. /**
  109. * @param int $tempId
  110. * @return array
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function getNoDeliveryListV1(int $tempId)
  116. {
  117. $freeList = $this->dao->getShippingList(['temp_id' => $tempId]);
  118. return Arr::formatShipping($freeList);
  119. }
  120. /**
  121. * 获得指定包邮城市地址
  122. * @param int $tempId
  123. * @return array
  124. */
  125. public function getNoDeliveryList(int $tempId)
  126. {
  127. $freeIdList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
  128. $freeData = [];
  129. $infos = $this->dao->getShippingArray(['uniqid' => $freeIdList, 'temp_id' => $tempId], '*', 'uniqid');
  130. foreach ($freeIdList as $uniqid) {
  131. $info = $infos[$uniqid];
  132. $freeData[] = [
  133. 'place' => $this->getNoDeliveryTemp($uniqid, $info['province_id']),
  134. ];
  135. }
  136. foreach ($freeData as &$item) {
  137. $item['placeName'] = implode(';', array_column($item['place'], 'name'));
  138. }
  139. return $freeData;
  140. }
  141. /**
  142. * 获取不送达的省份
  143. * @param string $uniqid
  144. * @param int $provinceId
  145. * @return array
  146. */
  147. public function getNoDeliveryTemp(string $uniqid, int $provinceId)
  148. {
  149. /** @var ShippingTemplatesNoDeliveryCityServices $service */
  150. $service = app()->make(ShippingTemplatesNoDeliveryCityServices::class);
  151. $infoList = $service->getUniqidList(['uniqid' => $uniqid]);
  152. $childrenData = [];
  153. foreach ($infoList as $item) {
  154. $childrenData[] = [
  155. 'city_id' => $item['province_id'],
  156. 'name' => $item['name'] ?? '全国',
  157. 'children' => $this->getCityTemp($uniqid, $provinceId)
  158. ];
  159. }
  160. return $childrenData;
  161. }
  162. /**
  163. * 获取市区数据
  164. * @param string $uniqid
  165. * @param int $provinceId
  166. * @return array
  167. */
  168. public function getCityTemp(string $uniqid, int $provinceId)
  169. {
  170. /** @var ShippingTemplatesNoDeliveryCityServices $service */
  171. $service = app()->make(ShippingTemplatesNoDeliveryCityServices::class);
  172. $infoList = $service->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
  173. $childrenData = [];
  174. foreach ($infoList as $item) {
  175. $childrenData[] = [
  176. 'city_id' => $item['city_id'],
  177. 'name' => $item['name'] ?? '全国',
  178. ];
  179. }
  180. return $childrenData;
  181. }
  182. }