Presell.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 Presell extends BaseModel {
  21. public $page_info;
  22. const PRESELL_STATE_CANCEL = 0;
  23. const PRESELL_STATE_TO_BEGIN = 1;
  24. const PRESELL_STATE_NORMAL = 2;
  25. const PRESELL_STATE_END = 3;
  26. private $presell_state_array = array(
  27. self::PRESELL_STATE_CANCEL => '已取消',
  28. self::PRESELL_STATE_TO_BEGIN => '待开始',
  29. self::PRESELL_STATE_NORMAL => '进行中',
  30. self::PRESELL_STATE_END => '已结束'
  31. );
  32. /**
  33. * 读取预售列表
  34. * @access public
  35. * @author csdeshang
  36. * @param array $condition 查询条件
  37. * @param int $pagesize 分页数
  38. * @param string $order 排序
  39. * @param string $field 所需字段
  40. * @return array 预售列表
  41. */
  42. public function getPresellList($condition, $pagesize = null, $order = 'presell_id desc', $field = '*') {
  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. $condition[]=array('presell_state','=',self::PRESELL_STATE_NORMAL);
  64. $condition[]=array('presell_end_time','>',TIMESTAMP);
  65. $presell_list = $this->getPresellList($condition, $pagesize, $order, $field);
  66. return $presell_list;
  67. }
  68. /**
  69. * 根据条件读取预售信息
  70. * @access public
  71. * @author csdeshang
  72. * @param array $condition 查询条件
  73. * @return array 预售信息
  74. */
  75. public function getPresellInfo($condition) {
  76. $presell_info = Db::name('presell')->where($condition)->find();
  77. return $presell_info;
  78. }
  79. /**
  80. * 根据预售编号读取预售信息
  81. * @access public
  82. * @author csdeshang
  83. * @param array $presell_id 预售活动编号
  84. * @param int $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  85. * @return array 预售信息
  86. */
  87. public function getPresellInfoByID($presell_id, $store_id = 0) {
  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. if (intval($presell_id) <= 0) {
  102. return null;
  103. }
  104. $condition = array();
  105. $condition[] = array('presell_id','=',$presell_id);
  106. $condition[] = array('presell_state','=',self::PRESELL_STATE_NORMAL);
  107. $condition[] = array('presell_end_time','>',TIMESTAMP);
  108. $presell_info = $this->getPresellInfo($condition);
  109. return $presell_info;
  110. }
  111. /**
  112. * 预售状态数组
  113. * @access public
  114. * @author csdeshang
  115. * @return type
  116. */
  117. public function getPresellStateArray() {
  118. return $this->presell_state_array;
  119. }
  120. /**
  121. * 增加
  122. * @access public
  123. * @author csdeshang
  124. * @param array $data 数据
  125. * @return type
  126. */
  127. public function addPresell($data) {
  128. $flag= Db::name('presell')->insertGetId($data);
  129. if($flag){
  130. // 发布预售锁定商品
  131. $this->_lockGoods($data['goods_commonid'],$data['goods_id']);
  132. }
  133. return $flag;
  134. }
  135. /**
  136. * 编辑更新
  137. * @param type $update 更新数据
  138. * @param type $condition 条件
  139. * @return type
  140. */
  141. public function editPresell($update, $condition) {
  142. return Db::name('presell')->where($condition)->update($update);
  143. }
  144. /**
  145. * 指定预售活动结束,参团成功的继续参团,不成功的保持默认.
  146. * @access public
  147. * @author csdeshang
  148. * @param type $condition
  149. * @return type
  150. */
  151. public function endPresell($condition=array()) {
  152. $condition[]=array('presell_state','=',self::PRESELL_STATE_NORMAL);
  153. $goods_commonid=Db::name('presell')->where($condition)->column('goods_commonid');
  154. $goods_id=Db::name('presell')->where($condition)->column('goods_id');
  155. $data['presell_state'] = self::PRESELL_STATE_END;
  156. $flag= Db::name('presell')->where($condition)->update($data);
  157. if($flag){
  158. if(!empty($goods_commonid)){
  159. $this->_unlockGoods($goods_commonid,$goods_id);
  160. }
  161. }
  162. return $flag;
  163. }
  164. /**
  165. * 取消预售活动
  166. * @access public
  167. * @author csdeshang
  168. * @param type $condition 条件
  169. * @return type
  170. */
  171. public function cancelPresell($condition) {
  172. $goods_commonid = Db::name('presell')->where($condition)->column('goods_commonid');
  173. $goods_id = Db::name('presell')->where($condition)->column('goods_id');
  174. $update = array();
  175. $update['presell_state'] = self::PRESELL_STATE_CANCEL;
  176. $flag= $this->editPresell($update, $condition);
  177. if($flag){
  178. if(!empty($goods_commonid)){
  179. $this->_unlockGoods($goods_commonid,$goods_id);
  180. }
  181. }
  182. return $flag;
  183. }
  184. /**
  185. * 删除预售活动
  186. * @access public
  187. * @author csdeshang
  188. * @param array $condition 检索条件
  189. * @return boolean
  190. */
  191. public function delPresell($condition) {
  192. return Db::name('presell')->where($condition)->delete();
  193. }
  194. /**
  195. * 锁定商品
  196. * @access private
  197. * @author csdeshang
  198. * @param type $goods_commonid 商品编号
  199. */
  200. private function _lockGoods($goods_commonid,$goods_id)
  201. {
  202. $condition = array();
  203. $condition[] = array('goods_commonid','=',$goods_commonid);
  204. $goods_model = model('goods');
  205. $goods_model->editGoodsCommonLock($condition);
  206. $condition = array();
  207. $condition[] = array('goods_id','=',$goods_id);
  208. $goods_model->editGoodsLock($condition);
  209. }
  210. /**
  211. * 解锁商品
  212. * @access private
  213. * @author csdeshang
  214. * @param type $goods_commonid 商品编号ID
  215. */
  216. private function _unlockGoods($goods_commonid,$goods_id)
  217. {
  218. $goods_model = model('goods');
  219. $goods_model->editGoodsUnlock(array(array('goods_id' ,'in', $goods_id)));
  220. $temp=Db::name('goods')->where(array(array('goods_id','in',$goods_id),array('goods_lock','=',1)))->column('goods_commonid');
  221. if(!empty($temp)){
  222. $goods_commonid=array_diff($goods_commonid,$temp);
  223. }
  224. if(!empty($goods_commonid)){
  225. $goods_model->editGoodsCommonUnlock(array(array('goods_commonid' ,'in', $goods_commonid)));
  226. }
  227. }
  228. /**
  229. * 获取预售是否可编辑状态
  230. * @access public
  231. * @author csdeshang
  232. * @param type $presell_info 预售信息
  233. * @return boolean
  234. */
  235. public function getPresellBtn($presell_info) {
  236. if (!$presell_info) {
  237. return false;
  238. }
  239. if ($presell_info['presell_state'] == self::PRESELL_STATE_TO_BEGIN && $presell_info['presell_start_time'] > TIMESTAMP) {
  240. $presell_info['editable'] = true;
  241. } else {
  242. $presell_info['editable'] = false;
  243. }
  244. return $presell_info;
  245. }
  246. /**
  247. * 获取状态文字
  248. * @access public
  249. * @author csdeshang
  250. * @param type $presell_info 预售信息
  251. * @return boolean
  252. */
  253. public function getPresellStateText($presell_info) {
  254. if (!$presell_info) {
  255. return false;
  256. }
  257. $presell_state_text = $this->presell_state_array[$presell_info['presell_state']];
  258. return $presell_state_text;
  259. }
  260. /**
  261. * 根据商品编号查询是否有可用预售活动,如果有返回预售信息,没有返回null
  262. * @param type $goods_id 商品id
  263. * @return array
  264. */
  265. public function getPresellInfoByGoodsID($goods_id) {
  266. $info = $this->_rGoodsPresellCache($goods_id);
  267. if (empty($info)) {
  268. $condition = array();
  269. $condition[] = array('goods_id','=',$goods_id);
  270. $condition[] = array('presell_state','=',self::PRESELL_STATE_NORMAL);
  271. $condition[] = array('presell_end_time','>',TIMESTAMP);
  272. $presell_info = $this->getPresellInfo($condition);
  273. //序列化存储到缓存
  274. $info['info'] = serialize($presell_info);
  275. $this->_wGoodsPresellCache($goods_id, $info);
  276. }
  277. $presell_goods_info = unserialize($info['info']);
  278. 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)) {
  279. $presell_goods_info = array();
  280. }
  281. return $presell_goods_info;
  282. }
  283. /**
  284. * 读取商品预售缓存
  285. * @access public
  286. * @author csdeshang
  287. * @param type $goods_id 商品id
  288. * @return type
  289. */
  290. private function _rGoodsPresellCache($goods_id) {
  291. return rcache($goods_id, 'goods_presell');
  292. }
  293. /**
  294. * 写入商品预售缓存
  295. * @access public
  296. * @author csdeshang
  297. * @param type $goods_id ID
  298. * @param type $info 信息
  299. * @return type
  300. */
  301. private function _wGoodsPresellCache($goods_id, $info) {
  302. return wcache($goods_id, $info, 'goods_presell');
  303. }
  304. /**
  305. * 删除商品预售缓存
  306. * @access public
  307. * @author csdeshang
  308. * @param int $goods_id 商品ID
  309. * @return boolean
  310. */
  311. public function _dGoodsPresellCache($goods_id) {
  312. return dcache($goods_id, 'goods_presell');
  313. }
  314. }