ShippingTemplatesServices.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. declare (strict_types=1);
  12. namespace app\services\shipping;
  13. use app\services\BaseServices;
  14. use app\dao\shipping\ShippingTemplatesDao;
  15. use crmeb\exceptions\AdminException;
  16. /**
  17. * 运费模板
  18. * Class ShippingTemplatesServices
  19. * @package app\services\shipping
  20. * @method getSelectList() 获取下拉选择列表
  21. * @method get($id) 获取一条数据
  22. * @method getShippingColumn(array $where, string $field, string $key) 获取运费模板指定条件下的数据
  23. */
  24. class ShippingTemplatesServices extends BaseServices
  25. {
  26. /**
  27. * 构造方法
  28. * ShippingTemplatesServices constructor.
  29. * @param ShippingTemplatesDao $dao
  30. */
  31. public function __construct(ShippingTemplatesDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 获取运费模板列表
  37. * @param array $where
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getShippingList(array $where)
  44. {
  45. [$page, $limit] = $this->getPageValue();
  46. $data = $this->dao->getShippingList($where, $page, $limit);
  47. $count = $this->dao->count($where);
  48. return compact('data', 'count');
  49. }
  50. /**
  51. * 获取需要修改的运费模板
  52. * @param int $id
  53. * @return mixed
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function getShipping(int $id)
  59. {
  60. $templates = $this->dao->get($id);
  61. if (!$templates) {
  62. throw new AdminException(400592);
  63. }
  64. /** @var ShippingTemplatesFreeServices $freeServices */
  65. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  66. /** @var ShippingTemplatesRegionServices $regionServices */
  67. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  68. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  69. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  70. $data['appointList'] = $freeServices->getFreeList($id);
  71. $data['templateList'] = $regionServices->getRegionList($id);
  72. $data['noDeliveryList'] = $noDeliveryServices->getNoDeliveryList($id);
  73. if (!isset($data['templateList'][0]['region'])) {
  74. $data['templateList'][0]['region'] = ['city_id' => 0, 'name' => '默认全国'];
  75. }
  76. $data['formData'] = [
  77. 'name' => $templates->name,
  78. 'type' => $templates->getData('type'),
  79. 'appoint_check' => intval($templates->getData('appoint')),
  80. 'no_delivery_check' => intval($templates->getData('no_delivery')),
  81. 'sort' => intval($templates->getData('sort')),
  82. ];
  83. return $data;
  84. }
  85. /**
  86. * 保存或者修改运费模板
  87. * @param int $id
  88. * @param array $temp
  89. * @param array $data
  90. * @return mixed
  91. */
  92. public function save(int $id, array $temp, array $data)
  93. {
  94. if ($id) {
  95. $res = $this->dao->update($id, $temp);
  96. } else {
  97. $id = $this->dao->insertGetId($temp);
  98. $res = true;
  99. }
  100. /** @var ShippingTemplatesRegionServices $regionServices */
  101. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  102. return $this->transaction(function () use ($regionServices, $data, $id, $res) {
  103. //设置区域配送
  104. $res = $res && $regionServices->saveRegion($data['region_info'], (int)$data['type'], (int)$id);
  105. if (!$res) {
  106. throw new AdminException(400593);
  107. }
  108. //设置指定包邮
  109. if ($data['appoint']) {
  110. /** @var ShippingTemplatesFreeServices $freeServices */
  111. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  112. $res = $res && $freeServices->saveFree($data['appoint_info'], (int)$data['type'], (int)$id);
  113. }
  114. //设置不送达
  115. if ($data['no_delivery']) {
  116. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  117. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  118. $res = $res && $noDeliveryServices->saveNoDelivery($data['no_delivery_info'], (int)$id);
  119. }
  120. if ($res) {
  121. return true;
  122. } else {
  123. throw new AdminException(100006);
  124. }
  125. });
  126. }
  127. /**
  128. * 删除运费模板
  129. * @param int $id
  130. */
  131. public function detete(int $id)
  132. {
  133. $this->dao->delete($id);
  134. /** @var ShippingTemplatesFreeServices $freeServices */
  135. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  136. /** @var ShippingTemplatesRegionServices $regionServices */
  137. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  138. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  139. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  140. $freeServices->delete($id, 'temp_id');
  141. $regionServices->delete($id, 'temp_id');
  142. $noDeliveryServices->delete($id, 'temp_id');
  143. }
  144. }