StoreCombination.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\models\store;
  8. use app\admin\model\store\StoreProductAttrValue;
  9. use crmeb\services\SystemConfigService;
  10. use crmeb\services\UtilService;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\basic\BaseModel;
  13. use app\models\store\StoreProduct;
  14. /**
  15. * TODO 拼团产品Model
  16. * Class StoreCombination
  17. * @package app\models\store
  18. */
  19. class StoreCombination extends BaseModel
  20. {
  21. /**
  22. * 数据表主键
  23. * @var string
  24. */
  25. protected $pk = 'id';
  26. /**
  27. * 模型名称
  28. * @var string
  29. */
  30. protected $name = 'store_combination';
  31. use ModelTrait;
  32. public function getDescriptionAttr($value)
  33. {
  34. return htmlspecialchars_decode($value);
  35. }
  36. /**
  37. * @param $where
  38. * @return array
  39. */
  40. public static function get_list($length = 10)
  41. {
  42. if ($post = input('post.')) {
  43. $where = $post['where'];
  44. $model = new self();
  45. $model = $model->alias('c');
  46. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  47. $model = $model->where('c.is_show', 1)->where('c.is_del', 0)->where('c.start_time', '<', time())->where('c.stop_time', '>', time());
  48. if (!empty($where['search'])) {
  49. $model = $model->where('c.title', 'like', "%{$where['search']}%");
  50. $model = $model->whereOr('s.keyword', 'like', "{$where['search']}%");
  51. }
  52. $model = $model->field('c.*,s.price as product_price');
  53. if ($where['key']) {
  54. if ($where['sales'] == 1) {
  55. $model = $model->order('c.sales desc');
  56. } else if ($where['sales'] == 2) {
  57. $model = $model->order('c.sales asc');
  58. }
  59. if ($where['price'] == 1) {
  60. $model = $model->order('c.price desc');
  61. } else if ($where['price'] == 2) {
  62. $model = $model->order('c.price asc');
  63. }
  64. if ($where['people'] == 1) {
  65. $model = $model->order('c.people asc');
  66. }
  67. if ($where['default'] == 1) {
  68. $model = $model->order('c.sort desc,c.id desc');
  69. }
  70. } else {
  71. $model = $model->order('c.sort desc,c.id desc');
  72. }
  73. $page = is_string($where['page']) ? (int)$where['page'] + 1 : $where['page'] + 1;
  74. $list = $model->page($page, $length)->select()->toArray();
  75. return ['list' => $list, 'page' => $page];
  76. }
  77. }
  78. /**
  79. * 获取拼团数据
  80. * @param int $page
  81. * @param int $limit
  82. * @return mixed
  83. */
  84. public static function getAll($page = 0, $limit = 20,$is_host=-1,$is_rec=-1)
  85. {
  86. $model = new self();
  87. $model = $model->alias('c');
  88. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  89. $model = $model->join('store_product_attr_result sr', 'sr.product_id=c.product_id and sr.type=0',"left");
  90. $model = $model->field('c.*,s.price as product_price,s.ot_price,s.samelevel_prise_maxperson,s.pink_zhitui_prize,s.pink_pay_person,s.cost_integral,s.pink_reward_rate,s.samelevel_reward_rate,s.level3_reward_rate,s.level2_reward_rate,s.level1_reward_rate,s.cost,sr.result');
  91. $model = $model->order('c.sort desc,c.id desc');
  92. $model = $model->where('c.is_show', 1);
  93. $model = $model->where('c.is_del', 0);
  94. $model = $model->where('c.start_time', '<', time());
  95. $model = $model->where('c.stop_time', '>', time());
  96. if($is_host>-1) $model = $model->where('c.is_host', $is_host);
  97. if($is_rec>-1) $model = $model->where('c.is_rec', $is_rec);
  98. if ($page) $model = $model->page($page, $limit);
  99. return $model->select()->each(function ($item) {
  100. $item['image'] = set_file_url($item['image']);
  101. $item['result'] = json_decode($item['result'],true);
  102. });
  103. }
  104. /*
  105. * 获取是否有拼团产品
  106. * */
  107. public static function getPinkIsOpen()
  108. {
  109. return self::alias('c')->join('StoreProduct s', 's.id=c.product_id')->where('c.is_show', 1)->where('c.is_del', 0)
  110. ->where('c.start_time', '<', time())->where('c.stop_time', '>', time())->count();
  111. }
  112. /**
  113. * 获取一条拼团数据
  114. * @param $id
  115. * @return mixed
  116. */
  117. public static function getCombinationOne($id)
  118. {
  119. $model = new self();
  120. $model = $model->alias('c');
  121. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  122. // $model = $model->field('c.*,s.price as product_price,SUM(s.sales+s.ficti) as total');
  123. $model = $model->field('c.*,s.price as product_price,s.ficti,s.ot_price');
  124. $model = $model->where('c.is_show', 1);
  125. $model = $model->where('c.is_del', 0);
  126. $model = $model->where('c.id', $id);
  127. // $model = $model->where('c.start_time','<',time());
  128. // $model = $model->where('c.stop_time','>',time()-86400);
  129. return $model->find();
  130. }
  131. /**
  132. * 获取一条拼团数据
  133. * @param $id
  134. * @return mixed
  135. */
  136. public static function getCombinationOneN($id)
  137. {
  138. $model = new self();
  139. $model = $model->alias('c');
  140. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  141. // $model = $model->field('c.*,s.price as product_price,SUM(s.sales+s.ficti) as total');
  142. $model = $model->field('c.*,s.price as product_price,s.ficti');
  143. // $model = $model->where('c.is_show', 1);
  144. // $model = $model->where('c.is_del', 0);
  145. $model = $model->where('c.id', $id);
  146. // $model = $model->where('c.start_time','<',time());
  147. // $model = $model->where('c.stop_time','>',time()-86400);
  148. return $model->find();
  149. }
  150. /**
  151. * 获取推荐的拼团产品
  152. * @return mixed
  153. */
  154. public static function getCombinationHost($limit = 0)
  155. {
  156. $model = new self();
  157. $model = $model->alias('c');
  158. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  159. $model = $model->field('c.id,c.image,c.price,c.sales,c.title,c.people,s.price as product_price');
  160. $model = $model->where('c.is_del', 0);
  161. $model = $model->where('c.is_host', 1);
  162. $model = $model->where('c.start_time', '<', time());
  163. $model = $model->where('c.stop_time', '>', time());
  164. if ($limit) $model = $model->limit($limit);
  165. return $model->select();
  166. }
  167. /**
  168. * 修改销量和库存
  169. * @param $num
  170. * @param $CombinationId
  171. * @return bool
  172. */
  173. public static function decCombinationStock($num, $CombinationId, $unique)
  174. {
  175. $product_id = self::where('id', $CombinationId)->value('product_id');
  176. @file_put_contents('pr.txt',$product_id);
  177. if ($unique) {
  178. $res = false !== StoreProductAttrValue::decProductAttrStock($CombinationId, $unique, $num, 3);
  179. $res = $res && self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  180. $sku = StoreProductAttrValue::where('product_id', $CombinationId)->where('unique', $unique)->where('type', 3)->value('suk');
  181. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  182. } else {
  183. $res = false !== self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  184. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  185. }
  186. return $res;
  187. }
  188. /**
  189. * 增加库存,减少销量
  190. * @param $num
  191. * @param $CombinationId
  192. * @return bool
  193. */
  194. public static function incCombinationStock($num, $CombinationId)
  195. {
  196. $combination = self::where('id', $CombinationId)->field(['stock', 'sales'])->find();
  197. if (!$combination) return true;
  198. if ($combination->sales > 0) $combination->sales = bcsub($combination->sales, $num, 0);
  199. if ($combination->sales < 0) $combination->sales = 0;
  200. $combination->stock = bcadd($combination->stock, $num, 0);
  201. return $combination->save();
  202. }
  203. /**
  204. * 判断库存是否足够
  205. * @param $id
  206. * @param $cart_num
  207. * @return int|mixed
  208. */
  209. public static function getCombinationStock($id, $cart_num)
  210. {
  211. $stock = self::where('id', $id)->value('stock');
  212. return $stock > $cart_num ? $stock : 0;
  213. }
  214. /**
  215. * 获取字段值
  216. * @param $id
  217. * @param $field
  218. * @return mixed
  219. */
  220. public static function getCombinationField($id, $field = 'title')
  221. {
  222. return self::where('id', $id)->value($field);
  223. }
  224. /**
  225. * 获取产品状态
  226. * @param $id
  227. * @return mixed
  228. */
  229. public static function isValidCombination($id)
  230. {
  231. $model = new self();
  232. $model = $model->where('id', $id);
  233. $model = $model->where('is_del', 0);
  234. $model = $model->where('is_show', 1);
  235. return $model->count();
  236. }
  237. /**
  238. * 增加浏览量
  239. * @param int $id
  240. * @return bool
  241. */
  242. public static function editIncBrowse($id = 0)
  243. {
  244. if (!$id) return false;
  245. $browse = self::where('id', $id)->value('browse');
  246. $browse = bcadd($browse, 1, 0);
  247. self::edit(['browse' => $browse], $id);
  248. }
  249. }