ShippingTemplatesServices.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. declare (strict_types=1);
  12. namespace app\services\product\shipping;
  13. use app\services\BaseServices;
  14. use app\dao\product\shipping\ShippingTemplatesDao;
  15. use app\services\product\product\StoreProductServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\CacheService;
  18. /**
  19. * 运费模板
  20. * Class ShippingTemplatesServices
  21. * @package app\services\product\shipping
  22. * @mixin ShippingTemplatesDao
  23. */
  24. class ShippingTemplatesServices extends BaseServices
  25. {
  26. /**
  27. * 计费类型
  28. * @var string[]
  29. */
  30. protected $group = [
  31. 1 => '按件数',
  32. 2 => '按重量',
  33. 3 => '按体积'
  34. ];
  35. /**
  36. * 构造方法
  37. * ShippingTemplatesServices constructor.
  38. * @param ShippingTemplatesDao $dao
  39. */
  40. public function __construct(ShippingTemplatesDao $dao)
  41. {
  42. $this->dao = $dao;
  43. }
  44. /**
  45. * 获取运费模板列表
  46. * @param array $where
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getShippingList(array $where)
  53. {
  54. [$page, $limit] = $this->getPageValue();
  55. $data = $this->dao->getShippingList($where, $page, $limit);
  56. if ($data) {
  57. $groupName = $this->group;
  58. foreach ($data as &$item) {
  59. $item['type'] = $groupName[$item['group']] ?? '';
  60. }
  61. }
  62. $count = $this->dao->count($where);
  63. return compact('data', 'count');
  64. }
  65. /**
  66. * @param array $where
  67. * @param $field
  68. * @param string|null $key
  69. * @param int $exprie
  70. * @return bool|mixed|null
  71. */
  72. public function getShippingColumnCache(array $where, $field, string $key = null, int $exprie = 60)
  73. {
  74. return CacheService::redisHandler('apiShipping')->remember(md5('Shipping_column' . json_encode($where)), function () use ($where, $field, $key) {
  75. return $this->dao->getShippingColumn($where, $field, $key);
  76. }, $exprie);
  77. }
  78. /**
  79. * 获取需要修改的运费模板
  80. * @param int $id
  81. * @return mixed
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function getShippingV1(int $id)
  87. {
  88. $templates = $this->dao->get($id);
  89. if (!$templates) {
  90. throw new AdminException('修改的模板不存在');
  91. }
  92. /** @var ShippingTemplatesFreeServices $freeServices */
  93. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  94. /** @var ShippingTemplatesRegionServices $regionServices */
  95. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  96. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  97. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  98. $data['appointList'] = $freeServices->getFreeListV1($id);
  99. $data['templateList'] = $regionServices->getRegionList($id);
  100. $data['noDeliveryList'] = $noDeliveryServices->getNoDeliveryList($id);
  101. if (!isset($data['templateList'][0]['region'])) {
  102. $data['templateList'][0]['region'] = ['city_id' => 0, 'name' => '默认全国'];
  103. }
  104. $data['formData'] = [
  105. 'name' => $templates->name,
  106. 'type' => $templates->getData('type'),
  107. 'appoint_check' => intval($templates->getData('appoint')),
  108. 'no_delivery_check' => intval($templates->getData('no_delivery')),
  109. 'sort' => intval($templates->getData('sort')),
  110. ];
  111. return $data;
  112. }
  113. /**
  114. * 获取需要修改的运费模板
  115. * @param int $id
  116. * @return mixed
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getShipping(int $id)
  122. {
  123. $templates = $this->dao->get($id);
  124. if (!$templates) {
  125. throw new AdminException('修改的模板不存在');
  126. }
  127. /** @var ShippingTemplatesFreeServices $freeServices */
  128. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  129. /** @var ShippingTemplatesRegionServices $regionServices */
  130. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  131. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  132. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  133. $data['appointList'] = $freeServices->getFreeListV1($id);
  134. $data['templateList'] = $regionServices->getRegionListV1($id);
  135. $data['noDeliveryList'] = $noDeliveryServices->getNoDeliveryListV1($id);
  136. if (!isset($data['templateList'][0]['city_ids'])) {
  137. $data['templateList'][0] = ['city_ids' => [[0]], 'regionName' => '默认全国'];
  138. }
  139. $data['formData'] = [
  140. 'name' => $templates->name,
  141. 'type' => $templates->getData('group'),
  142. 'appoint_check' => intval($templates->getData('appoint')),
  143. 'no_delivery_check' => intval($templates->getData('no_delivery')),
  144. 'sort' => intval($templates->getData('sort')),
  145. ];
  146. return $data;
  147. }
  148. /**
  149. * 保存或者修改运费模板
  150. * @param int $id
  151. * @param array $temp
  152. * @param array $data
  153. * @return mixed
  154. */
  155. public function save(int $id, array $temp, array $data)
  156. {
  157. /** @var ShippingTemplatesRegionServices $regionServices */
  158. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  159. $this->transaction(function () use ($regionServices, $data, $id, $temp) {
  160. if ($id) {
  161. $res = $this->dao->update($id, $temp);
  162. } else {
  163. $res = $this->dao->save($temp);
  164. }
  165. if (!$res) {
  166. throw new AdminException('保存失败');
  167. }
  168. if (!$id) $id = $res->id;
  169. //设置区域配送
  170. if (!$regionServices->saveRegionV1($data['region_info'], (int)$data['group'], (int)$id)) {
  171. throw new AdminException('指定区域邮费添加失败!');
  172. }
  173. //设置指定包邮
  174. if ($data['appoint']) {
  175. /** @var ShippingTemplatesFreeServices $freeServices */
  176. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  177. if (!$freeServices->saveFreeV1($data['appoint_info'], (int)$data['group'], (int)$id)) {
  178. throw new AdminException('指定包邮添加失败!');
  179. }
  180. }
  181. //设置不送达
  182. if ($data['no_delivery']) {
  183. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  184. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  185. if (!$noDeliveryServices->saveNoDeliveryV1($data['no_delivery_info'], (int)$id)) {
  186. throw new AdminException('指定不送达添加失败!');
  187. }
  188. }
  189. });
  190. return true;
  191. }
  192. /**
  193. * 删除运费模板
  194. * @param int $id
  195. */
  196. public function detete(int $id)
  197. {
  198. $this->dao->delete($id);
  199. /** @var ShippingTemplatesFreeServices $freeServices */
  200. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  201. /** @var ShippingTemplatesRegionServices $regionServices */
  202. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  203. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  204. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  205. /** @var StoreProductServices $storeProductServices */
  206. $storeProductServices = app()->make(StoreProductServices::class);
  207. $this->transaction(function () use ($id, $freeServices, $regionServices, $noDeliveryServices, $storeProductServices) {
  208. $freeServices->delete($id, 'temp_id');
  209. $regionServices->delete($id, 'temp_id');
  210. $noDeliveryServices->delete($id, 'temp_id');
  211. $storeProductServices->update(['temp_id' => $id], ['temp_id' => 1]);
  212. });
  213. }
  214. }