コード例 #1
0
ファイル: OrderModel.class.php プロジェクト: hcpzhe/foodorder
 public function myAdd($data)
 {
     /**
      * $data['goods'] = array([商品id]=>购买数量,...);
      */
     $goods = $data['goods'];
     unset($data['goods']);
     if (!is_array($goods) || empty($goods)) {
         $this->error = '新增失败,订单内必须有商品';
         return false;
     }
     // 计算合计总金额 amount
     $goods_ids = array_keys($goods);
     $goods_M = new Model('Goods');
     $goods_list = $goods_M->where(array('goods_id' => array('in', $goods_ids)))->getField('id,cate_id,goods_name,image,price');
     $data['amount'] = 0;
     $newgoods = array();
     foreach ($goods_list as $row) {
         $tmparr = array();
         $tmparr['id'] = $row['id'];
         $tmparr['num'] = (int) $goods[$row['id']];
         if ($tmparr['num'] > 0) {
             $tmparr['price'] = $row['price'];
             $tmparr['amount'] = $tmparr['price'] * $tmparr['num'];
             $data['amount'] += $tmparr['amount'];
             $newgoods[] = $tmparr;
         }
     }
     if (empty($newgoods)) {
         $this->error = '新增失败,商品数量必须存在';
         return false;
     }
     /********数据验证*******************************/
     if (preg_match('/^\\w+$/', $data['order_sn'])) {
         //如果传入的参数有订单编号
         $tmp_auto = $this->_auto;
         unset($tmp_auto[0]);
         //除去自动填充订单编号
         $this->auto($tmp_auto);
     }
     if (false === $this->create($data, self::MODEL_INSERT)) {
         return false;
     }
     // store_id 合法性
     $store_M = new Model('Store');
     $store = $store_M->where("`id`='" . $this->store_id . "'")->find();
     if (false === $store || empty($store)) {
         $this->error = '新增失败,店铺不存在';
         return false;
     }
     // member_id 合法性
     $member_M = new Model('Member');
     $member = $member_M->where("`id`='" . $this->member_id . "'")->find();
     if (false === $member || empty($member)) {
         $this->error = '新增失败,购买用户不存在';
         return false;
     }
     // payment_id 合法性
     $payment_M = new Model('Payment');
     $payment = $payment_M->where("`id`='" . $this->payment_id . "'")->find();
     if (false === $payment || empty($payment)) {
         $this->error = '新增失败,支付方式不存在';
         return false;
     }
     /*************************************************/
     $this->startTrans();
     $newid = $this->add();
     if (!$newid) {
         $this->error = '新增失败,数据库错误';
         $this->rollback();
         return false;
     }
     $og_M = new OrderGoodsModel();
     $og_flag = 0;
     foreach ($newgoods as $row) {
         if (!empty($goods_list[$row['id']])) {
             //商品必须存在
             $order_goods_data = array('order_id' => $newid, 'goods_id' => $row['id'], 'quantity' => $row['num'], 'price' => $row['price'], 'amount' => $row['amount']);
             if (false === $og_M->myAdd($order_goods_data)) {
                 continue;
             }
             $og_flag++;
         }
     }
     if ($og_flag <= 0) {
         $this->error = '新增失败,订单内没有合法的商品';
         $this->rollback();
         return false;
     }
     $this->commit();
     return $newid;
 }