StoreAttrTemplateDao.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\dao\store;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\StoreAttrTemplate;
  15. use think\db\BaseQuery;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\Model;
  20. /**
  21. * Class StoreAttrTemplateDao
  22. * @package app\common\dao\store
  23. * @author xaboy
  24. * @day 2020-05-06
  25. */
  26. class StoreAttrTemplateDao extends BaseDao
  27. {
  28. /**
  29. * @return BaseModel
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. protected function getModel(): string
  34. {
  35. return StoreAttrTemplate::class;
  36. }
  37. /**
  38. * @param $merId
  39. * @param array $where
  40. * @return BaseQuery
  41. * @author xaboy
  42. * @day 2020-05-06
  43. */
  44. public function search($merId, array $where = [])
  45. {
  46. return StoreAttrTemplate::getDB()->when(isset($where['keyword']),function($query) use($where){
  47. $query->whereLike('template_name',"%{$where['keyword']}%");
  48. })->where('mer_id', $merId)->order('attr_template_id DESC');
  49. }
  50. /**
  51. * @param int $merId
  52. * @param int $id
  53. * @param null $except
  54. * @return bool
  55. * @author xaboy
  56. * @day 2020-04-15
  57. */
  58. public function merExists(int $merId, int $id, $except = null)
  59. {
  60. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  61. }
  62. /**
  63. * @param int $merId
  64. * @param $field
  65. * @param $value
  66. * @param null $except
  67. * @return bool
  68. * @author xaboy
  69. * @day 2020-04-15
  70. */
  71. public function merFieldExists(int $merId, $field, $value, $except = null)
  72. {
  73. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  74. $query->where($field, '<>', $except);
  75. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  76. }
  77. /**
  78. * @param int $id
  79. * @param int $merId
  80. * @return array|Model|null
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. * @author xaboy
  85. * @day 2020-04-15
  86. */
  87. public function get( $id, $merId = 0)
  88. {
  89. return ($this->getModel())::getDB()->where('mer_id', $merId)->find($id);
  90. }
  91. /**
  92. * @param int $id
  93. * @param int $merId
  94. * @return int
  95. * @throws DbException
  96. * @author xaboy
  97. * @day 2020-04-15
  98. */
  99. public function delete($id, $merId = 0)
  100. {
  101. $query = ($this->getModel())::getDB()->where('mer_id', $merId);
  102. if (is_array($id)) {
  103. $query->where($this->getPk(), 'in',$id);
  104. } else {
  105. $query->where($this->getPk(), $id);
  106. }
  107. return $query->delete();
  108. }
  109. public function getList($merId)
  110. {
  111. return ($this->getModel())::getDB()->where('mer_id',$merId)->field('attr_template_id,template_name,template_value')->select();
  112. }
  113. }