| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- // +----------------------------------------------------------------------
- // | EasyAdmin
- // +----------------------------------------------------------------------
- // | PHP交流群: 763822524
- // +----------------------------------------------------------------------
- // | 开源协议 https://mit-license.org
- // +----------------------------------------------------------------------
- // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
- // +----------------------------------------------------------------------
- namespace app\common\model;
- use app\common\model\ShopGoodsSpecType as Spec;
- use app\common\model\TimeModel;
- use think\facade\Db;
- class ShopGoods extends TimeModel
- {
- protected $name = "shop_goods";
- protected $pk = 'goods_id';
- public function cate ()
- {
- return $this->belongsTo('app\common\model\ShopCategory', 'category', 'id');
- }
- public function supplier ()
- {
- return $this->belongsTo('app\common\model\ShopSupplier', 'supplier', 'id');
- }
- public static function singleSpecGoods ($goods)
- {
- if (isset($goods['goods_id'])){
- $goods_sn = self::where('goods_id', $goods['goods_id'])->value('goods_sn');
- \services\CacheServices::set(md5('goodsDetail_' . $goods_sn), null);
- }
- if (strlen($goods['spec_name']) > 30){
- sr_throw('规格参数太长'.strlen($goods['spec_name']));
- }
- //处理价格及库存
- $default_skus = $goods['default_skus'];
- $goods['goods_sn'] = createdGoodsSn();
- $goods['original_price'] = $goods['min_original_price'] = $goods['max_original_price'] = $default_skus['original_price'];
- $goods['price'] = $goods['min_price'] = $goods['max_price'] = $default_skus['price'];
- $goods['cost_price'] = $goods['min_cost_price'] = $goods['max_cost_price'] = $default_skus['cost_price'];
- $goods['inventory'] = $default_skus['stock'];
- $goods['goods_img_banner'] = explode('|', $goods['goods_img_banner']);
- $goods['goods_img_banner'] = json_encode($goods['goods_img_banner']);
- $goods['is_exist_many_spec'] = 0;
- $goods['rebate_score'] = $goods['rebate_score'];
- // $goods['menu_id'] = $goods['menu_id'];
- $goods['spec_name'] = $goods['spec_name'];
- // $goods['box_pic'] = getWebUrl().'/'.$goods['box_pic'];
- if ($goods['goods_type'] == 1){
- if ($goods['rebate_score'] <=0){
- sr_throw('返利积分积分不能为0');
- }
- }
- if ($goods['goods_type'] == 2){
- if (!in_array($goods['box_type'] , [10,20,30,40])){
- sr_throw('福袋类型商品 ,商品类型不能为空');
- }
- if (in_array($goods['box_type'], [20, 30, 40]) && $goods['price'] < env('boxsetting.ONE_BOX_PRICE')){
- sr_throw('高于普通的盒子类型价格不能低于购买盒子价格');
- }
- }
- // if ($goods['price'] < $goods['rebate_score']){
- // sr_throw('所送积分不能高于价格');
- // }
- if (!empty($goods['hot_keywords'])) {
- $goods['hot_keywords'] = implode(',', $goods['hot_keywords']);
- }
- if (!empty($goods['buynote_template_id'])) {
- $tmp = Db::name('shop_buynote')->where(['id' => $goods['buynote_template_id']])->value('content');
- $goods['buynote_template'] = $tmp;
- }
- if (empty($goods['sales_volume'])) {
- // $goods['sales_volume'] = mt_rand(60, 1000);
- $goods['sales_volume'] = 0;
- }
- // Db::startTrans();
- // try {
- $goodsId = isset($goods['goods_id']) ? $goods['goods_id'] : 0;
- //step:1 插入商品表
- if (!empty($goods['goods_id'])) {
- Db::name('shop_goods')->strict(false)->save($goods);
- } else {
- $goodsId = Db::name('shop_goods')->strict(false)->insertGetId($goods);
- }
- //step:2 插入商品与规格关系表【单规格】
- $specValueArr = ['goods_id' => $goodsId, 'spec_id' => 1, 'spec_name' => $goods['spec_name'], 'spec_value' => json_encode([['id' => 1, 'title' => $goods['spec_name']]], JSON_UNESCAPED_UNICODE)];
- if (!empty($goods['goods_id'])) {
- $spec = Db::name('shop_goods_spec_relation')->where(['goods_id' => $goods['goods_id']])->value('relation_id');
- $specValueArr['relation_id'] = $spec;
- }
- Db::name('shop_goods_spec_relation')->save($specValueArr);
- //step:3 插入商品规格表
- $specSkus = ['goods_id' => $goodsId, 'spec_ids' => 1, 'spec_text' => $goods['spec_name'], 'goods_sn' => Db::name('shop_goods')->where(['goods_id' => $goodsId])->value('goods_sn')];
- $specSkus = array_merge($default_skus, $specSkus);
- if (!empty($goods['goods_id'])) {
- $sku = Db::name('shop_goods_spec')->where(['goods_id' => $goods['goods_id']])->value('goods_spec_id');
- $specSkus['goods_spec_id'] = $sku;
- }
- $specSkus['rebate_score'] = $goods['rebate_score'];
- Db::name('shop_goods_spec')->save($specSkus);
- // Db::commit();
- // return true;
- // } catch (\Exception $e) {
- // Db::rollback();
- // return false;
- // }
- }
- public static function manySpecGoods ($goods)
- {
- $goods['is_exist_many_spec'] = 1;
- $specSkus = $goods['skus'];
- $specData = $goods['spec'];
- if (isset($goods['goods_id'])){
- $goods_sn = self::where('goods_id', $goods['goods_id'])->value('goods_sn');
- \services\CacheServices::set(md5('goodsDetail_' . $goods_sn), null);
- }
- //生成商品编号
- $goods['goods_sn'] = createdGoodsSn();
- //处理原价 按规格集合中-区间,最低,最高
- $mask_original_price = multi_minmax_inarray($specSkus, 'original_price');
- if (count(array_unique($mask_original_price)) == 1) {
- $original_price = $mask_original_price['min'];
- } else {
- $original_price = implode('-', $mask_original_price);
- }
- $goods['original_price'] = $original_price;
- $goods['min_original_price'] = $mask_original_price['min'];
- $goods['max_original_price'] = $mask_original_price['max'];
- $goods['goods_img_banner'] = explode('|', $goods['goods_img_banner']);
- $goods['goods_img_banner'] = json_encode($goods['goods_img_banner']);
- //处理销售价 按规格集合中-区间,最低,最高
- $price = multi_minmax_inarray($specSkus, 'price');
- if (count(array_unique($price)) == 1) {
- $prices = $price['min'];
- } else {
- $prices = implode('-', $price);
- }
- $goods['price'] = $prices;
- $goods['min_price'] = $price['min'];
- $goods['max_price'] = $price['max'];
- $goods['box_pic'] = 'static/img/box/box_img.png';
- //处理成本价 按规格集合中-区间,最低,最高
- $mask_cost_price = multi_minmax_inarray($specSkus, 'cost_price');
- if (count(array_unique($mask_cost_price)) == 1) {
- $cost_price = $mask_cost_price['min'];
- } else {
- $cost_price = implode('-', $mask_cost_price);
- }
- $goods['cost_price'] = $cost_price;
- $goods['min_cost_price'] = $mask_cost_price['min'];
- $goods['max_cost_price'] = $mask_cost_price['max'];
- //处理总库存 按规格集合中统计该商品总库存
- $goods['inventory'] = multi_array_sum($specSkus, 'stock');
- if (!empty($goods['hot_keywords'])) {
- $goods['hot_keywords'] = implode(',', $goods['hot_keywords']);
- }
- if (!empty($goods['buynote_template_id'])) {
- $tmp = Db::name('shop_buynote')->where(['id' => $goods['buynote_template_id']])->value('content');
- $goods['buynote_template'] = $tmp;
- }
- if (empty($goods['sales_volume'])) {
- // $goods['sales_volume'] = mt_rand(60, 1000);
- $goods['sales_volume'] = 0;
- }
- Db::startTrans();
- try {
- $goodsId = isset($goods['goods_id']) ? $goods['goods_id'] : 0;
- //step:1 插入商品表
- if (!empty($goods['goods_id'])) {
- Db::name('shop_goods')->strict(false)->save($goods);
- } else {
- $goodsId = Db::name('shop_goods')->strict(false)->insertGetId($goods);
- }
- $spec = new Spec();
- $specValueArr = [];
- if (!empty($goods['goods_id'])) {
- Db::name('shop_goods_spec_relation')->where(['goods_id' => $goods['goods_id']])->delete();
- Db::name('shop_goods_spec')->where(['goods_id' => $goods['goods_id']])->delete();
- }
- //step:2 插入商品与规格关系表
- foreach ($specData as $k => $v) {
- $key_arr = explode('-', $k);
- $specValueArr[$k]['goods_id'] = $goodsId;
- $specValueArr[$k]['spec_id'] = $key_arr[0];
- $specValueArr[$k]['spec_name'] = $key_arr[1];
- $v_ids = implode(',', $v);
- $v_ids = 'field(id,' . $v_ids . ')';//按whereIn id顺序排序
- $specValueArr[$k]['spec_value'] = json_encode($spec->where(['id' => $v])->field('id,title')->order(Db::raw($v_ids))->select(), JSON_UNESCAPED_UNICODE);
- }
- $specValueArr = array_values($specValueArr);
- Db::name('shop_goods_spec_relation')->insertAll($specValueArr);
- //step:3 插入商品规格表
- foreach ($specSkus as $k => &$v) {
- $specSkusArr = explode('-', $k);
- $specSkus_ids = implode(',', $specSkusArr);
- $specSkus_ids_order = 'field(id,' . $specSkus_ids . ')';//按whereIn id顺序排序
- $v['goods_id'] = $goodsId;
- $v['goods_sn'] = Db::name('shop_goods')->where(['goods_id' => $goodsId])->value('goods_sn');
- $v['spec_ids'] = $k;
- $v['spec_text'] = $spec->where(['id' => $specSkusArr])->order(Db::raw($specSkus_ids_order))->value("GROUP_CONCAT(title order by {$specSkus_ids_order} separator '-')");
- }
- $specSkus = array_values($specSkus);
- Db::name('shop_goods_spec')->insertAll($specSkus);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- return false;
- }
- }
- }
|