StoreWholesale.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 = '*',$is_news=0)
  113. {
  114. $time = time();
  115. $model = self::alias('n')->where('n.id', $id)->where('n.is_del', 0)->where('n.status', 1);
  116. if($is_news==0) $model = $model->where('n.start_time', '<', $time)->where('n.stop_time', '>', $time - 86400);
  117. $info = $model->field('n.*')->find();
  118. if ($info['id']) {
  119. return $info;
  120. } else {
  121. return [];
  122. }
  123. }
  124. /**
  125. * 获取批发是否已结束
  126. * @param $seckill_id
  127. * @return bool
  128. */
  129. public static function isWholeEnd($seckill_id)
  130. {
  131. $time_id = self::where('id', $seckill_id)->value('time_id');
  132. //秒杀时间段
  133. $seckillTime = sys_data('Whole_time') ?? [];
  134. $seckillTimeIndex = 0;
  135. $activityTime = [];
  136. if (count($seckillTime)) {
  137. foreach ($seckillTime as $key => &$value) {
  138. $currentHour = date('H');
  139. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  140. if ($activityEndHour > 24) {
  141. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  142. $value['state'] = '即将开始';
  143. $value['status'] = 2;
  144. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  145. } else {
  146. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
  147. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  148. $value['state'] = '抢购中';
  149. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  150. $value['status'] = 1;
  151. if (!$seckillTimeIndex) $seckillTimeIndex = $key;
  152. } else if ($currentHour < (int)$value['time']) {
  153. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  154. $value['state'] = '即将开始';
  155. $value['status'] = 2;
  156. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  157. } else if ($currentHour >= $activityEndHour) {
  158. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  159. $value['state'] = '已结束';
  160. $value['status'] = 0;
  161. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  162. }
  163. }
  164. if ($value['id'] == $time_id) {
  165. $activityTime = $value;
  166. break;
  167. }
  168. }
  169. }
  170. if (time() < $activityTime['stop'])
  171. return true;
  172. else
  173. return false;
  174. }
  175. /**
  176. * 检查批发活动状态
  177. * @param $seckill_id
  178. * @return bool
  179. */
  180. public static function checkStatus($seckill_id)
  181. {
  182. $time_id = self::where('id', $seckill_id)->value('time_id');
  183. //秒杀时间段
  184. $seckillTime = sys_data('Whole_time') ?? [];
  185. $seckillTimeIndex = 0;
  186. $activityTime = [];
  187. if (count($seckillTime)) {
  188. foreach ($seckillTime as $key => &$value) {
  189. $currentHour = date('H');
  190. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  191. if ($activityEndHour > 24) {
  192. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  193. $value['state'] = '即将开始';
  194. $value['status'] = 2;
  195. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  196. } else {
  197. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
  198. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  199. $value['state'] = '抢购中';
  200. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  201. $value['status'] = 1;
  202. if (!$seckillTimeIndex) $seckillTimeIndex = $key;
  203. } else if ($currentHour < (int)$value['time']) {
  204. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  205. $value['state'] = '即将开始';
  206. $value['status'] = 2;
  207. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  208. } else if ($currentHour >= $activityEndHour) {
  209. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  210. $value['state'] = '已结束';
  211. $value['status'] = 0;
  212. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  213. }
  214. }
  215. if ($value['id'] == $time_id) {
  216. $activityTime = $value;
  217. break;
  218. }
  219. }
  220. }
  221. return $activityTime;
  222. }
  223. public static function SaveExcel($where)
  224. {
  225. $model = new self;
  226. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  227. if ($where['store_name'] != '') $model = $model->where('title|id', 'LIKE', "%$where[store_name]%");
  228. $list = $model->order('id desc')->where('is_del', 0)->select();
  229. count($list) && $list = $list->toArray();
  230. $excel = [];
  231. foreach ($list as $item) {
  232. if ($item['status']) {
  233. if ($item['start_time'] > time())
  234. $item['start_name'] = '活动未开始';
  235. else if ($item['stop_time'] < time())
  236. $item['start_name'] = '活动已结束';
  237. else if ($item['stop_time'] > time() && $item['start_time'] < time())
  238. $item['start_name'] = '正在进行中';
  239. } else $item['start_name'] = '关闭';
  240. $excel[] = [
  241. $item['id'],
  242. $item['title'],
  243. $item['info'],
  244. $item['ot_price'],
  245. $item['price'],
  246. $item['stock'],
  247. $item['start_name'],
  248. $item['stop_time'],
  249. $item['stop_time'],
  250. $item['status'] ? '开启' : '关闭',
  251. ];
  252. }
  253. PHPExcelService::setExcelHeader(['编号', '活动标题', '活动简介', '原价', '秒杀价', '库存', '秒杀状态', '结束时间', '状态'])
  254. ->setExcelTile('秒杀产品导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time()))
  255. ->setExcelContent($excel)
  256. ->ExcelSave();
  257. }
  258. /**
  259. * @param $where
  260. * @return array
  261. */
  262. public static function systemPage($where)
  263. {
  264. $model = new self;
  265. $model = $model->alias('s');
  266. if ($where['status'] != '') $model = $model->where('s.status', $where['status']);
  267. if ($where['store_name'] != '') $model = $model->where('s.title|s.id', 'LIKE', "%$where[store_name]%");
  268. $model = $model->page(bcmul($where['page'], $where['limit'], 0), $where['limit']);
  269. $model = $model->order('s.id desc');
  270. $model = $model->where('s.is_del', 0);
  271. return self::page($model, function ($item) {
  272. $item['store_name'] = $item['title'];
  273. if ($item['status']) {
  274. if (strtotime($item['start_time']) > time())
  275. $item['start_name'] = '活动未开始';
  276. else if (bcadd(strtotime($item['stop_time']), 86400) < time())
  277. $item['start_name'] = '活动已结束';
  278. else if (bcadd(strtotime($item['stop_time']), 86400) > time() && strtotime($item['start_time']) < time()) {
  279. $config = SystemGroupData::get($item['time_id']);
  280. if ($config) {
  281. $arr = json_decode($config->value, true);
  282. $now_hour = date('H', time());
  283. $start_hour = $arr['time']['value'];
  284. $continued = $arr['continued']['value'];
  285. $end_hour = $start_hour + $continued;
  286. if ($start_hour > $now_hour) {
  287. $item['start_name'] = '活动未开始';
  288. } elseif ($end_hour < $now_hour) {
  289. $item['start_name'] = '活动已结束';
  290. } else {
  291. $item['start_name'] = '正在进行中';
  292. }
  293. $item['time_id_title'] = $arr['time']['value'];;
  294. } else {
  295. $item['start_name'] = '正在进行中';
  296. }
  297. }
  298. } else $item['start_name'] = '关闭';
  299. }, $where, $where['limit']);
  300. }
  301. /**
  302. * 获取批发产品id
  303. * @return array
  304. */
  305. public static function getWholeIdAll()
  306. {
  307. return self::where('is_del', 0)->column('id', 'id');
  308. }
  309. }