Ppintuan.php 11 KB

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