Pbargain.php 11 KB

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