ShippingTemplateRepository.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\common\repositories\store\shipping;
  12. use app\common\repositories\BaseRepository;
  13. use app\common\dao\store\shipping\ShippingTemplateDao as dao;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use think\facade\Db;
  16. class ShippingTemplateRepository extends BaseRepository
  17. {
  18. /**
  19. * ShippingTemplateRepository constructor.
  20. * @param dao $dao
  21. */
  22. public function __construct(dao $dao)
  23. {
  24. $this->dao = $dao;
  25. }
  26. /**
  27. * @Author:Qinii
  28. * @Date: 2020/5/13
  29. * @param int $merId
  30. * @return mixed
  31. */
  32. public function getList(int $merId)
  33. {
  34. return $this->dao->getList($merId);
  35. }
  36. /**
  37. * @Author:Qinii
  38. * @Date: 2020/5/8
  39. * @param int $merId
  40. * @param $id
  41. * @return bool
  42. */
  43. public function merExists(int $merId,$id)
  44. {
  45. return $this->dao->merFieldExists($merId,$this->getPk(),$id);
  46. }
  47. public function merDefaultExists(int $merId,$id)
  48. {
  49. $where = ['mer_id' => $merId,'is_default' => 1, $this->dao->getPk() => $id];
  50. return $this->dao->getWhere($where) ? true : false;
  51. }
  52. public function getProductUse(int $merId ,int $id)
  53. {
  54. return app()->make(ProductRepository::class)->merTempExists($merId,$id);
  55. }
  56. /**
  57. * @param int $id
  58. * @return mixed
  59. * @author Qinii
  60. */
  61. public function getOne(int $id)
  62. {
  63. $with = ['free','region','undelives'];
  64. $result = $this->dao->getWhere([$this->dao->getPk() => $id],'*',$with);
  65. $result->append(['free.city_ids','region.city_ids','undelives.city_ids']);
  66. return $result;
  67. }
  68. /**
  69. * @Author:Qinii
  70. * @Date: 2020/5/8
  71. * @param int $merId
  72. * @param array $where
  73. * @param int $page
  74. * @param int $limit
  75. * @return array
  76. */
  77. public function search(int $merId,array $where, int $page, int $limit)
  78. {
  79. $query = $this->dao->search($merId, $where);
  80. $count = $query->count($this->dao->getPk());
  81. $list = $query->page($page, $limit)->select();
  82. return compact('count', 'list');
  83. }
  84. /**
  85. * @Author:Qinii
  86. * @Date: 2020/5/8
  87. * @param int $id
  88. * @param array $data
  89. */
  90. public function update(int $id,array $data)
  91. {
  92. Db::transaction(function()use ($id,$data) {
  93. $region = $data['region'];
  94. $free = $data['free'] ?? '';
  95. $undelives = $data['undelives']??'';
  96. unset($data['region'],$data['free'],$data['undelives'],$data['city_ids']);
  97. $this->dao->update($id, $data);
  98. (app()->make(ShippingTemplateRegionRepository::class))->batchRemove([], [$id]);
  99. (app()->make(ShippingTemplateFreeRepository::class))->batchRemove([], [$id]);
  100. (app()->make(ShippingTemplateUndeliveRepository::class))->batchRemove([], [$id]);
  101. if($data['appoint']) {
  102. $settlefree = $this->settleFree($free, $id);
  103. (app()->make(ShippingTemplateFreeRepository::class)->insertAll($settlefree));
  104. }
  105. $settleRegion = $this->settleRegion($region,$id);
  106. (app()->make(ShippingTemplateRegionRepository::class)->insertAll($settleRegion));
  107. if($data['undelivery']){
  108. $settleUndelives = $this->settleUndelives($undelives,$id);
  109. (app()->make(ShippingTemplateUndeliveRepository::class))->create($settleUndelives);
  110. }
  111. });
  112. }
  113. /**
  114. * @Author:Qinii
  115. * @Date: 2020/5/13
  116. * @param array $data
  117. */
  118. public function create(array $data)
  119. {
  120. Db::transaction(function()use ($data) {
  121. $region = $data['region'];
  122. $free = $data['free'] ?? '';
  123. $undelives = $data['undelives'] ?? '';
  124. unset($data['region'],$data['free'],$data['undelives'],$data['city_ids']);
  125. $temp = $this->dao->create($data);
  126. if($data['appoint']) {
  127. $settlefree = $this->settleFree($free, $temp['shipping_template_id']);
  128. (app()->make(ShippingTemplateFreeRepository::class)->insertAll($settlefree));
  129. }
  130. $settleRegion = $this->settleRegion($region, $temp['shipping_template_id']);
  131. (app()->make(ShippingTemplateRegionRepository::class)->insertAll($settleRegion));
  132. if($data['undelivery']){
  133. $settleUndelives = $this->settleUndelives($undelives,$temp['shipping_template_id']);
  134. (app()->make(ShippingTemplateUndeliveRepository::class))->create($settleUndelives);
  135. }
  136. });
  137. }
  138. /**
  139. * @param $data
  140. * @param $id
  141. * @return array
  142. * @author Qinii
  143. */
  144. public function settleFree($data,$id)
  145. {
  146. foreach ($data as $v){
  147. $city = '/'.implode('/',$v['city_id']).'/';
  148. $free[] = [
  149. 'temp_id' => $id,
  150. 'city_id' => $city,
  151. 'number' => $v['number'],
  152. 'price' => $v['price']
  153. ];
  154. }
  155. return $free;
  156. }
  157. /**
  158. * @param $data
  159. * @param $id
  160. * @return array
  161. * @author Qinii
  162. */
  163. public function settleUndelives($data,$id)
  164. {
  165. return ['temp_id' => $id, 'city_id' => $data['city_id']];
  166. }
  167. /**
  168. * @Author:Qinii
  169. * @Date: 2020/5/13
  170. * @param $data
  171. * @param $id
  172. * @return array
  173. */
  174. public function settleRegion($data,$id)
  175. {
  176. $result = [];
  177. foreach ($data as $k => $v){
  178. $result[] = [
  179. 'city_id' => ($k > 0 ) ? '/'.implode('/',$v['city_id']).'/' : 0,
  180. 'temp_id' => $id,
  181. 'first' => $v['first'],
  182. 'first_price'=> $v['first_price'],
  183. 'continue' => $v['continue'],
  184. 'continue_price'=> $v['continue_price'],
  185. ];
  186. }
  187. return $result;
  188. }
  189. /**
  190. * @param int $merId
  191. * @author Qinii
  192. */
  193. public function createDefault(int $merId)
  194. {
  195. $data = [
  196. "name" => "默认模板",
  197. "type" => 1,
  198. "appoint" => 0,
  199. "undelivery" => 0,
  200. 'mer_id' => $merId,
  201. "region" => [[
  202. "first" => 0,
  203. "first_price" => 0,
  204. "continue" => 0,
  205. "continue_price" => 0,
  206. "city_id" => 0
  207. ]]
  208. ];
  209. return $this->create($data);
  210. }
  211. }