companymodel.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * @Author: wang jun
  4. * @Date: 2022-01-18 09:43:33
  5. * @Last Modified by: wang jun
  6. * @Last Modified time: 2022-01-19 13:28:44
  7. */
  8. namespace app\index\model;
  9. use think\Model;
  10. class companymodel extends Model
  11. {
  12. protected $table = 't_company';
  13. public function insertData($data)
  14. {
  15. if (!isset($data['create_time']) || empty($data['create_time']) || !is_string($data['create_time'])) {
  16. $data['create_time'] = date("Y-m-d H:i:s");
  17. }
  18. $data = $this->formatData($data);
  19. if (empty($data)) {
  20. return false;
  21. }
  22. $id = $this->insertGetId($data);
  23. return empty($id) ? false : $id;
  24. }
  25. /**
  26. * 校验入库数据
  27. * 20220119
  28. */
  29. private function formatData($data)
  30. {
  31. $useData = [];
  32. $fields = $this->getTableFields();
  33. foreach ($fields as $key => $value) {
  34. if (isset($data[$value])) {
  35. $useData[$value] = $data[$value];
  36. }
  37. }
  38. return $useData;
  39. }
  40. public function getInfo($where, $field = "*", $row = true)
  41. {
  42. $info = $this->field($field)->where($where);
  43. if ($row) {
  44. $info = $info->find();
  45. } else {
  46. $info = $info->select();
  47. }
  48. return empty($info) ? false : $info;
  49. }
  50. public function updateinfo($where, $updateData)
  51. {
  52. $row = $this->where($where)->update($updateData);
  53. return empty($row) ? false : $row;
  54. }
  55. public function deleteinfo($where)
  56. {
  57. $row = $this->where($where)->delete();
  58. return empty($row) ? false : $row;
  59. }
  60. public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
  61. {
  62. $sqlObj = $this->where($where);
  63. if ("count" != $field) {
  64. $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
  65. if ($row) {
  66. $data = $sqlObj->find();
  67. } else {
  68. $data = $sqlObj->select();
  69. }
  70. } else {
  71. $data = $sqlObj = $sqlObj->count();
  72. }
  73. return $data;
  74. }
  75. /**
  76. * 根据id获取信息
  77. * wj
  78. * 20220118
  79. */
  80. public function getinfobyid($id)
  81. {
  82. $where = ['id' => $id];
  83. $info = $this->where($where)->find();
  84. return $info;
  85. }
  86. /**
  87. * 根据id修改数据
  88. * wj
  89. * 20220118
  90. */
  91. public function updatebyid($id, $updateData)
  92. {
  93. $where = ['id' => $id];
  94. $updateData = $this->formatData($updateData);
  95. if (empty($updateData)) {
  96. return false;
  97. }
  98. $row = $this->where($where)->update($updateData);
  99. return $row;
  100. }
  101. }