BaseDao.Class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace JinDouYun\Dao;
  3. use Mall\Framework\Core\SqlHelper;
  4. use Mall\Framework\Factory;
  5. use JinDouYun\Cache\AllTableNameCache;
  6. class BaseDao extends SqlHelper
  7. {
  8. /**
  9. * @var \Mall\Framework\SearchClient\Client
  10. */
  11. private $search;
  12. /**
  13. * 当前操作的数据库配置标识
  14. * @var $serviceDB
  15. */
  16. private $serviceDB;
  17. /**
  18. * 参考文章: https://www.php.cn/php-weizijiaocheng-403412.html
  19. * 开启事务数量统计
  20. * BaseDao constructor.
  21. * @param string $serviceDB
  22. */
  23. private $transactions;
  24. public function __construct($serviceDB = 'default')
  25. {
  26. $this->serviceDB = $serviceDB;
  27. parent::__construct($serviceDB);
  28. //$this->setSearchIndex($serviceDB);
  29. }
  30. /**
  31. * 设置搜索引擎配置项
  32. *
  33. * @param string $serviceDB
  34. *
  35. * @throws \Exception
  36. * @return \Mall\Framework\SearchClient\Client
  37. */
  38. public function setSearchIndex($serviceDB)
  39. {
  40. $this->search = Factory::search($serviceDB);
  41. return $this->search;
  42. }
  43. /**
  44. * 计算分表表名
  45. *
  46. * @param string $prefix 表名前缀
  47. * @param int $id 索引表id
  48. * @param int $pNumber 分割数量
  49. *
  50. * @return string
  51. */
  52. public function getTableName($prefix, $id, $pNumber = 500000)
  53. {
  54. $prefix = trim($prefix, '_') . '_';
  55. $tableName = strtolower($prefix . ceil($id / $pNumber));
  56. return $tableName;
  57. }
  58. /**
  59. * 判断表是否存在
  60. * @param $tableName
  61. * @return bool
  62. */
  63. public function existsTable($tableName)
  64. {
  65. $databaseName = Factory::config()->get('db')[$this->serviceDB]['dbname'];
  66. if(AllTableNameCache::TableIsExists($databaseName, $tableName)) {
  67. return true;
  68. }else{
  69. return false;
  70. }
  71. }
  72. /**
  73. * 切换Dao层操作的表
  74. * 主要用作切换分表使用
  75. *
  76. * @param $tableName
  77. *
  78. * @throws \Exception
  79. */
  80. public function setTable($tableName)
  81. {
  82. $this->_table = $tableName;
  83. $databaseName = Factory::config()->get('db')[$this->serviceDB]['dbname'];
  84. // 当前使用库所有表名缓存如果不存在自动更新
  85. if (!AllTableNameCache::allTableNameCacheIsExists($databaseName)) {
  86. $tables = $this->db->select("SELECT TABLE_NAME FROM INFORMATION_SCHEMA. TABLES WHERE TABLE_SCHEMA = '{$databaseName}';") ?: [];
  87. if (!empty($tables)) {
  88. AllTableNameCache::allTableNameCache($databaseName, $tables);
  89. }
  90. }
  91. // 判断切换的表是否存在,不存在自动创建
  92. if (!AllTableNameCache::TableIsExists($databaseName, $tableName)) {
  93. $result = explode('_', $tableName);
  94. if ($result && !empty($result)) {
  95. $tablePrefix = '';
  96. for ($i = 0, $c = count($result); $i < ($c - 1); $i++) {
  97. if(!is_numeric($result[$i])){
  98. $tablePrefix .= $result[$i] . '_';
  99. }
  100. }
  101. $dbresult = $this->db->query("CREATE TABLE IF NOT EXISTS {$tableName} LIKE {$tablePrefix}1");
  102. if ($dbresult === false) {
  103. throw new \Exception($tableName . '分表创建错误. ErrorInfo: ' . var_export($this->db->error(), true));
  104. }
  105. } else {
  106. throw new \Exception($tableName . '不是一个正确得表名');
  107. }
  108. AllTableNameCache::addNewTableName($databaseName, $tableName);
  109. }
  110. }
  111. /**
  112. * 添加数据
  113. *
  114. * @param array $params
  115. *
  116. * @return bool|int
  117. */
  118. public function insert($params = array(), $multiple = false)
  119. {
  120. return parent::insert($params, $multiple);
  121. }
  122. /**
  123. * replace方式添加数据
  124. *
  125. * @param array $params
  126. *
  127. * @return bool|int
  128. */
  129. public function replace($params = array(), $multiple = false)
  130. {
  131. return parent::replace($params, $multiple);
  132. }
  133. /**
  134. * 更新数据
  135. *
  136. * @param array $data
  137. * @param null $where
  138. * @param null $limit
  139. * @param null $order
  140. *
  141. * @return int
  142. */
  143. public function update($data = array(), $where = null, $limit = null, $order = null)
  144. {
  145. return parent::update($data, $where, $limit, $order);
  146. }
  147. /**
  148. * 删除数据
  149. *
  150. * @param null $where
  151. * @param null $limit
  152. * @param null $order
  153. * @param array $data
  154. *
  155. * @return int
  156. */
  157. public function delete($where = null, $limit = null, $order = null, $data = array())
  158. {
  159. return parent::delete($where, $limit, $order, $data);
  160. }
  161. /**
  162. * 数据库自增自减操作
  163. * @param $fieldName 字段名称
  164. * @param $id 修改数据id
  165. * @param $action 操作 "+"/"-"
  166. * @param $step 修改数据步长
  167. * @return mixed 影响行数
  168. *
  169. */
  170. public function autodecrement($fieldName, $id, $action, $step)
  171. {
  172. $tableName = parent::get_Table();
  173. $sql = 'call autodecrement("'.$tableName.'", "'.$fieldName.'", '.$id.', "'.$action.'", '.$step.', @result);';
  174. $dbResult = parent::query($sql);
  175. if ($dbResult === false) {
  176. return false;
  177. }
  178. $sql = 'select @result as result;';
  179. $dbResult = parent::query($sql);
  180. if ($dbResult === false) {
  181. return false;
  182. }
  183. return $dbResult[0]['result'];
  184. }
  185. /**
  186. * 执行sql
  187. */
  188. public function query($sql)
  189. {
  190. return parent::query($sql);
  191. }
  192. /**
  193. * 执行sql
  194. */
  195. public function exportQuery($sql)
  196. {
  197. return parent::exportQuery($sql);
  198. }
  199. /**
  200. * 获取Sql操作错误
  201. *
  202. * @return string
  203. */
  204. public function error()
  205. {
  206. return parent::error();
  207. }
  208. /**
  209. * 过虑字段
  210. *
  211. * @param array $params 要筛选的数据
  212. *
  213. * @return array
  214. */
  215. public function getTablesFields($params)
  216. {
  217. $fields = [];
  218. foreach ($this->_fields as $field) {
  219. if (array_key_exists($field, $params)) {
  220. $fields[$field] = $params[$field];
  221. }
  222. }
  223. return $fields;
  224. }
  225. /**
  226. * 开启事务
  227. * @param bool $foreign_key_checks
  228. * @return mixed
  229. */
  230. public function beginTransaction($foreign_key_checks = false)
  231. {
  232. ++$this->transactions;
  233. if ($this->transactions == 1){
  234. return parent::beginTransaction($foreign_key_checks = false);
  235. }
  236. }
  237. /**
  238. * 提交事务
  239. * @return mixed
  240. */
  241. public function commit()
  242. {
  243. if($this->transactions == 1){
  244. return parent::commit();
  245. }
  246. --$this->transactions;
  247. }
  248. /**
  249. * 回滚事务
  250. * @return mixed
  251. */
  252. public function rollBack()
  253. {
  254. if($this->transactions == 1){
  255. $this->transactions = 0;
  256. return parent::rollBack();
  257. }else{
  258. --$this->transactions;
  259. }
  260. }
  261. /**
  262. * 根据DSL查询文档
  263. *
  264. * @param $query
  265. *
  266. * @return array
  267. */
  268. public function getSearchQueryDsl($query)
  269. {
  270. return $this->search->search($query);
  271. }
  272. /**
  273. * 根据DSL查询文档 滚动查询
  274. * @param $query
  275. * @param string $scroll
  276. * @param int $size
  277. * @return mixed
  278. */
  279. public function getScrollSearchQueryDsl($query, $scroll = '10s', $size = 2000)
  280. {
  281. $result = [];
  282. $scrollResult = $this->search->scrollSearch($query, $scroll, $size);
  283. while (isset($scrollResult['hits']['hits']) && count($scrollResult['hits']['hits']) > 0) {
  284. $result = array_merge($result, $scrollResult['hits']['hits']);
  285. $scroll_id = $scrollResult['_scroll_id'];
  286. $scrollResult = $this->search->request('/_search/scroll','POST',[
  287. "scroll_id" => $scroll_id,
  288. "scroll" => $scroll
  289. ],
  290. false,
  291. false
  292. );
  293. }
  294. if(!empty($result)) {
  295. $scrollResult['hits']['hits'] = $result;
  296. }
  297. return $scrollResult;
  298. }
  299. /**
  300. * 根据索引的主ID查询文档
  301. *
  302. * @param int $id 索引的DocumentId
  303. *
  304. * @return array
  305. */
  306. public function getSearchIndexDocument($id)
  307. {
  308. //$id = intval($id);
  309. return $this->search->get($id);
  310. }
  311. /**
  312. * 添加或更新ES索引
  313. *
  314. * @param array $data 创建索引数据
  315. * @param int $id 创建索引的DocumentId
  316. *
  317. * @return array
  318. */
  319. public function addUpSearchIndexDocument($data, $id)
  320. {
  321. return $this->search->index($data, $id);
  322. }
  323. /**
  324. * 局部更新索引内容
  325. * @param array $data 要更新的数据
  326. * @param int $id 要更新的文档下的数据id
  327. * @return array
  328. */
  329. public function esupdateTypeFieldVaule($data, $id)
  330. {
  331. return $this->search->updateFieldVaule($data, $id);
  332. }
  333. /**
  334. * 批量更新索引内容
  335. * @param $query
  336. * @param $data
  337. * @return array
  338. */
  339. public function esBatchUpdateTypeFieldVaule($data, $query)
  340. {
  341. return $this->search->batchUpdateFieldVaule($data, $query);
  342. }
  343. /**
  344. * 删除索引下面的指定文档
  345. */
  346. public function esdeleteTypeDocument($id)
  347. {
  348. return $this->search->delete($id);
  349. }
  350. }