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