Industry.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\models\merchant;
  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 Industry extends BaseModel
  9. {
  10. /**
  11. * 数据表主键
  12. * @var string
  13. */
  14. protected $pk = 'id';
  15. /**
  16. * 模型名称
  17. * @var string
  18. */
  19. protected $name = 'industry';
  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)->order('sort desc,id desc')->select()) && count($data) ? $data->toArray() : [];
  35. $count = self::setWhere($where)->where('id|pid', isset($where['pid']) && $where['pid'] != '' ? $where['pid'] : 0)->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['pid'] != '') {
  45. $model = $model->where('id|pid', $where['pid']);
  46. }
  47. if ($where['is_show'] != '') {
  48. $model = $model->where('is_show', $where['is_show']);
  49. }
  50. return $model;
  51. }
  52. /**
  53. * 删除行业
  54. * @param $id
  55. * @return bool
  56. */
  57. public static function delIndustry($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. $count = self::where('pid', $id)->count();
  67. if ($count)
  68. return self::setErrorInfo('请先删除下级子行业');
  69. else {
  70. $res = self::where('id', $id)->update(['is_del' => 1, 'update' => time()]);
  71. if ($res) {
  72. return true;
  73. } else {
  74. return self::setErrorInfo('删除失败');
  75. }
  76. }
  77. }
  78. public static function setIndustryShow($id, $show)
  79. {
  80. $count = self::where('id', $id)->count();
  81. if (!$count) return self::setErrorInfo('参数错误');
  82. $count = self::where('id', $id)->where('is_show', $show)->count();
  83. if ($count) return true;
  84. $pid = self::where('id', $id)->value('pid');
  85. self::beginTrans();
  86. $res1 = true;
  87. $res2 = self::where('id', $id)->update(['is_show' => $show, 'update' => time()]);
  88. if (!$pid) {//一级分类隐藏
  89. $count = self::where('pid', $id)->count();
  90. if ($count) {
  91. $count = self::where('pid', $id)->where('is_show', $show)->count();
  92. $countWhole = self::where('pid', $id)->count();
  93. if (!$count || $countWhole > $count) {
  94. $res1 = self::where('pid', $id)->update(['is_show' => $show]);
  95. }
  96. }
  97. }
  98. $res = $res1 && $res2;
  99. self::checkTrans($res);
  100. return $res;
  101. }
  102. /**
  103. * 分级排序列表
  104. * @param null $model
  105. * @param int $type
  106. * @return array
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. * @throws DbException
  110. */
  111. public static function getTierList($model = null, $type = 0)
  112. {
  113. if ($model === null) $model = new self();
  114. if (!$type) return sort_list_tier($model->order('sort desc,id desc')->where('pid', 0)->where('is_del', 0)->select()->toArray());
  115. return sort_list_tier($model->order('sort desc,id desc')->where('is_del', 0)->select()->toArray());
  116. }
  117. }