BxDolAIQuery.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolAIQuery extends BxDolDb
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. static public function getModelObject($iId)
  16. {
  17. $oDb = BxDolDb::getInstance();
  18. $aModel = $oDb->getRow("SELECT * FROM `sys_agents_models` WHERE `id` = :id", ['id' => $iId]);
  19. if(!$aModel || !is_array($aModel))
  20. return false;
  21. return $aModel;
  22. }
  23. static public function getProviderObject($iId)
  24. {
  25. $oDb = BxDolDb::getInstance();
  26. $aProvider = $oDb->getRow("SELECT * FROM `sys_agents_providers` WHERE `id` = :id", ['id' => $iId]);
  27. if(!$aProvider || !is_array($aProvider))
  28. return false;
  29. // get type
  30. $aProviderType = $oDb->getRow("SELECT * FROM `sys_agents_provider_types` WHERE `id` = :id", ['id' => $aProvider['type_id']]);
  31. if(!$aProviderType || !is_array($aProviderType))
  32. return false;
  33. $aProvider['type'] = $aProviderType;
  34. // get options
  35. $sQuery = "SELECT
  36. `tpo`.`name` AS `name`,
  37. `tpv`.`value` AS `value`
  38. FROM `sys_agents_provider_options` AS `tpo`
  39. LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tpo`.`id` = `tpv`.`option_id` AND `tpv`.`provider_id` = :provider_id
  40. WHERE 1 ORDER BY `tpo`.`order`";
  41. $aProvider['options'] = $oDb->getAllWithKey($sQuery, 'name', [
  42. 'provider_id' => $iId
  43. ]);
  44. return $aProvider;
  45. }
  46. static public function getProviderIdByName($sName)
  47. {
  48. return (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `sys_agents_providers` WHERE `name`=:name LIMIT 1", [
  49. 'name' => $sName
  50. ]);
  51. }
  52. public function getModelsBy($aParams = [])
  53. {
  54. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  55. $sWhereClause = "";
  56. switch($aParams['sample']) {
  57. case 'id':
  58. $aMethod['name'] = 'getRow';
  59. $aMethod['params'][1] = [
  60. 'id' => $aParams['id']
  61. ];
  62. $sWhereClause .= " AND `id`=:id";
  63. break;
  64. case 'all_pairs':
  65. $aMethod['name'] = 'getPairs';
  66. $aMethod['params'][1] = 'id';
  67. $aMethod['params'][2] = 'title';
  68. if(isset($aParams['for_asst'])) {
  69. $aMethod['params'][3] = [
  70. 'for_asst' => $aParams['for_asst']
  71. ];
  72. $sWhereClause = " AND `for_asst`=:for_asst";
  73. }
  74. if(isset($aParams['active'])) {
  75. $aMethod['params'][3] = [
  76. 'active' => $aParams['active']
  77. ];
  78. $sWhereClause = " AND `active`=:active";
  79. }
  80. break;
  81. }
  82. $aMethod['params'][0] = "SELECT *
  83. FROM `sys_agents_models`
  84. WHERE 1" . $sWhereClause;
  85. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  86. }
  87. public function getAutomatorsBy($aParams = [])
  88. {
  89. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  90. $sSelectClause = "`taa`.*";
  91. $sJoinClause = $sWhereClause = "";
  92. switch($aParams['sample']) {
  93. case 'id':
  94. $aMethod['name'] = 'getRow';
  95. $aMethod['params'][1] = [
  96. 'id' => $aParams['id']
  97. ];
  98. $sWhereClause .= " AND `taa`.`id`=:id";
  99. break;
  100. case 'id_full':
  101. $aMethod['name'] = 'getRow';
  102. $aMethod['params'][1] = [
  103. 'id' => $aParams['id']
  104. ];
  105. $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
  106. $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
  107. $sWhereClause .= " AND `taa`.`id`=:id";
  108. break;
  109. case 'events':
  110. $aMethod['params'][1] = [
  111. 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
  112. 'alert_unit' => $aParams['alert_unit'],
  113. 'alert_action' => $aParams['alert_action']
  114. ];
  115. $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
  116. if(isset($aParams['active'])) {
  117. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  118. $sWhereClause .= " AND `taa`.`active`=:active";
  119. }
  120. break;
  121. case 'schedulers':
  122. $aMethod['params'][1] = [
  123. 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
  124. ];
  125. $sWhereClause .= " AND `taa`.`type`=:type";
  126. if(isset($aParams['active'])) {
  127. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  128. $sWhereClause .= " AND `taa`.`active`=:active";
  129. }
  130. break;
  131. case 'webhooks':
  132. $aMethod['params'][1] = [
  133. 'type' => BX_DOL_AI_AUTOMATOR_WEBHOOK,
  134. ];
  135. $sWhereClause .= " AND `taa`.`type`=:type";
  136. if(isset($aParams['provider_id'])) {
  137. $aMethod['params'][1]['provider_id'] = (int)$aParams['provider_id'];
  138. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  139. $sWhereClause .= " AND `tap`.`provider_id`=:provider_id";
  140. }
  141. if(isset($aParams['active'])) {
  142. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  143. $sWhereClause .= " AND `taa`.`active`=:active";
  144. }
  145. break;
  146. case 'providers_by_id_pairs':
  147. $aMethod['name'] = 'getPairs';
  148. $aMethod['params'][1] = 'id';
  149. $aMethod['params'][2] = 'provider_id';
  150. $aMethod['params'][3] = [
  151. 'id' => $aParams['id']
  152. ];
  153. $sSelectClause = "`taap`.`id`, `taap`.`provider_id`";
  154. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `taap` ON `taa`.`id`=`taap`.`automator_id`";
  155. $sWhereClause = " AND `taa`.`id`=:id";
  156. break;
  157. case 'helpers_by_id_pairs':
  158. $aMethod['name'] = 'getPairs';
  159. $aMethod['params'][1] = 'id';
  160. $aMethod['params'][2] = 'helper_id';
  161. $aMethod['params'][3] = [
  162. 'id' => $aParams['id']
  163. ];
  164. $sSelectClause = "`taah`.`id`, `taah`.`helper_id`";
  165. $sJoinClause = "INNER JOIN `sys_agents_automators_helpers` AS `taah` ON `taa`.`id`=`taah`.`automator_id`";
  166. $sWhereClause = " AND `taa`.`id`=:id";
  167. break;
  168. case 'assistants_by_id_pairs':
  169. $aMethod['name'] = 'getPairs';
  170. $aMethod['params'][1] = 'id';
  171. $aMethod['params'][2] = 'assistant_id';
  172. $aMethod['params'][3] = [
  173. 'id' => $aParams['id']
  174. ];
  175. $sSelectClause = "`taaa`.`id`, `taaa`.`assistant_id`";
  176. $sJoinClause = "INNER JOIN `sys_agents_automators_assistants` AS `taaa` ON `taa`.`id`=`taaa`.`automator_id`";
  177. $sWhereClause = " AND `taa`.`id`=:id";
  178. break;
  179. }
  180. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  181. FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
  182. WHERE 1" . $sWhereClause;
  183. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  184. }
  185. public function updateAutomators($aSetClause, $aWhereClause)
  186. {
  187. if(empty($aSetClause) || empty($aWhereClause))
  188. return false;
  189. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  190. }
  191. public function insertAutomatorProvider($aParamsSet)
  192. {
  193. if(empty($aParamsSet))
  194. return false;
  195. return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  196. }
  197. public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
  198. {
  199. if(empty($aParamsSet) || empty($aParamsWhere))
  200. return false;
  201. return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  202. }
  203. public function deleteAutomatorProviders($aParamsWhere)
  204. {
  205. if(empty($aParamsWhere))
  206. return false;
  207. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  208. }
  209. public function deleteAutomatorProvidersById($mixedId)
  210. {
  211. if(!is_array($mixedId))
  212. $mixedId = [$mixedId];
  213. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  214. }
  215. public function insertAutomatorHelper($aParamsSet)
  216. {
  217. if(empty($aParamsSet))
  218. return false;
  219. return (int)$this->query("INSERT INTO `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  220. }
  221. public function updateAutomatorHelper($aParamsSet, $aParamsWhere)
  222. {
  223. if(empty($aParamsSet) || empty($aParamsWhere))
  224. return false;
  225. return $this->query("UPDATE `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  226. }
  227. public function deleteAutomatorHelpers($aParamsWhere)
  228. {
  229. if(empty($aParamsWhere))
  230. return false;
  231. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  232. }
  233. public function deleteAutomatorHelpersById($mixedId)
  234. {
  235. if(!is_array($mixedId))
  236. $mixedId = [$mixedId];
  237. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  238. }
  239. public function insertAutomatorAssistant($aParamsSet)
  240. {
  241. if(empty($aParamsSet))
  242. return false;
  243. return (int)$this->query("INSERT INTO `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  244. }
  245. public function updateAutomatorAssistant($aParamsSet, $aParamsWhere)
  246. {
  247. if(empty($aParamsSet) || empty($aParamsWhere))
  248. return false;
  249. return $this->query("UPDATE `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  250. }
  251. public function deleteAutomatorAssistants($aParamsWhere)
  252. {
  253. if(empty($aParamsWhere))
  254. return false;
  255. return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  256. }
  257. public function deleteAutomatorAssistantsById($mixedId)
  258. {
  259. if(!is_array($mixedId))
  260. $mixedId = [$mixedId];
  261. return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  262. }
  263. public function getProviderTypesBy($aParams = [])
  264. {
  265. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  266. $sWhereClause = "";
  267. switch($aParams['sample']) {
  268. case 'id':
  269. $aMethod['name'] = 'getRow';
  270. $aMethod['params'][1] = [
  271. 'id' => $aParams['id']
  272. ];
  273. $sWhereClause .= " AND `id`=:id";
  274. break;
  275. case 'name':
  276. $aMethod['name'] = 'getRow';
  277. $aMethod['params'][1] = [
  278. 'name' => $aParams['name']
  279. ];
  280. $sWhereClause .= " AND `name`=:name";
  281. break;
  282. case 'all_pairs':
  283. $aMethod['name'] = 'getPairs';
  284. $aMethod['params'][1] = 'id';
  285. $aMethod['params'][2] = 'title';
  286. if(isset($aParams['active'])) {
  287. $aMethod['params'][3] = [
  288. 'active' => $aParams['active']
  289. ];
  290. $sWhereClause = " AND `active`=:active";
  291. }
  292. break;
  293. }
  294. $aMethod['params'][0] = "SELECT *
  295. FROM `sys_agents_provider_types`
  296. WHERE 1" . $sWhereClause;
  297. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  298. }
  299. public function getProviderOptionsBy($aParams = [])
  300. {
  301. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  302. $sWhereClause = "";
  303. switch($aParams['sample']) {
  304. case 'id':
  305. $aMethod['name'] = 'getRow';
  306. $aMethod['params'][1] = [
  307. 'id' => $aParams['id']
  308. ];
  309. $sWhereClause .= " AND `id`=:id";
  310. break;
  311. case 'provider_type_id':
  312. $aMethod['params'][1] = [
  313. 'provider_type_id' => $aParams['provider_type_id']
  314. ];
  315. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  316. break;
  317. }
  318. $aMethod['params'][0] = "SELECT *
  319. FROM `sys_agents_provider_options`
  320. WHERE 1" . $sWhereClause;
  321. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  322. }
  323. public function getProvidersBy($aParams = [])
  324. {
  325. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  326. $sSelectClause = "`tp`.*";
  327. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  328. switch($aParams['sample']) {
  329. case 'id':
  330. $aMethod['name'] = 'getRow';
  331. $aMethod['params'][1] = [
  332. 'id' => $aParams['id']
  333. ];
  334. $sWhereClause = " AND `tp`.`id`=:id";
  335. break;
  336. case 'ids':
  337. $sSelectClause .= ", `tpt`.`name` AS `type_name`";
  338. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  339. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  340. break;
  341. case 'options_by_id':
  342. $aMethod['params'][1] = [
  343. 'id' => $aParams['id']
  344. ];
  345. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  346. $sJoinClause = "LEFT JOIN `sys_agents_provider_options` AS `tpo` ON `tp`.`type_id`=`tpo`.`provider_type_id` LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tp`.`id`=`tpv`.`provider_id` AND `tpo`.`id`=`tpv`.`option_id`";
  347. $sWhereClause = " AND `tp`.`id`=:id";
  348. break;
  349. case 'all_pairs':
  350. $aMethod['name'] = 'getPairs';
  351. $aMethod['params'][1] = 'id';
  352. $aMethod['params'][2] = 'name';
  353. if(isset($aParams['active'])) {
  354. $aMethod['params'][3] = [
  355. 'active' => $aParams['active']
  356. ];
  357. $sWhereClause = " AND `tp`.`active`=:active";
  358. }
  359. break;
  360. }
  361. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  362. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  363. $aMethod['params'][0] = "SELECT
  364. " . $sSelectClause . "
  365. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  366. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  367. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  368. }
  369. public function insertProviderValue($aParamsSet)
  370. {
  371. if(empty($aParamsSet) || !is_array($aParamsSet) || !isset($aParamsSet['value']))
  372. return false;
  373. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `value`=:value", [
  374. 'value' => $aParamsSet['value']
  375. ]) > 0 ? (int)$this->lastId() : false;
  376. }
  377. public function updateProviderValue($aParamsSet, $aParamsWhere)
  378. {
  379. if(empty($aParamsSet))
  380. return false;
  381. $sWhereClause = "1";
  382. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  383. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  384. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  385. }
  386. public function deleteProviderValues($aParamsWhere)
  387. {
  388. if(empty($aParamsWhere))
  389. return false;
  390. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  391. }
  392. public function getHelpersBy($aParams = [])
  393. {
  394. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  395. $sSelectClause = "`th`.*";
  396. $sJoinClause = $sWhereClause = "";
  397. switch($aParams['sample']) {
  398. case 'id':
  399. $aMethod['name'] = 'getRow';
  400. $aMethod['params'][1] = [
  401. 'id' => $aParams['id']
  402. ];
  403. $sWhereClause .= " AND `th`.`id`=:id";
  404. break;
  405. case 'name':
  406. $aMethod['name'] = 'getRow';
  407. $aMethod['params'][1] = [
  408. 'name' => $aParams['name']
  409. ];
  410. $sWhereClause .= " AND `th`.`name`=:name";
  411. break;
  412. case 'ids':
  413. $sWhereClause = " AND `th`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  414. break;
  415. case 'all_pairs':
  416. $aMethod['name'] = 'getPairs';
  417. $aMethod['params'][1] = 'id';
  418. $aMethod['params'][2] = 'name';
  419. if(isset($aParams['active'])) {
  420. $aMethod['params'][3] = [
  421. 'active' => $aParams['active']
  422. ];
  423. $sWhereClause = " AND `th`.`active`=:active";
  424. }
  425. break;
  426. }
  427. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  428. FROM `sys_agents_helpers` AS `th` " . $sJoinClause . "
  429. WHERE 1" . $sWhereClause;
  430. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  431. }
  432. public function getAssistantsBy($aParams = [])
  433. {
  434. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  435. $sSelectClause = "`ta`.*";
  436. $sJoinClause = $sWhereClause = "";
  437. switch($aParams['sample']) {
  438. case 'id':
  439. $aMethod['name'] = 'getRow';
  440. $aMethod['params'][1] = [
  441. 'id' => $aParams['id']
  442. ];
  443. $sWhereClause .= " AND `ta`.`id`=:id";
  444. break;
  445. case 'name':
  446. $aMethod['name'] = 'getRow';
  447. $aMethod['params'][1] = [
  448. 'name' => $aParams['name']
  449. ];
  450. $sWhereClause .= " AND `ta`.`name`=:name";
  451. break;
  452. case 'ids':
  453. $sWhereClause = " AND `ta`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  454. break;
  455. case 'all_pairs':
  456. $aMethod['name'] = 'getPairs';
  457. $aMethod['params'][1] = 'id';
  458. $aMethod['params'][2] = 'name';
  459. if(isset($aParams['active'])) {
  460. $aMethod['params'][3] = [
  461. 'active' => $aParams['active']
  462. ];
  463. $sWhereClause = " AND `ta`.`active`=:active";
  464. }
  465. break;
  466. }
  467. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  468. FROM `sys_agents_assistants` AS `ta` " . $sJoinClause . "
  469. WHERE 1" . $sWhereClause;
  470. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  471. }
  472. public function updateAssistants($aSetClause, $aWhereClause)
  473. {
  474. if(empty($aSetClause) || empty($aWhereClause))
  475. return false;
  476. return (int)$this->query("UPDATE `sys_agents_assistants` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  477. }
  478. public function getChatsBy($aParams = [])
  479. {
  480. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  481. $sSelectClause = "`tac`.*";
  482. $sJoinClause = $sWhereClause = "";
  483. switch($aParams['sample']) {
  484. case 'id':
  485. $aMethod['name'] = 'getRow';
  486. $aMethod['params'][1] = [
  487. 'id' => $aParams['id']
  488. ];
  489. $sWhereClause .= " AND `tac`.`id`=:id";
  490. break;
  491. case 'name':
  492. $aMethod['name'] = 'getRow';
  493. $aMethod['params'][1] = [
  494. 'name' => $aParams['name']
  495. ];
  496. $sWhereClause .= " AND `tac`.`name`=:name";
  497. break;
  498. case 'assistant_id':
  499. $aMethod['params'][1] = [
  500. 'assistant_id' => $aParams['assistant_id']
  501. ];
  502. $sWhereClause .= " AND `tac`.`assistant_id`=:assistant_id";
  503. break;
  504. }
  505. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  506. FROM `sys_agents_assistants_chats` AS `tac` " . $sJoinClause . "
  507. WHERE 1" . $sWhereClause;
  508. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  509. }
  510. public function updateChats($aSetClause, $aWhereClause)
  511. {
  512. if(empty($aSetClause) || empty($aWhereClause))
  513. return false;
  514. return (int)$this->query("UPDATE `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  515. }
  516. public function deleteChats($aParamsWhere)
  517. {
  518. if(empty($aParamsWhere))
  519. return false;
  520. return (int)$this->query("DELETE FROM `sys_agents_assistants_chats` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  521. }
  522. public function getFilesBy($aParams = [])
  523. {
  524. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  525. $sSelectClause = "`taf`.*";
  526. $sJoinClause = $sWhereClause = "";
  527. switch($aParams['sample']) {
  528. case 'id':
  529. $aMethod['name'] = 'getRow';
  530. $aMethod['params'][1] = [
  531. 'id' => $aParams['id']
  532. ];
  533. $sWhereClause .= " AND `taf`.`id`=:id";
  534. break;
  535. case 'assistant_id':
  536. $aMethod['params'][1] = [
  537. 'assistant_id' => $aParams['assistant_id']
  538. ];
  539. $sWhereClause .= " AND `taf`.`assistant_id`=:assistant_id";
  540. break;
  541. }
  542. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  543. FROM `sys_agents_assistants_files` AS `taf` " . $sJoinClause . "
  544. WHERE 1" . $sWhereClause;
  545. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  546. }
  547. public function insertFile($aParamsSet)
  548. {
  549. if(empty($aParamsSet) || !is_array($aParamsSet))
  550. return false;
  551. return (int)$this->query("INSERT INTO `sys_agents_assistants_files` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  552. }
  553. public function updateFiles($aSetClause, $aWhereClause)
  554. {
  555. if(empty($aSetClause) || empty($aWhereClause))
  556. return false;
  557. return (int)$this->query("UPDATE `sys_agents_assistants_files` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  558. }
  559. public function deleteFiles($aParamsWhere)
  560. {
  561. if(empty($aParamsWhere))
  562. return false;
  563. return (int)$this->query("DELETE FROM `sys_agents_assistants_files` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  564. }
  565. }
  566. /** @} */