123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace app\models\merchant;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class Industry extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'industry';
- use ModelTrait;
- public static function vaildWhere()
- {
- return self::where('is_del', 0);
- }
- /**
- * @param null $where
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function getList($where = null)
- {
- $data = ($data = self::setWhere($where)->order('sort desc,id desc')->select()) && count($data) ? $data->toArray() : [];
- $count = self::setWhere($where)->where('id|pid', isset($where['pid']) && $where['pid'] != '' ? $where['pid'] : 0)->count();
- return ['count' => $count, 'list' => $data];
- }
- public static function setWhere($where)
- {
- $model = (new self)::vaildWhere();
- if ($where['name']) {
- $model = $model->where('name', 'like', '%' . $where['name'] . '%');
- }
- if ($where['pid'] != '') {
- $model = $model->where('id|pid', $where['pid']);
- }
- if ($where['is_show'] != '') {
- $model = $model->where('is_show', $where['is_show']);
- }
- return $model;
- }
- /**
- * 删除行业
- * @param $id
- * @return bool
- */
- public static function delIndustry($id)
- {
- try {
- if (!self::vaildWhere()->where(['id' => $id])->find()) {
- return self::setErrorInfo('行业未找到或已删除');
- }
- } catch (DbException $e) {
- return self::setErrorInfo($e->getMessage());
- }
- $count = self::where('pid', $id)->count();
- if ($count)
- return self::setErrorInfo('请先删除下级子行业');
- else {
- $res = self::where('id', $id)->update(['is_del' => 1, 'update' => time()]);
- if ($res) {
- return true;
- } else {
- return self::setErrorInfo('删除失败');
- }
- }
- }
- public static function setIndustryShow($id, $show)
- {
- $count = self::where('id', $id)->count();
- if (!$count) return self::setErrorInfo('参数错误');
- $count = self::where('id', $id)->where('is_show', $show)->count();
- if ($count) return true;
- $pid = self::where('id', $id)->value('pid');
- self::beginTrans();
- $res1 = true;
- $res2 = self::where('id', $id)->update(['is_show' => $show, 'update' => time()]);
- if (!$pid) {//一级分类隐藏
- $count = self::where('pid', $id)->count();
- if ($count) {
- $count = self::where('pid', $id)->where('is_show', $show)->count();
- $countWhole = self::where('pid', $id)->count();
- if (!$count || $countWhole > $count) {
- $res1 = self::where('pid', $id)->update(['is_show' => $show]);
- }
- }
- }
- $res = $res1 && $res2;
- self::checkTrans($res);
- return $res;
- }
- /**
- * 分级排序列表
- * @param null $model
- * @param int $type
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws DbException
- */
- public static function getTierList($model = null, $type = 0)
- {
- if ($model === null) $model = new self();
- if (!$type) return sort_list_tier($model->order('sort desc,id desc')->where('pid', 0)->where('is_del', 0)->select()->toArray());
- return sort_list_tier($model->order('sort desc,id desc')->where('is_del', 0)->select()->toArray());
- }
- }
|