This commit is contained in:
AdisakKanthawilang 2024-11-01 13:55:37 +07:00
parent e0f78a89e9
commit c082ae8891

View file

@ -1679,7 +1679,7 @@ export class PositionController extends Controller {
} }
masterId = [...new Set(masterId)]; masterId = [...new Set(masterId)];
} }
const revisionCondition = { const revisionCondition = {
orgRevisionId: body.revisionId, orgRevisionId: body.revisionId,
}; };
@ -1695,171 +1695,157 @@ export class PositionController extends Controller {
child4: null, child4: null,
privilege: "OWNER", privilege: "OWNER",
}; };
const conditions = [ const conditions = [
{
...checkChildConditions,
...typeCondition,
...revisionCondition,
...(masterId.length > 0 ? { id: In(masterId) } : {}),
},
];
let [posMaster, total] = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.leftJoinAndSelect("posMaster.orgRoot", "orgRoot")
.leftJoinAndSelect("posMaster.orgChild1", "orgChild1")
.leftJoinAndSelect("posMaster.orgChild2", "orgChild2")
.leftJoinAndSelect("posMaster.orgChild3", "orgChild3")
.leftJoinAndSelect("posMaster.orgChild4", "orgChild4")
.leftJoinAndSelect("posMaster.current_holder", "current_holder")
.leftJoinAndSelect("posMaster.next_holder", "next_holder")
.leftJoinAndSelect("posMaster.orgRevision", "orgRevision")
.where(conditions)
.andWhere(
_data.root != undefined && _data.root != null
? _data.root[0] != null
? `posMaster.orgRootId IN (:...root)`
: `posMaster.orgRootId is null`
: "1=1",
{ {
root: _data.root, ...checkChildConditions,
...typeCondition,
...revisionCondition,
...(body.keyword &&
(masterId.length > 0
? { id: In(masterId) }
: { posMasterNo: Like(`%${body.keyword}%`) })),
}, },
) ];
.andWhere(
_data.child1 != undefined && _data.child1 != null let [posMaster, total] = await AppDataSource.getRepository(PosMaster)
? _data.child1[0] != null .createQueryBuilder("posMaster")
? `posMaster.orgChild1Id IN (:...child1)` .leftJoinAndSelect("posMaster.orgRoot", "orgRoot")
: `posMaster.orgChild1Id is null` .leftJoinAndSelect("posMaster.orgChild1", "orgChild1")
: "1=1", .leftJoinAndSelect("posMaster.orgChild2", "orgChild2")
{ .leftJoinAndSelect("posMaster.orgChild3", "orgChild3")
child1: _data.child1, .leftJoinAndSelect("posMaster.orgChild4", "orgChild4")
}, .leftJoinAndSelect("posMaster.current_holder", "current_holder")
) .leftJoinAndSelect("posMaster.next_holder", "next_holder")
.andWhere( .leftJoinAndSelect("posMaster.orgRevision", "orgRevision")
_data.child2 != undefined && _data.child2 != null .where(conditions)
? _data.child2[0] != null .andWhere(
? `posMaster.orgChild2Id IN (:...child2)` _data.root != undefined && _data.root != null
: `posMaster.orgChild2Id is null` ? _data.root[0] != null
: "1=1", ? `posMaster.orgRootId IN (:...root)`
{ : `posMaster.orgRootId is null`
child2: _data.child2, : "1=1",
}, {
) root: _data.root,
.andWhere( },
_data.child3 != undefined && _data.child3 != null )
? _data.child3[0] != null .andWhere(
? `posMaster.orgChild3Id IN (:...child3)` _data.child1 != undefined && _data.child1 != null
: `posMaster.orgChild3Id is null` ? _data.child1[0] != null
: "1=1", ? `posMaster.orgChild1Id IN (:...child1)`
{ : `posMaster.orgChild1Id is null`
child3: _data.child3, : "1=1",
}, {
) child1: _data.child1,
.andWhere( },
_data.child4 != undefined && _data.child4 != null )
? _data.child4[0] != null .andWhere(
? `posMaster.orgChild4Id IN (:...child4)` _data.child2 != undefined && _data.child2 != null
: `posMaster.orgChild4Id is null` ? _data.child2[0] != null
: "1=1", ? `posMaster.orgChild2Id IN (:...child2)`
{ : `posMaster.orgChild2Id is null`
child4: _data.child4, : "1=1",
}, {
) child2: _data.child2,
.andWhere( },
new Brackets((qb) => { )
qb.orWhere( .andWhere(
new Brackets((qb) => { _data.child3 != undefined && _data.child3 != null
qb.andWhere( ? _data.child3[0] != null
body.keyword != null && body.keyword != "" ? `posMaster.orgChild3Id IN (:...child3)`
? body.isAll == false : `posMaster.orgChild3Id is null`
? searchShortName : "1=1",
: `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` {
: "1=1", child3: _data.child3,
) },
.andWhere(checkChildConditions) )
.andWhere(typeCondition) .andWhere(
.andWhere(revisionCondition); _data.child4 != undefined && _data.child4 != null
}), ? _data.child4[0] != null
) ? `posMaster.orgChild4Id IN (:...child4)`
.orWhere( : `posMaster.orgChild4Id is null`
new Brackets((qb) => { : "1=1",
qb.andWhere( {
body.keyword != null && body.keyword != "" child4: _data.child4,
? `CONCAT(current_holder.prefix, current_holder.firstName," ",current_holder.lastName) like '%${body.keyword}%'` },
: "1=1", )
{ .orWhere(
keyword: `%${body.keyword}%`, new Brackets((qb) => {
}, qb.andWhere(
) body.keyword != null && body.keyword != ""
.andWhere(checkChildConditions) ? body.isAll == false
.andWhere(typeCondition) ? searchShortName
.andWhere(revisionCondition); : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'`
}), : "1=1",
) )
.orWhere( .andWhere(checkChildConditions)
new Brackets((qb) => { .andWhere(typeCondition)
qb.andWhere( .andWhere(revisionCondition);
body.keyword != null && body.keyword != "" }),
? `CASE WHEN orgRevision.orgRevisionIsDraft = true THEN CONCAT(next_holder.prefix, next_holder.firstName,' ', next_holder.lastName) ELSE CONCAT(current_holder.prefix, current_holder.firstName,' ' , current_holder.lastName) END LIKE '%${body.keyword}%'` )
: "1=1", .orWhere(
{ new Brackets((qb) => {
keyword: `%${body.keyword}%`, qb.andWhere(
}, body.keyword != null && body.keyword != ""
) ? `CONCAT(current_holder.prefix, current_holder.firstName," ",current_holder.lastName) like '%${body.keyword}%'`
.andWhere(checkChildConditions) : "1=1",
.andWhere(typeCondition) {
.andWhere(revisionCondition); keyword: `%${body.keyword}%`,
}), },
) )
.orWhere( .andWhere(checkChildConditions)
new Brackets((qb) => { .andWhere(typeCondition)
qb.andWhere( .andWhere(revisionCondition);
body.keyword != null && body.keyword != "" }),
? body.isAll == false )
? searchShortName .orWhere(
: `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` new Brackets((qb) => {
: "1=1", qb.andWhere(
) body.keyword != null && body.keyword != ""
.andWhere(checkChildConditions) ? `CASE WHEN orgRevision.orgRevisionIsDraft = true THEN CONCAT(next_holder.prefix, next_holder.firstName,' ', next_holder.lastName) ELSE CONCAT(current_holder.prefix, current_holder.firstName,' ' , current_holder.lastName) END LIKE '%${body.keyword}%'`
.andWhere(typeCondition) : "1=1",
.andWhere(revisionCondition); {
}), keyword: `%${body.keyword}%`,
); },
}), )
) .andWhere(checkChildConditions)
.orderBy("posMaster.posMasterOrder", "ASC") .andWhere(typeCondition)
.skip((body.page - 1) * body.pageSize) .andWhere(revisionCondition);
.take(body.pageSize) }),
.getManyAndCount(); )
.orderBy("posMaster.posMasterOrder", "ASC")
//แก้ค้นหา .skip((body.page - 1) * body.pageSize)
let _position: any[] = []; .take(body.pageSize)
let x: any = null; .getManyAndCount();
let y: any = null;
if (body.keyword != null && body.keyword != "") { //แก้ค้นหา
const position = await this.positionRepository.find({ let _position: any[] = [];
relations: ["posType", "posLevel", "posExecutive"], let x: any = null;
where: { posMasterId: In(posMaster.map((x) => x.id)) }, let y: any = null;
order: { createdAt: "ASC" }, if (body.keyword != null && body.keyword != ""){
}); const position = await this.positionRepository.find({
for (let data of position) { relations: ["posType", "posLevel", "posExecutive"],
x = data.posMasterId; where: { posMasterId: In(posMaster.map(x => x.id))},
if (y != x) { order: { createdAt: "ASC" }
if ( });
data.positionName.includes(body.keyword) || for(let data of position){
data.posType.posTypeName.includes(body.keyword) || x = data.posMasterId
data.posLevel.posLevelName.includes(body.keyword) if(y != x){
) { if(
_position.push(data); data.positionName.includes(body.keyword) ||
data.posType.posTypeName.includes(body.keyword) ||
data.posLevel.posLevelName.includes(body.keyword)
){
_position.push(data);
}
} }
y = x;
} }
y = x;
} }
}
if(_position.length > 0){
if (_position.length > 0) { posMaster = posMaster.filter((x) => _position.some(y => y.posMasterId === x.id));
posMaster = posMaster.filter((x) => _position.some((y) => y.posMasterId === x.id)); }
}
const formattedData = await Promise.all( const formattedData = await Promise.all(
posMaster.map(async (posMaster) => { posMaster.map(async (posMaster) => {