Goods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace app\api\model\service;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. class Goods extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'service_goods';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. /**
  19. * 获取项目列表
  20. * @param $param
  21. * @return bool|\PDOStatement|string|\think\Collection
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public static function searchGoods($param)
  27. {
  28. extract($param);
  29. $order = 'weigh desc,id desc';
  30. if (isset($is_rank) && $is_rank != '') {
  31. $order = $is_rank == 1? 'salenums desc,weigh desc':'salenums asc,weigh desc';
  32. }
  33. if (isset($is_price) && $is_rank == '') {
  34. $order = $is_price == 1? 'price desc,weigh desc':'price asc,weigh desc';
  35. }
  36. $newWhere= "status='normal' AND shop_state=1 AND deletetime IS NULL";
  37. if (isset($name) && $name != '') {
  38. $newWhere.= " AND name LIKE '%{$name}%'";
  39. }
  40. if(isset($goods_id))
  41. {
  42. $newWhere.= " AND id={$goods_id}";
  43. }
  44. if(isset($category_id) && $category_id != '')
  45. {
  46. $newWhere.= " AND category_id={$category_id}";
  47. }
  48. if(isset($two_category_id) && $two_category_id != '')
  49. {
  50. $newWhere.= " AND two_category_id={$two_category_id}";
  51. }
  52. if(isset($category_ids) && $category_ids != '')
  53. {
  54. $categoryIdsArray = explode(',', $category_ids);
  55. $newWhere.= " AND category_id IN {$categoryIdsArray}";
  56. $newWhere.= " AND shop_id=0";
  57. }
  58. if(isset($shop_id) && $shop_id != '')
  59. {
  60. $newWhere.= " AND shop_id={$shop_id}";
  61. }
  62. if(isset($city) && $city != '')
  63. {
  64. $newWhere.= " AND (city='{$city}' OR city IS NULL)";
  65. }
  66. if(isset($skill_cate_id) && $skill_cate_id != '')
  67. {
  68. $newWhere.= " AND FIND_IN_SET('$skill_cate_id',skill_cate_ids)";
  69. }
  70. if (isset($goods_ids) && $goods_ids != '') {
  71. $newWhere.= " AND id IN ({$goods_ids})";
  72. }
  73. if(isset($two_category_id) && $two_category_id != '')
  74. {
  75. $newWhere.= " AND two_category_id={$two_category_id}";
  76. }
  77. $limit = $limit?:10;
  78. $page = empty($page)?0:($page-1)*$limit;
  79. $limit = $page.','.$limit;
  80. $config = config('database');
  81. $db = $config['prefix'].'service_goods';
  82. $list = Db::query("SELECT id,name,image,type,skill_cate_ids,price,response_hour,shop_id,tag_name,cost_seconds,salenums FROM {$db} WHERE $newWhere ORDER BY {$order} LIMIT {$limit}");
  83. return $list;
  84. }
  85. public static function getNewShopGoods($ids,$type=0)
  86. {
  87. if(!$ids)
  88. {
  89. return '';
  90. }
  91. $idArr = explode(',',$ids);
  92. $list = [];
  93. $i = 0;
  94. foreach ($idArr as $val)
  95. {
  96. if($i>=2 && $type == 0)
  97. {
  98. break;
  99. }
  100. $goods = self::where(['id'=>$val,'status'=>'normal','shop_state'=>1])->field('id,name,image,type,skill_cate_ids,price,response_hour,shop_id,tag_name,cost_seconds,salenums')->find();
  101. if($goods)
  102. {
  103. $list[] = $goods;
  104. $i++;
  105. }
  106. }
  107. return $list;
  108. }
  109. /**
  110. * 添加商品
  111. * @param $post
  112. * @return true
  113. * @throws Exception
  114. */
  115. public static function addGoods($post)
  116. {
  117. $goods = new Goods($post);
  118. $goods->allowField(true)->save();
  119. if($post['spec_type'] == 1) {
  120. $spuList = json_decode($post['spu'], true);
  121. $spu = [];
  122. foreach ($spuList as $value) {
  123. $spu[] = [
  124. 'goods_id' => $goods->id,
  125. 'name' => $value['name'],
  126. 'skudetail' => implode(',', array_column($value['info'], 'name'))
  127. ];
  128. }
  129. if (!model('\app\api\model\service\GoodsSpu')->allowField(true)->saveAll($spu)) {
  130. throw new Exception('添加规格失败');
  131. }
  132. $skuList = json_decode($post['sku'], true);
  133. $sku = [];
  134. foreach ($skuList as $value) {
  135. $sku[] = [
  136. 'goods_id' => $goods->id,
  137. 'name' => $value['name'],
  138. 'price' => $value['price'],
  139. 'cost_seconds'=>$value['cost_seconds'],
  140. ];
  141. }
  142. if (!model('\app\api\model\service\GoodsSku')->allowField(true)->saveAll($sku)) {
  143. throw new Exception('添加规格详情失败');
  144. }
  145. }
  146. if(isset($post['addGoods']))
  147. {
  148. $goodsAdd = [];
  149. $addGoodsList = json_decode($post['addGoods'], true);
  150. foreach ($addGoodsList as $value)
  151. {
  152. $goodsAdd[] = [
  153. 'goods_id' => $goods->id,
  154. 'name' => $value['name'],
  155. 'price' => $value['price'],
  156. 'cost_seconds' => $value['cost_seconds']
  157. ];
  158. }
  159. if (!model('\app\api\model\service\GoodsAdd')->allowField(true)->saveAll($goodsAdd)) {
  160. throw new Exception('添加附加项目失败');
  161. }
  162. }
  163. return true;
  164. }
  165. public static function editGoods($post)
  166. {
  167. $post['shop_state'] = 0;
  168. $goods = new \app\api\model\service\Goods ();
  169. $spec_type = self::where(['id'=>$post['id']])->value('spec_type');
  170. $goods->allowField(true)->save($post,['id'=>$post['id']]);
  171. if($post['spec_type'] == 1) {
  172. $spuList = json_decode($post['spu'], true);
  173. $spu = [];
  174. foreach ($spuList as $value) {
  175. $spu[] = [
  176. 'goods_id' => $goods->id,
  177. 'name' => $value['name'],
  178. 'skudetail' => implode(',', array_column($value['info'], 'name'))
  179. ];
  180. }
  181. if($spec_type == 1)
  182. {
  183. $goodsSpu = new GoodsSpu();
  184. $goodsSpu->save(['status'=>'hidden'],['goods_id' => $post['id'],'status'=>'normal']);
  185. $goodsSku = new GoodsSku();
  186. $goodsSku->save(['status'=>'hidden'],['goods_id' => $post['id'],'status'=>'normal']);
  187. }
  188. if (!model('\app\api\model\service\GoodsSpu')->allowField(true)->saveAll($spu)) {
  189. throw new Exception('添加规格失败');
  190. }
  191. $skuList = json_decode($post['sku'], true);
  192. $sku = [];
  193. foreach ($skuList as $value) {
  194. $sku[] = [
  195. 'goods_id' => $goods->id,
  196. 'name' => $value['name'],
  197. 'price' => $value['price'],
  198. 'cost_seconds' => $value['cost_seconds']
  199. ];
  200. }
  201. if (!model('\app\api\model\service\GoodsSku')->allowField(true)->saveAll($sku)) {
  202. throw new Exception('添加规格详情失败');
  203. }
  204. }
  205. if(isset($post['addGoods']))
  206. {
  207. $goodsAdd = [];
  208. $addGoodsList = json_decode($post['addGoods'], true);
  209. foreach ($addGoodsList as $value)
  210. {
  211. $goodsAdd[] = [
  212. 'goods_id' => $goods->id,
  213. 'name' => $value['name'],
  214. 'price' => $value['price'],
  215. 'cost_seconds' => $value['cost_seconds']
  216. ];
  217. }
  218. $goodsSku = new GoodsAdd();
  219. $goodsSku->save(['state'=>0],['goods_id' => $post['id'],'state'=>1]);
  220. if (!model('\app\api\model\service\GoodsAdd')->allowField(true)->saveAll($goodsAdd)) {
  221. throw new Exception('添加附加项目失败');
  222. }
  223. }
  224. return true;
  225. }
  226. public static function getSkillGoods($ids)
  227. {
  228. if(!$ids)
  229. {
  230. return '';
  231. }
  232. $idArr = explode(',',$ids);
  233. $list = [];
  234. foreach ($idArr as $val)
  235. {
  236. $goods = self::where(['id'=>$val,'status'=>'normal','shop_state'=>1])->field('id,name,shop_id,image,price,tag_name,response_hour,salenums,type')->find();
  237. if($goods)
  238. {
  239. $goods['shopName'] = $goods['shop_id']?Shop::getName($goods['shop_id']):'';
  240. $list[] = $goods;
  241. }
  242. }
  243. return $list;
  244. }
  245. public static function getShopPlatformGoods($ids,$shopUid)
  246. {
  247. $shopId = Shop::where(['user_id'=>$shopUid])->value('id');
  248. if(!$ids || !$shopId)
  249. {
  250. return '';
  251. }
  252. $shopGoodsList = self::where(['shop_id'=>$shopId])->column('id');
  253. $idArr = explode(',',$ids);
  254. $list = [];
  255. foreach ($idArr as $val)
  256. {
  257. $goods = !in_array($val,$shopGoodsList) ? self::where(['id'=>$val,'status'=>'normal','shop_state'=>1])->field('id,name,shop_id,image,price,tag_name,response_hour,salenums,type')->find() :'';
  258. if($goods)
  259. {
  260. $goods['shopName'] = $goods['shop_id']?Shop::getName($goods['shop_id']):'';
  261. $list[] = $goods;
  262. }
  263. }
  264. return $list;
  265. }
  266. /**
  267. * 项目下架
  268. * @param $id
  269. * @return Goods
  270. */
  271. public static function downGoods($id)
  272. {
  273. return self::where(['id'=>$id])->update(['status'=>'hidden']);
  274. }
  275. /**
  276. * 项目下架
  277. * @param $id
  278. * @return Goods
  279. */
  280. public static function upGoods($id)
  281. {
  282. return self::where(['id'=>$id])->update(['status'=>'normal']);
  283. }
  284. /**
  285. * 获取商品详情
  286. * @param $id
  287. * @param $uid
  288. * @return array|bool|\PDOStatement|string|Model
  289. * @throws \think\db\exception\DataNotFoundException
  290. * @throws \think\db\exception\ModelNotFoundException
  291. * @throws \think\exception\DbException
  292. */
  293. public static function getInfo($id,$uid='')
  294. {
  295. $where['id'] = $id;
  296. $info = self::where($where)->field('id,name,cost_seconds,image,price,cost_seconds,response_hour,shop_id,tag_name,to_shop,salenums,images,spec_type,choose_skill_type,flow_path_images,illustrate_images,start_hour,end_hour')->find();
  297. if(!$info)
  298. {
  299. return false;
  300. }
  301. $info['images'] = explode(',',$info['images']);
  302. $info['shopname'] = $info['shop_id']?Shop::getName($info['shop_id']):'';
  303. $info['flow_path_images'] = explode(',',$info['flow_path_images']);
  304. $info['illustrate_images'] = explode(',',$info['illustrate_images']);
  305. $info['followState'] = $uid ? Follow::getState(['follow_id'=>$id,'user_id'=>$uid,'type'=>1],1):0;
  306. $info['spu'] = $info['spec_type'] == 1?GoodsSpu::getSpuList($info['id']):[];
  307. $info['sku'] = $info['spec_type'] == 1?GoodsSku::getSkuList($info['id']):[];
  308. return $info;
  309. }
  310. public static function getShopGoods($id)
  311. {
  312. $where = [
  313. 'id' => $id,
  314. 'status'=>'normal'
  315. ];
  316. $use = ['goods_id'=>$id];
  317. $goods = self::where(['id'=>$id])->find();
  318. if($goods['spec_type'] == 1)
  319. {
  320. $goods = self::where($where)->with(['spu' => function($query) use ($use) {
  321. $query->where(['goods_id'=>$use['goods_id'],'status'=>'normal']);
  322. },'sku' => function($query) use ($use) {
  323. $query->where(['goods_id'=>$use['goods_id'],'status'=>'normal']);
  324. }])->order('weigh desc')->find();
  325. }
  326. $goods['addGoods'] = GoodsAdd::getList($id);
  327. $goods['category_name'] = Category::getName($goods['category_id']);
  328. $goods['two_category_name'] = Category::getName($goods['two_category_id']);
  329. $goods['skill_cate_name_list'] = SkillCate::getNameList($goods['skill_cate_ids']);
  330. return $goods;
  331. }
  332. public static function getCount($params)
  333. {
  334. $params['status'] = 'normal';
  335. return self::where($params)->count();
  336. }
  337. public static function getName($id)
  338. {
  339. return self::where('id',$id)->value('name');
  340. }
  341. public function spu()
  342. {
  343. return $this->hasMany('GoodsSpu','goods_id');
  344. }
  345. public function sku()
  346. {
  347. return $this->hasMany('GoodsSku','goods_id');
  348. }
  349. }