StoreAttrTemplateRepository.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\common\repositories\store;
  3. use app\common\dao\BaseDao;
  4. use app\common\dao\store\StoreAttrTemplateDao;
  5. use app\common\repositories\BaseRepository;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\exception\ValidateException;
  10. use think\Model;
  11. /**
  12. * Class StoreAttrTemplateRepository
  13. * @package app\common\repositories\store
  14. * @author zfy
  15. * @day 2020-05-06
  16. * @mixin StoreAttrTemplateDao
  17. */
  18. class StoreAttrTemplateRepository extends BaseRepository
  19. {
  20. /**
  21. * @var StoreAttrTemplateDao
  22. */
  23. protected $dao;
  24. /**
  25. * StoreAttrTemplateRepository constructor.
  26. * @param StoreAttrTemplateDao $dao
  27. */
  28. public function __construct(StoreAttrTemplateDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * @param $merId
  34. * @param array $where
  35. * @param $page
  36. * @param $limit
  37. * @return array
  38. * @throws DbException
  39. * @throws DataNotFoundException
  40. * @throws ModelNotFoundException
  41. * @author zfy
  42. * @day 2020-05-06
  43. */
  44. public function getList($merId, array $where, $page, $limit)
  45. {
  46. $query = $this->dao->search($merId, $where);
  47. $count = $query->count($this->dao->getPk());
  48. $list = $query->page($page, $limit)->select();
  49. return compact('count', 'list');
  50. }
  51. /**
  52. * @param array $data
  53. * @return array
  54. * @author zfy
  55. * @day 2020-05-06
  56. */
  57. protected function checkValue(array $data)
  58. {
  59. $arr = [];
  60. foreach ($data['template_value'] as $k => $value) {
  61. if (!is_array($value)) throw new ValidateException('规则有误');
  62. if (!($value['value'] ?? null)) throw new ValidateException('请输入规则名称');
  63. if (!($value['detail'] ?? null) || !count($value['detail'])) throw new ValidateException('请添加规则值');
  64. if(in_array($value['value'],$arr)) throw new ValidateException('规格重复');
  65. $arr[] = $value['value'];
  66. if (count($value['detail']) != count(array_unique($value['detail']))) throw new ValidateException('属性重复') ;
  67. $data['template_value'][$k] = [
  68. 'value' => $value['value'],
  69. 'detail' => $value['detail'],
  70. ];
  71. }
  72. return $data;
  73. }
  74. /**
  75. * @param array $data
  76. * @return BaseDao|Model
  77. * @author zfy
  78. * @day 2020-05-06
  79. */
  80. public function create(array $data)
  81. {
  82. $data = $this->checkValue($data);
  83. return $this->dao->create($data);
  84. }
  85. /**
  86. * @param int $id
  87. * @param array $data
  88. * @return int
  89. * @throws DbException
  90. * @author zfy
  91. * @day 2020-05-06
  92. */
  93. public function update(int $id, array $data)
  94. {
  95. $data = $this->checkValue($data);
  96. $data['template_value'] = json_encode($data['template_value']);
  97. return $this->dao->update($id, $data);
  98. }
  99. public function list(int $merId)
  100. {
  101. return $this->dao->getList($merId);
  102. }
  103. }