ShippingTemplatesFreeServices.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\ShippingTemplatesFreeDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * 包邮
  17. * Class ShippingTemplatesFreeServices
  18. * @package app\services\shipping
  19. * @method delete($id, ?string $key = null) 删除数据
  20. * @method isFree($tempId, $cityid, $number, $price) 是否可以满足包邮
  21. */
  22. class ShippingTemplatesFreeServices extends BaseServices
  23. {
  24. /**
  25. * 构造方法
  26. * ShippingTemplatesFreeServices constructor.
  27. * @param ShippingTemplatesDao $dao
  28. */
  29. public function __construct(ShippingTemplatesFreeDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 添加包邮信息
  35. * @param array $appointInfo
  36. * @param int $type
  37. * @param int $tempId
  38. * @return bool
  39. * @throws \Exception
  40. */
  41. public function saveFree(array $appointInfo, int $type = 0, int $tempId = 0)
  42. {
  43. $res = true;
  44. if ($tempId) {
  45. if ($this->dao->count(['temp_id' => $tempId])) {
  46. $res = $this->dao->delete($tempId, 'temp_id');
  47. }
  48. }
  49. $placeList = [];
  50. foreach ($appointInfo as $item) {
  51. if (isset($item['place']) && is_array($item['place'])) {
  52. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  53. foreach ($item['place'] as $value) {
  54. if (isset($value['children']) && is_array($value['children'])) {
  55. foreach ($value['children'] as $vv) {
  56. if (!isset($vv['city_id'])) {
  57. throw new AdminException(400591);
  58. }
  59. $placeList [] = [
  60. 'temp_id' => $tempId,
  61. 'province_id' => $value['city_id'] ?? 0,
  62. 'city_id' => $vv['city_id'] ?? 0,
  63. 'number' => $item['a_num'] ?? 0,
  64. 'price' => $item['a_price'] ?? 0,
  65. 'type' => $type,
  66. 'uniqid' => $uniqid,
  67. ];
  68. }
  69. }
  70. }
  71. }
  72. }
  73. if (count($placeList)) {
  74. return $res && $this->dao->saveAll($placeList);
  75. } else {
  76. return $res;
  77. }
  78. }
  79. /**
  80. * 获得指定包邮城市地址
  81. * @param int $tempId
  82. * @return array
  83. */
  84. public function getFreeList(int $tempId)
  85. {
  86. $freeIdList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
  87. $freeData = [];
  88. $infos = $this->dao->getShippingArray(['uniqid' => $freeIdList, 'temp_id' => $tempId], '*', 'uniqid');
  89. foreach ($freeIdList as $uniqid) {
  90. $info = $infos[$uniqid];
  91. $freeData[] = [
  92. 'place' => $this->getFreeTemp($uniqid, $info['province_id']),
  93. 'a_num' => $info['number'] ? floatval($info['number']) : 0,
  94. 'a_price' => $info['price'] ? floatval($info['price']) : 0,
  95. ];
  96. }
  97. foreach ($freeData as &$item) {
  98. $item['placeName'] = implode(';', array_column($item['place'], 'name'));
  99. }
  100. return $freeData;
  101. }
  102. /**
  103. * 获取包邮的省份
  104. * @param string $uniqid
  105. * @param int $provinceId
  106. * @return array
  107. */
  108. public function getFreeTemp(string $uniqid, int $provinceId)
  109. {
  110. /** @var ShippingTemplatesFreeCityServices $service */
  111. $service = app()->make(ShippingTemplatesFreeCityServices::class);
  112. $infoList = $service->getUniqidList(['uniqid' => $uniqid]);
  113. $childrenData = [];
  114. foreach ($infoList as $item) {
  115. $childrenData[] = [
  116. 'city_id' => $item['province_id'],
  117. 'name' => $item['name'] ?? '全国',
  118. 'children' => $this->getCityTemp($uniqid, $item['province_id'])
  119. ];
  120. }
  121. return $childrenData;
  122. }
  123. /**
  124. * 获取市区数据
  125. * @param string $uniqid
  126. * @param int $provinceId
  127. * @return array
  128. */
  129. public function getCityTemp(string $uniqid, int $provinceId)
  130. {
  131. /** @var ShippingTemplatesFreeCityServices $service */
  132. $service = app()->make(ShippingTemplatesFreeCityServices::class);
  133. $infoList = $service->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
  134. $childrenData = [];
  135. foreach ($infoList as $item) {
  136. $childrenData[] = [
  137. 'city_id' => $item['city_id'],
  138. 'name' => $item['name'] ?? '全国',
  139. ];
  140. }
  141. return $childrenData;
  142. }
  143. }