BaseModel.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\helperService;
  4. use think\Model;
  5. class BaseModel extends Model
  6. {
  7. public function __construct($data=""){
  8. parent::__construct($data);
  9. if(!is_array($data) || empty($data)){
  10. //实例化,将产品促销过期数据更新
  11. ProductPromoModel::updateExpireTs();
  12. //将订单超过半小时的退单
  13. OrderModel::checkOrder();
  14. }
  15. }
  16. protected function BaseModel($res)
  17. {
  18. return helperService::modelDataToArr($res);
  19. }
  20. public function getPage($page,$pageSize,$condition=[],$is_count = false,$order=''){
  21. $this->alias('a');
  22. if(!empty($condition)){
  23. $this->where($condition);
  24. }
  25. if(!empty($order)){
  26. $this->order($order);
  27. }
  28. if($is_count){
  29. return $this->count();
  30. }
  31. return $this->page($page,$pageSize)->column('*');
  32. }
  33. /**
  34. * 保存数据
  35. * @param $data
  36. * @param array $condition
  37. * @return $this
  38. */
  39. public function saveData($data,$condition=[]){
  40. if($condition){
  41. return $this->where($condition)->update($data);
  42. }
  43. return $this->insertGetId($data);
  44. }
  45. /**
  46. * 保存多条数据
  47. * @param $data
  48. * @return bool|int|string
  49. */
  50. public function insertAllDate($data){
  51. if(empty($data) && !is_array($data)){
  52. return false;
  53. }
  54. return $this->insertAll($data);
  55. }
  56. /**
  57. * 获取一条数据信息
  58. * @param array $condition
  59. * @param bool $is_cache
  60. * @param string $order
  61. * @return array|false|\PDOStatement|string|Model
  62. */
  63. public function getOne($condition=[],$is_cache=false,$order=''){
  64. if($condition){
  65. $this->where($condition);
  66. }
  67. if($is_cache){
  68. $this->cache($is_cache,60);
  69. }
  70. if($order){
  71. $this->order($order);
  72. }
  73. return $this->BaseModel($this->find());
  74. }
  75. /**
  76. * 获取总条数据信息
  77. * @param array $condition
  78. * @return array|false|\PDOStatement|string|Model
  79. */
  80. public function getCount($condition=[]){
  81. if($condition){
  82. return $this->where($condition)->count();
  83. }
  84. return $this->count();
  85. }
  86. /**
  87. * 获取总条数据信息
  88. * @param array $condition
  89. * @param string $field
  90. * @return int|string
  91. */
  92. public function getSum($condition=[],$field=''){
  93. if($condition){
  94. $this->where($condition);
  95. }
  96. return $this->sum($field);
  97. }
  98. /**
  99. * 获取多条数据信息
  100. * @param array $condition 条件
  101. * @param string $order 排序
  102. * @param int $limit 限制数
  103. * @return array
  104. */
  105. public function getMulti($condition=[],$order='',$limit=0){
  106. if($order){
  107. $this->order($order);
  108. }
  109. if($limit){
  110. $this->limit($limit);
  111. }
  112. if($condition){
  113. return $this->BaseModel($this->where($condition)->select());
  114. }
  115. return $this->BaseModel($this->select());
  116. }
  117. /**
  118. * 根据条件删除数据
  119. * @param array $condition
  120. * @return bool|int
  121. */
  122. public function removeData($condition=[]){
  123. //删除功能要严格控制
  124. if(empty($condition)){
  125. return false;
  126. }
  127. return $this->where($condition)->delete();
  128. }
  129. /**
  130. *增/改
  131. *@param array $data 数据
  132. *@param array $where 添加条件
  133. *@return array $res 返回结果
  134. */
  135. public function kuyun_insert($data,$where){
  136. if(!empty($where)){
  137. $where = array_filter($where);
  138. $res = $this->where($where)->update($data);
  139. }else{
  140. $res = $this->insertGetId($data);
  141. }
  142. return $res;
  143. }
  144. /**
  145. *多查
  146. *@param array $where 条件
  147. *@param array $join 联合查询
  148. *@param string $order 排序
  149. *@param number $limit
  150. *@param number $page 是否分页
  151. *@param bull $count 是否查数量
  152. *@return array number
  153. */
  154. public function kuyun_select($where=[],$join=[],$order='',$field='',$limit=0,$page=0,$is_count=false){
  155. if(!empty($where)){
  156. $where = array_filter($where);
  157. $this->where($where);
  158. }
  159. if(!empty($join)){
  160. $this->join($join);
  161. }
  162. if(!empty($order)){
  163. $this->order($order);
  164. }
  165. if(!empty($field)){
  166. $this->field($field);
  167. }
  168. if(!empty($limit)){
  169. $this->limit($limit);
  170. }
  171. if($page!=0){
  172. $pageSize = 10;
  173. $this->page($page,$pageSize);
  174. }
  175. if($is_count){
  176. return $this->count();
  177. }else{
  178. return $this->select();
  179. }
  180. }
  181. /**
  182. *单查
  183. *@param array $where 查询条件
  184. *@param string $field 查询指定字段
  185. *@param array $join 联合查询
  186. */
  187. public function kuyun_find($where,$field,$join){
  188. if(!empty($where)){
  189. $where = array_filter($where);
  190. $this->where($where);
  191. }
  192. if(!empty($field)){
  193. $this->field($field);
  194. }
  195. if(!empty($join)){
  196. $this->join($join);
  197. }
  198. return $this->find();
  199. }
  200. /**
  201. *物理删除
  202. *@param array $where删除条件
  203. *@return number 删除id
  204. */
  205. public function kuyun_delete($where){
  206. return $this->where($where)->delete();
  207. }
  208. }