GuaranteeTemplateRepository.php 4.5 KB

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