Ppintuan.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 Ppintuan extends BaseModel
  19. {
  20. public $page_info;
  21. const PINTUAN_STATE_CLOSE = 0;
  22. const PINTUAN_STATE_NORMAL = 1;
  23. const PINTUAN_STATE_CANCEL = 2;
  24. private $pintuan_state_array = array(
  25. self::PINTUAN_STATE_CLOSE => '已结束',
  26. self::PINTUAN_STATE_NORMAL => '正常',
  27. self::PINTUAN_STATE_CANCEL => '管理员关闭'
  28. );
  29. /**
  30. * 读取拼团列表
  31. * @access public
  32. * @author csdeshang
  33. * @param array $condition 查询条件
  34. * @param int $pagesize 分页数
  35. * @param string $order 排序
  36. * @param string $field 所需字段
  37. * @return array 拼团列表
  38. */
  39. public function getPintuanList($condition, $pagesize = null, $order = '', $field = '*')
  40. {
  41. if ($pagesize) {
  42. $res = Db::name('ppintuan')->field($field)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  43. $pintuan_list = $res->items();
  44. $this->page_info = $res;
  45. } else {
  46. $pintuan_list = Db::name('ppintuan')->field($field)->where($condition)->order($order)->select()->toArray();
  47. }
  48. if (!empty($pintuan_list)) {
  49. for ($i = 0, $j = count($pintuan_list); $i < $j; $i++) {
  50. $pintuan_list[$i] = $this->getPintuanExtendInfo($pintuan_list[$i]);
  51. }
  52. }
  53. return $pintuan_list;
  54. }
  55. /**
  56. * 根据条件读取限制折扣信息
  57. * @access public
  58. * @author csdeshang
  59. * @param array $condition 查询条件
  60. * @return array 拼团信息
  61. */
  62. public function getPintuanInfo($condition)
  63. {
  64. $pintuan_info = Db::name('ppintuan')->where($condition)->find();
  65. if (!empty($pintuan_info)) {
  66. $pintuan_info = $this->getPintuanExtendInfo($pintuan_info);
  67. }
  68. return $pintuan_info;
  69. }
  70. /**
  71. * 根据拼团编号读取限制折扣信息
  72. * @access public
  73. * @author csdeshang
  74. * @param array $pintuan_id 限制折扣活动编号
  75. * @param int $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  76. * @return array 拼团信息
  77. */
  78. public function getPintuanInfoByID($pintuan_id, $store_id = 0)
  79. {
  80. if (intval($pintuan_id) <= 0) {
  81. return null;
  82. }
  83. $condition = array();
  84. $condition[] = array('pintuan_id', '=', $pintuan_id);
  85. $pintuan_info = $this->getPintuanInfo($condition);
  86. if ($store_id > 0 && $pintuan_info['store_id'] != $store_id) {
  87. return null;
  88. } else {
  89. return $pintuan_info;
  90. }
  91. }
  92. /**
  93. * 拼团状态数组
  94. * @access public
  95. * @author csdeshang
  96. * @return type
  97. */
  98. public function getPintuanStateArray()
  99. {
  100. return $this->pintuan_state_array;
  101. }
  102. /**
  103. * 增加
  104. * @access public
  105. * @author csdeshang
  106. * @param array $data 数据
  107. * @return type
  108. */
  109. public function addPintuan($data)
  110. {
  111. $data['pintuan_state'] = self::PINTUAN_STATE_NORMAL;
  112. $flag = Db::name('ppintuan')->insertGetId($data);
  113. if ($flag) {
  114. // 发布拼团锁定商品
  115. $this->_lockGoods($data['pintuan_goods_commonid']);
  116. }
  117. return $flag;
  118. }
  119. /**
  120. * 删除活动
  121. * @author csdeshang
  122. * @param type $condition 删除条件
  123. * @return type
  124. */
  125. public function delPintuan($condition)
  126. {
  127. return Db::name('ppintuan')->where($condition)->delete();
  128. }
  129. /**
  130. * 锁定商品
  131. * @access private
  132. * @author csdeshang
  133. * @param type $goods_commonid 商品编号
  134. */
  135. private function _lockGoods($goods_commonid)
  136. {
  137. $condition = array();
  138. $condition[] = array('goods_commonid', '=', $goods_commonid);
  139. $goods_model = model('goods');
  140. $goods_model->editGoodsCommonLock($condition);
  141. $goods_model->editGoodsLock($condition);
  142. }
  143. /**
  144. * 解锁商品
  145. * @access private
  146. * @author csdeshang
  147. * @param type $goods_commonid 商品编号ID
  148. */
  149. private function _unlockGoods($goods_commonid)
  150. {
  151. $goods_model = model('goods');
  152. $goods_model->editGoodsCommonUnlock(array(array('goods_commonid', 'in', $goods_commonid)));
  153. $goods_model->editGoodsUnlock(array(array('goods_commonid', 'in', $goods_commonid)));
  154. }
  155. /**
  156. * 编辑更新
  157. * @param type $update 更新数据
  158. * @param type $condition 条件
  159. * @return type
  160. */
  161. public function editPintuan($update, $condition)
  162. {
  163. $goods_commonids = Db::name('ppintuan')->where($condition)->column('pintuan_goods_commonid');
  164. foreach ($goods_commonids as $goods_commonid) {
  165. $this->_dGoodsPintuanCache($goods_commonid);
  166. }
  167. return Db::name('ppintuan')->where($condition)->update($update);
  168. }
  169. /**
  170. * 指定拼团活动结束,参团成功的继续参团,不成功的保持默认.
  171. * @access public
  172. * @author csdeshang
  173. * @param type $condition
  174. * @return type
  175. */
  176. public function endPintuan($condition)
  177. {
  178. $goods_commonid = Db::name('ppintuan')->where($condition)->column('pintuan_goods_commonid');
  179. $data['pintuan_state'] = self::PINTUAN_STATE_CLOSE;
  180. $flag = Db::name('ppintuan')->where($condition)->update($data);
  181. if ($flag) {
  182. if (!empty($goods_commonid)) {
  183. $this->_unlockGoods($goods_commonid);
  184. }
  185. }
  186. return $flag;
  187. }
  188. /**
  189. * 获取拼团扩展信息,包括状态文字和是否可编辑状态
  190. * @access public
  191. * @author csdeshang
  192. * @param type $pintuan_info 拼团信息
  193. * @return boolean
  194. */
  195. public function getPintuanExtendInfo($pintuan_info)
  196. {
  197. $pintuan_info['pintuan_state_text'] = $this->pintuan_state_array[$pintuan_info['pintuan_state']];
  198. //是否有正在参加拼团的订单
  199. $pintuan_order_count = Db::name('ppintuanorder')->where(array('pintuan_id' => $pintuan_info['pintuan_id'], 'pintuanorder_state' => 1))->count();
  200. if ($pintuan_info['pintuan_state'] == self::PINTUAN_STATE_NORMAL && $pintuan_info['pintuan_end_time'] > TIMESTAMP) {
  201. $pintuan_info['editable'] = true;
  202. } else {
  203. $pintuan_info['editable'] = false;
  204. }
  205. return $pintuan_info;
  206. }
  207. /**
  208. * 过期修改状态
  209. * @access public
  210. * @author csdeshang
  211. * @param type $condition 条件
  212. * @return boolean
  213. */
  214. public function editExpirePintuan($condition)
  215. {
  216. $condition[] = array('pintuan_end_time', '<', TIMESTAMP);
  217. $condition[] = array('pintuan_state', '=', self::PINTUAN_STATE_NORMAL);
  218. $updata = array();
  219. $update['pintuan_state'] = self::PINTUAN_STATE_CLOSE;
  220. $this->editPintuan($update, $condition);
  221. return true;
  222. }
  223. /**
  224. * 读取拼团列表
  225. * @access public
  226. * @author csdeshang
  227. * @param type $condition 条件
  228. * @param type $pagesize 分页
  229. * @param type $order 排序
  230. * @param type $field 字段
  231. * @param type $limit 限制
  232. * @return type
  233. */
  234. public function getPintuanExtendList($condition, $pagesize = null, $order = 'pintuan_state asc', $field = '*', $limit = 0)
  235. {
  236. $pintuan_list = $this->getPintuanList($condition, $pagesize, $order, $field, $limit);
  237. if (!empty($pintuan_list)) {
  238. for ($i = 0, $j = count($pintuan_list); $i < $j; $i++) {
  239. $pintuan_list[$i] = $this->getPintuanExtendInfo($pintuan_list[$i]);
  240. }
  241. }
  242. return $pintuan_list;
  243. }
  244. /**
  245. * 根据商品编号查询是否有可用拼团活动,如果有返回抢购信息,没有返回null
  246. * @param type $goods_commonid 商品id
  247. * @return array
  248. */
  249. public function getPintuanInfoByGoodsCommonID($goods_commonid)
  250. {
  251. $info = $this->_rGoodsPintuanCache($goods_commonid);
  252. if (empty($info)) {
  253. $condition = array();
  254. $condition[] = array('pintuan_state', '=', self::PINTUAN_STATE_NORMAL);
  255. $condition[] = array('pintuan_end_time', '>', TIMESTAMP);
  256. $condition[] = array('pintuan_goods_commonid', '=', $goods_commonid);
  257. $pintuan_goods_list = $this->getPintuanExtendList($condition, null, 'pintuan_starttime asc', '*', 1);
  258. $pintuan_goods = isset($pintuan_goods_list[0]) ? $pintuan_goods_list[0] : "";
  259. if (!empty($pintuan_goods)) {
  260. //获取此商品拼团对应的开团人
  261. $condition = array();
  262. $condition[] = array('pintuangroup_joined', '>', 0);
  263. $condition[] = array('pintuan_id', '=', $pintuan_goods['pintuan_id']);
  264. $condition[] = array('pintuangroup_state', '=', 1);
  265. $pintuan_goods['pintuangroup_list'] = model('ppintuangroup')->getPpintuangroupList($condition);
  266. }
  267. //序列化存储到缓存
  268. $info['info'] = serialize($pintuan_goods);
  269. $this->_wGoodsPintuanCache($goods_commonid, $info);
  270. }
  271. $pintuan_goods_info = unserialize($info['info']);
  272. if (!empty($pintuan_goods_info) && ($pintuan_goods_info['pintuan_starttime'] > TIMESTAMP || $pintuan_goods_info['pintuan_end_time'] < TIMESTAMP)) {
  273. $pintuan_goods_info = array();
  274. }
  275. return $pintuan_goods_info;
  276. }
  277. /**
  278. * 读取商品抢购缓存
  279. * @access public
  280. * @author csdeshang
  281. * @param type $goods_commonid 商品id
  282. * @return type
  283. */
  284. private function _rGoodsPintuanCache($goods_commonid)
  285. {
  286. return rcache($goods_commonid, 'goods_pintuan');
  287. }
  288. /**
  289. * 写入商品抢购缓存
  290. * @access public
  291. * @author csdeshang
  292. * @param type $goods_commonid ID
  293. * @param type $info 信息
  294. * @return type
  295. */
  296. private function _wGoodsPintuanCache($goods_commonid, $info)
  297. {
  298. return wcache($goods_commonid, $info, 'goods_pintuan');
  299. }
  300. /**
  301. * 删除商品抢购缓存
  302. * @access public
  303. * @author csdeshang
  304. * @param int $goods_commonid 商品ID
  305. * @return boolean
  306. */
  307. public function _dGoodsPintuanCache($goods_commonid)
  308. {
  309. return dcache($goods_commonid, 'goods_pintuan');
  310. }
  311. }