BusinessCardPosition.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace addons\BusinessCard\common\models;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_business_card_position".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $parent_id 所属部门ID
  12. * @property string $name 名称
  13. * @property int $sort 排序,大的靠前
  14. * @property int $status 状态[1启用 0禁用 -1删除]
  15. * @property int $created_at 创建时间
  16. * @property int $updated_at 更新时间
  17. */
  18. class BusinessCardPosition extends \common\models\base\BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return 'qimall_addons_business_card_position';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'parent_id', 'sort', 'status', 'created_at', 'updated_at', 'is_default_position'], 'integer'],
  34. [['name'], 'string', 'max' => 48],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => 'Mall ID',
  45. 'parent_id' => 'Parent ID',
  46. 'name' => 'Name',
  47. 'sort' => 'Sort',
  48. 'status' => 'Status',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. 'is_default_position' => 'Is Default Position',
  52. ];
  53. }
  54. public function getDepartmentPosition() {
  55. return $this->hasMany(self::class, ['parent_id' => 'id'])->where(['status' => StatusEnum::ENABLED]);
  56. }
  57. /**
  58. * 获取部门
  59. *
  60. * @author hpei
  61. * @return array
  62. */
  63. public static function getDepartment($mall_id) {
  64. return self::find()
  65. ->select(['name', 'id'])
  66. ->where(['mall_id' => $mall_id, 'parent_id' => 0])
  67. ->andWhere(['<>','status', StatusEnum::DELETE])
  68. ->asArray()->all();
  69. }
  70. /**
  71. * 获取职位
  72. *
  73. * @author hpei
  74. * @return array
  75. */
  76. public static function getPosition($mall_id, $select, $index = false) {
  77. $model = self::find()
  78. ->select($select)
  79. ->where(['mall_id' => $mall_id])
  80. ->andWhere(['<>','status', StatusEnum::DELETE])
  81. ->andWhere(['<>','parent_id', 0]);
  82. if ($index !== false) {
  83. $list = $model->indexBy($index)->column();
  84. } else {
  85. $list = $model->asArray()->all();
  86. }
  87. return $list;
  88. }
  89. /**
  90. * 保存数据
  91. *
  92. * @author hpei
  93. * @return bool|Exception
  94. */
  95. public static function setData($params) {
  96. try {
  97. $position = self::findOne(['mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED,'name'=>$params['name'],'parent_id' =>$params['parent_id']]);
  98. if (empty($params['id'])) {
  99. $model = new self();
  100. $model->loadDefaultValues();
  101. if ($position) {
  102. if($params['parent_id']>0){
  103. self::$static_error = '该职位已经存在';
  104. }else{
  105. self::$static_error = '该部门已经存在';
  106. }
  107. return false;
  108. }
  109. } else {
  110. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  111. if ($position && $model->name != $params['name']) {
  112. if($params['parent_id']>0){
  113. self::$static_error = '该职位已经存在';
  114. }else{
  115. self::$static_error = '该部门已经存在';
  116. }
  117. return false;
  118. }
  119. if (empty($model)) throw new \Exception('数据不存在或者已删除');
  120. }
  121. if(!$params['sort']) $params['sort'] = 99;
  122. if (!empty($params['is_default_position']) && $params['parent_id'] > 0) {
  123. self::updateAll(
  124. [
  125. 'is_default_position' => StatusEnum::DISABLED
  126. ],
  127. [
  128. 'mall_id' => $params['mall_id'],
  129. 'is_default_position' => StatusEnum::ENABLED
  130. ]
  131. );
  132. }
  133. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  134. return $model->id;
  135. } catch (\Exception $e) {
  136. self::$static_error = $e->getMessage();
  137. return false;
  138. }
  139. }
  140. /**
  141. * 新部门创建接口
  142. * @param array $params
  143. * @return false
  144. */
  145. public static function newSaveData(array $params): bool
  146. {
  147. try {
  148. $departmentInfo = self::find()->where(['name' => $params['name'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED])->one();
  149. if($params['id']) { //编辑
  150. if($departmentInfo && $departmentInfo->id != $params['id']) throw new Exception('部门已存在!');
  151. $model = self::findOne(['id' => $params['id']]);
  152. } else {
  153. if ($departmentInfo) throw new Exception("部门已存在!");
  154. $model = new self();
  155. }
  156. $model->name = $params['name'];
  157. $model->mall_id = $params['mall_id'];
  158. $model->sort = $params['sort'];
  159. $res = $model->save();
  160. if ($res === false) throw new Exception("操作失败");
  161. foreach ($params['position'] as $item) { //进行职位插入
  162. $positionModel = new self();
  163. if ($item['id']) $positionModel = self::findOne(['id' => $item['id']]);
  164. $positionModel->name = $item['name'];
  165. $positionModel->mall_id = $params['mall_id'];
  166. $positionModel->parent_id = $model->id;
  167. $positionModel->sort = $params['sort'];
  168. $positionModel->save();
  169. }
  170. return true;
  171. } catch (Exception $e) {
  172. self::$static_error = $e->getMessage();
  173. return false;
  174. }
  175. }
  176. /**
  177. * 检测
  178. *
  179. * @author hpei
  180. * @return bool
  181. */
  182. public static function checkPosition($params) {
  183. $model = self::find();
  184. $model->where(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  185. if ($params['isParent'] === false) {
  186. $model->andWhere(['>', 'parent_id', 0]);
  187. } else {
  188. $model->andWhere(['=', 'parent_id', 0]);
  189. }
  190. return $model->count() > 0;
  191. }
  192. /**
  193. * 获取部门和对应的职位
  194. *
  195. * @author hepi
  196. * @return array
  197. */
  198. public static function departmentPosition($mall_id) {
  199. $params = [
  200. 'where' => [['=', 'mall_id', $mall_id], ['<>', 'status' , StatusEnum::DELETE], ['=', 'parent_id', 0]],
  201. 'select' => ['id', 'parent_id', 'name'],
  202. 'order' => 'sort desc',
  203. 'with' => ['departmentPosition'],
  204. ];
  205. $model = self::find();
  206. self::paramPack($params, $model);
  207. return $model->asArray()->all();
  208. }
  209. //删除
  210. public static function del($mall_id,$id){
  211. $res = BusinessCardUser::find()->where(['department_id'=>$id,'mall_id'=>$mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]])
  212. ->one();
  213. if(!empty($res)) {
  214. throw new \Exception('该部门存在人员');
  215. }
  216. BusinessCardPosition::updateAll(['status'=>StatusEnum::DELETE],['mall_id'=>$mall_id,'id'=>$id]);
  217. BusinessCardPosition::updateAll(['status'=>StatusEnum::DELETE],['mall_id'=>$mall_id,'parent_id'=>$id]);
  218. }
  219. /**
  220. * 切换是否职位
  221. *
  222. * @param array $params
  223. *
  224. * @return void
  225. */
  226. public static function changeIsDefaultPosition(array $params)
  227. {
  228. if ($params['is_default_position']) {
  229. self::updateAll(
  230. [
  231. 'is_default_position' => StatusEnum::DISABLED
  232. ],
  233. [
  234. 'mall_id' => $params['mall_id'],
  235. 'is_default_position' => StatusEnum::ENABLED
  236. ]
  237. );
  238. }
  239. self::updateAll(
  240. [
  241. 'is_default_position' => $params['is_default_position']
  242. ],
  243. [
  244. 'mall_id' => $params['mall_id'],
  245. 'id' => $params['id']
  246. ]
  247. );
  248. }
  249. }