| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace addons\BusinessCard\common\models;
- use common\enums\StatusEnum;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_business_card_position".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $parent_id 所属部门ID
- * @property string $name 名称
- * @property int $sort 排序,大的靠前
- * @property int $status 状态[1启用 0禁用 -1删除]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class BusinessCardPosition extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_business_card_position';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'parent_id', 'sort', 'status', 'created_at', 'updated_at', 'is_default_position'], 'integer'],
- [['name'], 'string', 'max' => 48],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'parent_id' => 'Parent ID',
- 'name' => 'Name',
- 'sort' => 'Sort',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'is_default_position' => 'Is Default Position',
- ];
- }
- public function getDepartmentPosition() {
- return $this->hasMany(self::class, ['parent_id' => 'id'])->where(['status' => StatusEnum::ENABLED]);
- }
- /**
- * 获取部门
- *
- * @author hpei
- * @return array
- */
- public static function getDepartment($mall_id) {
- return self::find()
- ->select(['name', 'id'])
- ->where(['mall_id' => $mall_id, 'parent_id' => 0])
- ->andWhere(['<>','status', StatusEnum::DELETE])
- ->asArray()->all();
- }
- /**
- * 获取职位
- *
- * @author hpei
- * @return array
- */
- public static function getPosition($mall_id, $select, $index = false) {
- $model = self::find()
- ->select($select)
- ->where(['mall_id' => $mall_id])
- ->andWhere(['<>','status', StatusEnum::DELETE])
- ->andWhere(['<>','parent_id', 0]);
- if ($index !== false) {
- $list = $model->indexBy($index)->column();
- } else {
- $list = $model->asArray()->all();
- }
- return $list;
- }
- /**
- * 保存数据
- *
- * @author hpei
- * @return bool|Exception
- */
- public static function setData($params) {
- try {
- $position = self::findOne(['mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED,'name'=>$params['name'],'parent_id' =>$params['parent_id']]);
- if (empty($params['id'])) {
- $model = new self();
- $model->loadDefaultValues();
- if ($position) {
- if($params['parent_id']>0){
- self::$static_error = '该职位已经存在';
- }else{
- self::$static_error = '该部门已经存在';
- }
- return false;
- }
- } else {
- $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if ($position && $model->name != $params['name']) {
- if($params['parent_id']>0){
- self::$static_error = '该职位已经存在';
- }else{
- self::$static_error = '该部门已经存在';
- }
- return false;
- }
- if (empty($model)) throw new \Exception('数据不存在或者已删除');
- }
- if(!$params['sort']) $params['sort'] = 99;
- if (!empty($params['is_default_position']) && $params['parent_id'] > 0) {
- self::updateAll(
- [
- 'is_default_position' => StatusEnum::DISABLED
- ],
- [
- 'mall_id' => $params['mall_id'],
- 'is_default_position' => StatusEnum::ENABLED
- ]
- );
- }
- if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- return $model->id;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 新部门创建接口
- * @param array $params
- * @return false
- */
- public static function newSaveData(array $params): bool
- {
- try {
- $departmentInfo = self::find()->where(['name' => $params['name'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED])->one();
- if($params['id']) { //编辑
- if($departmentInfo && $departmentInfo->id != $params['id']) throw new Exception('部门已存在!');
- $model = self::findOne(['id' => $params['id']]);
- } else {
- if ($departmentInfo) throw new Exception("部门已存在!");
- $model = new self();
- }
- $model->name = $params['name'];
- $model->mall_id = $params['mall_id'];
- $model->sort = $params['sort'];
- $res = $model->save();
- if ($res === false) throw new Exception("操作失败");
- foreach ($params['position'] as $item) { //进行职位插入
- $positionModel = new self();
- if ($item['id']) $positionModel = self::findOne(['id' => $item['id']]);
- $positionModel->name = $item['name'];
- $positionModel->mall_id = $params['mall_id'];
- $positionModel->parent_id = $model->id;
- $positionModel->sort = $params['sort'];
- $positionModel->save();
- }
- return true;
- } catch (Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 检测
- *
- * @author hpei
- * @return bool
- */
- public static function checkPosition($params) {
- $model = self::find();
- $model->where(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if ($params['isParent'] === false) {
- $model->andWhere(['>', 'parent_id', 0]);
- } else {
- $model->andWhere(['=', 'parent_id', 0]);
- }
- return $model->count() > 0;
- }
- /**
- * 获取部门和对应的职位
- *
- * @author hepi
- * @return array
- */
- public static function departmentPosition($mall_id) {
- $params = [
- 'where' => [['=', 'mall_id', $mall_id], ['<>', 'status' , StatusEnum::DELETE], ['=', 'parent_id', 0]],
- 'select' => ['id', 'parent_id', 'name'],
- 'order' => 'sort desc',
- 'with' => ['departmentPosition'],
- ];
- $model = self::find();
- self::paramPack($params, $model);
- return $model->asArray()->all();
- }
- //删除
- public static function del($mall_id,$id){
- $res = BusinessCardUser::find()->where(['department_id'=>$id,'mall_id'=>$mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]])
- ->one();
- if(!empty($res)) {
- throw new \Exception('该部门存在人员');
- }
- BusinessCardPosition::updateAll(['status'=>StatusEnum::DELETE],['mall_id'=>$mall_id,'id'=>$id]);
- BusinessCardPosition::updateAll(['status'=>StatusEnum::DELETE],['mall_id'=>$mall_id,'parent_id'=>$id]);
- }
- /**
- * 切换是否职位
- *
- * @param array $params
- *
- * @return void
- */
- public static function changeIsDefaultPosition(array $params)
- {
- if ($params['is_default_position']) {
- self::updateAll(
- [
- 'is_default_position' => StatusEnum::DISABLED
- ],
- [
- 'mall_id' => $params['mall_id'],
- 'is_default_position' => StatusEnum::ENABLED
- ]
- );
- }
- self::updateAll(
- [
- 'is_default_position' => $params['is_default_position']
- ],
- [
- 'mall_id' => $params['mall_id'],
- 'id' => $params['id']
- ]
- );
- }
- }
|