StoreWholesale.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\services\PHPExcelService;
  5. use crmeb\traits\ModelTrait;
  6. use app\admin\model\system\SystemGroupData;
  7. class StoreWholesale extends BaseModel
  8. {
  9. use ModelTrait;
  10. protected function getImagesAttr($value)
  11. {
  12. return json_decode($value, true) ?: [];
  13. }
  14. public function getDescriptionAttr($value)
  15. {
  16. return htmlspecialchars_decode($value);
  17. }
  18. public static function lst($where)
  19. {
  20. }
  21. public static function getWholeCount()
  22. {
  23. $seckillTime = sys_data('Whole_time') ?: [];//秒杀时间段
  24. $timeInfo = ['time' => 0, 'continued' => 0];
  25. foreach ($seckillTime as $key => $value) {
  26. $currentHour = date('H');
  27. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  28. if ($currentHour >= (int)$value['time'] && $currentHour < (int)$activityEndHour && (int)$activityEndHour < 24) {
  29. $timeInfo = $value;
  30. break;
  31. }
  32. }
  33. if ($timeInfo['time'] == 0) return 0;
  34. $activityEndHour = bcadd((int)$timeInfo['time'], (int)$timeInfo['continued'], 0);
  35. $startTime = bcadd(strtotime(date('Y-m-d')), bcmul($timeInfo['time'], 3600, 0));
  36. $stopTime = bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  37. echo $startTime;
  38. return self::where('is_del', 0)->where('status', 1)->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime)->count();
  39. }
  40. /**
  41. * 获取批发列表
  42. * @param $time
  43. * @param int $page
  44. * @param int $limit
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public static function WholeList($time, $page = 0, $limit = 20)
  51. {
  52. if ($page) $list = self::alias('n')->where('n.is_del', 0)->where('n.status', 1)->where('n.start_time', '<=', time())->where('n.stop_time', '>=', time() - 86400)->where('n.time_id', $time)->field('n.*')->order('n.sort desc')->page($page, $limit)->select();
  53. else $list = self::alias('n')->where('n.is_del', 0)->where('n.status', 1)->where('n.start_time', '<=', time())->where('n.stop_time', '>=', time() - 86400)->where('n.time_id', $time)->field('n.*')->order('sort desc')->select();
  54. if ($list) return $list->hidden(['cost', 'add_time', 'is_del'])->toArray();
  55. return [];
  56. }
  57. /**
  58. * 获取所有批发产品
  59. * @param string $field
  60. * @return array
  61. */
  62. public static function getListAll($offset = 0, $limit = 10, $field = 'id,image,title,price,ot_price,start_time,stop_time,stock,sales')
  63. {
  64. $time = time();
  65. $model = self::where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  66. ->where('start_time', '<', $time)->where('stop_time', '>', $time)->order('sort DESC,add_time DESC');
  67. $model = $model->limit($offset, $limit);
  68. $list = $model->select();
  69. if ($list) return $list->toArray();
  70. else return [];
  71. }
  72. /**
  73. * 获取热门推荐的批发产品
  74. * @param int $limit
  75. * @param string $field
  76. * @return array
  77. */
  78. public static function getHotList($limit = 0, $field = 'id,image,title,price,ot_price,start_time,stop_time,stock')
  79. {
  80. $time = time();
  81. $model = self::where('is_hot', 1)->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  82. ->where('start_time', '<', $time)->where('stop_time', '>', $time)->order('sort DESC,add_time DESC');
  83. if ($limit) $model->limit($limit);
  84. $list = $model->select();
  85. if ($list) return $list->toArray();
  86. else return [];
  87. }
  88. /**
  89. * 获取批发是否有开启
  90. * @return bool
  91. */
  92. public static function getWholeContStatus()
  93. {
  94. $time = time();
  95. $count = self::where('is_del', 0)->where('status', 1)->where('start_time', '<', $time)->where('stop_time', '>', $time)->count();
  96. return $count ? true : false;
  97. }
  98. /** 获取批发产品库存
  99. * @param $id
  100. * @return mixed
  101. */
  102. public static function getProductStock($id)
  103. {
  104. return self::where('id', $id)->value('stock');
  105. }
  106. /**
  107. * 获取一条产品
  108. * @param $id
  109. * @param string $field
  110. * @return array|false|\PDOStatement|string|\think\Model
  111. */
  112. public static function getValidProduct($id, $field = '*')
  113. {
  114. $time = time();
  115. $info = self::alias('n')->where('n.id', $id)->where('n.is_del', 0)->where('n.status', 1)->where('n.start_time', '<', $time)->where('n.stop_time', '>', $time - 86400)->field('n.*')->find();
  116. if ($info['id']) {
  117. return $info;
  118. } else {
  119. return [];
  120. }
  121. }
  122. /**
  123. * 获取批发是否已结束
  124. * @param $seckill_id
  125. * @return bool
  126. */
  127. public static function isWholeEnd($seckill_id)
  128. {
  129. $time_id = self::where('id', $seckill_id)->value('time_id');
  130. //秒杀时间段
  131. $seckillTime = sys_data('Whole_time') ?? [];
  132. $seckillTimeIndex = 0;
  133. $activityTime = [];
  134. if (count($seckillTime)) {
  135. foreach ($seckillTime as $key => &$value) {
  136. $currentHour = date('H');
  137. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  138. if ($activityEndHour > 24) {
  139. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  140. $value['state'] = '即将开始';
  141. $value['status'] = 2;
  142. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  143. } else {
  144. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
  145. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  146. $value['state'] = '抢购中';
  147. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  148. $value['status'] = 1;
  149. if (!$seckillTimeIndex) $seckillTimeIndex = $key;
  150. } else if ($currentHour < (int)$value['time']) {
  151. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  152. $value['state'] = '即将开始';
  153. $value['status'] = 2;
  154. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  155. } else if ($currentHour >= $activityEndHour) {
  156. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  157. $value['state'] = '已结束';
  158. $value['status'] = 0;
  159. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  160. }
  161. }
  162. if ($value['id'] == $time_id) {
  163. $activityTime = $value;
  164. break;
  165. }
  166. }
  167. }
  168. if (time() < $activityTime['stop'])
  169. return true;
  170. else
  171. return false;
  172. }
  173. /**
  174. * 检查批发活动状态
  175. * @param $seckill_id
  176. * @return bool
  177. */
  178. public static function checkStatus($seckill_id)
  179. {
  180. $time_id = self::where('id', $seckill_id)->value('time_id');
  181. //秒杀时间段
  182. $seckillTime = sys_data('Whole_time') ?? [];
  183. $seckillTimeIndex = 0;
  184. $activityTime = [];
  185. if (count($seckillTime)) {
  186. foreach ($seckillTime as $key => &$value) {
  187. $currentHour = date('H');
  188. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  189. if ($activityEndHour > 24) {
  190. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  191. $value['state'] = '即将开始';
  192. $value['status'] = 2;
  193. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  194. } else {
  195. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
  196. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  197. $value['state'] = '抢购中';
  198. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  199. $value['status'] = 1;
  200. if (!$seckillTimeIndex) $seckillTimeIndex = $key;
  201. } else if ($currentHour < (int)$value['time']) {
  202. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  203. $value['state'] = '即将开始';
  204. $value['status'] = 2;
  205. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  206. } else if ($currentHour >= $activityEndHour) {
  207. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  208. $value['state'] = '已结束';
  209. $value['status'] = 0;
  210. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  211. }
  212. }
  213. if ($value['id'] == $time_id) {
  214. $activityTime = $value;
  215. break;
  216. }
  217. }
  218. }
  219. return $activityTime;
  220. }
  221. public static function SaveExcel($where)
  222. {
  223. $model = new self;
  224. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  225. if ($where['store_name'] != '') $model = $model->where('title|id', 'LIKE', "%$where[store_name]%");
  226. $list = $model->order('id desc')->where('is_del', 0)->select();
  227. count($list) && $list = $list->toArray();
  228. $excel = [];
  229. foreach ($list as $item) {
  230. if ($item['status']) {
  231. if ($item['start_time'] > time())
  232. $item['start_name'] = '活动未开始';
  233. else if ($item['stop_time'] < time())
  234. $item['start_name'] = '活动已结束';
  235. else if ($item['stop_time'] > time() && $item['start_time'] < time())
  236. $item['start_name'] = '正在进行中';
  237. } else $item['start_name'] = '关闭';
  238. $excel[] = [
  239. $item['id'],
  240. $item['title'],
  241. $item['info'],
  242. $item['ot_price'],
  243. $item['price'],
  244. $item['stock'],
  245. $item['start_name'],
  246. $item['stop_time'],
  247. $item['stop_time'],
  248. $item['status'] ? '开启' : '关闭',
  249. ];
  250. }
  251. PHPExcelService::setExcelHeader(['编号', '活动标题', '活动简介', '原价', '秒杀价', '库存', '秒杀状态', '结束时间', '状态'])
  252. ->setExcelTile('秒杀产品导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time()))
  253. ->setExcelContent($excel)
  254. ->ExcelSave();
  255. }
  256. /**
  257. * @param $where
  258. * @return array
  259. */
  260. public static function systemPage($where)
  261. {
  262. $model = new self;
  263. $model = $model->alias('s');
  264. if ($where['status'] != '') $model = $model->where('s.status', $where['status']);
  265. if ($where['store_name'] != '') $model = $model->where('s.title|s.id', 'LIKE', "%$where[store_name]%");
  266. $model = $model->page(bcmul($where['page'], $where['limit'], 0), $where['limit']);
  267. $model = $model->order('s.id desc');
  268. $model = $model->where('s.is_del', 0);
  269. return self::page($model, function ($item) {
  270. $item['store_name'] = $item['title'];
  271. if ($item['status']) {
  272. if (strtotime($item['start_time']) > time())
  273. $item['start_name'] = '活动未开始';
  274. else if (bcadd(strtotime($item['stop_time']), 86400) < time())
  275. $item['start_name'] = '活动已结束';
  276. else if (bcadd(strtotime($item['stop_time']), 86400) > time() && strtotime($item['start_time']) < time()) {
  277. $config = SystemGroupData::get($item['time_id']);
  278. if ($config) {
  279. $arr = json_decode($config->value, true);
  280. $now_hour = date('H', time());
  281. $start_hour = $arr['time']['value'];
  282. $continued = $arr['continued']['value'];
  283. $end_hour = $start_hour + $continued;
  284. if ($start_hour > $now_hour) {
  285. $item['start_name'] = '活动未开始';
  286. } elseif ($end_hour < $now_hour) {
  287. $item['start_name'] = '活动已结束';
  288. } else {
  289. $item['start_name'] = '正在进行中';
  290. }
  291. } else {
  292. $item['start_name'] = '正在进行中';
  293. }
  294. }
  295. } else $item['start_name'] = '关闭';
  296. }, $where, $where['limit']);
  297. }
  298. /**
  299. * 获取批发产品id
  300. * @return array
  301. */
  302. public static function getWholeIdAll()
  303. {
  304. return self::where('is_del', 0)->column('id', 'id');
  305. }
  306. }