Pbargain.php 11 KB

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