StoreAttrTemplateRepository.php 3.6 KB

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