StoreBargain.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\models\store;
  8. use app\models\store\StoreBargainUser;
  9. use app\models\store\StoreBargainUserHelp;
  10. use crmeb\basic\BaseModel;
  11. use crmeb\traits\ModelTrait;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. /**
  16. * TODO 砍价商品Model
  17. * Class StoreBargain
  18. * @package app\models\store
  19. */
  20. class StoreBargain extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'id';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'store_bargain';
  32. use ModelTrait;
  33. public static function merSet($mer_id)
  34. {
  35. return $mer_id ? self::where('mer_id', $mer_id) : new self;
  36. }
  37. protected function getAddTimeAttr($value)
  38. {
  39. if ($value) return date('Y-m-d H:i:s', $value);
  40. return '';
  41. }
  42. /**
  43. * 正在开启的砍价活动
  44. * @param int $status
  45. * @param bool $mer_id
  46. * @return StoreBargain
  47. */
  48. public static function validWhere($status = 1, $mer_id = false)
  49. {
  50. return self::merSet($mer_id)->where('is_del', 0)->where('status', $status)->where('start_time', '<', time())->where('stop_time', '>', time());
  51. }
  52. /**
  53. * 判断砍价商品是否开启
  54. * @param int $bargainId
  55. * @param bool $mer_id
  56. * @return int|string
  57. */
  58. public static function validBargain($bargainId = 0, $mer_id = false)
  59. {
  60. $model = self::validWhere(1, $mer_id);
  61. return $bargainId ? $model->where('id', $bargainId)->count('id') : $model->count('id');
  62. }
  63. /**
  64. * TODO 获取正在开启的砍价商品编号
  65. * @return array
  66. */
  67. public static function validBargainNumber()
  68. {
  69. return self::validWhere()->column('id');
  70. }
  71. /**
  72. * 获取正在进行中的砍价商品
  73. * @param int $page
  74. * @param int $limit
  75. * @param bool $mer_id
  76. * @param string $field
  77. * @return array
  78. * @throws DataNotFoundException
  79. * @throws DbException
  80. * @throws ModelNotFoundException
  81. */
  82. public static function getList($page = 0, $limit = 20, $mer_id = false, $field = 'id,product_id,title,price,min_price,image')
  83. {
  84. $model = self::validWhere(1, $mer_id)->field($field);
  85. if ($page) $model = $model->page($page, $limit);
  86. $list = $model->select()->each(function ($item) {
  87. $item['people'] = count(StoreBargainUser::getUserIdList($item['id']));
  88. $item['price'] = floatval($item['price']);
  89. });
  90. return $list ? $list->toArray() : [];
  91. }
  92. /**
  93. * TODO 获取一条正在进行中的砍价商品
  94. * @param int $bargainId $bargainId 砍价商品编号
  95. * @param bool $mer_id
  96. * @param string $field
  97. * @return array
  98. * @throws DataNotFoundException
  99. * @throws DbException
  100. * @throws ModelNotFoundException
  101. */
  102. public static function getBargainTerm($bargainId = 0, $mer_id = false, $field = 'id,product_id,bargain_num,num,unit_name,image,title,price,min_price,image,start_time,stop_time,rule,info')
  103. {
  104. if (!$bargainId) return [];
  105. $model = self::validWhere(1, $mer_id);
  106. $bargain = $model->field($field)->where('id', $bargainId)->find();
  107. if ($bargain) return $bargain->toArray();
  108. else return [];
  109. }
  110. /**
  111. * 获取一条砍价商品
  112. * @param int $bargainId
  113. * @param string $field
  114. * @return array
  115. */
  116. public static function getBargain($bargainId = 0, $field = 'id,product_id,title,price,min_price,image')
  117. {
  118. if (!$bargainId) return [];
  119. $model = new self();
  120. $bargain = $model->field($field)->where('id', $bargainId)->find();
  121. if ($bargain) return $bargain->toArray();
  122. else return [];
  123. }
  124. /**
  125. * 获取最高价和最低价
  126. * @param int $bargainId
  127. * @return array
  128. */
  129. public static function getBargainMaxMinPrice($bargainId = 0)
  130. {
  131. if (!$bargainId) return [];
  132. return self::where('id', $bargainId)->field('bargain_min_price,bargain_max_price')->find()->toArray();
  133. }
  134. /**
  135. * 获取砍价次数
  136. * @param int $bargainId
  137. * @return mixed
  138. */
  139. public static function getBargainNum($bargainId = 0)
  140. {
  141. return self::where('id', $bargainId)->value('bargain_num');
  142. }
  143. /**
  144. * 判断当前砍价是否活动进行中
  145. * @param int $bargainId
  146. * @return bool
  147. */
  148. public static function setBargainStatus($bargainId = 0)
  149. {
  150. $model = self::validWhere();
  151. $count = $model->where('id', $bargainId)->count();
  152. if ($count) return true;
  153. else return false;
  154. }
  155. /**
  156. * 获取库存
  157. * @param int $bargainId
  158. * @return mixed
  159. */
  160. public static function getBargainStock($bargainId = 0)
  161. {
  162. return self::where('id', $bargainId)->value('stock');
  163. }
  164. /**
  165. * 获取字段值
  166. * @param $bargainId
  167. * @param string $field
  168. * @return mixed
  169. */
  170. public static function getBargainField($bargainId, $field = 'title')
  171. {
  172. return self::where('id', $bargainId)->value($field);
  173. }
  174. /**
  175. * 修改销量和库存
  176. * @param $num
  177. * @param $CombinationId
  178. * @return bool
  179. */
  180. public static function decBargainStock($num, $bargainId, $unique)
  181. {
  182. $product_id = self::where('id', $bargainId)->value('product_id');
  183. if ($unique) {
  184. $res = false !== StoreProductAttrValue::decProductAttrStock($bargainId, $unique, $num, 2);
  185. $res = $res && self::where('id', $bargainId)->dec('stock', $num)->inc('sales', $num)->update();
  186. $sku = StoreProductAttrValue::where('product_id', $bargainId)->where('unique', $unique)->where('type', 2)->value('suk');
  187. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  188. } else {
  189. $res = false !== self::where('id', $bargainId)->dec('stock', $num)->inc('sales', $num)->update();
  190. }
  191. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  192. return $res;
  193. }
  194. /**
  195. * TODO 增加库存减销量
  196. * @param $num
  197. * @param $bargainId
  198. * @return bool
  199. * @throws DataNotFoundException
  200. * @throws ModelNotFoundException
  201. * @throws \think\exception\DbException
  202. */
  203. public static function IncBargainStock($num, $bargainId)
  204. {
  205. $bargain = self::where('id', $bargainId)->field(['stock', 'sales'])->find();
  206. if (!$bargain) return true;
  207. if ($bargain->sales > 0) $bargain->sales = bcsub($bargain->sales, $num, 0);
  208. if ($bargain->sales < 0) $bargain->sales = 0;
  209. $bargain->stock = bcadd($bargain->stock, $num, 0);
  210. return $bargain->save();
  211. }
  212. /**
  213. * TODO 获取所有砍价商品的浏览量
  214. * @return mixed
  215. */
  216. public static function getBargainLook()
  217. {
  218. return self::sum('look');
  219. }
  220. /**
  221. * TODO 获取正在开启的砍价活动
  222. * @return int|string
  223. */
  224. public static function getListCount()
  225. {
  226. return self::validWhere()->count();
  227. }
  228. /**
  229. * TODO 获取所有砍价商品的分享量
  230. * @return mixed
  231. */
  232. public static function getBargainShare()
  233. {
  234. return self::sum('share');
  235. }
  236. /**
  237. * TODO 添加砍价商品分享次数
  238. * @param int $id
  239. * @return StoreBargain|bool
  240. */
  241. public static function addBargainShare($id = 0)
  242. {
  243. if (!$id) return false;
  244. return self::where('id', $id)->inc('share', 1)->update();
  245. }
  246. /**
  247. * TODO 添加砍价商品浏览次数
  248. * @param int $id $id 砍价商品编号
  249. * @return StoreBargain|bool
  250. */
  251. public static function addBargainLook($id = 0)
  252. {
  253. if (!$id) return false;
  254. return self::where('id', $id)->inc('look', 1)->update();
  255. }
  256. /**
  257. * @param $where
  258. * @return array
  259. */
  260. public static function systemPage($where)
  261. {
  262. $model = new self;
  263. $model = self::isWhere($where, $model);
  264. $model = $model->order('id desc');
  265. $model = $model->where('is_del', 0);
  266. $model = $model->where('mer_id', $where['mer_id']);
  267. $model = self::getModelTime($where, $model, "add_time");
  268. $count = $model->count();
  269. $list = $model->page((int)$where['page'], (int)$where['limit'])
  270. ->select()
  271. ->each(function ($item) {
  272. if ($item['status']) {
  273. if ($item['start_time'] > time())
  274. $item['start_name'] = '活动未开始';
  275. else if ($item['stop_time'] < time())
  276. $item['start_name'] = '活动已结束';
  277. else if ($item['stop_time'] > time() && $item['start_time'] < time())
  278. $item['start_name'] = '正在进行中';
  279. }
  280. $item['count_people_all'] = StoreBargainUser::getCountPeopleAll($item['id']);//参与人数
  281. $item['count_people_help'] = StoreBargainUserHelp::getCountPeopleHelp($item['id']);//帮忙砍价人数
  282. $item['count_people_success'] = StoreBargainUser::getCountPeopleAll($item['id'], 3);//砍价成功人数
  283. });
  284. return compact('count', 'list');
  285. }
  286. /**
  287. * 查出导出数据
  288. * @param $where
  289. * @return array
  290. */
  291. public static function exportData($where)
  292. {
  293. $model = new self;
  294. $model = self::isWhere($where, $model);
  295. $model = $model->order('id desc');
  296. $model = $model->where('is_del', 0);
  297. $model = self::getModelTime($where, $model, "add_time");
  298. $list = $model->select()->toArray();
  299. return $list;
  300. }
  301. public static function isWhere($where = array(), $model = self::class)
  302. {
  303. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  304. if ($where['store_name'] != '') $model = $model->where('id|title', 'LIKE', "%$where[store_name]%");
  305. if ($where['mer_id'] != '') $model = $model->where('mer_id', $where['mer_id']);
  306. // if($where['data'] != '') $model = $model->whereTime('add_time', 'between', explode('-',$where['data']));
  307. return $model;
  308. }
  309. /**
  310. * 详情
  311. */
  312. public static function getOne($id)
  313. {
  314. $info = self::get($id);
  315. if ($info) {
  316. if ($info['start_time'])
  317. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  318. if ($info['stop_time'])
  319. $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
  320. if (isset($start_time) && isset($stop_time))
  321. $info['section_time'] = [$start_time, $stop_time];
  322. else
  323. $info['section_time'] = [];
  324. unset($info['start_time'], $info['stop_time']);
  325. }
  326. if ($info['images'])
  327. $info['images'] = json_decode($info['images'], true);
  328. else
  329. $info['images'] = [];
  330. $info['give_integral'] = intval($info['give_integral']);
  331. $info['price'] = floatval($info['price']);
  332. $info['postage'] = floatval($info['postage']);
  333. $info['cost'] = floatval($info['cost']);
  334. $info['bargain_max_price'] = floatval($info['bargain_max_price']);
  335. $info['bargain_min_price'] = floatval($info['bargain_min_price']);
  336. $info['min_price'] = floatval($info['min_price']);
  337. $info['weight'] = floatval($info['weight']);
  338. $info['volume'] = floatval($info['volume']);
  339. $info['description'] = StoreDescription::getDescription($id, 2);
  340. $info['attrs'] = self::attr_list($id);
  341. return $info;
  342. }
  343. public static function attr_list($id)
  344. {
  345. $bargainInfo = self::where('id', $id)->find();
  346. $bargainResult = StoreProductAttrResult::where('product_id', $id)->where('type', 2)->value('result');
  347. $items = json_decode($bargainResult, true)['attr'];
  348. $productAttr = self::get_attr($items, $bargainInfo['product_id'], 0);
  349. $seckillAttr = self::get_attr($items, $id, 2);
  350. foreach ($productAttr as $pk => $pv) {
  351. foreach ($seckillAttr as &$sv) {
  352. if ($pv['detail'] == $sv['detail']) {
  353. $productAttr[$pk] = $sv;
  354. }
  355. }
  356. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  357. }
  358. $attrs['items'] = $items;
  359. $attrs['value'] = $productAttr;
  360. foreach ($items as $key => $item) {
  361. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  362. }
  363. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  364. $header[] = ['title' => '砍价起始金额', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  365. $header[] = ['title' => '砍价最低价', 'slot' => 'min_price', 'align' => 'center', 'minWidth' => 80];
  366. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  367. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  368. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  369. $header[] = ['title' => '限量', 'slot' => 'quota', 'align' => 'center', 'minWidth' => 80];
  370. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  371. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  372. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  373. $attrs['header'] = $header;
  374. return $attrs;
  375. }
  376. public static function get_attr($attr, $id, $type)
  377. {
  378. $value = attr_format($attr)[1];
  379. $valueNew = [];
  380. $count = 0;
  381. if ($type == 2) {
  382. $min_price = self::where('id', $id)->value('min_price');
  383. } else {
  384. $min_price = 0;
  385. }
  386. foreach ($value as $key => $item) {
  387. $detail = $item['detail'];
  388. sort($item['detail'], SORT_STRING);
  389. $suk = implode(',', $item['detail']);
  390. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  391. if (count($sukValue)) {
  392. foreach (array_values($detail) as $k => $v) {
  393. $valueNew[$count]['value' . ($k + 1)] = $v;
  394. }
  395. $valueNew[$count]['detail'] = json_encode($detail);
  396. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  397. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  398. $valueNew[$count]['min_price'] = $min_price ? floatval($min_price) : 0;
  399. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  400. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  401. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  402. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  403. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  404. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  405. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  406. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  407. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  408. $valueNew[$count]['opt'] = $type != 0 ? true : false;
  409. $count++;
  410. }
  411. }
  412. return $valueNew;
  413. }
  414. }