@@ -6,7 +6,7 @@ import { Equal, FindOperator, ILike, In, Repository, SelectQueryBuilder } from '
66import { DepartmentDto } from './dto/department.dto' ;
77import { ICurrentUserType } from '@src/decorators' ;
88import { HttpStatusCode } from 'axios' ;
9- import { DepartmentPageVo , DepartmentVo } from './vo/department.vo' ;
9+ import { DepartmentPageVo , DepartmentVo , SimplenessDepartmentVo } from './vo/department.vo' ;
1010import { TenantEntity } from '../tenant/entities/tenant.entity' ;
1111import { QueryDepartmentDto } from './dto/department.query' ;
1212import { PageEnum , StatusEnum } from '@src/enums' ;
@@ -182,6 +182,7 @@ export class DepartmentService {
182182 . offset ( ( pageNumber - 1 ) * pageSize )
183183 . limit ( pageSize )
184184 . getRawMany ( ) ;
185+ console . log ( data , '????????????????' ) ;
185186 return {
186187 data,
187188 total,
@@ -190,6 +191,26 @@ export class DepartmentService {
190191 } ;
191192 }
192193
194+ /**
195+ * @Author : 水痕
196+ * @Date : 2023-10-18 20:27:28
197+ * @LastEditors : 水痕
198+ * @Description : 获取当前商户下的部门
199+ * @param {ICurrentUserType } currentUser
200+ * @return {* }
201+ */
202+ async getDepartmentListApi ( currentUser : ICurrentUserType ) : Promise < SimplenessDepartmentVo [ ] > {
203+ const { id, tenantId, accountType } = currentUser ;
204+ const query = new Map < string , FindOperator < string > > ( ) ;
205+ query . set ( 'tenantId' , Equal ( tenantId + '' ) ) ;
206+ if ( accountType != AccountTypeEnum . SUPER_ACCOUNT ) {
207+ query . set ( 'parentId' , Equal ( id + '' ) ) ;
208+ }
209+ return await this . departmentRepository . find ( {
210+ where : mapToObj ( query ) ,
211+ select : [ 'id' , 'title' , 'parentId' ] ,
212+ } ) ;
213+ }
193214 /**
194215 * @Author : 水痕
195216 * @Date : 2023-10-18 15:21:30
@@ -206,30 +227,39 @@ export class DepartmentService {
206227 * @Author : 水痕
207228 * @Date : 2023-10-18 15:52:37
208229 * @LastEditors : 水痕
209- * @Description : 根据id列表修改状态
230+ * @Description : 批量删除部门
210231 * @param {number } idList
211232 * @return {* }
212233 */
213234 async batchDeleteDepartmentByIdListApi ( idList : number [ ] ) : Promise < string > {
214- const departmentEntityList : Pick < DepartmentEntity , 'status' > [ ] =
215- await this . departmentRepository . find ( { where : { id : In ( idList ) } , select : [ 'status' ] } ) ;
216- const statusList = departmentEntityList . map ( ( item ) => item . status ) ;
217- if ( [ ...new Set ( statusList ) ] . length > 1 ) {
218- throw new HttpException ( '当前部门多个状态,不能批量操作' , HttpStatusCode . Ok ) ;
235+ const departmentEntityList : Pick < DepartmentEntity , 'parentId' > [ ] =
236+ await this . departmentRepository . find ( {
237+ where : { parentId : In ( idList ) } ,
238+ select : [ 'parentId' , 'id' ] ,
239+ } ) ;
240+ if ( departmentEntityList . length > 0 ) {
241+ throw new HttpException ( '当前部门有子部门,不能直接删除' , HttpStatusCode . Ok ) ;
219242 }
220- const status = statusList [ 0 ] == StatusEnum . FORBIDDEN ? StatusEnum . NORMAL : StatusEnum . FORBIDDEN ;
221- const { affected } = await this . departmentRepository . update ( { id : In ( idList ) } , { status } ) ;
243+ const departmentEntityList1 : Pick < DepartmentEntity , 'parentId' > [ ] =
244+ await this . departmentRepository . find ( {
245+ where : { id : In ( idList ) } ,
246+ select : [ 'parentId' ] ,
247+ } ) ;
248+ if ( departmentEntityList1 . map ( ( item ) => item . parentId ) . includes ( - 1 ) ) {
249+ throw new HttpException ( '当前部门有子部门,不能直接删除' , HttpStatusCode . Ok ) ;
250+ }
251+ const { affected } = await this . departmentRepository . softDelete ( { id : In ( idList ) } ) ;
222252 if ( affected ) {
223- return '修改成功 ' ;
253+ return '删除成功 ' ;
224254 } else {
225- return '修改失败 ' ;
255+ return '删除失败 ' ;
226256 }
227257 }
228258 /**
229259 * @Author : 水痕
230260 * @Date : 2023-10-18 15:58:14
231261 * @LastEditors : 水痕
232- * @Description : 批量删除部门
262+ * @Description :根据id列表修改状态
233263 * @param {number } idList
234264 * @return {* }
235265 */
@@ -240,11 +270,12 @@ export class DepartmentService {
240270 if ( [ ...new Set ( statusList ) ] . length > 1 ) {
241271 throw new HttpException ( '当前部门多个状态,不能批量操作' , HttpStatusCode . Ok ) ;
242272 }
243- const { affected } = await this . departmentRepository . softDelete ( { id : In ( idList ) } ) ;
273+ const status = statusList [ 0 ] == StatusEnum . FORBIDDEN ? StatusEnum . NORMAL : StatusEnum . FORBIDDEN ;
274+ const { affected } = await this . departmentRepository . update ( { id : In ( idList ) } , { status } ) ;
244275 if ( affected ) {
245- return '删除成功 ' ;
276+ return '修改成功 ' ;
246277 } else {
247- return '删除失败 ' ;
278+ return '修改失败 ' ;
248279 }
249280 }
250281 /**
0 commit comments