StoreBargain.php 8.2 KB

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