ShopGoods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model;
  12. use app\common\model\ShopGoodsSpecType as Spec;
  13. use app\common\model\TimeModel;
  14. use think\facade\Db;
  15. class ShopGoods extends TimeModel
  16. {
  17. protected $name = "shop_goods";
  18. protected $pk = 'goods_id';
  19. public function cate ()
  20. {
  21. return $this->belongsTo('app\common\model\ShopCategory', 'category', 'id');
  22. }
  23. public function supplier ()
  24. {
  25. return $this->belongsTo('app\common\model\ShopSupplier', 'supplier', 'id');
  26. }
  27. public static function singleSpecGoods ($goods)
  28. {
  29. if (isset($goods['goods_id'])){
  30. $goods_sn = self::where('goods_id', $goods['goods_id'])->value('goods_sn');
  31. \services\CacheServices::set(md5('goodsDetail_' . $goods_sn), null);
  32. }
  33. if (strlen($goods['spec_name']) > 30){
  34. sr_throw('规格参数太长'.strlen($goods['spec_name']));
  35. }
  36. //处理价格及库存
  37. $default_skus = $goods['default_skus'];
  38. $goods['goods_sn'] = createdGoodsSn();
  39. $goods['original_price'] = $goods['min_original_price'] = $goods['max_original_price'] = $default_skus['original_price'];
  40. $goods['price'] = $goods['min_price'] = $goods['max_price'] = $default_skus['price'];
  41. $goods['cost_price'] = $goods['min_cost_price'] = $goods['max_cost_price'] = $default_skus['cost_price'];
  42. $goods['inventory'] = $default_skus['stock'];
  43. $goods['goods_img_banner'] = explode('|', $goods['goods_img_banner']);
  44. $goods['goods_img_banner'] = json_encode($goods['goods_img_banner']);
  45. $goods['is_exist_many_spec'] = 0;
  46. $goods['rebate_score'] = $goods['rebate_score'];
  47. // $goods['menu_id'] = $goods['menu_id'];
  48. $goods['spec_name'] = $goods['spec_name'];
  49. // $goods['box_pic'] = getWebUrl().'/'.$goods['box_pic'];
  50. if ($goods['goods_type'] == 1){
  51. if ($goods['rebate_score'] <=0){
  52. sr_throw('返利积分积分不能为0');
  53. }
  54. }
  55. if ($goods['goods_type'] == 2){
  56. if (!in_array($goods['box_type'] , [10,20,30,40])){
  57. sr_throw('福袋类型商品 ,商品类型不能为空');
  58. }
  59. if (in_array($goods['box_type'], [20, 30, 40]) && $goods['price'] < env('boxsetting.ONE_BOX_PRICE')){
  60. sr_throw('高于普通的盒子类型价格不能低于购买盒子价格');
  61. }
  62. }
  63. // if ($goods['price'] < $goods['rebate_score']){
  64. // sr_throw('所送积分不能高于价格');
  65. // }
  66. if (!empty($goods['hot_keywords'])) {
  67. $goods['hot_keywords'] = implode(',', $goods['hot_keywords']);
  68. }
  69. if (!empty($goods['buynote_template_id'])) {
  70. $tmp = Db::name('shop_buynote')->where(['id' => $goods['buynote_template_id']])->value('content');
  71. $goods['buynote_template'] = $tmp;
  72. }
  73. if (empty($goods['sales_volume'])) {
  74. // $goods['sales_volume'] = mt_rand(60, 1000);
  75. $goods['sales_volume'] = 0;
  76. }
  77. // Db::startTrans();
  78. // try {
  79. $goodsId = isset($goods['goods_id']) ? $goods['goods_id'] : 0;
  80. //step:1 插入商品表
  81. if (!empty($goods['goods_id'])) {
  82. Db::name('shop_goods')->strict(false)->save($goods);
  83. } else {
  84. $goodsId = Db::name('shop_goods')->strict(false)->insertGetId($goods);
  85. }
  86. //step:2 插入商品与规格关系表【单规格】
  87. $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)];
  88. if (!empty($goods['goods_id'])) {
  89. $spec = Db::name('shop_goods_spec_relation')->where(['goods_id' => $goods['goods_id']])->value('relation_id');
  90. $specValueArr['relation_id'] = $spec;
  91. }
  92. Db::name('shop_goods_spec_relation')->save($specValueArr);
  93. //step:3 插入商品规格表
  94. $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')];
  95. $specSkus = array_merge($default_skus, $specSkus);
  96. if (!empty($goods['goods_id'])) {
  97. $sku = Db::name('shop_goods_spec')->where(['goods_id' => $goods['goods_id']])->value('goods_spec_id');
  98. $specSkus['goods_spec_id'] = $sku;
  99. }
  100. $specSkus['rebate_score'] = $goods['rebate_score'];
  101. Db::name('shop_goods_spec')->save($specSkus);
  102. // Db::commit();
  103. // return true;
  104. // } catch (\Exception $e) {
  105. // Db::rollback();
  106. // return false;
  107. // }
  108. }
  109. public static function manySpecGoods ($goods)
  110. {
  111. $goods['is_exist_many_spec'] = 1;
  112. $specSkus = $goods['skus'];
  113. $specData = $goods['spec'];
  114. if (isset($goods['goods_id'])){
  115. $goods_sn = self::where('goods_id', $goods['goods_id'])->value('goods_sn');
  116. \services\CacheServices::set(md5('goodsDetail_' . $goods_sn), null);
  117. }
  118. //生成商品编号
  119. $goods['goods_sn'] = createdGoodsSn();
  120. //处理原价 按规格集合中-区间,最低,最高
  121. $mask_original_price = multi_minmax_inarray($specSkus, 'original_price');
  122. if (count(array_unique($mask_original_price)) == 1) {
  123. $original_price = $mask_original_price['min'];
  124. } else {
  125. $original_price = implode('-', $mask_original_price);
  126. }
  127. $goods['original_price'] = $original_price;
  128. $goods['min_original_price'] = $mask_original_price['min'];
  129. $goods['max_original_price'] = $mask_original_price['max'];
  130. $goods['goods_img_banner'] = explode('|', $goods['goods_img_banner']);
  131. $goods['goods_img_banner'] = json_encode($goods['goods_img_banner']);
  132. //处理销售价 按规格集合中-区间,最低,最高
  133. $price = multi_minmax_inarray($specSkus, 'price');
  134. if (count(array_unique($price)) == 1) {
  135. $prices = $price['min'];
  136. } else {
  137. $prices = implode('-', $price);
  138. }
  139. $goods['price'] = $prices;
  140. $goods['min_price'] = $price['min'];
  141. $goods['max_price'] = $price['max'];
  142. $goods['box_pic'] = 'static/img/box/box_img.png';
  143. //处理成本价 按规格集合中-区间,最低,最高
  144. $mask_cost_price = multi_minmax_inarray($specSkus, 'cost_price');
  145. if (count(array_unique($mask_cost_price)) == 1) {
  146. $cost_price = $mask_cost_price['min'];
  147. } else {
  148. $cost_price = implode('-', $mask_cost_price);
  149. }
  150. $goods['cost_price'] = $cost_price;
  151. $goods['min_cost_price'] = $mask_cost_price['min'];
  152. $goods['max_cost_price'] = $mask_cost_price['max'];
  153. //处理总库存 按规格集合中统计该商品总库存
  154. $goods['inventory'] = multi_array_sum($specSkus, 'stock');
  155. if (!empty($goods['hot_keywords'])) {
  156. $goods['hot_keywords'] = implode(',', $goods['hot_keywords']);
  157. }
  158. if (!empty($goods['buynote_template_id'])) {
  159. $tmp = Db::name('shop_buynote')->where(['id' => $goods['buynote_template_id']])->value('content');
  160. $goods['buynote_template'] = $tmp;
  161. }
  162. if (empty($goods['sales_volume'])) {
  163. // $goods['sales_volume'] = mt_rand(60, 1000);
  164. $goods['sales_volume'] = 0;
  165. }
  166. Db::startTrans();
  167. try {
  168. $goodsId = isset($goods['goods_id']) ? $goods['goods_id'] : 0;
  169. //step:1 插入商品表
  170. if (!empty($goods['goods_id'])) {
  171. Db::name('shop_goods')->strict(false)->save($goods);
  172. } else {
  173. $goodsId = Db::name('shop_goods')->strict(false)->insertGetId($goods);
  174. }
  175. $spec = new Spec();
  176. $specValueArr = [];
  177. if (!empty($goods['goods_id'])) {
  178. Db::name('shop_goods_spec_relation')->where(['goods_id' => $goods['goods_id']])->delete();
  179. Db::name('shop_goods_spec')->where(['goods_id' => $goods['goods_id']])->delete();
  180. }
  181. //step:2 插入商品与规格关系表
  182. foreach ($specData as $k => $v) {
  183. $key_arr = explode('-', $k);
  184. $specValueArr[$k]['goods_id'] = $goodsId;
  185. $specValueArr[$k]['spec_id'] = $key_arr[0];
  186. $specValueArr[$k]['spec_name'] = $key_arr[1];
  187. $v_ids = implode(',', $v);
  188. $v_ids = 'field(id,' . $v_ids . ')';//按whereIn id顺序排序
  189. $specValueArr[$k]['spec_value'] = json_encode($spec->where(['id' => $v])->field('id,title')->order(Db::raw($v_ids))->select(), JSON_UNESCAPED_UNICODE);
  190. }
  191. $specValueArr = array_values($specValueArr);
  192. Db::name('shop_goods_spec_relation')->insertAll($specValueArr);
  193. //step:3 插入商品规格表
  194. foreach ($specSkus as $k => &$v) {
  195. $specSkusArr = explode('-', $k);
  196. $specSkus_ids = implode(',', $specSkusArr);
  197. $specSkus_ids_order = 'field(id,' . $specSkus_ids . ')';//按whereIn id顺序排序
  198. $v['goods_id'] = $goodsId;
  199. $v['goods_sn'] = Db::name('shop_goods')->where(['goods_id' => $goodsId])->value('goods_sn');
  200. $v['spec_ids'] = $k;
  201. $v['spec_text'] = $spec->where(['id' => $specSkusArr])->order(Db::raw($specSkus_ids_order))->value("GROUP_CONCAT(title order by {$specSkus_ids_order} separator '-')");
  202. }
  203. $specSkus = array_values($specSkus);
  204. Db::name('shop_goods_spec')->insertAll($specSkus);
  205. Db::commit();
  206. return true;
  207. } catch (\Exception $e) {
  208. Db::rollback();
  209. return false;
  210. }
  211. }
  212. }