123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace app\index\service;
- use app\index\model\BuyCarModel;
- use app\index\model\ProductAttrModel;
- use app\index\model\ProductAttrValueModel;
- use app\index\model\ProductImgModel;
- use app\index\model\ProductModel;
- use app\index\model\ProductCategoryModel;
- use app\index\model\ProductOrderDetailModel;
- use app\index\model\RelProductAttrModel;
- use think\Db;
- class ProductService extends BaseService
- {
- /**
- * @param $data
- * @return mixed
- * 修改产品
- */
- public function upProductInfo($data)
- {
- $where['product_id'] = $data['product_id'];
- $ProductModel = new ProductModel();
- $ProductModel->upInfo($where,$data);
- return $this->return_code[200];
- }
- /**
- * @param $data
- * @return mixed
- * 添加产品
- */
- public function addProduct($data){
- $product_value_id = $data['product_value_id'];
- unset($data['product_value_id']);
- $db_logistic = Db::connect('db_mall');
- $db_logistic->startTrans();
- $ProductModel = new ProductModel();
- $id = $ProductModel->addInfo($data);
- if(!$id){
- $db_logistic->rollback();
- return $this->return_code[40001];
- }
- $arr = [];
- foreach($product_value_id as $k=>$v){
- $arr[$k]['product_id'] = $id;
- $arr[$k]['product_value_id'] = $v;
- }
- $RelProductAttrModel = new RelProductAttrModel();
- $rows = $RelProductAttrModel->addAllInfo($arr);
- if(!$rows){
- $db_logistic->rollback();
- return $this->return_code[40002];
- }
- $db_logistic->commit();
- return $this->return_code[200];
- }
- public function addProductCategory($data){
- $ProductCategoryModel = new ProductCategoryModel();
- $id = $ProductCategoryModel->addInfo($data);
- return $id;
- }
- public function upProductCategory($data){
- $where['category_id'] = $data['category_id'];
- $ProductCategoryModel = new ProductCategoryModel();
- $res = $ProductCategoryModel->upInfo($where,$data);
- return $res;
- }
- public function addProductAttr($data){
- $ProductAttrModel = new ProductAttrModel();
- $ProductAttrModel->addInfo($data);
- return $this->return_code[200];
- }
- public function upProductAttr($data){
- $where['attr_id'] = $data['attr_id'];
- $ProductTypeModel = new ProductAttrModel();
- $ProductTypeModel->upInfo($where,$data);
- return $this->return_code[200];
- }
- //添加产品属性值
- public function addProductAttrValue($data){
- $arr = [];
- foreach($data['value_name'] as $k=>$v){
- $arr[$k]['attr_id'] = $data['attr_id'];
- $arr[$k]['value_name'] = $v;
- }
- $ProductAttrValueModel = new ProductAttrValueModel();
- $ProductAttrValueModel->addAllInfo($arr);
- return $this->return_code[200];
- }
- //修改产品属性值
- public function upProductAttrValue($data){
- $where['product_value_id'] = $data['product_value_id'];
- $ProductTypeValueModel = new ProductAttrValueModel();
- $ProductTypeValueModel->upInfo($where,$data);
- return $this->return_code[200];
- }
- //添加图片
- public function addProductImg($data){
- $arr = [];
- foreach($data['img'] as $k=>$v){
- $arr[$k]['product_id'] = $data['product_id'];
- $arr[$k]['img'] = $v;
- }
- $ProductImgModel = new ProductImgModel();
- $ProductImgModel->addAllInfo($arr);
- return $this->return_code[200];
- }
- //修改图片
- public function upProductImg($data){
- $where['id'] = $data['id'];
- $ProductImgModel = new ProductImgModel();
- $ProductImgModel->upInfo($where,$data);
- return $this->return_code[200];
- }
- /**
- * 获取分类通过条件
- * @param $condition
- * @return array|false|\PDOStatement|string|\think\Model
- */
- public function getProductCategoryByCondition($condition){
- $productCategory = new ProductCategoryModel();
- return $productCategory->where($condition)->find();
- }
- /**
- * 获取购物车里面的产品数量和价格
- * @param $order_no
- * @param $productIds
- * @param $user_id
- * @return int
- */
- public function getOrderPriceByProductIds($order_no,$productIds,$user_id){
- $buyCarModel = new BuyCarModel();
- $orderDetailModel = new ProductOrderDetailModel();
- $buyCar = $buyCarModel->getCarAndProductInfo(['buy_car.product_id'=>['in',$productIds],'user_id'=>$user_id]);
- $totalPrice = 0;
- foreach($buyCar as $buyItem){
- $totalPrice += $buyItem['num']*$buyItem['sales_price'];
- $orderDetailModel->insertGetId([
- 'order_no'=>$order_no,
- 'product_id'=>$buyItem['product_id'],
- 'price'=>$buyItem['sales_price'],
- 'num'=>$buyItem['num'],
- ]);
- $buyCarModel->where(['id'=>$buyItem['buy_car_id']])->delete();
- }
- return $totalPrice;
- }
- /**
- * 减少商品的库存
- * @param $product_id
- * @param $num
- * @return bool
- */
- public function reduceStock($product_id,$num){
- $productModel = new ProductModel();
- $oneProduct = $productModel->getOne(['product_id'=>$product_id]);
- if(empty($oneProduct) || $oneProduct['remain_num'] < $num){
- HelperService::returnJson(['code'=>'400','msg'=>"product($product_id) remain num is less $num"]);
- }
- return $productModel->upInfo([
- 'product_id'=>$product_id,
- 'remain_num'=>$oneProduct['remain_num']
- ],['remain_num'=>$oneProduct['remain_num'] - $num]);
- }
- }
|