Pxianshigoods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. * 秒杀活动商品模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. *
  13. * ----------------------------------------------------------------------------
  14. *
  15. * ============================================================================
  16. * 数据层模型
  17. */
  18. class Pxianshigoods extends BaseModel
  19. {
  20. public $page_info;
  21. const XIANSHI_GOODS_STATE_CANCEL = 0;
  22. const XIANSHI_GOODS_STATE_NORMAL = 1;
  23. /**
  24. * 读取秒杀商品列表
  25. * @access public
  26. * @author csdeshang
  27. * @param type $condition 条件
  28. * @param type $pagesize 分页
  29. * @param type $order 排序
  30. * @param type $field 字段
  31. * @param type $limit 个数限制
  32. * @return array 秒杀商品列表
  33. */
  34. public function getXianshigoodsList($condition, $pagesize = null, $order = '', $field = '*', $limit = 0)
  35. {
  36. if ($pagesize) {
  37. $res = Db::name('pxianshigoods')->field($field)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  38. $this->page_info = $res;
  39. return $res->items();
  40. } else {
  41. return $xianshigoods_list = Db::name('pxianshigoods')->field($field)->where($condition)->limit($limit)->order($order)->select()->toArray();
  42. }
  43. }
  44. /**
  45. * 读取秒杀商品列表
  46. * @access public
  47. * @author csdeshang
  48. * @param type $condition 查询条件
  49. * @param type $pagesize 分页
  50. * @param type $order 排序
  51. * @param type $field 字段
  52. * @param type $limit 个数限制
  53. * @return array
  54. */
  55. public function getXianshigoodsExtendList($condition, $pagesize = null, $order = '', $field = '*', $limit = 0)
  56. {
  57. $xianshigoods_list = $this->getXianshigoodsList($condition, $pagesize, $order, $field, $limit);
  58. if (!empty($xianshigoods_list)) {
  59. for ($i = 0, $j = count($xianshigoods_list); $i < $j; $i++) {
  60. $xianshigoods_list[$i] = $this->getXianshigoodsExtendInfo($xianshigoods_list[$i]);
  61. }
  62. }
  63. return $xianshigoods_list;
  64. }
  65. /**
  66. * 获取秒杀折列表
  67. * @access public
  68. * @author csdeshang
  69. * @param type $condition 条件
  70. * @param type $pagesize 分页
  71. * @param type $order 排序
  72. * @param type $field 字段
  73. * @param type $limit 限制
  74. * @return type
  75. */
  76. public function getXianshigoodsExtendIds($condition, $pagesize = null, $order = '', $field = 'goods_id', $limit = 0)
  77. {
  78. $xianshigoods_id_list = $this->getXianshigoodsList($condition, $pagesize, $order, $field, $limit);
  79. if (!empty($xianshigoods_id_list)) {
  80. for ($i = 0; $i < count($xianshigoods_id_list); $i++) {
  81. $xianshigoods_id_list[$i] = $xianshigoods_id_list[$i]['goods_id'];
  82. }
  83. }
  84. return $xianshigoods_id_list;
  85. }
  86. /**
  87. * 根据条件读取限制折扣商品信息
  88. * @access public
  89. * @author csdeshang
  90. * @param type $condition 条件
  91. * @return array
  92. */
  93. public function getXianshigoodsInfo($condition)
  94. {
  95. $result = Db::name('pxianshigoods')->where($condition)->find();
  96. return $result;
  97. }
  98. /**
  99. * 根据秒杀商品编号读取限制折扣商品信息
  100. * @access public
  101. * @author csdeshang
  102. * @param type $xianshigoods_id ID编号
  103. * @param type $store_id 店铺ID
  104. * @return type
  105. */
  106. public function getXianshigoodsInfoByID($xianshigoods_id, $store_id = 0)
  107. {
  108. if (intval($xianshigoods_id) <= 0) {
  109. return null;
  110. }
  111. $condition = array();
  112. $condition[] = array('xianshigoods_id', '=', $xianshigoods_id);
  113. $xianshigoods_info = $this->getXianshigoodsInfo($condition);
  114. if ($store_id > 0 && $xianshigoods_info['store_id'] != $store_id) {
  115. return null;
  116. } else {
  117. return $xianshigoods_info;
  118. }
  119. }
  120. /**
  121. * @access public
  122. * @author csdeshang
  123. * 增加秒杀商品
  124. * @param type $xianshigoods_info 秒杀商品信息
  125. * @return bool
  126. */
  127. public function addXianshigoods($xianshigoods_info)
  128. {
  129. $xianshigoods_info['xianshigoods_state'] = self::XIANSHI_GOODS_STATE_NORMAL;
  130. $xianshigoods_id = Db::name('pxianshigoods')->insertGetId($xianshigoods_info);
  131. if ($xianshigoods_id) {
  132. // 发布秒杀锁定商品
  133. $this->_lockGoods($xianshigoods_info['goods_commonid'], $xianshigoods_info['goods_id']);
  134. }
  135. // 删除商品秒杀缓存
  136. $this->_dGoodsXianshiCache($xianshigoods_info['goods_id']);
  137. $xianshigoods_info['xianshigoods_id'] = $xianshigoods_id;
  138. $xianshigoods_info = $this->getXianshigoodsExtendInfo($xianshigoods_info);
  139. return $xianshigoods_info;
  140. }
  141. /**
  142. * 更新
  143. * @access public
  144. * @author csdeshang
  145. * @param type $update 数据更新
  146. * @param type $condition 条件
  147. * @return type
  148. */
  149. public function editXianshigoods($update, $condition)
  150. {
  151. $result = Db::name('pxianshigoods')->where($condition)->update($update);
  152. if ($result) {
  153. $xianshigoods_list = $this->getXianshigoodsList($condition, null, '', 'goods_id');
  154. if (!empty($xianshigoods_list)) {
  155. foreach ($xianshigoods_list as $val) {
  156. // 删除商品秒杀缓存
  157. $this->_dGoodsXianshiCache($val['goods_id']);
  158. // 插入对列 更新促销价格
  159. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'updateGoodsPromotionPriceByGoodsId', 'cron_value' => serialize($val['goods_id'])));
  160. }
  161. }
  162. }
  163. return $result;
  164. }
  165. /**
  166. * 删除
  167. * @access public
  168. * @author csdeshang
  169. * @param type $condition 条件
  170. * @return type
  171. */
  172. public function delXianshigoods($condition)
  173. {
  174. $xianshigoods_list = $this->getXianshigoodsList($condition, null, '', 'goods_id,goods_commonid');
  175. $result = Db::name('pxianshigoods')->where($condition)->delete();
  176. if ($result) {
  177. if (!empty($xianshigoods_list)) {
  178. foreach ($xianshigoods_list as $val) {
  179. $this->_unlockGoods($val['goods_commonid'], $val['goods_id']);
  180. // 删除商品秒杀缓存
  181. $this->_dGoodsXianshiCache($val['goods_id']);
  182. // 插入对列 更新促销价格
  183. model('cron')->addCron(array('cron_exetime' => TIMESTAMP, 'cron_type' => 'updateGoodsPromotionPriceByGoodsId', 'cron_value' => serialize($val['goods_id'])));
  184. }
  185. }
  186. }
  187. return $result;
  188. }
  189. /**
  190. * 获取秒杀商品扩展信息
  191. * @access public
  192. * @author csdeshang
  193. * @param type $xianshi_info 信息
  194. * @return string
  195. */
  196. public function getXianshigoodsExtendInfo($xianshi_info)
  197. {
  198. $xianshi_info['goods_url'] = (string)url('/home/Goods/index', array('goods_id' => $xianshi_info['goods_id']));
  199. $xianshi_info['image_url'] = goods_cthumb($xianshi_info['goods_image'], 60, $xianshi_info['store_id']);
  200. $xianshi_info['xianshigoods_price'] = ds_price_format($xianshi_info['xianshigoods_price']);
  201. $xianshi_info['xianshi_discount'] = number_format($xianshi_info['xianshigoods_price'] / $xianshi_info['goods_price'] * 10, 1, '.', '') . '折';
  202. return $xianshi_info;
  203. }
  204. /**
  205. * 获取推荐秒杀商品
  206. * @access public
  207. * @author csdeshang
  208. * @param type $count 推荐数量
  209. * @return type
  210. */
  211. public function getXianshigoodsCommendList($count = 4)
  212. {
  213. $condition = array();
  214. $condition[] = array('xianshigoods_state', '=', self::XIANSHI_GOODS_STATE_NORMAL);
  215. $condition[] = array('xianshigoods_starttime', '<', TIMESTAMP);
  216. $condition[] = array('xianshigoods_end_time', '>', TIMESTAMP);
  217. $xianshi_list = $this->getXianshigoodsExtendList($condition, null, 'xianshigoods_recommend desc', '*', $count);
  218. return $xianshi_list;
  219. }
  220. /**
  221. * 锁定商品
  222. * @access private
  223. * @author csdeshang
  224. * @param type $goods_commonid 商品编号
  225. */
  226. private function _lockGoods($goods_commonid, $goods_id)
  227. {
  228. $condition = array();
  229. $condition[] = array('goods_commonid', '=', $goods_commonid);
  230. $goods_model = model('goods');
  231. $goods_model->editGoodsCommonLock($condition);
  232. $condition = array();
  233. $condition[] = array('goods_id', '=', $goods_id);
  234. $goods_model->editGoodsLock($condition);
  235. }
  236. /**
  237. * 解锁商品
  238. * @access private
  239. * @author csdeshang
  240. * @param type $goods_commonid 商品编号ID
  241. */
  242. private function _unlockGoods($goods_commonid, $goods_id)
  243. {
  244. $goods_model = model('goods');
  245. $goods_model->editGoodsUnlock(array('goods_id' => $goods_id));
  246. if (!$goods_model->getGoodsCount(array('goods_commonid' => $goods_commonid, 'goods_lock' => 1))) {
  247. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  248. }
  249. }
  250. /**
  251. * 根据商品编号查询是否有可用秒杀活动,如果有返回秒杀活动,没有返回null
  252. * @access public
  253. * @author csdeshang
  254. * @param type $goods_id 商品id
  255. * @return array
  256. */
  257. public function getXianshigoodsInfoByGoodsID($goods_id)
  258. {
  259. $info = $this->_rGoodsXianshiCache($goods_id);
  260. if (empty($info)) {
  261. $condition = array();
  262. $condition[] = array('xianshigoods_state', '=', self::XIANSHI_GOODS_STATE_NORMAL);
  263. $condition[] = array('xianshigoods_end_time', '>', TIMESTAMP);
  264. $condition[] = array('goods_id', '=', $goods_id);
  265. $xianshigoods_list = $this->getXianshigoodsExtendList($condition, null, 'xianshigoods_starttime asc', '*', 1);
  266. $info['info'] = isset($xianshigoods_list[0]) ? serialize($xianshigoods_list[0]) : serialize("");
  267. $this->_wGoodsXianshiCache($goods_id, $info);
  268. }
  269. $xianshigoods_info = unserialize($info['info']);
  270. if (!empty($xianshigoods_info) && ($xianshigoods_info['xianshigoods_starttime'] > TIMESTAMP || $xianshigoods_info['xianshigoods_end_time'] < TIMESTAMP)) {
  271. $xianshigoods_info = array();
  272. }
  273. return $xianshigoods_info;
  274. }
  275. /**
  276. * 根据商品编号查询是否有可用秒杀活动,如果有返回秒杀活动,没有返回null
  277. * @access public
  278. * @author csdeshang
  279. * @param type $goods_string 商品编号字符串,例:'11,22,33'
  280. * @return type
  281. */
  282. public function getXianshigoodsListByGoodsString($goods_string)
  283. {
  284. $xianshigoods_list = $this->_getXianshigoodsListByGoods($goods_string);
  285. $xianshigoods_list = array_under_reset($xianshigoods_list, 'goods_id');
  286. return $xianshigoods_list;
  287. }
  288. /**
  289. * 根据商品编号查询是否有可用秒杀活动,如果有返回秒杀活动,没有返回null
  290. * @access public
  291. * @author csdeshang
  292. * @param type $goods_id_string 商品编号字符串
  293. * @return type
  294. */
  295. private function _getXianshigoodsListByGoods($goods_id_string)
  296. {
  297. $condition = array();
  298. $condition[] = array('xianshigoods_state', '=', self::XIANSHI_GOODS_STATE_NORMAL);
  299. $condition[] = array('xianshigoods_starttime', '<', TIMESTAMP);
  300. $condition[] = array('xianshigoods_end_time', '>', TIMESTAMP);
  301. $condition[] = array('goods_id', 'in', $goods_id_string);
  302. $xianshigoods_list = $this->getXianshigoodsExtendList($condition, null, 'xianshigoods_id desc', '*');
  303. return $xianshigoods_list;
  304. }
  305. /**
  306. * 读取商品秒杀缓存
  307. * @access public
  308. * @author csdeshang
  309. * @param type $goods_id 商品id
  310. * @return type
  311. */
  312. private function _rGoodsXianshiCache($goods_id)
  313. {
  314. return rcache($goods_id, 'goods_xianshi');
  315. }
  316. /**
  317. * 写入商品秒杀缓存
  318. * @access public
  319. * @author csdeshang
  320. * @param int $goods_id 商品id
  321. * @param array $info 信息
  322. * @return boolean
  323. */
  324. private function _wGoodsXianshiCache($goods_id, $info)
  325. {
  326. return wcache($goods_id, $info, 'goods_xianshi');
  327. }
  328. /**
  329. * 删除商品秒杀缓存
  330. * @access public
  331. * @author csdeshang
  332. * @param type $goods_id 商品id
  333. * @return bool
  334. */
  335. private function _dGoodsXianshiCache($goods_id)
  336. {
  337. return dcache($goods_id, 'goods_xianshi');
  338. }
  339. }