'int', 'platform_id' =>'int', 'language_id' =>'int', 'site_id' =>'int', 'name' =>'string', 'main_img' =>'string', 'model_number' =>'string', 'goods_category_id' =>'int', 'price' =>'int', 'is_new' =>'int', 'is_discount' =>'int', 'is_hot' =>'int', 'is_recommend' =>'int', 'details' =>'string', 'title' =>'string', 'keywords' =>'string', 'description' =>'string', 'sort' =>'int', 'status' =>'int', 'create_time' =>'int', 'update_time' =>'int', ]; /** * 商品链接关联模型 */ public function GoodsBuyNow() { return $this->hasMany(GoodsBuyNow::class); } /** * 商品关联商品 */ public function GoodsConnect() { return $this->hasMany(GoodsConnect::class); } /** * 商品手册关联模型 * @return \think\model\relation\HasMany */ public function GoodsSoftManual() { return $this->hasMany(GoodsSoftManual::class); } /** * 商品主图关联模型 * @return \think\model\relation\HasMany */ public function GoodsImages() { return $this->hasMany(GoodsImages::class); } /** * 商品属性关联 * @return \think\model\relation\HasMany */ public function goodsAttr() { return $this->hasMany(GoodsAttribute::class); } /** * 获取列表 * @param array $data 查询条件 * @param mixed $simple 模糊搜索条件 * @param mixed $focus 精准搜索条件 * @param null $sort * @param null $field * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getAllList(array $data,$simple = null,$focus = null,$sort = null,$field = null):array { $fields = array_keys($this->schema); $collection = $this->field($field)->where(function ($query) use ($data,$fields,$simple,$focus) { if (in_array('platform_id',$fields)) { $query->where('platform_id',$data['platform_id']); } //模糊查询条件 if (!empty($simple)) { foreach ($simple as $key => $item) { if (in_array($key,$fields)) { $query->where($key,'like',"%".$item."%"); } } } //精准查询 if (!empty($focus)) { foreach ($focus as $key => $item) { if (in_array($key,$fields)) { $query->where($key,$item); } } } //分类查询 if (!empty($data['goods_category_id']) && is_array($data['goods_category_id'])) { $query->whereIn('goods_category_id',implode(',',$data['goods_category_id'])); } }); //先筛选再分 if (isset($data['page']) && isset($data['size'])) { $count = $collection->order($sort)->select()->count(); $page = $collection->order($sort)->limit((int)$data['size'])->page((int)$data['page'])->select()->toArray(); return ['page'=>array_values($page),'count'=>$count]; } else { $resData = $collection->order($sort)->select()->toArray(); return array_values($resData); } } /** * 新增数据 * @param array $data * @return bool */ public function addData(array $data):bool { //开启事务 $this->startTrans(); //增加主图 if (isset($data['goods_images'][0]['url'])) { $data['main_img'] = doImageURLtoRelativePath($data['goods_images'][0]['url']); } else { //TODO::没有上传数据则使用空,后面可以替换为默认图片 $data['main_img'] = ''; } $goods = $this->create($data); //处理数据结构 $newBuyNow = []; foreach ($data['buy_now'] as $key => $value) { if ($value['name'] != '' && $value['url'] != '') { $newBuyNow[$key]['goods_id'] = $goods->id; $newBuyNow[$key]['name'] = $value['name']; $newBuyNow[$key]['url'] = $value['url']; } } $data['buy_now'] = $newBuyNow; if (!empty($data['buy_now'])) { $result1 = $goods->GoodsBuyNow()->saveAll($data['buy_now']); } else { unset($data['buy_now']); $result1 = true; } $newGoodsImages = []; foreach ($data['goods_images'] as $key => $value) { if ($key < 8) { $newGoodsImages[$key]['goods_id'] = $goods->id; $newGoodsImages[$key]['image'] = doImageURLtoRelativePath($value['url']); } } $data['goods_images'] = $newGoodsImages; if (!empty($data['goods_images'])) { $result2 = $goods->GoodsImages()->saveAll($data['goods_images']); } else { unset($data['goods_images']); $result2 = true; } $newConnectGoods = []; foreach ($data['connect_goods'] as $key => $value) { if ($value['connect_good_id'] != '' && $value['goods_category_id'] != '') { $newConnectGoods[$key]['goods_id'] = $goods->id; $newConnectGoods[$key]['connect_good_id'] = $value['connect_good_id']; $newConnectGoods[$key]['goods_category_id'] = $value['goods_category_id']; } } $data['connect_goods'] = $newConnectGoods; if (!empty($data['connect_goods'])) { $result3 = $goods->GoodsConnect()->saveAll($data['connect_goods']); } else { unset($data['connect_goods']); $result3 = true; } //处理驱动文档数据 $newSoft = []; foreach ($data['soft_manual'] as $key => $value) { if ($value['file_type_id'] != '' && $value['soft_manual_id'] != '') { $newSoft[$key]['goods_id'] = $goods->id; $newSoft[$key]['file_type_id'] = $value['file_type_id']; $newSoft[$key]['soft_manual_id'] = $value['soft_manual_id']; } } $data['soft_manual'] = $newSoft; if (!empty($data['soft_manual'])) { $result4 = $goods->GoodsSoftManual()->saveAll($data['soft_manual']); } else { unset($data['soft_manual']); $result4 = true; } //处理属性attr $newAttr = []; foreach ($data['attr'] as $key => $item) { if ($item['attribute_id'] != '' && $item['value'] != '') { $newAttr[$key]['goods_id'] = $goods->id; $newAttr[$key]['attribute_id'] = $item['attribute_id']; $newAttr[$key]['value'] = $item['value']; } } $data['attr'] = $newAttr; if (!empty($data['attr'])) { $result5 = $goods->goodsAttr()->saveAll($data['attr']); } else { unset($data['attr']); $result5 = true; } //模型关联新增 if($result1 && $result2 && $result3 && $result4 && $result5) { $this->commit(); return true; } else { $this->rollback(); return false; } } /** * 获取详情 * @param array $data * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getInfo(array $data):array { //初始化默认结构 $defaultBuyNow = [['name' => '','url' => '']]; $defaultSoftManual = [['file_type_id'=>'','soft_manual_id'=>'']]; $defaultAttr = [['id'=>'','value'=>'']]; $defaultGoodsImages = [['name'=>'','url'=>'']]; $defaultConnectGoods = [ ['connect_good_id'=>'','goods_category_id'=>''], ['connect_good_id'=>'','goods_category_id'=>''], ['connect_good_id'=>'','goods_category_id'=>''], ['connect_good_id'=>'','goods_category_id'=>''], ]; $goods = $this->find($data['id']); $goods['buy_now'] = $this->GoodsBuyNow()->where('goods_id',$data['id'])->select()->toArray(); $goods['goods_images'] = $this->GoodsImages()->where('goods_id',$data['id'])->select()->toArray(); $goods['connect_goods'] = $this->GoodsConnect()->where('goods_id',$data['id'])->select()->toArray(); $goods['soft_manual'] = $this->GoodsSoftManual()->where('goods_id',$data['id'])->select()->toArray(); $goods['attr'] = $this->goodsAttr()->where('goods_id',$data['id'])->select()->toArray(); //处理buynow if (!empty($goods['buy_now'])) { $newBuyNow = []; foreach ($goods['buy_now'] as $key => $item) { $newBuyNow[$key]['name'] = $item['name']; $newBuyNow[$key]['url'] = $item['url']; } $goods['buy_now'] = $newBuyNow; } else { $goods['buy_now'] = $defaultBuyNow; } //处理属性attr if (!empty($goods['attr'])) { $newAttr = []; foreach ($goods['attr'] as $key => $item) { $newAttr[$key]['attribute_id'] = $item['attribute_id']; $newAttr[$key]['value'] = $item['value']; } $goods['attr'] = $newAttr; } else { $goods['attr'] = $defaultAttr; } //处理图片 if (!empty($goods['goods_images'])) { $newGoodsImages = []; foreach ($goods['goods_images'] as $key => $item) { if ($key < 8) { $newGoodsImages[$key]['id'] = $item['id']; $newGoodsImages[$key]['name'] = basename($item['image']); $newGoodsImages[$key]['url'] = $item['image']; } } $goods['goods_images'] = $newGoodsImages; } else { $goods['goods_images'] = $defaultGoodsImages; } //处理关联产品数据 if (!empty($goods['connect_goods'])) { $newConnectGoods = []; foreach ($goods['connect_goods'] as $key => $item) { $newConnectGoods[$key]['id'] = $item['id']; $newConnectGoods[$key]['connect_good_id'] = $item['connect_good_id']; $newConnectGoods[$key]['type'] = $key; $newConnectGoods[$key]['goods_category_id'] = $item['goods_category_id']; } if (count($newConnectGoods) < 4 ) { $arr = [['connect_good_id'=>'','goods_category_id'=>'']]; switch (count($newConnectGoods)) { case 1: $newConnectGoods = array_merge(array_merge(array_merge($newConnectGoods,$arr),$arr),$arr); break; case 2: $newConnectGoods = array_merge(array_merge($newConnectGoods,$arr),$arr); break; case 3: $newConnectGoods = array_merge($newConnectGoods,$arr); break; } } $goods['connect_goods'] = $newConnectGoods; } else { $goods['connect_goods'] = $defaultConnectGoods; } //处理下载驱动手册 if (!empty($goods['soft_manual'])) { $newSoft = []; foreach ($goods['soft_manual'] as $key => $good) { $newSoft[$key]['file_type_id'] = $good['file_type_id']; $newSoft[$key]['soft_manual_id'] = $good['soft_manual_id']; $newSoft[$key]['list'] = SoftManual::where('file_type_id',$good['file_type_id'])->select()->toArray(); } $goods['soft_manual'] = $newSoft; } else { $goods['soft_manual'] = $defaultSoftManual; } return $goods->toArray(); } /** * 编辑数据 * @param $data * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function editData(array $data):bool { $this->startTrans(); $goods = $this->find($data['id']); //如何编辑商品修改了分类数据,那么需要把相关产品对应的分类一同删除 if ($goods['goods_category_id'] != $data['goods_category_id']) { $connect = GoodsConnect::where(['goods_id'=>$data['id'],'goods_category_id'=>$goods['goods_category_id']])->column(['id']); if (!empty($connect)) { GoodsConnect::destroy($connect); } } // dd($goods['goods_category_id'] , $data['goods_category_id']); //增加主图 $data['main_img'] = doImageURLtoRelativePath($data['goods_images'][0]['url']); $result1 = $goods->save($data); //先将原先的全部删除 $result2 = $goods->GoodsBuyNow->delete(); $result3 = $goods->GoodsImages->delete(); $result4 = $goods->GoodsConnect->delete(); $result5 = $goods->GoodsSoftManual->delete(); $result6 = $goods->goodsAttr->delete(); $result = false; if ($result1 && $result2 && $result3 && $result4 && $result5 && $result6) { //处理数据结构 $newBuyNow = []; foreach ($data['buy_now'] as $key => $value) { if ($value['name'] != '' && $value != '') { $newBuyNow[$key]['goods_id'] = $data['id']; $newBuyNow[$key]['name'] = $value['name']; $newBuyNow[$key]['url'] = $value['url']; } } $data['buy_now'] = $newBuyNow; if (!empty($data['buy_now'])) { $result11 = $goods->GoodsBuyNow()->saveAll($data['buy_now']); } else { $result11 = true; } $newGoodsImages = []; foreach ($data['goods_images'] as $key => $value) { if ($key < 8) { $newGoodsImages[$key]['goods_id'] = $data['id']; $newGoodsImages[$key]['image'] = doImageURLtoRelativePath($value['url']); } } $data['goods_images'] = $newGoodsImages; if (!empty($data['goods_images'])) { $result22 = $goods->GoodsImages()->saveAll($data['goods_images']); } else { $result22 = true; } $newConnectGoods = []; foreach ($data['connect_goods'] as $key => $value) { if ($value['goods_category_id'] != '' && $value['connect_good_id'] != '') { $newConnectGoods[$key]['goods_id'] = $data['id']; $newConnectGoods[$key]['connect_good_id'] = $value['connect_good_id']; $newConnectGoods[$key]['goods_category_id'] = $value['goods_category_id']; } } $data['connect_goods'] = $newConnectGoods; if (!empty($data['connect_goods'])) { $result33 = $goods->GoodsConnect()->saveAll($data['connect_goods']); } else { $result33 = true; } //处理驱动文档数据 $newSoft = []; foreach ($data['soft_manual'] as $key => $value) { if ($value['file_type_id'] != '' && $value['soft_manual_id'] != '') { $newSoft[$key]['goods_id'] = $data['id']; $newSoft[$key]['file_type_id'] = $value['file_type_id']; $newSoft[$key]['soft_manual_id'] = $value['soft_manual_id']; } } $data['soft_manual'] = $newSoft; if (!empty($data['soft_manual'])) { $result44 = $goods->GoodsSoftManual()->saveAll($data['soft_manual']); } else { $result44 = true; } //处理属性attr $newAttr = []; foreach ($data['attr'] as $key => $item) { if ($item['attribute_id'] != '' && $item['value'] != '') { $newAttr[$key]['goods_id'] = $goods->id; $newAttr[$key]['attribute_id'] = $item['attribute_id']; $newAttr[$key]['value'] = $item['value']; } } $data['attr'] = $newAttr; if (!empty($data['attr'])) { $result55 = $goods->goodsAttr()->saveAll($data['attr']); } else { $result55 = true; } $result = $result11 && $result22 && $result33 && $result44 && $result55; } if ($result) { $this->commit(); return true; } else { $this->rollback(); return false; } } /** * 删除数据 * @param array $data * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function delData(array $data):bool { $goods = $this->find($data['id']); $result1 = $goods->delete(); //先将原先的全部删除 $result2 = $goods->GoodsBuyNow->delete(); $result3 = $goods->GoodsImages->delete(); $result4 = $goods->GoodsConnect->delete(); $result5 = $goods->GoodsSoftManual->delete(); $result6 = $goods->goodsAttr->delete(); return $result1 && $result2 && $result3 && $result4 && $result5 && $result6; } /** * 获取平台语言包列表 * @param $data * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getPlatformLanguage($data) { $result = (new Platform())->getPlatformLanguage($data); if ($result) { foreach ($result as $key => $item) { $result[$key]['language_name'] = $this->getLanguageName($item['site_id']); $result[$key]['coin_name'] = $this->getCoinName($item['currency_id']); foreach ($item['PlatformLanguage'] as $key1 => $item1) { $result[$key]['PlatformLanguage'][$key1]['language_name'] = $this->getLanguageName($item1['site_id']); } foreach ($item['PlatformCoin'] as $key1 => $item1) { $result[$key]['PlatformCoin'][$key1]['coin_name'] = $this->getCoinName($item1['currency_id']); } } return $result->toArray(); } return []; } /** * 通过分类ID获取商品列表 * @param $data * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getGoodsInfoByGoodsCategoryId($data) { return $this->where(['goods_category_id'=>$data['goods_category_id'],'status'=>0])->select()->toArray(); } /** * 批量删除数据 * @param array $data * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function batchDelData(array $data):bool { $newResult = true; foreach ($data as $item) { $result = $this->delData($item); $newResult = $newResult && $result; } return $newResult; } /** * 批量移除商品分类 * @param $data * @return bool */ public function batchRemoveOldCate($data) { return GoodsCate::whereIn('goods_id',implode(',',$data))->delete(); } /** * @param $data * @return bool|\think\Collection * @throws \Exception */ public function goodsBatchEditCategory($data) { $this->startTrans(); try { //修改分类时,先查询当前分类下的分类数据 foreach ($data as $datum) { $goods = $this->find($datum['id']); if ($goods['goods_category_id'] != $datum['goods_category_id']) { $connect = GoodsConnect::where(['goods_id'=>$datum['id'],'goods_category_id'=>$goods['goods_category_id']])->column('id'); if (!empty($connect)) { GoodsConnect::destroy($connect); } } } $result = $this->saveAll($data); if ($result) { $this->commit(); return true; } else { $this->rollback(); return false; } }catch (\Exception $e) { $this->rollback(); return false; } } /** * 获取分类及其子分类 * @param $data * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getAllSonCategory($data) { $newData = (new GoodsCategory())->getAllSonCategory($data); array_push($newData,$data['id']); return $newData; } }