| | |
| | | |
| | | use app\api\model\plus\seckill\Active as ActiveModel; |
| | | use app\common\model\product\Product as ProductModel; |
| | | use app\common\model\settings\Setting; |
| | | use app\common\service\product\BaseProductService; |
| | | use app\common\library\helper; |
| | | use app\api\model\supplier\Supplier as SupplierModel; |
| | |
| | | use app\common\model\supplier\User as SupplierUserModel; |
| | | use app\api\model\user\CardRecord as CardRecordModel; // 会员卡 by lyzflash |
| | | use app\common\model\plus\advance\Product as AdvanceProductModel; |
| | | use app\supplier\model\product\ProductSku; |
| | | |
| | | /** |
| | | * 商品模型 |
| | |
| | | { |
| | | return $this->save(['product_status' => $data['product_status']]); |
| | | } |
| | | |
| | | public function add($data) |
| | | { |
| | | if (!isset($data['image']) || empty($data['image'])) { |
| | | $this->error = '请上传商品图片'; |
| | | return false; |
| | | } |
| | | //如果支持核销 by yj |
| | | if ($data['is_virtual'] && $data['is_verify']) { |
| | | if ($data['verify_type'] == 20) { |
| | | if (empty($data['verify_time'])) { |
| | | $this->error = '请选择核销有效时间'; |
| | | return false; |
| | | } |
| | | $data["verify_start_time"] = strtotime(array_shift($data['verify_time'])); |
| | | $data["verify_end_time"] = strtotime(array_pop($data['verify_time'])); |
| | | } |
| | | if ($data['verify_store_ids']) { |
| | | $data['verify_store_ids'] = implode(',', array_unique($data['verify_store_ids'])); |
| | | } |
| | | } |
| | | $data['content'] = isset($data['content']) ? $data['content'] : ''; |
| | | $data['app_id'] = $data['sku']['app_id'] = self::$app_id; |
| | | |
| | | $this->processContent($data); |
| | | // 开启事务 |
| | | $this->startTrans(); |
| | | try { |
| | | // 添加商品 |
| | | $this->save($data); |
| | | // 商品规格 |
| | | $this->addProductSpec($data); |
| | | // 商品图片 |
| | | $this->addProductImages($data['image'], $data['shop_supplier_id']); |
| | | // 商品详情图片 |
| | | if($data['is_picture'] == 1){ |
| | | $this->addProductContentImages($data['contentImage']); |
| | | } |
| | | $this->commit(); |
| | | return true; |
| | | } catch (\Exception $e) { |
| | | $this->error = $e->getMessage(); |
| | | $this->rollback(); |
| | | return false; |
| | | } |
| | | } |
| | | /** |
| | | * 处理内容 |
| | | */ |
| | | private function processContent(&$data) |
| | | { |
| | | $pattern = "/src=[\"\'](.*?)[\"\']/is"; |
| | | preg_match_all($pattern, $data['content'], $match); |
| | | } |
| | | /** |
| | | * 添加商品图片 |
| | | */ |
| | | private function addProductImages($images, $shop_supplier_id) |
| | | { |
| | | $this->image()->delete(); |
| | | $data = array_map(function ($images) use ($shop_supplier_id) { |
| | | isset($images['file_id']) && $image_id = $images['file_id']; |
| | | isset($images['image_id']) && $image_id = $images['image_id']; |
| | | if(!isset($images['file_id']) && !isset($images['image_id'])){ |
| | | $image_id = $images['file_path']; |
| | | } |
| | | return [ |
| | | 'image_id' => $image_id, |
| | | 'app_id' => self::$app_id |
| | | ]; |
| | | }, $images); |
| | | return $this->image()->saveAll($data); |
| | | } |
| | | |
| | | /** |
| | | * 添加商品详情图片 |
| | | */ |
| | | private function addProductContentImages($images) |
| | | { |
| | | $this->contentImage()->delete(); |
| | | $data = array_map(function ($images) { |
| | | return [ |
| | | 'image_id' => isset($images['file_id']) ? $images['file_id'] : $images['image_id'], |
| | | 'image_type' => 1, |
| | | 'app_id' => self::$app_id |
| | | ]; |
| | | }, $images); |
| | | return $this->contentImage()->saveAll($data); |
| | | } |
| | | |
| | | /** |
| | | * 编辑商品 |
| | | */ |
| | | public function edit($data,$shop_supplier_id) |
| | | { |
| | | if (!isset($data['image']) || empty($data['image'])) { |
| | | $this->error = '请上传商品图片'; |
| | | return false; |
| | | } |
| | | //如果支持核销 by yj |
| | | if ($data['is_verify']) { |
| | | if ($data['verify_type'] == 20) { |
| | | if (empty($data['verify_time'])) { |
| | | $this->error = '请选择核销有效时间'; |
| | | return false; |
| | | } |
| | | $data["verify_start_time"] = strtotime(array_shift($data['verify_time'])); |
| | | $data["verify_end_time"] = strtotime(array_pop($data['verify_time'])); |
| | | } |
| | | if ($data['verify_store_ids']) { |
| | | $data['verify_store_ids'] = implode(',', array_unique($data['verify_store_ids'])); |
| | | } |
| | | } |
| | | $data['spec_type'] = isset($data['spec_type']) ? $data['spec_type'] : $this['spec_type']; |
| | | $data['content'] = isset($data['content']) ? $data['content'] : ''; |
| | | $data['app_id'] = $data['sku']['app_id'] = self::$app_id; |
| | | $productSkuIdList = helper::getArrayColumn(($this['sku']), 'product_sku_id'); |
| | | return $this->transaction(function () use ($data, $productSkuIdList) { |
| | | // 商品状态,如果已审核过的,看平台配置是否需要再次审核 |
| | | $edit_audit = Setting::getItem('store')['edit_audit']; |
| | | if($edit_audit && $data['audit_status'] == 0){ |
| | | $data['audit_status'] = 0; |
| | | } |
| | | // 保存商品 |
| | | $this->save($data); |
| | | // 商品规格 |
| | | $this->addProductSpec($data, true, $productSkuIdList); |
| | | // 商品图片 |
| | | $this->addProductImages($data['image'], $this['shop_supplier_id']); |
| | | // 商品详情图片 |
| | | if($data['is_picture'] == 1){ |
| | | $this->addProductContentImages($data['contentImage']); |
| | | } |
| | | return true; |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 添加商品规格 |
| | | */ |
| | | private function addProductSpec($data, $isUpdate = false, $productSkuIdList = []) |
| | | { |
| | | // 更新模式: 先删除所有规格 |
| | | $model = new ProductSku; |
| | | $isUpdate && $model->removeAll($this['product_id']); |
| | | $stock = 0;//总库存 |
| | | $product_price = 0; |
| | | $line_price = 0; |
| | | // 商城设置 |
| | | $settings = Setting::getItem('store'); |
| | | // 添加规格数据 |
| | | if ($data['spec_type'] == '10') { |
| | | // 删除多规格遗留数据 |
| | | $isUpdate && $model->removeSkuBySpec($this['product_id']); |
| | | // 单规格 |
| | | $this->sku()->save($data['sku']); |
| | | $stock = $data['sku']['stock_num']; |
| | | $product_price = $data['sku']['product_price']; |
| | | $line_price = $data['sku']['line_price']; |
| | | } else if ($data['spec_type'] == '20') { |
| | | // 添加商品与规格关系记录 |
| | | $model->addProductSpecRel($this['product_id'], $data['spec_many']['spec_attr']); |
| | | // 添加商品sku |
| | | $model->addSkuList($this['product_id'], $data['spec_many']['spec_list'], $productSkuIdList); |
| | | $product_price = $data['spec_many']['spec_list'][0]['spec_form']['product_price']; |
| | | foreach ($data['spec_many']['spec_list'] as &$item) { |
| | | $stock += $item['spec_form']['stock_num']; |
| | | if($item['spec_form']['product_price'] < $product_price){ |
| | | $product_price = $item['spec_form']['product_price']; |
| | | } |
| | | if($item['spec_form']['line_price'] < $line_price){ |
| | | $line_price = $item['spec_form']['line_price']; |
| | | } |
| | | } |
| | | } |
| | | $save_data = [ |
| | | 'product_stock' => $stock, |
| | | 'product_price' => $product_price, |
| | | 'line_price' => $line_price |
| | | ]; |
| | | // 商品价格 |
| | | $save_data['product_price'] = $product_price; |
| | | $this->save($save_data); |
| | | } |
| | | } |