StoreBargain.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\models\store;
  8. use app\admin\model\store\StoreProductAttrValue;
  9. use app\admin\model\store\StoreProductAttrValue as StoreProductAttrValueModel;
  10. use crmeb\basic\BaseModel;
  11. use crmeb\traits\ModelTrait;
  12. use app\models\store\StoreProduct;
  13. /**
  14. * TODO 砍价产品Model
  15. * Class StoreBargain
  16. * @package app\models\store
  17. */
  18. class StoreBargain extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'store_bargain';
  30. use ModelTrait;
  31. public function getDescriptionAttr($value)
  32. {
  33. return htmlspecialchars_decode($value);
  34. }
  35. public function getRuleAttr($value)
  36. {
  37. return htmlspecialchars_decode($value);
  38. }
  39. /**
  40. * 正在开启的砍价活动
  41. * @param int $status
  42. * @return StoreBargain
  43. */
  44. public static function validWhere($status = 1)
  45. {
  46. return self::where('is_del', 0)->where('status', $status)->where('start_time', '<', time())->where('stop_time', '>', time());
  47. }
  48. /**
  49. * 判断砍价产品是否开启
  50. * @param int $bargainId
  51. * @return int|string
  52. */
  53. public static function validBargain($bargainId = 0)
  54. {
  55. $model = self::validWhere();
  56. return $bargainId ? $model->where('id', $bargainId)->count('id') : $model->count('id');
  57. }
  58. /**
  59. * TODO 获取正在开启的砍价产品编号
  60. * @return array
  61. */
  62. public static function validBargainNumber()
  63. {
  64. return self::validWhere()->column('id');
  65. }
  66. /**
  67. * 获取正在进行中的砍价产品
  68. * @param int $page
  69. * @param int $limit
  70. * @param string $field
  71. * @return array
  72. */
  73. public static function getList($page = 0, $limit = 20, $field = 'id,product_id,title,price,min_price,image')
  74. {
  75. $model = self::validWhere()->field($field);
  76. if ($page) $model = $model->page($page, $limit);
  77. $list = $model->select()->each(function ($item) {
  78. $item['people'] = count(StoreBargainUser::getUserIdList($item['id']));
  79. });
  80. return $list ? $list->toArray() : [];
  81. }
  82. /**
  83. * TODO 获取一条正在进行中的砍价产品
  84. * @param int $bargainId $bargainId 砍价产品编号
  85. * @param string $field
  86. * @return array
  87. * @throws \think\Exception
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @throws \think\exception\DbException
  91. */
  92. public static function getBargainTerm($bargainId = 0, $field = 'id,product_id,bargain_num,num,unit_name,image,title,price,min_price,image,start_time,stop_time,rule,info')
  93. {
  94. if (!$bargainId) return [];
  95. $model = self::validWhere();
  96. $bargain = $model->field($field)->where('id', $bargainId)->find();
  97. if ($bargain) return $bargain->toArray();
  98. else return [];
  99. }
  100. /**
  101. * 获取一条砍价产品
  102. * @param int $bargainId
  103. * @param string $field
  104. * @return array
  105. */
  106. public static function getBargain($bargainId = 0, $field = 'id,product_id,title,price,min_price,image')
  107. {
  108. if (!$bargainId) return [];
  109. $model = new self();
  110. $bargain = $model->field($field)->where('id', $bargainId)->find();
  111. if ($bargain) return $bargain->toArray();
  112. else return [];
  113. }
  114. /**
  115. * 获取最高价和最低价
  116. * @param int $bargainId
  117. * @return array
  118. */
  119. public static function getBargainMaxMinPrice($bargainId = 0)
  120. {
  121. if (!$bargainId) return [];
  122. return self::where('id', $bargainId)->field('bargain_min_price,bargain_max_price')->find()->toArray();
  123. }
  124. /**
  125. * 获取砍价次数
  126. * @param int $bargainId
  127. * @return mixed
  128. */
  129. public static function getBargainNum($bargainId = 0)
  130. {
  131. return self::where('id', $bargainId)->value('bargain_num');
  132. }
  133. /**
  134. * 判断当前砍价是否活动进行中
  135. * @param int $bargainId
  136. * @return bool
  137. */
  138. public static function setBargainStatus($bargainId = 0)
  139. {
  140. $model = self::validWhere();
  141. $count = $model->where('id', $bargainId)->count();
  142. if ($count) return true;
  143. else return false;
  144. }
  145. /**
  146. * 获取库存
  147. * @param int $bargainId
  148. * @return mixed
  149. */
  150. public static function getBargainStock($bargainId = 0)
  151. {
  152. return self::where('id', $bargainId)->value('stock');
  153. }
  154. /**
  155. * 获取字段值
  156. * @param $bargainId
  157. * @param string $field
  158. * @return mixed
  159. */
  160. public static function getBargainField($bargainId, $field = 'title')
  161. {
  162. return self::where('id', $bargainId)->value($field);
  163. }
  164. /**
  165. * 修改销量和库存
  166. * @param $num
  167. * @param $CombinationId
  168. * @return bool
  169. */
  170. public static function decBargainStock($num, $bargainId, $unique)
  171. {
  172. $product_id = self::where('id',$bargainId)->value('product_id');
  173. if ($unique) {
  174. $res = false !== StoreProductAttrValue::decProductAttrStock($bargainId, $unique, $num, 2);
  175. $res = $res && self::where('id', $bargainId)->dec('stock', $num)->dec('quota', $num)->inc('sales', $num)->update();
  176. $sku = StoreProductAttrValue::where('product_id',$bargainId)->where('unique',$unique)->where('type',2)->value('suk');
  177. $res = $res && StoreProductAttrValue::where('product_id',$product_id)->where('suk',$sku)->where('type',0)->dec('stock',$num)->inc('sales',$num)->update();
  178. } else {
  179. $res = false !== self::where('id', $bargainId)->dec('stock', $num)->inc('sales', $num)->update();
  180. }
  181. $res = $res && StoreProduct::where('id',$product_id)->dec('stock', $num)->inc('sales', $num)->update();
  182. return $res;
  183. }
  184. /**
  185. * TODO 增加库存减销量
  186. * @param $num
  187. * @param $bargainId
  188. * @return bool
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. * @throws \think\exception\DbException
  192. */
  193. public static function IncBargainStock($num, $bargainId, $unique = '')
  194. {
  195. $bargain = self::where('id', $bargainId)->field(['product_id', 'stock', 'sales', 'quota'])->find();
  196. if (!$bargain) return true;
  197. if ($bargain->sales > 0) $bargain->sales = bcsub($bargain->sales, $num, 0);
  198. if ($bargain->sales < 0) $bargain->sales = 0;
  199. $res = true;
  200. if ($unique) {
  201. $res = false !== StoreProductAttrValueModel::incProductAttrStock($bargainId, $unique, $num, 2);
  202. $sku = StoreProductAttrValue::where('product_id',$bargainId)->where('unique',$unique)->where('type',2)->value('suk');
  203. $res = $res && StoreProductAttrValue::where('product_id',$bargain['product_id'])->where('suk',$sku)->where('type',0)->inc('stock',$num)->dec('sales',$num)->update();
  204. }
  205. $bargain->stock = bcadd($bargain->stock, $num, 0);
  206. $bargain->quota = bcadd($bargain->quota, $num, 0);
  207. $res = $res && $bargain->save() && StoreProduct::where('id',$bargain['product_id'])->inc('stock', $num)->dec('sales', $num)->update();
  208. return $res;
  209. }
  210. /**
  211. * TODO 获取所有砍价产品的浏览量
  212. * @return mixed
  213. */
  214. public static function getBargainLook()
  215. {
  216. return self::sum('look');
  217. }
  218. /**
  219. * TODO 获取正在开启的砍价活动
  220. * @return int|string
  221. */
  222. public static function getListCount()
  223. {
  224. return self::validWhere()->count();
  225. }
  226. /**
  227. * TODO 获取所有砍价产品的分享量
  228. * @return mixed
  229. */
  230. public static function getBargainShare()
  231. {
  232. return self::sum('share');
  233. }
  234. /**
  235. * TODO 添加砍价产品分享次数
  236. * @param int $id
  237. * @return StoreBargain|bool
  238. */
  239. public static function addBargainShare($id = 0)
  240. {
  241. if (!$id) return false;
  242. return self::where('id', $id)->inc('share', 1)->update();
  243. }
  244. /**
  245. * TODO 添加砍价产品浏览次数
  246. * @param int $id $id 砍价产品编号
  247. * @return StoreBargain|bool
  248. */
  249. public static function addBargainLook($id = 0)
  250. {
  251. if (!$id) return false;
  252. return self::where('id', $id)->inc('look', 1)->update();
  253. }
  254. }