StoreTemplate.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\models\system;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\traits\ModelTrait;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. class StoreTemplate extends BaseModel
  9. {
  10. /**
  11. * 数据表主键
  12. * @var string
  13. */
  14. protected $pk = 'id';
  15. /**
  16. * 模型名称
  17. * @var string
  18. */
  19. protected $name = 'store_template';
  20. use ModelTrait;
  21. public static function vaildWhere()
  22. {
  23. return self::where('is_del', 0);
  24. }
  25. /**
  26. * @param null $where
  27. * @return array
  28. * @throws DataNotFoundException
  29. * @throws DbException
  30. * @throws ModelNotFoundException
  31. */
  32. public static function getList($where = null)
  33. {
  34. $data = ($data = self::setWhere($where)->select()) && count($data) ? $data->toArray() : [];
  35. $count = self::setWhere($where)->count();
  36. return ['count' => $count, 'list' => $data];
  37. }
  38. public static function setWhere($where)
  39. {
  40. $model = (new self)::vaildWhere();
  41. if ($where['name']) {
  42. $model = $model->where('name', 'like', '%' . $where['name'] . '%');
  43. }
  44. if ($where['use_version']) {
  45. $model = $model->where('use_version', $where['use_version']);
  46. }
  47. if ($where['is_ban']) {
  48. $model = $model->where('is_ban', $where['is_ban']);
  49. }
  50. return $model;
  51. }
  52. /**
  53. * 删除模板
  54. * @param $id
  55. * @return bool
  56. */
  57. public static function delTemplate($id)
  58. {
  59. try {
  60. if (!self::vaildWhere()->where(['id' => $id])->find()) {
  61. return self::setErrorInfo('模板未找到或已删除');
  62. }
  63. } catch (DbException $e) {
  64. return self::setErrorInfo($e->getMessage());
  65. }
  66. $res = self::where('id', $id)->update(['is_del' => 1]);
  67. if ($res) {
  68. return true;
  69. } else {
  70. return self::setErrorInfo('删除失败');
  71. }
  72. }
  73. public static function setTemplateBan($id, $ban)
  74. {
  75. $count = self::where('id', $id)->count();
  76. if (!$count) return self::setErrorInfo('参数错误');
  77. $count = self::where('id', $id)->where('is_ban', $ban)->count();
  78. if ($count) return true;
  79. self::beginTrans();
  80. $res = self::where('id', $id)->update(['is_ban' => $ban]);
  81. self::checkTrans($res);
  82. return $res;
  83. }
  84. }