chengkun
2025-08-29 73cdff843994b42beef7a22844326f83fee104de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
 
namespace services;
 
use think\Exception;
use think\facade\Db;
 
 
class DbService {
    /**
     * 获取分类列表
     * @param string $tableName
     * @param string $order
     * @return array
     */
    public function duplicateUpdateAll(string $tableName, string $onDuplicateKey, array $insertDatas): bool{
        $insertKeyStr = "";
        $insertValueArr = [];
 
        foreach($insertDatas as $key=>$sval) {
            $insertValue = '("';
            $insertValue .= implode('","',array_values($sval));
            $insertValue .= '")';
            array_push($insertValueArr,$insertValue);
            if (empty($key)) {
                $insertKeyStr .= '(';
                $insertKeyStr .= implode(',',array_keys($sval));
                $insertKeyStr .= ')';
            }
        }
        $insertValueSql = implode(',',$insertValueArr);
 
//        throw new Exception("insert into ".$tableName.$insertKeyStr." values".$insertValueSql." on duplicate key update $onDuplicateKey");
 
        $insertReturn = Db::execute("insert into ".$tableName.$insertKeyStr." values".$insertValueSql." on duplicate key update $onDuplicateKey");
        if (!$insertReturn) {
            return false;
        }
        return true;
    }
}