mysql批量插入更新操作

//添加关联赠品(确定)
    public function addGiveGoods($ids,$child,$parent_sku_no){
        $personnel_code=$_SESSION['user']['personnel_code'];
        $time=date("Y-m-d H:i:s",time());
        $sku_gift=M('sku_gift');
        $sku_gift_stock=M('sku_gift_stock');
        $sku_nos=array_column($child,'sku_no');
        $sgg_where['sku_no']=array('in',$sku_nos);
        $sku_gift_stock_info=$sku_gift_stock->field('sku_no,is_online')->where($sgg_where)->select();
        foreach($sku_gift_stock_info as $k=>$v){
            $sgg[$v['sku_no']]=$v['is_online'];
        }
        foreach($child as $k=>$v){
            $data[]=array(
                'id'=>$v['id'],
                'parent_sku_no'=>$parent_sku_no,
                'sku_no'=>$v['sku_no'],
                'create_time'=>$time,
                'modified'=>$time,
                'num'=>$v['num'],
                'is_deleted'=>0,
                'is_online'=>empty($sgg[$v['sku_no']])?0:1,
                'update_user'=>$personnel_code,
                'create_user'=>$personnel_code,
            );
            if(empty($v['id'])){
                $pars[]=array(
                    'message'=>$_SESSION['user']['true_name'].":赠品".$v['sku_no']."被商品".$parent_sku_no."关联了",
                    'create_time'=>$time,
                    'create_id'=>$personnel_code,
                    'type'=>2,
                    'sku_no'=>$v
                );
            }
        }
        $sku_nos=explode(",",$sku_nos);
        foreach($data as $k=>$v){
            if(empty($v['id'])){
                $update[]="('','".$v['parent_sku_no']."','".$v['sku_no']."','".$v['create_time']."','".$v['modified']."','".$v['num']."','".$v['is_deleted']."','".$v['is_online']."','".$v['update_user']."','".$v['create_user']."')";
            }else{
                $update[]="('".$v['id']."','".null."','".null."','".null."','".$v['modified']."','".$v['num']."','".null."','".null."','".$v['update_user']."','".null."')";
            }
        }
        if(!empty($update)){
            $value=implode(',',$update);
        }
        if(!empty($data)){
            $res=M()->execute("insert into ".C('DB_PREFIX')."sku_gift(`id`,`parent_sku_no`,`sku_no`,`create_time`,`modified`,`num`,`is_deleted`,`is_online`,`update_user`,`create_user`) VALUES $value ON DUPLICATE KEY UPDATE `id`=VALUES(id),`parent_sku_no`=VALUES(parent_sku_no),`sku_no`=VALUES(sku_no),`create_time`=VALUES(create_time),`modified`=VALUES(modified),`num`=VALUES(num),`is_deleted`=VALUES(is_deleted),`is_online`=VALUES(is_online),`update_user`=VALUES(update_user),`create_user`=VALUES(create_user)");
            if(!$res){
                return 100;
            }
        }
        if(!empty($ids)){
            $ids=implode(",",$ids);
            $sg_where['id']=array('in',$ids);
            $res_in=$sku_gift->field('parent_sku_no,sku_no')->where($sg_where)->select();
            $ar=array(
                'is_deleted'=>1,
                'modified'=>$time,
                'update_user'=>$personnel_code,
            );
            $res=$sku_gift->data($ar)->where($sg_where)->save();
            if(!$res){
                return 101;
            }
            $skus=implode(",",array_column($res_in,'sku_no'));
            if(!empty($res_in)){
                $pars=array(
                    'message'=>$_SESSION['user']['true_name'].":商品".$parent_sku_no."的关联赠品".$skus."被删除了",
                    'create_time'=>date('Y-m-d H:i:s',time()),
                    'create_id'=>$_SESSION['user']['personnel_code'],
                    'type'=>1,
                    'sku_no'=>$parent_sku_no
                );
                $license=new LicenseModel();
                $license->writeLog('sku_gift_log',$pars);
            }
        }
        if(!empty($pars)){
            //写日志
            $sku_noss=implode(",",$sku_nos);
            $params=array(
                'message'=>$_SESSION['user']['true_name'].":商品".$parent_sku_no."添加了关联赠品".$sku_noss,
                'create_time'=>date('Y-m-d H:i:s',time()),
                'create_id'=>$_SESSION['user']['personnel_code'],
                'type'=>1,
                'sku_no'=>$parent_sku_no
            );
            $license=new LicenseModel();
            $license->writeLog('sku_gift_log',$params);
            $license->writeAllLog('sku_gift_log',$pars);
        }
        return true;
    }
INSERT INTO lc_sku_gift (
    `id`,
    `parent_sku_no`,
    `sku_no`,
    `create_time`,
    `modified`,
    `num`,
    `is_deleted`,
    `is_online`,
    `update_user`,
    `create_user`
)
VALUES
    (
        '',
        'P001514-01',
        'P002645',
        '2018-05-25 09:15:47',
        '2018-05-25 09:15:47',
        '1',
        '0',
        '1',
        '800010',
        '800010'
    ),
    (
        '',
        'P001514-01',
        'P002689',
        '2018-05-25 09:15:47',
        '2018-05-25 09:15:47',
        '1',
        '0',
        '1',
        '800010',
        '800010'
    ),
    (
        '61',
        '',
        '',
        '',
        '2018-05-25 09:15:47',
        '1',
        '',
        '',
        '800010',
        ''
    ),
    (
        '66',
        '',
        '',
        '',
        '2018-05-25 09:15:47',
        '1',
        '',
        '',
        '800010',
        ''
    ),
    (
        '67',
        '',
        '',
        '',
        '2018-05-25 09:15:47',
        '1',
        '',
        '',
        '800010',
        ''
    ),
    (
        '68',
        '',
        '',
        '',
        '2018-05-25 09:15:47',
        '1',
        '',
        '',
        '800010',
        ''
    ),
    (
        '69',
        '',
        '',
        '',
        '2018-05-25 09:15:47',
        '1',
        '',
        '',
        '800010',
        ''
    ),
    (
        '91',
        '',
        '',
        '',
        '2018-05-25 09:15:47',
        '1',
        '',
        '',
        '800010',
        ''
    ) ON DUPLICATE KEY UPDATE `id` =
VALUES
    (id),
    `parent_sku_no` =
VALUES
    (parent_sku_no),
    `sku_no` =
VALUES
    (sku_no),
    `create_time` =
VALUES
    (create_time),
    `modified` =
VALUES
    (modified),
    `num` =
VALUES
    (num),
    `is_deleted` =
VALUES
    (is_deleted),
    `is_online` =
VALUES
    (is_online),
    `update_user` =
VALUES
    (update_user),
    `create_user` =
VALUES
    (create_user)

猜你喜欢

转载自www.cnblogs.com/zouke1220/p/9086710.html