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