ShippingTemplatesFreeServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\ShippingTemplatesFreeDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\CacheService;
  16. use crmeb\utils\Arr;
  17. /**
  18. * 包邮
  19. * Class ShippingTemplatesFreeServices
  20. * @package app\services\product\shipping
  21. * @mixin ShippingTemplatesFreeDao
  22. */
  23. class ShippingTemplatesFreeServices extends BaseServices
  24. {
  25. /**
  26. * 构造方法
  27. * ShippingTemplatesFreeServices constructor.
  28. * @param ShippingTemplatesFreeDao $dao
  29. */
  30. public function __construct(ShippingTemplatesFreeDao $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 isFreeListCache(array $tempIds, array $cityIds, int $expire = 60)
  41. {
  42. return CacheService::redisHandler('apiShipping')->remember(md5('isFreeList' . json_encode([$tempIds, $cityIds])), function () use ($tempIds, $cityIds) {
  43. return $this->dao->isFreeList($tempIds, $cityIds, 0, 'temp_id,number,price', 'temp_id');
  44. }, $expire);
  45. }
  46. /**
  47. * 添加包邮信息
  48. * @param array $appointInfo
  49. * @param int $group
  50. * @param int $tempId
  51. * @return bool
  52. * @throws \Exception
  53. */
  54. public function saveFreeV1(array $appointInfo, int $group = 0, int $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. $placeList = [];
  63. mt_srand();
  64. foreach ($appointInfo as $item) {
  65. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  66. foreach ($item['city_ids'] as $cityId) {
  67. $placeList [] = [
  68. 'temp_id' => $tempId,
  69. 'city_id' => $cityId[count($cityId) - 1],
  70. 'value' => json_encode($cityId),
  71. 'number' => $item['number'] ?? 0,
  72. 'price' => $item['price'] ?? 0,
  73. 'group' => $group,
  74. 'uniqid' => $uniqid,
  75. ];
  76. }
  77. }
  78. if (count($placeList)) {
  79. return $res && $this->dao->saveAll($placeList);
  80. } else {
  81. return $res;
  82. }
  83. }
  84. /**
  85. * 添加包邮信息
  86. * @param array $appointInfo
  87. * @param int $group
  88. * @param int $tempId
  89. * @return bool
  90. * @throws \Exception
  91. */
  92. public function saveFree(array $appointInfo, int $group = 0, int $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. $placeList = [];
  101. mt_srand();
  102. foreach ($appointInfo as $item) {
  103. if (isset($item['place']) && is_array($item['place'])) {
  104. $uniqid = uniqid('adminapi') . rand(1000, 9999);
  105. foreach ($item['place'] 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. $placeList [] = [
  112. 'temp_id' => $tempId,
  113. 'province_id' => $value['city_id'] ?? 0,
  114. 'city_id' => $vv['city_id'] ?? 0,
  115. 'number' => $item['a_num'] ?? 0,
  116. 'price' => $item['a_price'] ?? 0,
  117. 'group' => $group,
  118. 'uniqid' => $uniqid,
  119. ];
  120. }
  121. }
  122. }
  123. }
  124. }
  125. if (count($placeList)) {
  126. return $res && $this->dao->saveAll($placeList);
  127. } else {
  128. return $res;
  129. }
  130. }
  131. /**
  132. * @param int $tempId
  133. * @return array
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function getFreeListV1(int $tempId)
  139. {
  140. $freeList = $this->dao->getShippingList(['temp_id' => $tempId]);
  141. return Arr::formatShipping($freeList);
  142. }
  143. /**
  144. * 获得指定包邮城市地址
  145. * @param int $tempId
  146. * @return array
  147. */
  148. public function getFreeList(int $tempId)
  149. {
  150. $freeIdList = $this->dao->getShippingGroupArray(['temp_id' => $tempId], 'uniqid', 'uniqid', '');
  151. $freeData = [];
  152. $infos = $this->dao->getShippingArray(['uniqid' => $freeIdList, 'temp_id' => $tempId], '*', 'uniqid');
  153. foreach ($freeIdList as $uniqid) {
  154. $info = $infos[$uniqid];
  155. $freeData[] = [
  156. 'place' => $this->getFreeTemp($uniqid, $info['province_id']),
  157. 'a_num' => $info['number'] ? floatval($info['number']) : 0,
  158. 'a_price' => $info['price'] ? floatval($info['price']) : 0,
  159. ];
  160. }
  161. foreach ($freeData as &$item) {
  162. $item['placeName'] = implode(';', array_column($item['place'], 'name'));
  163. }
  164. return $freeData;
  165. }
  166. /**
  167. * 获取包邮的省份
  168. * @param string $uniqid
  169. * @param int $provinceId
  170. * @return array
  171. */
  172. public function getFreeTemp(string $uniqid, int $provinceId)
  173. {
  174. /** @var ShippingTemplatesFreeCityServices $service */
  175. $service = app()->make(ShippingTemplatesFreeCityServices::class);
  176. $infoList = $service->getUniqidList(['uniqid' => $uniqid]);
  177. $childrenData = [];
  178. foreach ($infoList as $item) {
  179. $childrenData[] = [
  180. 'city_id' => $item['province_id'],
  181. 'name' => $item['name'] ?? '全国',
  182. 'children' => $this->getCityTemp($uniqid, $provinceId)
  183. ];
  184. }
  185. return $childrenData;
  186. }
  187. /**
  188. * 获取市区数据
  189. * @param string $uniqid
  190. * @param int $provinceId
  191. * @return array
  192. */
  193. public function getCityTemp(string $uniqid, int $provinceId)
  194. {
  195. /** @var ShippingTemplatesFreeCityServices $service */
  196. $service = app()->make(ShippingTemplatesFreeCityServices::class);
  197. $infoList = $service->getUniqidList(['uniqid' => $uniqid, 'province_id' => $provinceId], false);
  198. $childrenData = [];
  199. foreach ($infoList as $item) {
  200. $childrenData[] = [
  201. 'city_id' => $item['city_id'],
  202. 'name' => $item['name'] ?? '全国',
  203. ];
  204. }
  205. return $childrenData;
  206. }
  207. }