Pbargain.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 Pbargain extends BaseModel {
  20. public $page_info;
  21. const PINTUAN_STATE_CANCEL = 0;
  22. const PINTUAN_STATE_TO_BEGIN = 1;
  23. const PINTUAN_STATE_NORMAL = 2;
  24. const PINTUAN_STATE_END = 3;
  25. private $bargain_state_array = array(
  26. self::PINTUAN_STATE_CANCEL => '已取消',
  27. self::PINTUAN_STATE_TO_BEGIN => '待开始',
  28. self::PINTUAN_STATE_NORMAL => '进行中',
  29. self::PINTUAN_STATE_END => '已结束'
  30. );
  31. /**
  32. * 读取砍价列表
  33. * @access public
  34. * @author csdeshang
  35. * @param array $condition 查询条件
  36. * @param int $pagesize 分页数
  37. * @param string $order 排序
  38. * @param string $field 所需字段
  39. * @return array 砍价列表
  40. */
  41. public function getBargainList($condition, $pagesize = null, $order = 'bargain_id desc', $field = '*') {
  42. if($pagesize){
  43. $res = Db::name('pbargain')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  44. $bargain_list = $res->items();
  45. $this->page_info = $res;
  46. return $bargain_list;
  47. }else{
  48. return Db::name('pbargain')->field($field)->where($condition)->order($order)->select()->toArray();
  49. }
  50. }
  51. /**
  52. * 读取砍价列表
  53. * @access public
  54. * @author csdeshang
  55. * @param array $condition 查询条件
  56. * @param int $pagesize 分页数
  57. * @param string $order 排序
  58. * @param string $field 所需字段
  59. * @return array 砍价列表
  60. */
  61. public function getOnlineBargainList($condition, $pagesize = null, $order = 'bargain_id desc', $field = '*') {
  62. $condition[]=array('bargain_state','=',self::PINTUAN_STATE_NORMAL);
  63. $condition[]=array('bargain_endtime','>',TIMESTAMP);
  64. $bargain_list = $this->getBargainList($condition, $pagesize, $order, $field);
  65. return $bargain_list;
  66. }
  67. /**
  68. * 根据条件读取砍价信息
  69. * @access public
  70. * @author csdeshang
  71. * @param array $condition 查询条件
  72. * @return array 砍价信息
  73. */
  74. public function getBargainInfo($condition) {
  75. $bargain_info = Db::name('pbargain')->where($condition)->find();
  76. return $bargain_info;
  77. }
  78. /**
  79. * 根据砍价编号读取砍价信息
  80. * @access public
  81. * @author csdeshang
  82. * @param array $bargain_id 砍价活动编号
  83. * @param int $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  84. * @return array 砍价信息
  85. */
  86. public function getBargainInfoByID($bargain_id, $store_id = 0) {
  87. if (intval($bargain_id) <= 0) {
  88. return null;
  89. }
  90. $condition = array();
  91. $condition[] = array('bargain_id','=',$bargain_id);
  92. $bargain_info = $this->getBargainInfo($condition);
  93. if ($store_id > 0 && $bargain_info['store_id'] != $store_id) {
  94. return null;
  95. } else {
  96. return $bargain_info;
  97. }
  98. }
  99. public function getOnlineBargainInfoByID($bargain_id){
  100. if (intval($bargain_id) <= 0) {
  101. return null;
  102. }
  103. $condition = array();
  104. $condition[] = array('bargain_id','=',$bargain_id);
  105. $condition[] = array('bargain_state','=',self::PINTUAN_STATE_NORMAL);
  106. $condition[] = array('bargain_endtime','>',TIMESTAMP);
  107. $bargain_info = $this->getBargainInfo($condition);
  108. return $bargain_info;
  109. }
  110. /**
  111. * 砍价状态数组
  112. * @access public
  113. * @author csdeshang
  114. * @return type
  115. */
  116. public function getBargainStateArray() {
  117. return $this->bargain_state_array;
  118. }
  119. /**
  120. * 增加
  121. * @access public
  122. * @author csdeshang
  123. * @param array $data 数据
  124. * @return type
  125. */
  126. public function addBargain($data) {
  127. $flag= Db::name('pbargain')->insertGetId($data);
  128. if($flag){
  129. // 发布砍价锁定商品
  130. $this->_lockGoods($data['bargain_goods_commonid'],$data['bargain_goods_id']);
  131. }
  132. return $flag;
  133. }
  134. /**
  135. * 编辑更新
  136. * @param type $update 更新数据
  137. * @param type $condition 条件
  138. * @return type
  139. */
  140. public function editBargain($update, $condition) {
  141. $goods_ids=Db::name('pbargain')->where($condition)->column('bargain_goods_id');
  142. foreach($goods_ids as $goods_id){
  143. $this->_dGoodsBargainCache($goods_id);
  144. }
  145. return Db::name('pbargain')->where($condition)->update($update);
  146. }
  147. /**
  148. * 指定砍价活动结束,参团成功的继续参团,不成功的保持默认.
  149. * @access public
  150. * @author csdeshang
  151. * @param type $condition
  152. * @return type
  153. */
  154. public function endBargain($condition=array()) {
  155. $condition[]=array('bargain_state','=',self::PINTUAN_STATE_NORMAL);
  156. $goods_commonid=Db::name('pbargain')->where($condition)->column('bargain_goods_commonid');
  157. $goods_id=Db::name('pbargain')->where($condition)->column('bargain_goods_id');
  158. $data['bargain_state'] = self::PINTUAN_STATE_END;
  159. $flag= Db::name('pbargain')->where($condition)->update($data);
  160. if($flag){
  161. if(!empty($goods_commonid)){
  162. $this->_unlockGoods($goods_commonid,$goods_id);
  163. }
  164. }
  165. return $flag;
  166. }
  167. /**
  168. * 删除砍价活动
  169. * @access public
  170. * @author csdeshang
  171. * @param array $condition 检索条件
  172. * @return boolean
  173. */
  174. public function delBargain($condition) {
  175. return Db::name('pbargain')->where($condition)->delete();
  176. }
  177. /**
  178. * 取消砍价活动
  179. * @access public
  180. * @author csdeshang
  181. * @param type $condition 条件
  182. * @return type
  183. */
  184. public function cancelBargain($condition) {
  185. $goods_commonid = Db::name('pbargain')->where($condition)->column('bargain_goods_commonid');
  186. $goods_id = Db::name('pbargain')->where($condition)->column('bargain_goods_id');
  187. $update = array();
  188. $update['bargain_state'] = self::PINTUAN_STATE_CANCEL;
  189. $flag= $this->editBargain($update, $condition);
  190. if($flag){
  191. if(!empty($goods_commonid)){
  192. $this->_unlockGoods($goods_commonid,$goods_id);
  193. }
  194. }
  195. return $flag;
  196. }
  197. /**
  198. * 锁定商品
  199. * @access private
  200. * @author csdeshang
  201. * @param type $goods_commonid 商品编号
  202. */
  203. private function _lockGoods($goods_commonid,$goods_id)
  204. {
  205. $condition = array();
  206. $condition[] = array('goods_commonid','=',$goods_commonid);
  207. $goods_model = model('goods');
  208. $goods_model->editGoodsCommonLock($condition);
  209. $condition = array();
  210. $condition[] = array('goods_id','=',$goods_id);
  211. $goods_model->editGoodsLock($condition);
  212. }
  213. /**
  214. * 解锁商品
  215. * @access private
  216. * @author csdeshang
  217. * @param type $goods_commonid 商品编号ID
  218. */
  219. private function _unlockGoods($goods_commonid,$goods_id)
  220. {
  221. $goods_model = model('goods');
  222. $goods_model->editGoodsUnlock(array(array('goods_id' ,'in', $goods_id)));
  223. $temp=Db::name('goods')->where(array(array('goods_id','in',$goods_id),array('goods_lock','=',1)))->column('goods_commonid');
  224. if(!empty($temp)){
  225. $goods_commonid=array_diff($goods_commonid,$temp);
  226. }
  227. if(!empty($goods_commonid)){
  228. $goods_model->editGoodsCommonUnlock(array(array('goods_commonid' ,'in', $goods_commonid)));
  229. }
  230. }
  231. /**
  232. * 获取砍价是否可编辑状态
  233. * @access public
  234. * @author csdeshang
  235. * @param type $bargain_info 砍价信息
  236. * @return boolean
  237. */
  238. public function getBargainBtn($bargain_info) {
  239. if (!$bargain_info) {
  240. return false;
  241. }
  242. if ($bargain_info['bargain_state'] == self::PINTUAN_STATE_TO_BEGIN && $bargain_info['bargain_begintime'] > TIMESTAMP) {
  243. $bargain_info['editable'] = true;
  244. } else {
  245. $bargain_info['editable'] = false;
  246. }
  247. return $bargain_info;
  248. }
  249. /**
  250. * 获取状态文字
  251. * @access public
  252. * @author csdeshang
  253. * @param type $bargain_info 砍价信息
  254. * @return boolean
  255. */
  256. public function getBargainStateText($bargain_info) {
  257. if (!$bargain_info) {
  258. return false;
  259. }
  260. $bargain_state_text = $this->bargain_state_array[$bargain_info['bargain_state']];
  261. return $bargain_state_text;
  262. }
  263. /**
  264. * 根据商品编号查询是否有可用砍价活动,如果有返回抢购信息,没有返回null
  265. * @param type $goods_id 商品id
  266. * @return array
  267. */
  268. public function getBargainInfoByGoodsID($goods_id) {
  269. $info = $this->_rGoodsBargainCache($goods_id);
  270. if (empty($info)) {
  271. $condition = array();
  272. $condition[] = array('bargain_goods_id','=',$goods_id);
  273. $condition[] = array('bargain_state','=',self::PINTUAN_STATE_NORMAL);
  274. $condition[] = array('bargain_endtime','>',TIMESTAMP);
  275. $bargain_info = $this->getBargainInfo($condition);
  276. //序列化存储到缓存
  277. $info['info'] = serialize($bargain_info);
  278. $this->_wGoodsBargainCache($goods_id, $info);
  279. }
  280. $bargain_goods_info = unserialize($info['info']);
  281. if (!empty($bargain_goods_info) && ($bargain_goods_info['bargain_state']!=2 || $bargain_goods_info['bargain_begintime'] > TIMESTAMP || $bargain_goods_info['bargain_endtime'] < TIMESTAMP)) {
  282. $bargain_goods_info = array();
  283. }
  284. return $bargain_goods_info;
  285. }
  286. /**
  287. * 读取商品抢购缓存
  288. * @access public
  289. * @author csdeshang
  290. * @param type $goods_id 商品id
  291. * @return type
  292. */
  293. private function _rGoodsBargainCache($goods_id) {
  294. return rcache($goods_id, 'goods_bargain');
  295. }
  296. /**
  297. * 写入商品抢购缓存
  298. * @access public
  299. * @author csdeshang
  300. * @param type $goods_id ID
  301. * @param type $info 信息
  302. * @return type
  303. */
  304. private function _wGoodsBargainCache($goods_id, $info) {
  305. return wcache($goods_id, $info, 'goods_bargain');
  306. }
  307. /**
  308. * 删除商品抢购缓存
  309. * @access public
  310. * @author csdeshang
  311. * @param int $goods_id 商品ID
  312. * @return boolean
  313. */
  314. public function _dGoodsBargainCache($goods_id) {
  315. return dcache($goods_id, 'goods_bargain');
  316. }
  317. }