ShippingTemplatesRegionServices.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\ShippingTemplatesRegionDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\CacheService;
  16. use crmeb\utils\Arr;
  17. /**
  18. * 指定邮费
  19. * Class ShippingTemplatesRegionServices
  20. * @package app\services\product\shipping
  21. * @mixin ShippingTemplatesRegionDao
  22. */
  23. class ShippingTemplatesRegionServices extends BaseServices
  24. {
  25. /**
  26. * 构造方法
  27. * ShippingTemplatesRegionServices constructor.
  28. * @param ShippingTemplatesRegionDao $dao
  29. */
  30. public function __construct(ShippingTemplatesRegionDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * @param array $tempIds
  36. * @param array $cityIds
  37. * @param int $expire
  38. * @return bool|mixed|null
  39. */
  40. public function getTempRegionListCache(array $tempIds, array $cityIds, int $expire = 60)
  41. {
  42. return CacheService::redisHandler('apiShipping')->remember(md5('RegionList' . json_encode([$tempIds, $cityIds])), function () use ($tempIds, $cityIds) {
  43. return $this->dao->getTempRegionList($tempIds, $cityIds, 'temp_id,first,first_price,continue,continue_price', 'temp_id');
  44. }, $expire);
  45. }
  46. /**
  47. * 添加运费信息
  48. * @param array $regionInfo
  49. * @param int $group
  50. * @param int $tempId
  51. * @return bool
  52. * @throws \Exception
  53. */
  54. public function saveRegionV1(array $regionInfo, int $group = 0, $tempId = 0)
  55. {
  56. $res = true;
  57. if ($tempId) {
  58. if ($this->dao->count(['temp_id' => $tempId])) {
  59. $res = $this->dao->delete($tempId, 'temp_id');
  60. }
  61. }
  62. $regionList = [];
  63. mt_srand();
  64. foreach ($regionInfo as $item) {
  65. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  66. if (isset($item['city_ids']) && $item['city_ids']) {
  67. foreach ($item['city_ids'] as $value) {
  68. $regionList[] = [
  69. 'temp_id' => $tempId,
  70. 'city_id' => $value ? $value[count($value) - 1] : 0,
  71. 'value' => json_encode($value),
  72. 'first' => $item['first'] ?? 0,
  73. 'first_price' => $item['first_price'] ?? 0,
  74. 'continue' => $item['continue'] ?? 0,
  75. 'continue_price' => $item['continue_price'] ?? 0,
  76. 'group' => $group,
  77. 'uniqid' => $uniqid,
  78. ];
  79. }
  80. }
  81. }
  82. return $res && $this->dao->saveAll($regionList);
  83. }
  84. /**
  85. * 添加运费信息
  86. * @param array $regionInfo
  87. * @param int $group
  88. * @param int $tempId
  89. * @return bool
  90. * @throws \Exception
  91. */
  92. public function saveRegion(array $regionInfo, int $group = 0, $tempId = 0)
  93. {
  94. $res = true;
  95. if ($tempId) {
  96. if ($this->dao->count(['temp_id' => $tempId])) {
  97. $res = $this->dao->delete($tempId, 'temp_id');
  98. }
  99. }
  100. $regionList = [];
  101. mt_srand();
  102. foreach ($regionInfo as $item) {
  103. if (isset($item['region']) && is_array($item['region'])) {
  104. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  105. foreach ($item['region'] as $value) {
  106. if (isset($value['children']) && is_array($value['children'])) {
  107. foreach ($value['children'] as $vv) {
  108. if (!isset($vv['city_id'])) {
  109. throw new AdminException('缺少城市id无法保存');
  110. }
  111. $regionList[] = [
  112. 'temp_id' => $tempId,
  113. 'province_id' => $value['city_id'] ?? 0,
  114. 'city_id' => $vv['city_id'] ?? 0,
  115. 'first' => $item['first'] ?? 0,
  116. 'first_price' => $item['price'] ?? 0,
  117. 'continue' => $item['continue'] ?? 0,
  118. 'continue_price' => $item['continue_price'] ?? 0,
  119. 'group' => $group,
  120. 'uniqid' => $uniqid,
  121. ];
  122. }
  123. } else {
  124. $regionList[0] = [
  125. 'temp_id' => $tempId,
  126. 'province_id' => 0,
  127. 'city_id' => 0,
  128. 'first' => $item['first'] ?? 0,
  129. 'first_price' => $item['price'] ?? 0,
  130. 'continue' => $item['continue'] ?? 0,
  131. 'continue_price' => $item['continue_price'] ?? 0,
  132. 'group' => $group,
  133. 'uniqid' => $uniqid,
  134. ];
  135. }
  136. }
  137. }
  138. }
  139. return $res && $this->dao->saveAll($regionList);
  140. }
  141. /**
  142. * @param int $tempId
  143. * @return array
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function getRegionListV1(int $tempId)
  149. {
  150. $freeList = $this->dao->getShippingList(['temp_id' => $tempId]);
  151. return Arr::formatShipping($freeList);
  152. }
  153. /**
  154. * 获取某个运费模板下的城市数据
  155. * @param int $tempId
  156. * @return array
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function getRegionList(int $tempId)
  162. {
  163. $regionList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
  164. $regionData = [];
  165. $infos = $this->dao->getShippingArray(['uniqid' => $regionList, 'temp_id' => $tempId], '*', 'uniqid');
  166. foreach ($regionList as $uniqid) {
  167. $info = $infos[$uniqid];
  168. if ($info['province_id'] == 0) {
  169. $regionData[] = [
  170. 'region' => [
  171. 'city_id' => 0,
  172. 'name' => '默认全国',
  173. ],
  174. 'regionName' => '默认全国',
  175. 'first' => $info['first'] ? floatval($info['first']) : 0,
  176. 'price' => $info['first_price'] ? floatval($info['first_price']) : 0,
  177. 'continue' => $info['continue'] ? floatval($info['continue']) : 0,
  178. 'continue_price' => $info['continue_price'] ? floatval($info['continue_price']) : 0,
  179. 'uniqid' => $info['uniqid'],
  180. ];
  181. } else {
  182. $regionData[] = [
  183. 'region' => $this->getRegionTemp($uniqid, $info['province_id']),
  184. 'regionName' => '',
  185. 'first' => $info['first'] ? floatval($info['first']) : 0,
  186. 'price' => $info['first_price'] ? floatval($info['first_price']) : 0,
  187. 'continue' => $info['continue'] ? floatval($info['continue']) : 0,
  188. 'continue_price' => $info['continue_price'] ? floatval($info['continue_price']) : 0,
  189. 'uniqid' => $info['uniqid'],
  190. ];
  191. }
  192. }
  193. foreach ($regionData as &$item) {
  194. if (!$item['regionName']) {
  195. $item['regionName'] = implode(';', array_map(function ($val) {
  196. return $val['name'];
  197. }, $item['region']));
  198. }
  199. }
  200. return $regionData;
  201. }
  202. /**
  203. * 获取省份下运费模板
  204. * @param string $uniqid
  205. * @param int $provinceId
  206. * @return array
  207. */
  208. public function getRegionTemp(string $uniqid, int $provinceId)
  209. {
  210. /** @var ShippingTemplatesRegionCityServices $services */
  211. $services = app()->make(ShippingTemplatesRegionCityServices::class);
  212. $infoList = $services->getUniqidList(['uniqid' => $uniqid]);
  213. $childrenData = [];
  214. foreach ($infoList as $item) {
  215. $childrenData[] = [
  216. 'city_id' => $item['province_id'],
  217. 'name' => $item['name'] ?? '全国',
  218. 'children' => $this->getCityTemp($uniqid, $item['province_id'])
  219. ];
  220. }
  221. return $childrenData;
  222. }
  223. /**
  224. * 获取市区下的数据
  225. * @param string $uniqid
  226. * @param int $provinceId
  227. * @return array
  228. */
  229. public function getCityTemp(string $uniqid, int $provinceId)
  230. {
  231. /** @var ShippingTemplatesRegionCityServices $services */
  232. $services = app()->make(ShippingTemplatesRegionCityServices::class);
  233. $infoList = $services->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
  234. $childrenData = [];
  235. foreach ($infoList as $item) {
  236. $childrenData[] = [
  237. 'city_id' => $item['city_id'],
  238. 'name' => $item['name'] ?? '全国',
  239. ];
  240. }
  241. return $childrenData;
  242. }
  243. }