finish api load

main
nocode 1 year ago
parent 0326d60d87
commit 000512bd28
  1. 51
      Modules/Agents/Services/AgentService.php
  2. 10
      app/Models/AgentUser.php

@ -102,7 +102,10 @@ class AgentService
$queryAgent->whereBetween('created_at', [$startDate->toDateString(), $endDate->toDateString()]); $queryAgent->whereBetween('created_at', [$startDate->toDateString(), $endDate->toDateString()]);
} }
if (!empty($input['keyword'])) { if (!empty($input['keyword'])) {
$queryAgent->where('name', 'LIKE', '%' . $input['keyword'] . '%'); $queryAgent->where('name', 'LIKE', '%' . $input['keyword'] . '%')
->orWhere('phone', 'LIKE', '%' . $input['keyword'] . '%')
->orWhere('email', 'LIKE', '%' . $input['keyword'] . '%');
;
} }
$listCustomer = $queryAgent->orderBy('name', 'asc')->get(); $listCustomer = $queryAgent->orderBy('name', 'asc')->get();
if (!empty($listCustomer)) { if (!empty($listCustomer)) {
@ -150,10 +153,54 @@ class AgentService
if (!empty($input['grant'])) { if (!empty($input['grant'])) {
$myAgentDown->onlyGrant($input['grant']); $myAgentDown->onlyGrant($input['grant']);
} }
$listAgentGet = $myAgentDown->get();
if (!empty($listAgentGet)) {
$listAgentById = [];
foreach ($listAgentGet as $kAgent) {
// get parent id by path
$kAgent->grant_parent_id = $this->getParentAgent($kAgent->parent_path);
$listAgentById[$kAgent->agent_id] = $kAgent;
}
}
if (!empty($listAgentById)) {
foreach ($listAgentById as $vByAgent) {
dd($myAgentDown->get()); if (!empty($listAgentById[$vByAgent->grant_parent_id])) {
$agentParent = $listAgentById[$vByAgent->grant_parent_id];
$upGrant = $agentParent->code ?? null;
}
$data[] = [
'id' => $vByAgent->id ?? '',
'name' => $vByAgent->agent_name ?? '',
'agents_code' => $vByAgent->code ?? '',
'created_date' => $vByAgent->created_at ?? '',
'phone' => $vByAgent->agent_phone ?? '',
'email' => $vByAgent->email ?? '',
'grant' => $vByAgent->grant ?? '',
'up_grant' => $upGrant ?? ''
];
}
}
return ['status' => true, 'data' => $data, 'msg' => $this->getMessageReturn('success')]; return ['status' => true, 'data' => $data, 'msg' => $this->getMessageReturn('success')];
} }
/**
* Summary of getChildAgent
* @param mixed $input
* @return array
*/
public function getParentAgent($parentPath)
{
$parts = explode('-', $parentPath);
// remove element null
$parts = array_filter($parts, fn($part) => $part !== '');
if (count($parts) == 1) {
return min($parts);
} else {
return $parts[count($parts) - 1] ?? null;
}
}
/** /**
* Summary of myAgentList * Summary of myAgentList
* @param mixed $input * @param mixed $input

@ -32,7 +32,13 @@ class AgentUser extends Model
public function scopeJoinAgent($query) public function scopeJoinAgent($query)
{ {
return $query->join('se_agent', 'se_agent.id', '=', 'se_agent_user.agent_id') return $query->join('se_agent', 'se_agent.id', '=', 'se_agent_user.agent_id')
->select('se_agent_user.*', 'se_agent.name as agent_name', 'se_agent.parent_path', 'se_agent.grant', 'se_agent.type'); ->select('se_agent_user.*',
'se_agent.name as agent_name',
'se_agent.parent_path',
'se_agent.grant',
'se_agent.phone as agent_phone',
// 'se_agent.email as agent_email',
'se_agent.type');
} }
/** /**
* Summary of scopeBetweenDates * Summary of scopeBetweenDates
@ -53,7 +59,7 @@ class AgentUser extends Model
*/ */
public function scopeByAgentRoot($query, $agentRootId) public function scopeByAgentRoot($query, $agentRootId)
{ {
return $query->where($this->table . '.agent_root_id', $agentRootId)->where($this->table . '.agent_id', "<>",$agentRootId); return $query->where($this->table . '.agent_root_id', $agentRootId);
} }
/** /**
* Summary of scopeSearchAgentName * Summary of scopeSearchAgentName

Loading…
Cancel
Save