ShippingTemplatesRegionServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\ShippingTemplatesRegionDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * 指定邮费
  17. * Class ShippingTemplatesRegionServices
  18. * @package app\services\shipping
  19. * @method delete($id, ?string $key = null) 删除数据
  20. * @method getTempRegionList(array $tempIds, array $cityId) 根据运费模板id和城市id获得包邮数据列表
  21. */
  22. class ShippingTemplatesRegionServices extends BaseServices
  23. {
  24. /**
  25. * 构造方法
  26. * ShippingTemplatesRegionServices constructor.
  27. * @param ShippingTemplatesRegionDao $dao
  28. */
  29. public function __construct(ShippingTemplatesRegionDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 添加运费信息
  35. * @param array $regionInfo
  36. * @param int $type
  37. * @param int $tempId
  38. * @return bool
  39. * @throws \Exception
  40. */
  41. public function saveRegion(array $regionInfo, int $type = 0, $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. $regionList = [];
  50. foreach ($regionInfo as $item) {
  51. if (isset($item['region']) && is_array($item['region'])) {
  52. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  53. foreach ($item['region'] 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. $regionList[] = [
  60. 'temp_id' => $tempId,
  61. 'province_id' => $value['city_id'] ?? 0,
  62. 'city_id' => $vv['city_id'] ?? 0,
  63. 'first' => $item['first'] ?? 0,
  64. 'first_price' => $item['price'] ?? 0,
  65. 'continue' => $item['continue'] ?? 0,
  66. 'continue_price' => $item['continue_price'] ?? 0,
  67. 'type' => $type,
  68. 'uniqid' => $uniqid,
  69. ];
  70. }
  71. } else {
  72. $regionList[0] = [
  73. 'temp_id' => $tempId,
  74. 'province_id' => 0,
  75. 'city_id' => 0,
  76. 'first' => $item['first'] ?? 0,
  77. 'first_price' => $item['price'] ?? 0,
  78. 'continue' => $item['continue'] ?? 0,
  79. 'continue_price' => $item['continue_price'] ?? 0,
  80. 'type' => $type,
  81. 'uniqid' => $uniqid,
  82. ];
  83. }
  84. }
  85. }
  86. }
  87. return $res && $this->dao->saveAll($regionList);
  88. }
  89. /**
  90. * 获取某个运费模板下的城市数据
  91. * @param int $tempId
  92. * @return array
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function getRegionList(int $tempId)
  98. {
  99. $regionList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
  100. $regionData = [];
  101. $infos = $this->dao->getShippingArray(['uniqid' => $regionList, 'temp_id' => $tempId], '*', 'uniqid');
  102. foreach ($regionList as $uniqid) {
  103. $info = $infos[$uniqid];
  104. if ($info['province_id'] == 0) {
  105. $regionData[] = [
  106. 'region' => [
  107. 'city_id' => 0,
  108. 'name' => '默认全国',
  109. ],
  110. 'regionName' => '默认全国',
  111. 'first' => $info['first'] ? floatval($info['first']) : 0,
  112. 'price' => $info['first_price'] ? floatval($info['first_price']) : 0,
  113. 'continue' => $info['continue'] ? floatval($info['continue']) : 0,
  114. 'continue_price' => $info['continue_price'] ? floatval($info['continue_price']) : 0,
  115. 'uniqid' => $info['uniqid'],
  116. ];
  117. } else {
  118. $regionData[] = [
  119. 'region' => $this->getRegionTemp($uniqid, $info['province_id']),
  120. 'regionName' => '',
  121. 'first' => $info['first'] ? floatval($info['first']) : 0,
  122. 'price' => $info['first_price'] ? floatval($info['first_price']) : 0,
  123. 'continue' => $info['continue'] ? floatval($info['continue']) : 0,
  124. 'continue_price' => $info['continue_price'] ? floatval($info['continue_price']) : 0,
  125. 'uniqid' => $info['uniqid'],
  126. ];
  127. }
  128. }
  129. foreach ($regionData as &$item) {
  130. if (!$item['regionName']) {
  131. $item['regionName'] = implode(';', array_map(function ($val) {
  132. return $val['name'];
  133. }, $item['region']));
  134. }
  135. }
  136. return $regionData;
  137. }
  138. /**
  139. * 获取省份下运费模板
  140. * @param string $uniqid
  141. * @param int $provinceId
  142. * @return array
  143. */
  144. public function getRegionTemp(string $uniqid, int $provinceId)
  145. {
  146. /** @var ShippingTemplatesRegionCityServices $services */
  147. $services = app()->make(ShippingTemplatesRegionCityServices::class);
  148. $infoList = $services->getUniqidList(['uniqid' => $uniqid]);
  149. $childrenData = [];
  150. foreach ($infoList as $item) {
  151. $childrenData[] = [
  152. 'city_id' => $item['province_id'],
  153. 'name' => $item['name'] ?? '全国',
  154. 'children' => $this->getCityTemp($uniqid, $item['province_id'])
  155. ];
  156. }
  157. return $childrenData;
  158. }
  159. /**
  160. * 获取市区下的数据
  161. * @param string $uniqid
  162. * @param int $provinceId
  163. * @return array
  164. */
  165. public function getCityTemp(string $uniqid, int $provinceId)
  166. {
  167. /** @var ShippingTemplatesRegionCityServices $services */
  168. $services = app()->make(ShippingTemplatesRegionCityServices::class);
  169. $infoList = $services->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
  170. $childrenData = [];
  171. foreach ($infoList as $item) {
  172. $childrenData[] = [
  173. 'city_id' => $item['city_id'],
  174. 'name' => $item['name'] ?? '全国',
  175. ];
  176. }
  177. return $childrenData;
  178. }
  179. }