Presell.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 Presell extends BaseModel
  16. {
  17. public $page_info;
  18. const PRESELL_STATE_CANCEL = 0;
  19. const PRESELL_STATE_TO_BEGIN = 1;
  20. const PRESELL_STATE_NORMAL = 2;
  21. const PRESELL_STATE_END = 3;
  22. private $presell_state_array = array(
  23. self::PRESELL_STATE_CANCEL => '已取消',
  24. self::PRESELL_STATE_TO_BEGIN => '待开始',
  25. self::PRESELL_STATE_NORMAL => '进行中',
  26. self::PRESELL_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 getPresellList($condition, $pagesize = null, $order = 'presell_id desc', $field = '*')
  39. {
  40. if ($pagesize) {
  41. $res = Db::name('presell')->field($field)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  42. $presell_list = $res->items();
  43. $this->page_info = $res;
  44. return $presell_list;
  45. } else {
  46. return Db::name('presell')->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 getOnlinePresellList($condition, $pagesize = null, $order = 'presell_id desc', $field = '*')
  60. {
  61. $condition[] = array('presell_state', '=', self::PRESELL_STATE_NORMAL);
  62. $condition[] = array('presell_end_time', '>', TIMESTAMP);
  63. $presell_list = $this->getPresellList($condition, $pagesize, $order, $field);
  64. return $presell_list;
  65. }
  66. /**
  67. * 根据条件读取预售信息
  68. * @access public
  69. * @author csdeshang
  70. * @param array $condition 查询条件
  71. * @return array 预售信息
  72. */
  73. public function getPresellInfo($condition)
  74. {
  75. $presell_info = Db::name('presell')->where($condition)->find();
  76. return $presell_info;
  77. }
  78. /**
  79. * 根据预售编号读取预售信息
  80. * @access public
  81. * @author csdeshang
  82. * @param array $presell_id 预售活动编号
  83. * @param int $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  84. * @return array 预售信息
  85. */
  86. public function getPresellInfoByID($presell_id, $store_id = 0)
  87. {
  88. if (intval($presell_id) <= 0) {
  89. return null;
  90. }
  91. $condition = array();
  92. $condition[] = array('presell_id', '=', $presell_id);
  93. $presell_info = $this->getPresellInfo($condition);
  94. if ($store_id > 0 && $presell_info['store_id'] != $store_id) {
  95. return null;
  96. } else {
  97. return $presell_info;
  98. }
  99. }
  100. public function getOnlinePresellInfoByID($presell_id)
  101. {
  102. if (intval($presell_id) <= 0) {
  103. return null;
  104. }
  105. $condition = array();
  106. $condition[] = array('presell_id', '=', $presell_id);
  107. $condition[] = array('presell_state', '=', self::PRESELL_STATE_NORMAL);
  108. $condition[] = array('presell_end_time', '>', TIMESTAMP);
  109. $presell_info = $this->getPresellInfo($condition);
  110. return $presell_info;
  111. }
  112. /**
  113. * 预售状态数组
  114. * @access public
  115. * @author csdeshang
  116. * @return type
  117. */
  118. public function getPresellStateArray()
  119. {
  120. return $this->presell_state_array;
  121. }
  122. /**
  123. * 增加
  124. * @access public
  125. * @author csdeshang
  126. * @param array $data 数据
  127. * @return type
  128. */
  129. public function addPresell($data)
  130. {
  131. $flag = Db::name('presell')->insertGetId($data);
  132. if ($flag) {
  133. // 发布预售锁定商品
  134. $this->_lockGoods($data['goods_commonid'], $data['goods_id']);
  135. }
  136. return $flag;
  137. }
  138. /**
  139. * 编辑更新
  140. * @param type $update 更新数据
  141. * @param type $condition 条件
  142. * @return type
  143. */
  144. public function editPresell($update, $condition)
  145. {
  146. $goods_ids = Db::name('presell')->where($condition)->column('goods_id');
  147. foreach ($goods_ids as $goods_id) {
  148. $this->_dGoodsPresellCache($goods_id);
  149. }
  150. return Db::name('presell')->where($condition)->update($update);
  151. }
  152. /**
  153. * 指定预售活动结束,参团成功的继续参团,不成功的保持默认.
  154. * @access public
  155. * @author csdeshang
  156. * @param type $condition
  157. * @return type
  158. */
  159. public function endPresell($condition = array())
  160. {
  161. $condition[] = array('presell_state', '=', self::PRESELL_STATE_NORMAL);
  162. $goods_commonid = Db::name('presell')->where($condition)->column('goods_commonid');
  163. $goods_id = Db::name('presell')->where($condition)->column('goods_id');
  164. $data['presell_state'] = self::PRESELL_STATE_END;
  165. $flag = Db::name('presell')->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 type $condition 条件
  178. * @return type
  179. */
  180. public function cancelPresell($condition)
  181. {
  182. $goods_commonid = Db::name('presell')->where($condition)->column('goods_commonid');
  183. $goods_id = Db::name('presell')->where($condition)->column('goods_id');
  184. $update = array();
  185. $update['presell_state'] = self::PRESELL_STATE_CANCEL;
  186. $flag = $this->editPresell($update, $condition);
  187. if ($flag) {
  188. if (!empty($goods_commonid)) {
  189. $this->_unlockGoods($goods_commonid, $goods_id);
  190. }
  191. }
  192. return $flag;
  193. }
  194. /**
  195. * 删除预售活动
  196. * @access public
  197. * @author csdeshang
  198. * @param array $condition 检索条件
  199. * @return boolean
  200. */
  201. public function delPresell($condition)
  202. {
  203. return Db::name('presell')->where($condition)->delete();
  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 $presell_info 预售信息
  244. * @return boolean
  245. */
  246. public function getPresellBtn($presell_info)
  247. {
  248. if (!$presell_info) {
  249. return false;
  250. }
  251. if ($presell_info['presell_state'] == self::PRESELL_STATE_TO_BEGIN && $presell_info['presell_start_time'] > TIMESTAMP) {
  252. $presell_info['editable'] = true;
  253. } else {
  254. $presell_info['editable'] = false;
  255. }
  256. return $presell_info;
  257. }
  258. /**
  259. * 获取状态文字
  260. * @access public
  261. * @author csdeshang
  262. * @param type $presell_info 预售信息
  263. * @return boolean
  264. */
  265. public function getPresellStateText($presell_info)
  266. {
  267. if (!$presell_info) {
  268. return false;
  269. }
  270. $presell_state_text = $this->presell_state_array[$presell_info['presell_state']];
  271. return $presell_state_text;
  272. }
  273. /**
  274. * 根据商品编号查询是否有可用预售活动,如果有返回预售信息,没有返回null
  275. * @param type $goods_id 商品id
  276. * @return array
  277. */
  278. public function getPresellInfoByGoodsID($goods_id)
  279. {
  280. $info = $this->_rGoodsPresellCache($goods_id);
  281. if (empty($info)) {
  282. $condition = array();
  283. $condition[] = array('goods_id', '=', $goods_id);
  284. $condition[] = array('presell_state', '=', self::PRESELL_STATE_NORMAL);
  285. $condition[] = array('presell_end_time', '>', TIMESTAMP);
  286. $presell_info = $this->getPresellInfo($condition);
  287. //序列化存储到缓存
  288. $info['info'] = serialize($presell_info);
  289. $this->_wGoodsPresellCache($goods_id, $info);
  290. }
  291. $presell_goods_info = unserialize($info['info']);
  292. if (!empty($presell_goods_info) && ($presell_goods_info['presell_state'] != 2 || $presell_goods_info['presell_start_time'] > TIMESTAMP || $presell_goods_info['presell_end_time'] < TIMESTAMP)) {
  293. $presell_goods_info = array();
  294. }
  295. return $presell_goods_info;
  296. }
  297. /**
  298. * 读取商品预售缓存
  299. * @access public
  300. * @author csdeshang
  301. * @param type $goods_id 商品id
  302. * @return type
  303. */
  304. private function _rGoodsPresellCache($goods_id)
  305. {
  306. return rcache($goods_id, 'goods_presell');
  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 _wGoodsPresellCache($goods_id, $info)
  317. {
  318. return wcache($goods_id, $info, 'goods_presell');
  319. }
  320. /**
  321. * 删除商品预售缓存
  322. * @access public
  323. * @author csdeshang
  324. * @param int $goods_id 商品ID
  325. * @return boolean
  326. */
  327. public function _dGoodsPresellCache($goods_id)
  328. {
  329. return dcache($goods_id, 'goods_presell');
  330. }
  331. }