ShippingTemplatesNoDeliveryServices.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\shipping;
  12. use app\dao\shipping\ShippingTemplatesNoDeliveryDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * 不送达
  17. * Class ShippingTemplatesNoDeliveryServices
  18. * @package app\services\shipping
  19. * @method isNoDelivery($tempId, $cityid) 是否不送达
  20. */
  21. class ShippingTemplatesNoDeliveryServices extends BaseServices
  22. {
  23. /**
  24. * 构造方法
  25. * ShippingTemplatesNoDeliveryServices constructor.
  26. * @param ShippingTemplatesNoDeliveryDao $dao
  27. */
  28. public function __construct(ShippingTemplatesNoDeliveryDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 添加不送达信息
  34. * @param array $noDeliveryInfo
  35. * @param int $tempId
  36. * @return bool|mixed
  37. */
  38. public function saveNoDelivery(array $noDeliveryInfo, int $tempId = 0)
  39. {
  40. $res = true;
  41. if ($tempId) {
  42. if ($this->dao->count(['temp_id' => $tempId])) {
  43. $res = $this->dao->delete($tempId, 'temp_id');
  44. }
  45. }
  46. $placeList = [];
  47. mt_srand();
  48. foreach ($noDeliveryInfo as $item) {
  49. if (isset($item['place']) && is_array($item['place'])) {
  50. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  51. foreach ($item['place'] as $value) {
  52. if (isset($value['children']) && is_array($value['children'])) {
  53. foreach ($value['children'] as $vv) {
  54. if (!isset($vv['city_id'])) {
  55. throw new AdminException(400591);
  56. }
  57. $placeList [] = [
  58. 'temp_id' => $tempId,
  59. 'province_id' => $value['city_id'] ?? 0,
  60. 'city_id' => $vv['city_id'] ?? 0,
  61. 'uniqid' => $uniqid,
  62. ];
  63. }
  64. }
  65. }
  66. }
  67. }
  68. if (count($placeList)) {
  69. return $res && $this->dao->saveAll($placeList);
  70. } else {
  71. return $res;
  72. }
  73. }
  74. /**
  75. * 获得指定包邮城市地址
  76. * @param int $tempId
  77. * @return array
  78. */
  79. public function getNoDeliveryList(int $tempId)
  80. {
  81. $freeIdList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
  82. $freeData = [];
  83. foreach ($freeIdList as $uniqid) {
  84. $freeData[] = [
  85. 'place' => $this->getNoDeliveryTemp($uniqid),
  86. ];
  87. }
  88. foreach ($freeData as &$item) {
  89. $item['placeName'] = implode(';', array_column($item['place'], 'name'));
  90. }
  91. return $freeData;
  92. }
  93. /**
  94. * 获取不送达的省份
  95. * @param string $uniqid
  96. * @return array
  97. */
  98. public function getNoDeliveryTemp(string $uniqid)
  99. {
  100. /** @var ShippingTemplatesNoDeliveryCityServices $service */
  101. $service = app()->make(ShippingTemplatesNoDeliveryCityServices::class);
  102. $infoList = $service->getUniqidList(['uniqid' => $uniqid]);
  103. $childrenData = [];
  104. foreach ($infoList as $item) {
  105. $childrenData[] = [
  106. 'city_id' => $item['province_id'],
  107. 'name' => $item['name'] ?? '全国',
  108. 'children' => $this->getCityTemp($uniqid, $item['province_id'])
  109. ];
  110. }
  111. return $childrenData;
  112. }
  113. /**
  114. * 获取市区数据
  115. * @param string $uniqid
  116. * @param int $provinceId
  117. * @return array
  118. */
  119. public function getCityTemp(string $uniqid, int $provinceId)
  120. {
  121. /** @var ShippingTemplatesNoDeliveryCityServices $service */
  122. $service = app()->make(ShippingTemplatesNoDeliveryCityServices::class);
  123. $infoList = $service->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
  124. $childrenData = [];
  125. foreach ($infoList as $item) {
  126. $childrenData[] = [
  127. 'city_id' => $item['city_id'],
  128. 'name' => $item['name'] ?? '全国',
  129. ];
  130. }
  131. return $childrenData;
  132. }
  133. }