1
0

BaseModel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\model;
  15. use app\common\service\FileService;
  16. use think\facade\Db;
  17. use think\Model;
  18. /**
  19. * 基础模型
  20. * Class BaseModel
  21. * @package app\common\model
  22. */
  23. class BaseModel extends Model
  24. {
  25. /**
  26. * @notes 公共处理图片,补全路径
  27. * @param $value
  28. * @return string
  29. * @author 张无忌
  30. * @date 2021/9/10 11:02
  31. */
  32. public function getImageAttr($value)
  33. {
  34. return trim($value) ? FileService::getFileUrl($value) : '';
  35. }
  36. /**
  37. * @notes 公共图片处理,去除图片域名
  38. * @param $value
  39. * @return mixed|string
  40. * @author 张无忌
  41. * @date 2021/9/10 11:04
  42. */
  43. public function setImageAttr($value)
  44. {
  45. return trim($value) ? FileService::setFileUrl($value) : '';
  46. }
  47. public static function getDb(){
  48. $model = new static();
  49. return $model->db()->getTable();
  50. }
  51. /**
  52. * 批量更新字段
  53. * [1 => ['filed1' => 'value1','filed2'=>'value2'],2=> ['filed1'=>'value1']] key为ID主键
  54. * 获取DB 对象并赋予 模型本身的 connection 连接
  55. * @return mixed
  56. */
  57. public static function updateWhenCase(array $dataMap)
  58. {
  59. if(is_array($dataMap) && count($dataMap) > 0 ) {
  60. // 构建 SQL 语句
  61. $sql = "UPDATE " . self::getDb() . " SET ";
  62. $uniqueFieldValues = [];
  63. foreach ($dataMap as $id => $data) {
  64. foreach ($data as $field => $value) {
  65. if (!isset($uniqueFieldValues[$field])) {
  66. $uniqueFieldValues[$field] = [];
  67. }
  68. $uniqueFieldValues[$field][] = ['id' => $id, 'value' => $value];
  69. }
  70. }
  71. $placeholders = [];
  72. foreach ($uniqueFieldValues as $field => $data) {
  73. $str = " `$field` = CASE id ";
  74. foreach ($data as $value) {
  75. $str .= " WHEN {$value['id']} THEN " . self::stringFilter($value['value']);
  76. }
  77. $str .= " ELSE " . $field . " END";
  78. $placeholders[] = $str;
  79. }
  80. $sql .= implode(', ', $placeholders);
  81. $sql .= " where id in (" . implode(",", array_keys($dataMap)) . ")";
  82. // 执行 SQL 语句
  83. Db::execute($sql);
  84. }
  85. }
  86. public static function stringFilter($string)
  87. {
  88. if(is_null($string)) {
  89. return 'null';
  90. }
  91. return "'" . str_replace("'","\'",$string) . "'";
  92. }
  93. }