public function addToGoods() { $id = array_unique((array) I('id', 0)); $id = is_array($id) ? implode(',', $id) : $id; if (empty($id)) { $this->error('请选择要操作的数据!'); } $where['id'] = array('in', $id); $CollectGoodsModel = new CollectGoodsModel(); $info = $this->lists('CollectGoods', $where); $GoodsModel = new GoodsModel(); $add = 0; $err = 0; $exist = 0; foreach ($info as $row) { if ($GoodsModel->create($row)) { $res = $GoodsModel->addGoods(); if ($res > 0) { $where = array('id' => $row['id']); $CollectGoodsModel->where($where)->delete(); $add++; } elseif ($res == -2) { $exist++; } else { $err++; } } else { $err++; } } if ($err > 0) { $this->error($err . '件商品上架失败,' . $add . '件商品上架成功', U('Collect/collect')); } else { $this->success($add . '件商品上架成功,' . $exist . '件重复商品未添加', U('Collect/index')); } }