Pxianshigoods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * 秒杀活动商品模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. * DSMall多用户商城
  11. * ============================================================================
  12. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  13. * 网站地址: http://www.csdeshang.com
  14. * ----------------------------------------------------------------------------
  15. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  16. * 不允许对程序代码以任何形式任何目的的再发布。
  17. * ============================================================================
  18. * 数据层模型
  19. */
  20. class Pxianshigoods extends BaseModel {
  21. public $page_info;
  22. const XIANSHI_GOODS_STATE_CANCEL = 0;
  23. const XIANSHI_GOODS_STATE_NORMAL = 1;
  24. /**
  25. * 读取秒杀商品列表
  26. * @access public
  27. * @author csdeshang
  28. * @param type $condition 条件
  29. * @param type $pagesize 分页
  30. * @param type $order 排序
  31. * @param type $field 字段
  32. * @param type $limit 个数限制
  33. * @return array 秒杀商品列表
  34. */
  35. public function getXianshigoodsList($condition, $pagesize = null, $order = '', $field = '*', $limit = 0) {
  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. $xianshigoods_list = $this->getXianshigoodsList($condition, $pagesize, $order, $field, $limit);
  57. if (!empty($xianshigoods_list)) {
  58. for ($i = 0, $j = count($xianshigoods_list); $i < $j; $i++) {
  59. $xianshigoods_list[$i] = $this->getXianshigoodsExtendInfo($xianshigoods_list[$i]);
  60. }
  61. }
  62. return $xianshigoods_list;
  63. }
  64. /**
  65. * 获取秒杀折列表
  66. * @access public
  67. * @author csdeshang
  68. * @param type $condition 条件
  69. * @param type $pagesize 分页
  70. * @param type $order 排序
  71. * @param type $field 字段
  72. * @param type $limit 限制
  73. * @return type
  74. */
  75. public function getXianshigoodsExtendIds($condition, $pagesize = null, $order = '', $field = 'goods_id', $limit = 0) {
  76. $xianshigoods_id_list = $this->getXianshigoodsList($condition, $pagesize, $order, $field, $limit);
  77. if (!empty($xianshigoods_id_list)) {
  78. for ($i = 0; $i < count($xianshigoods_id_list); $i++) {
  79. $xianshigoods_id_list[$i] = $xianshigoods_id_list[$i]['goods_id'];
  80. }
  81. }
  82. return $xianshigoods_id_list;
  83. }
  84. /**
  85. * 根据条件读取限制折扣商品信息
  86. * @access public
  87. * @author csdeshang
  88. * @param type $condition 条件
  89. * @return array
  90. */
  91. public function getXianshigoodsInfo($condition) {
  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. if (intval($xianshigoods_id) <= 0) {
  105. return null;
  106. }
  107. $condition = array();
  108. $condition[] = array('xianshigoods_id','=',$xianshigoods_id);
  109. $xianshigoods_info = $this->getXianshigoodsInfo($condition);
  110. if ($store_id > 0 && $xianshigoods_info['store_id'] != $store_id) {
  111. return null;
  112. } else {
  113. return $xianshigoods_info;
  114. }
  115. }
  116. /**
  117. * @access public
  118. * @author csdeshang
  119. * 增加秒杀商品
  120. * @param type $xianshigoods_info 秒杀商品信息
  121. * @return bool
  122. */
  123. public function addXianshigoods($xianshigoods_info) {
  124. $xianshigoods_info['xianshigoods_state'] = self::XIANSHI_GOODS_STATE_NORMAL;
  125. $xianshigoods_id = Db::name('pxianshigoods')->insertGetId($xianshigoods_info);
  126. if($xianshigoods_id){
  127. // 发布秒杀锁定商品
  128. $this->_lockGoods($xianshigoods_info['goods_commonid'],$xianshigoods_info['goods_id']);
  129. }
  130. // 删除商品秒杀缓存
  131. $this->_dGoodsXianshiCache($xianshigoods_info['goods_id']);
  132. $xianshigoods_info['xianshigoods_id'] = $xianshigoods_id;
  133. $xianshigoods_info = $this->getXianshigoodsExtendInfo($xianshigoods_info);
  134. return $xianshigoods_info;
  135. }
  136. /**
  137. * 更新
  138. * @access public
  139. * @author csdeshang
  140. * @param type $update 数据更新
  141. * @param type $condition 条件
  142. * @return type
  143. */
  144. public function editXianshigoods($update, $condition) {
  145. $result = Db::name('pxianshigoods')->where($condition)->update($update);
  146. if ($result) {
  147. $xianshigoods_list = $this->getXianshigoodsList($condition, null, '', 'goods_id');
  148. if (!empty($xianshigoods_list)) {
  149. foreach ($xianshigoods_list as $val) {
  150. // 删除商品秒杀缓存
  151. $this->_dGoodsXianshiCache($val['goods_id']);
  152. // 插入对列 更新促销价格
  153. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'updateGoodsPromotionPriceByGoodsId','cron_value'=>serialize($val['goods_id'])));
  154. }
  155. }
  156. }
  157. return $result;
  158. }
  159. /**
  160. * 删除
  161. * @access public
  162. * @author csdeshang
  163. * @param type $condition 条件
  164. * @return type
  165. */
  166. public function delXianshigoods($condition) {
  167. $xianshigoods_list = $this->getXianshigoodsList($condition, null, '', 'goods_id,goods_commonid');
  168. $result = Db::name('pxianshigoods')->where($condition)->delete();
  169. if ($result) {
  170. if (!empty($xianshigoods_list)) {
  171. foreach ($xianshigoods_list as $val) {
  172. $this->_unlockGoods($val['goods_commonid'],$val['goods_id']);
  173. // 删除商品秒杀缓存
  174. $this->_dGoodsXianshiCache($val['goods_id']);
  175. // 插入对列 更新促销价格
  176. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'updateGoodsPromotionPriceByGoodsId','cron_value'=>serialize($val['goods_id'])));
  177. }
  178. }
  179. }
  180. return $result;
  181. }
  182. /**
  183. * 获取秒杀商品扩展信息
  184. * @access public
  185. * @author csdeshang
  186. * @param type $xianshi_info 信息
  187. * @return string
  188. */
  189. public function getXianshigoodsExtendInfo($xianshi_info) {
  190. $xianshi_info['goods_url'] = (string)url('/home/Goods/index', array('goods_id' => $xianshi_info['goods_id']));
  191. $xianshi_info['image_url'] = goods_cthumb($xianshi_info['goods_image'], 60, $xianshi_info['store_id']);
  192. $xianshi_info['xianshigoods_price'] = ds_price_format($xianshi_info['xianshigoods_price']);
  193. $xianshi_info['xianshi_discount'] = number_format($xianshi_info['xianshigoods_price'] / $xianshi_info['goods_price'] * 10, 1, '.', '') . '折';
  194. return $xianshi_info;
  195. }
  196. /**
  197. * 获取推荐秒杀商品
  198. * @access public
  199. * @author csdeshang
  200. * @param type $count 推荐数量
  201. * @return type
  202. */
  203. public function getXianshigoodsCommendList($count = 4) {
  204. $condition = array();
  205. $condition[] = array('xianshigoods_state', '=', self::XIANSHI_GOODS_STATE_NORMAL);
  206. $condition[] = array('xianshigoods_starttime', '<', TIMESTAMP);
  207. $condition[] = array('xianshigoods_end_time', '>', TIMESTAMP);
  208. $xianshi_list = $this->getXianshigoodsExtendList($condition, null, 'xianshigoods_recommend desc', '*', $count);
  209. return $xianshi_list;
  210. }
  211. /**
  212. * 锁定商品
  213. * @access private
  214. * @author csdeshang
  215. * @param type $goods_commonid 商品编号
  216. */
  217. private function _lockGoods($goods_commonid,$goods_id)
  218. {
  219. $condition = array();
  220. $condition[] = array('goods_commonid','=',$goods_commonid);
  221. $goods_model = model('goods');
  222. $goods_model->editGoodsCommonLock($condition);
  223. $condition = array();
  224. $condition[] = array('goods_id','=',$goods_id);
  225. $goods_model->editGoodsLock($condition);
  226. }
  227. /**
  228. * 解锁商品
  229. * @access private
  230. * @author csdeshang
  231. * @param type $goods_commonid 商品编号ID
  232. */
  233. private function _unlockGoods($goods_commonid,$goods_id)
  234. {
  235. $goods_model = model('goods');
  236. $goods_model->editGoodsUnlock(array('goods_id' => $goods_id));
  237. if(!$goods_model->getGoodsCount(array('goods_commonid' => $goods_commonid,'goods_lock'=>1))){
  238. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  239. }
  240. }
  241. /**
  242. * 根据商品编号查询是否有可用秒杀活动,如果有返回秒杀活动,没有返回null
  243. * @access public
  244. * @author csdeshang
  245. * @param type $goods_id 商品id
  246. * @return array
  247. */
  248. public function getXianshigoodsInfoByGoodsID($goods_id) {
  249. $info = $this->_rGoodsXianshiCache($goods_id);
  250. if (empty($info)) {
  251. $condition = array();
  252. $condition[] = array('xianshigoods_state','=',self::XIANSHI_GOODS_STATE_NORMAL);
  253. $condition[] = array('xianshigoods_end_time','>',TIMESTAMP);
  254. $condition[] = array('goods_id','=',$goods_id);
  255. $xianshigoods_list = $this->getXianshigoodsExtendList($condition, null, 'xianshigoods_starttime asc', '*', 1);
  256. $info['info'] = isset($xianshigoods_list[0]) ? serialize($xianshigoods_list[0]) : serialize("");
  257. $this->_wGoodsXianshiCache($goods_id, $info);
  258. }
  259. $xianshigoods_info = unserialize($info['info']);
  260. if (!empty($xianshigoods_info) && ($xianshigoods_info['xianshigoods_starttime'] > TIMESTAMP || $xianshigoods_info['xianshigoods_end_time'] < TIMESTAMP)) {
  261. $xianshigoods_info = array();
  262. }
  263. return $xianshigoods_info;
  264. }
  265. /**
  266. * 根据商品编号查询是否有可用秒杀活动,如果有返回秒杀活动,没有返回null
  267. * @access public
  268. * @author csdeshang
  269. * @param type $goods_string 商品编号字符串,例:'11,22,33'
  270. * @return type
  271. */
  272. public function getXianshigoodsListByGoodsString($goods_string) {
  273. $xianshigoods_list = $this->_getXianshigoodsListByGoods($goods_string);
  274. $xianshigoods_list = array_under_reset($xianshigoods_list, 'goods_id');
  275. return $xianshigoods_list;
  276. }
  277. /**
  278. * 根据商品编号查询是否有可用秒杀活动,如果有返回秒杀活动,没有返回null
  279. * @access public
  280. * @author csdeshang
  281. * @param type $goods_id_string 商品编号字符串
  282. * @return type
  283. */
  284. private function _getXianshigoodsListByGoods($goods_id_string) {
  285. $condition = array();
  286. $condition[] = array('xianshigoods_state','=',self::XIANSHI_GOODS_STATE_NORMAL);
  287. $condition[] = array('xianshigoods_starttime','<',TIMESTAMP);
  288. $condition[] = array('xianshigoods_end_time','>',TIMESTAMP);
  289. $condition[] = array('goods_id','in', $goods_id_string);
  290. $xianshigoods_list = $this->getXianshigoodsExtendList($condition, null, 'xianshigoods_id desc', '*');
  291. return $xianshigoods_list;
  292. }
  293. /**
  294. * 读取商品秒杀缓存
  295. * @access public
  296. * @author csdeshang
  297. * @param type $goods_id 商品id
  298. * @return type
  299. */
  300. private function _rGoodsXianshiCache($goods_id) {
  301. return rcache($goods_id, 'goods_xianshi');
  302. }
  303. /**
  304. * 写入商品秒杀缓存
  305. * @access public
  306. * @author csdeshang
  307. * @param int $goods_id 商品id
  308. * @param array $info 信息
  309. * @return boolean
  310. */
  311. private function _wGoodsXianshiCache($goods_id, $info) {
  312. return wcache($goods_id, $info, 'goods_xianshi');
  313. }
  314. /**
  315. * 删除商品秒杀缓存
  316. * @access public
  317. * @author csdeshang
  318. * @param type $goods_id 商品id
  319. * @return bool
  320. */
  321. private function _dGoodsXianshiCache($goods_id) {
  322. return dcache($goods_id, 'goods_xianshi');
  323. }
  324. }