GuaranteeTemplateRepository.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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;
  12. use app\common\dao\store\GuaranteeTemplateDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use FormBuilder\Factory\Elm;
  16. use think\exception\ValidateException;
  17. use think\facade\Db;
  18. use think\facade\Route;
  19. class GuaranteeTemplateRepository extends BaseRepository
  20. {
  21. /**
  22. * @var GuaranteeTemplateDao
  23. */
  24. protected $dao;
  25. /**
  26. * GuaranteeRepository constructor.
  27. * @param GuaranteeTemplateDao $dao
  28. */
  29. public function __construct(GuaranteeTemplateDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * TODO 平台列表
  35. * @param $where
  36. * @param $page
  37. * @param $limit
  38. * @return array
  39. * @author Qinii
  40. * @day 5/17/21
  41. */
  42. public function getList($where,$page, $limit)
  43. {
  44. $query = $this->dao->getSearch($where)->with(['template_value.value'])->order('sort DESC');
  45. $count = $query->count();
  46. $list = $query->page($page,$limit)->select();
  47. return compact('count','list');
  48. }
  49. /**
  50. * TODO 创建
  51. * @param array $data
  52. * @author Qinii
  53. * @day 5/17/21
  54. */
  55. public function create(array $data)
  56. {
  57. Db::transaction(function() use($data){
  58. $template = [
  59. 'template_name' => $data['template_name'],
  60. 'mer_id' => $data['mer_id'],
  61. 'status' => $data['status'],
  62. 'sort' => $data['sort']
  63. ];
  64. $guaranteeData = $this->dao->create($template);
  65. $make = app()->make(GuaranteeRepository::class);
  66. foreach ($data['template_value'] as $datum){
  67. $where = [ 'status' => 1,'is_del' => 0,'guarantee_id' => $datum];
  68. $ret = $make->getWhere($where);
  69. if(!$ret) throw new ValidateException('ID['.$datum.']不存在');
  70. $value[] = [
  71. 'guarantee_id' => $datum ,
  72. 'guarantee_template_id' => $guaranteeData->guarantee_template_id,
  73. 'mer_id' => $data['mer_id']
  74. ];
  75. }
  76. app()->make(GuaranteeValueRepository::class)->insertAll($value);
  77. });
  78. }
  79. /**
  80. * TODO 编辑
  81. * @param int $id
  82. * @param array $data
  83. * @author Qinii
  84. * @day 5/17/21
  85. */
  86. public function edit(int $id,array $data)
  87. {
  88. Db::transaction(function() use($id,$data){
  89. $template = [
  90. 'template_name' => $data['template_name'],
  91. 'status' => $data['status'],
  92. 'sort' => $data['sort']
  93. ];
  94. $make = app()->make(GuaranteeRepository::class);
  95. $makeValue = app()->make(GuaranteeValueRepository::class);
  96. foreach ($data['template_value'] as $datum){
  97. $where = [ 'status' => 1,'is_del' => 0,'guarantee_id' => $datum];
  98. $ret = $make->getWhere($where);
  99. if(!$ret) throw new ValidateException('ID['.$datum.']不存在');
  100. $value[] = [
  101. 'guarantee_id' => $datum ,
  102. 'guarantee_template_id' => $id,
  103. 'mer_id' => $data['mer_id']
  104. ];
  105. }
  106. $this->dao->update($id,$template);
  107. $makeValue->clear($id);
  108. $makeValue->insertAll($value);
  109. });
  110. }
  111. /**
  112. * TODO 详情
  113. * @param int $id
  114. * @param int $merId
  115. * @return array|\think\Model|null
  116. * @author Qinii
  117. * @day 5/17/21
  118. */
  119. public function detail(int $id,int $merId)
  120. {
  121. $where = [
  122. 'mer_id' => $merId,
  123. 'guarantee_template_id' => $id,
  124. ];
  125. $ret = $this->dao->getSearch($where)->find();
  126. $ret->append(['template_value']);
  127. if(!$ret) throw new ValidateException('数据不存在');
  128. return $ret;
  129. }
  130. public function delete($id)
  131. {
  132. $productId = app()->make(ProductRepository::class)->getSearch(['guarantee_template_id' => $id])->column('product_id');
  133. if($productId) throw new ValidateException('有商品正在使用此模板,商品ID:'.implode(',',$productId));
  134. Db::transaction(function() use($id){
  135. $this->dao->delete($id);
  136. app()->make(GuaranteeValueRepository::class)->clear($id);
  137. });
  138. }
  139. public function list($merId)
  140. {
  141. $where = [
  142. 'status' => 1,
  143. 'is_del' => 0,
  144. 'mer_id' => $merId
  145. ];
  146. return $this->dao->getSearch($where)->order('sort DESC')->select()->toArray();
  147. }
  148. }