BaseDao.Class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace Jobs\Dao;
  3. use Mall\Framework\Core\SqlHelper;
  4. use Mall\Framework\Factory;
  5. use Jobs\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. * 切换Dao层操作的表
  60. * 主要用作切换分表使用
  61. *
  62. * @param $tableName
  63. *
  64. * @throws \Exception
  65. */
  66. public function setTable($tableName)
  67. {
  68. $this->_table = $tableName;
  69. $databaseName = Factory::config()->get('db')[$this->serviceDB]['dbname'];
  70. // 当前使用库所有表名缓存如果不存在自动更新
  71. if (!AllTableNameCache::allTableNameCacheIsExists($databaseName)) {
  72. $tables = $this->db->select("SELECT TABLE_NAME FROM INFORMATION_SCHEMA. TABLES WHERE TABLE_SCHEMA = '{$databaseName}';") ?: [];
  73. if (!empty($tables)) {
  74. AllTableNameCache::allTableNameCache($databaseName, $tables);
  75. }
  76. }
  77. var_dump("xxxxaaaa");
  78. var_dump($databaseName);
  79. var_dump($tableName);
  80. // 判断切换的表是否存在,不存在自动创建
  81. if (!AllTableNameCache::TableIsExists($databaseName, $tableName)) {
  82. $result = explode('_', $tableName);
  83. if ($result && !empty($result)) {
  84. $tablePrefix = '';
  85. for ($i = 0, $c = count($result); $i < ($c - 1); $i++) {
  86. if(!is_numeric($result[$i])){
  87. $tablePrefix .= $result[$i] . '_';
  88. }
  89. }
  90. $dbresult = $this->db->query("CREATE TABLE {$tableName} LIKE {$tablePrefix}1");
  91. if ($dbresult === false) {
  92. throw new \Exception($tableName . '分表创建错误. ErrorInfo: ' . var_export($this->db->error(), true));
  93. }
  94. } else {
  95. throw new \Exception($tableName . '不是一个正确得表名');
  96. }
  97. AllTableNameCache::addNewTableName($databaseName, $tableName);
  98. }
  99. }
  100. /**
  101. * 添加数据
  102. *
  103. * @param array $params
  104. *
  105. * @return bool|int
  106. */
  107. public function insert($params = array(), $multiple = false)
  108. {
  109. return parent::insert($params, $multiple);
  110. }
  111. /**
  112. * replace方式添加数据
  113. *
  114. * @param array $params
  115. *
  116. * @return bool|int
  117. */
  118. public function replace($params = array(), $multiple = false)
  119. {
  120. return parent::replace($params, $multiple);
  121. }
  122. /**
  123. * 更新数据
  124. *
  125. * @param array $data
  126. * @param null $where
  127. * @param null $limit
  128. * @param null $order
  129. *
  130. * @return int
  131. */
  132. public function update($data = array(), $where = null, $limit = null, $order = null)
  133. {
  134. return parent::update($data, $where, $limit, $order);
  135. }
  136. /**
  137. * 删除数据
  138. *
  139. * @param null $where
  140. * @param null $limit
  141. * @param null $order
  142. * @param array $data
  143. *
  144. * @return int
  145. */
  146. public function delete($where = null, $limit = null, $order = null, $data = array())
  147. {
  148. return parent::delete($where, $limit, $order, $data);
  149. }
  150. /**
  151. * 执行sql
  152. */
  153. public function query($sql)
  154. {
  155. return parent::query($sql);
  156. }
  157. /**
  158. * 获取Sql操作错误
  159. *
  160. * @return string
  161. */
  162. public function error()
  163. {
  164. return parent::error();
  165. }
  166. /**
  167. * 过虑字段
  168. *
  169. * @param array $params 要筛选的数据
  170. *
  171. * @return array
  172. */
  173. public function getTablesFields($params)
  174. {
  175. $fields = [];
  176. foreach ($this->_fields as $field) {
  177. if (array_key_exists($field, $params)) {
  178. $fields[$field] = $params[$field];
  179. }
  180. }
  181. return $fields;
  182. }
  183. /**
  184. * 根据DSL查询文档
  185. *
  186. * @param $query
  187. *
  188. * @return array
  189. */
  190. public function getSearchQueryDsl($query)
  191. {
  192. return $this->search->search($query);
  193. }
  194. /**
  195. * 根据索引的主ID查询文档
  196. *
  197. * @param int $id 索引的DocumentId
  198. *
  199. * @return array
  200. */
  201. public function getSearchIndexDocument($id)
  202. {
  203. $id = intval($id);
  204. return $this->search->get($id);
  205. }
  206. /**
  207. * 添加或更新ES索引
  208. *
  209. * @param array $data 创建索引数据
  210. * @param int $id 创建索引的DocumentId
  211. *
  212. * @return array
  213. */
  214. public function addUpSearchIndexDocument($data, $id)
  215. {
  216. return $this->search->index($data, $id);
  217. }
  218. /**
  219. * 局部更新索引内容
  220. * @param array $data 要更新的数据
  221. * @param int $id 要更新的文档下的数据id
  222. * @return array
  223. */
  224. public function esupdateTypeFieldVaule($data, $id)
  225. {
  226. return $this->search->updateFieldVaule($data, $id);
  227. }
  228. /**
  229. * 删除索引下面的指定文档
  230. */
  231. public function esdeleteTypeDocument($id)
  232. {
  233. return $this->search->delete($id);
  234. }
  235. /**
  236. * 开启事务
  237. * @param bool $foreign_key_checks
  238. * @return mixed
  239. */
  240. public function beginTransaction($foreign_key_checks = false)
  241. {
  242. ++$this->transactions;
  243. if ($this->transactions == 1){
  244. return parent::beginTransaction($foreign_key_checks = false);
  245. }
  246. }
  247. /**
  248. * 提交事务
  249. * @return mixed
  250. */
  251. public function commit()
  252. {
  253. if($this->transactions == 1){
  254. return parent::commit();
  255. }
  256. --$this->transactions;
  257. }
  258. /**
  259. * 回滚事务
  260. * @return mixed
  261. */
  262. public function rollBack()
  263. {
  264. if($this->transactions == 1){
  265. $this->transactions = 0;
  266. return parent::rollBack();
  267. }else{
  268. --$this->transactions;
  269. }
  270. }
  271. }