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