Pxianshigoods.php 13 KB

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