BaseModel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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)->select();
  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 $field
  61. * @return mixed
  62. */
  63. public function getOne($condition=[],$is_cache=false,$field=''){
  64. if($field){
  65. $this->field($field);
  66. }
  67. if($condition){
  68. $this->where($condition);
  69. }
  70. if($is_cache){
  71. $this->cache($is_cache,60);
  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 $order 排序
  90. * @param int $limit 限制数
  91. * @return array
  92. */
  93. public function getMulti($condition=[],$order='',$limit=0){
  94. if($order){
  95. $this->order($order);
  96. }
  97. if($limit){
  98. $this->limit($limit);
  99. }
  100. if($condition){
  101. return $this->BaseModel($this->where($condition)->select());
  102. }
  103. return $this->BaseModel($this->select());
  104. }
  105. /**
  106. * 根据条件删除数据
  107. * @param array $condition
  108. * @return bool|int
  109. */
  110. public function removeData($condition=[]){
  111. //删除功能要严格控制
  112. if(empty($condition)){
  113. return false;
  114. }
  115. return $this->where($condition)->delete();
  116. }
  117. }