Goods.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\products;
  10. use app\admin\service\platform\Category as CategoryService;
  11. use app\admin\service\Service;
  12. use app\admin\model\goods\Goods as GoodsModel;
  13. use app\common\models\goods\GoodsItem as GoodsItemModel;
  14. use app\common\models\goods\ProductsBrand as BrandModel;
  15. use app\common\models\goods\Attribute as AttributeModel;
  16. use app\common\models\goods\AttributeData as AttributeDataModel;
  17. use app\common\models\goods\GoodsExtends as GoodsExtendsModel;
  18. use app\common\models\goods\GoodsModel as GoodsParamsModel;
  19. use app\common\models\goods\GoodsImage as GoodsImageModel;
  20. use app\common\models\goods\GoodsAttribute as GoodsAttributeModel;
  21. use app\common\models\promotion\PromotionGroup as PromotionGroupModel;
  22. use app\common\models\promotion\PromotionGroupItem as PromotionGroupItemModel;
  23. use app\common\models\promotion\PromotionRegiment as PromotionRegimentModel;
  24. use app\common\models\promotion\PromotionRegimentItem as PromotionRegimentItemModel;
  25. use app\common\models\promotion\PromotionPoint as PromotionPointModel;
  26. use app\common\models\promotion\PromotionPointItem as PromotionPointItemModel;
  27. use app\common\models\promotion\PromotionSecond as PromotionSecondModel;
  28. use app\common\models\promotion\PromotionSecondItem as PromotionSecondItemModel;
  29. use app\common\models\promotion\PromotionPrice as PromotionPriceModel;
  30. use app\common\models\promotion\PromotionPriceItem as PromotionPriceItemModel;
  31. use app\common\models\goods\Distribution as DistributionModel;
  32. use app\common\models\goods\ProductsModel;
  33. use mall\utils\Tool;
  34. use think\facade\Db;
  35. use think\facade\Env;
  36. /**
  37. * 商品服务类
  38. * Class Goods
  39. * @package app\admin\service\products
  40. */
  41. class Goods extends Service {
  42. /**
  43. * 获取列表数据
  44. * @param $data
  45. * @param string $goodsType
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public static function getList($data,$goodsType="0"){
  52. $fields = ["cat_id","status","brand_id","title","goods_type"];
  53. $searchData = [
  54. 'cat_id'=>$data['key']["cat_id"]??'',
  55. 'status'=>$data['key']["status"]??'',
  56. 'brand_id'=>$data['key']["brand_id"]??'',
  57. 'title'=>$data['key']["title"]??'',
  58. 'goods_type'=>$goodsType
  59. ];
  60. $count = GoodsModel::withSearch($fields,$searchData)->withJoin("category")->count();
  61. $result = array_map(function ($res){
  62. $res["photo"] = Tool::thumb($res["photo"]);
  63. return $res;
  64. },GoodsModel::withSearch($fields,$searchData)->withJoin("category")->order("goods.id","desc")->page($data["page"]??1,$data["limit"]??10)->select()->toArray());
  65. return ["count"=>$count, "data"=>$result];
  66. }
  67. /**
  68. * 获取指定商品规格列表
  69. * @param $goods_id
  70. * @return array|\think\Model|null
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public static function getGoodsItemData($goods_id){
  76. if(!$row=GoodsModel::where(["id"=>$goods_id])->find()){
  77. throw new \Exception("您要查找的数据不存在",0);
  78. }
  79. $products = GoodsItemModel::where(["goods_id"=>$goods_id])->select()->toArray();
  80. $temp = [];
  81. foreach($products as $key=>$item){
  82. $temp[$key] = $item;
  83. $arr = explode(",",$item["spec_key"]);
  84. foreach($arr as $value){
  85. $param = explode(":",$value);
  86. $name = AttributeModel::where(["id"=>$param[0]])->value("name");
  87. $value = AttributeDataModel::where(["id"=>$param[1],"pid"=>$param[0]])->value("value");
  88. $temp[$key]['spec_item'][] = $name . ':' . $value;
  89. }
  90. if(!empty($temp[$key]['spec_item'])){
  91. $temp[$key]['spec_item'] = implode(",", $temp[$key]['spec_item']);
  92. }
  93. }
  94. $row['item'] = $temp;
  95. return $row;
  96. }
  97. /**
  98. * 获取列表搜索分类和品牌数据
  99. * @return array
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public static function getSearchData(){
  105. return [
  106. "cat"=>CategoryService::getTree(["status"=>0,"module"=>"goods"]),
  107. "brand"=>BrandModel::where("status",0)->select()->toArray()
  108. ];
  109. }
  110. public static function getGoodsListData($goods_id){
  111. $result = GoodsModel::where("id","in",$goods_id)->select()->toArray();
  112. $array = [];
  113. foreach($result as $value){
  114. $array[] = $value["id"];
  115. }
  116. return [ "data"=>$result,"goods_id"=>$array ];
  117. }
  118. /**
  119. * 详情
  120. * @param $id
  121. * @return array
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. */
  126. public static function detail($id){
  127. $goods = GoodsModel::where("id",$id)->find();
  128. $goodsExtends = GoodsExtendsModel::where(['goods_id'=>$id])->select()->toArray();
  129. $goodsAttribute = [];
  130. foreach($goodsExtends as $val){
  131. $goodsAttribute[] = $val["attribute"];
  132. }
  133. return [
  134. "cat"=>CategoryService::getTree(["status"=>0,"module"=>"goods"]),
  135. "photo"=>GoodsImageModel::where(['goods_id'=>$id])->select()->toArray(),
  136. "brand"=>BrandModel::where("status",0)->select()->toArray(),
  137. "distribution"=>DistributionModel::where("status",0)->select()->toArray(),
  138. "attribute"=>AttributeModel::where(["pid"=>0])->select()->toArray(),
  139. "model"=>ProductsModel::select()->toArray(),
  140. "goods_extends"=>$goodsAttribute,
  141. "data"=>$goods??[]
  142. ];
  143. }
  144. /**
  145. * 保存数据
  146. * @param array $data
  147. * @return bool
  148. * @throws \Exception
  149. */
  150. public static function save($data=[]){
  151. if(DistributionModel::where(["id"=>$data["delivery_id"]])->count() <=0){
  152. throw new \Exception("请设置运费模板",0);
  153. }
  154. $post = $data;
  155. $post['sell_price'] = $data['product_sell_price'];
  156. $post['market_price'] = $data['product_market_price'];
  157. $post['cost_price'] = $data['product_cost_price'];
  158. $post['goods_weight'] = $data['product_weight'];
  159. $post['store_nums'] = $data['product_store_nums'];
  160. if(empty($post['goods_number'])){
  161. $data['goods_number'] = $post['goods_number'] = self::goodsNumber();
  162. }
  163. if(GoodsModel::where("id",$data["id"])->count()){
  164. GoodsModel::where("id",$data["id"])->save($post);
  165. }else{
  166. unset($data["id"],$post["id"]);
  167. $data["id"] = GoodsModel::create($post)->id;
  168. }
  169. $i = 0;
  170. $data['spec_list_key'] = !empty($data['spec_list_key']) ? $data['spec_list_key'] : [];
  171. $spec_temp = array();
  172. foreach ($data['spec_list_key'] as $val) {
  173. $arr = explode(',', $val);
  174. foreach ($arr as $item) {
  175. $a = explode(':', $item);
  176. $spec_temp[$i]['goods_id'] = $data['id'];
  177. $spec_temp[$i]['attr_id'] = $a[0];
  178. $spec_temp[$i]['attr_data_id'] = $a[1];
  179. $i++;
  180. }
  181. }
  182. $j = 0;
  183. $data['spec_list_data'] = !empty($data['spec_list_data']) ? $data['spec_list_data'] : [];
  184. foreach ($data['spec_list_data'] as $val) {
  185. $arr = explode(',', $val);
  186. foreach ($arr as $item) {
  187. $a = explode(':', $item);
  188. $spec_temp[$j]['name'] = $a[0];
  189. $spec_temp[$j]['value'] = $a[1];
  190. $j++;
  191. }
  192. }
  193. $spec_temp_data = [];
  194. foreach($spec_temp as $value){
  195. $spec_temp_data[$value['goods_id'] . '_' . $value['attr_id'] . '_' . $value["attr_data_id"]] = $value;
  196. }
  197. GoodsAttributeModel::where(["goods_id" => $data["id"]])->delete();
  198. $shop_goods_attribute = [];
  199. foreach ($spec_temp_data as $item) {
  200. $shop_goods_attribute[] = $item;
  201. }
  202. if(!empty($shop_goods_attribute)){
  203. GoodsAttributeModel::insertAll($shop_goods_attribute);
  204. }
  205. $order_no = 1;
  206. GoodsItemModel::where(["goods_id" => $data["id"]])->delete();
  207. $shop_goods_item = [];
  208. $data['sell_price'] = !empty($data['sell_price']) ? $data['sell_price'] : [];
  209. foreach ($data['sell_price'] as $key => $item) {
  210. $shop_goods_item[] = [
  211. "goods_id" => $data["id"],
  212. "spec_key" => $data['spec_list_key'][$key],
  213. "goods_number" => $data['goods_number'] . '-' . $order_no,
  214. "store_nums" => $data['store_nums'][$key],
  215. "market_price" => $data['market_price'][$key],
  216. "sell_price" => $item,
  217. "cost_price" => $data['cost_price'][$key],
  218. "goods_weight" => $data['goods_weight'][$key]
  219. ];
  220. $order_no++;
  221. }
  222. if(!empty($shop_goods_item)){
  223. GoodsItemModel::insertAll($shop_goods_item);
  224. }
  225. GoodsExtendsModel::where(['goods_id' => $data['id']])->delete();
  226. $data['goods_extends'] = !empty($data['goods_extends']) ? $data['goods_extends'] : [];
  227. foreach ($data['goods_extends'] as $val) {
  228. GoodsExtendsModel::create(['attribute' => $val, 'goods_id' => $data['id']]);
  229. }
  230. $attr = [];
  231. foreach ($_POST as $key => $val) {
  232. if (strpos($key, 'attr_id_') !== false) {
  233. $attr[ltrim($key, 'attr_id_')] = $val;
  234. }
  235. }
  236. GoodsParamsModel::where(['goods_id' => $data["id"]])->delete();
  237. $shop_goods_module = [];
  238. if ($data['model_id'] > 0 && !empty($attr)) {
  239. $sort = 0;
  240. foreach ($attr as $key => $val) {
  241. $shop_goods_module[] = [
  242. 'goods_id' => $data["id"],
  243. 'model_id' => $data['model_id'],
  244. 'attribute_id' => $key,
  245. 'attribute_value' => is_array($val) ? join(',', $val) : $val,
  246. 'sort' => $sort
  247. ];
  248. $sort++;
  249. }
  250. if(!empty($shop_goods_module)){
  251. GoodsParamsModel::insertAll($shop_goods_module);
  252. }
  253. }
  254. $images = [];
  255. if(!empty($data["images"])){
  256. foreach($data["images"] as $value){
  257. $images[] = [
  258. "goods_id" => $data["id"],
  259. "path" => $value,
  260. "create_time" => time()
  261. ];
  262. }
  263. }
  264. GoodsImageModel::where("goods_id",$data["id"])->delete();
  265. if(!empty($images)){
  266. GoodsImageModel::insertAll($images);
  267. }
  268. return true;
  269. }
  270. /**
  271. * 删除商品
  272. * @param $params
  273. * @return bool
  274. * @throws \Exception
  275. */
  276. public static function delete($params){
  277. try{
  278. GoodsModel::startTrans();
  279. $array = array_map("intval",explode(",",$params));
  280. foreach($array as $id) {
  281. $row = GoodsModel::where('id', $id)->find();
  282. if(empty($row)) continue;
  283. GoodsModel::where("id",$id)->delete();
  284. GoodsAttributeModel::where(['goods_id' => $id])->delete();
  285. GoodsExtendsModel::where(['goods_id' => $id])->delete();
  286. GoodsItemModel::where(['goods_id' => $id])->delete();
  287. GoodsParamsModel::where(['goods_id' => $id])->delete();
  288. GoodsImageModel::where(['goods_id' => $id])->delete();
  289. if ($promotion_group = PromotionGroupModel::where("goods_id", $id)->find()) {
  290. PromotionGroupModel::where('id',$promotion_group["id"])->delete();
  291. PromotionGroupItemModel::where('pid', $promotion_group["id"])->delete();
  292. }
  293. if ($promotion_regiment = PromotionRegimentModel::where("goods_id", $id)->find()) {
  294. PromotionRegimentModel::where("id",$promotion_regiment["id"])->delete();
  295. PromotionRegimentItemModel::where("pid", $promotion_regiment["id"])->delete();
  296. }
  297. if ($promotion_second = PromotionSecondModel::where("goods_id", $id)->find()) {
  298. PromotionSecondModel::where("id",$promotion_second["id"])->delete();
  299. PromotionSecondItemModel::where("pid", $promotion_second["id"])->delete();
  300. }
  301. if ($promotion_point = PromotionPointModel::where("goods_id", $id)->find()) {
  302. PromotionPointModel::where("id",$promotion_point["id"])->delete();
  303. PromotionPointItemModel::where('pid', $promotion_point["id"])->delete();
  304. }
  305. if ($promotion_price = PromotionPriceModel::where("goods_id", $id)->find()) {
  306. PromotionPriceModel::where("id",$promotion_price["id"])->delete();
  307. PromotionPriceItemModel::where("pid",$promotion_price['id'])->delete();
  308. }
  309. }
  310. GoodsModel::commit();
  311. return true;
  312. }catch (\Exception $ex){
  313. GoodsModel::rollback();
  314. throw new \Exception($ex->getMessage(),$ex->getCode());
  315. }
  316. }
  317. /**
  318. * 更新字段值
  319. * @return GoodsModel
  320. */
  321. public static function setFields(){
  322. $data = self::getFields();
  323. return GoodsModel::where("id",$data["id"])->update([$data["name"]=>$data["value"]]);
  324. }
  325. }