StoreAttrTemplateDao.php 2.6 KB

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