StoreBargain.php 7.8 KB

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