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