StoreAttrTemplateDao.php 2.7 KB

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