Wholesalegoods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 Wholesalegoods extends BaseModel {
  21. public $page_info;
  22. const WHOLESALE_GOODS_STATE_CANCEL = 0;
  23. const WHOLESALE_GOODS_STATE_NORMAL = 1;
  24. /**
  25. * 读取批发商品列表
  26. * @access public
  27. * @author csdeshang
  28. * @param type $condition 条件
  29. * @param type $pagesize 分页
  30. * @param type $order 排序
  31. * @param type $field 字段
  32. * @param type $limit 个数限制
  33. * @return array 批发商品列表
  34. */
  35. public function getWholesalegoodsList($condition, $pagesize = null, $order = '', $field = '*', $limit = 0) {
  36. if ($pagesize) {
  37. $res = Db::name('wholesalegoods')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  38. $this->page_info = $res;
  39. return $res->items();
  40. } else {
  41. return $wholesalegoods_list = Db::name('wholesalegoods')->field($field)->where($condition)->limit($limit)->order($order)->select()->toArray();
  42. }
  43. }
  44. /**
  45. * 读取批发商品列表
  46. * @access public
  47. * @author csdeshang
  48. * @param type $condition 查询条件
  49. * @param type $pagesize 分页
  50. * @param type $order 排序
  51. * @param type $field 字段
  52. * @param type $limit 个数限制
  53. * @return array
  54. */
  55. public function getWholesalegoodsExtendList($condition, $pagesize = null, $order = '', $field = '*', $limit = 0) {
  56. $wholesalegoods_list = $this->getWholesalegoodsList($condition, $pagesize, $order, $field, $limit);
  57. if (!empty($wholesalegoods_list)) {
  58. for ($i = 0, $j = count($wholesalegoods_list); $i < $j; $i++) {
  59. $wholesalegoods_list[$i] = $this->getWholesalegoodsExtendInfo($wholesalegoods_list[$i]);
  60. }
  61. }
  62. return $wholesalegoods_list;
  63. }
  64. /**
  65. * 获取秒杀折列表
  66. * @access public
  67. * @author csdeshang
  68. * @param type $condition 条件
  69. * @param type $pagesize 分页
  70. * @param type $order 排序
  71. * @param type $field 字段
  72. * @param type $limit 限制
  73. * @return type
  74. */
  75. public function getWholesalegoodsExtendIds($condition, $pagesize = null, $order = '', $field = 'goods_id', $limit = 0) {
  76. $wholesalegoods_id_list = $this->getWholesalegoodsList($condition, $pagesize, $order, $field, $limit);
  77. if (!empty($wholesalegoods_id_list)) {
  78. for ($i = 0; $i < count($wholesalegoods_id_list); $i++) {
  79. $wholesalegoods_id_list[$i] = $wholesalegoods_id_list[$i]['goods_id'];
  80. }
  81. }
  82. return $wholesalegoods_id_list;
  83. }
  84. /**
  85. * 根据条件读取限制折扣商品信息
  86. * @access public
  87. * @author csdeshang
  88. * @param type $condition 条件
  89. * @return array
  90. */
  91. public function getWholesalegoodsInfo($condition) {
  92. $result = Db::name('wholesalegoods')->where($condition)->find();
  93. return $result;
  94. }
  95. /**
  96. * 根据批发商品编号读取限制折扣商品信息
  97. * @access public
  98. * @author csdeshang
  99. * @param type $wholesalegoods_id ID编号
  100. * @param type $store_id 店铺ID
  101. * @return type
  102. */
  103. public function getWholesalegoodsInfoByID($wholesalegoods_id, $store_id = 0) {
  104. if (intval($wholesalegoods_id) <= 0) {
  105. return null;
  106. }
  107. $condition = array();
  108. $condition[] = array('wholesalegoods_id','=',$wholesalegoods_id);
  109. $wholesalegoods_info = $this->getWholesalegoodsInfo($condition);
  110. if ($store_id > 0 && $wholesalegoods_info['store_id'] != $store_id) {
  111. return null;
  112. } else {
  113. return $wholesalegoods_info;
  114. }
  115. }
  116. /**
  117. * @access public
  118. * @author csdeshang
  119. * 增加批发商品
  120. * @param type $wholesalegoods_info 批发商品信息
  121. * @return bool
  122. */
  123. public function addWholesalegoods($wholesalegoods_info) {
  124. $wholesalegoods_info['wholesalegoods_state'] = self::WHOLESALE_GOODS_STATE_NORMAL;
  125. $wholesalegoods_id = Db::name('wholesalegoods')->insertGetId($wholesalegoods_info);
  126. if($wholesalegoods_id){
  127. // 发布秒杀锁定商品
  128. $this->_lockGoods($wholesalegoods_info['goods_commonid'],$wholesalegoods_info['goods_id']);
  129. }
  130. // 删除商品批发缓存
  131. $this->_dGoodsWholesaleCache($wholesalegoods_info['goods_id']);
  132. $wholesalegoods_info['wholesalegoods_id'] = $wholesalegoods_id;
  133. $wholesalegoods_info = $this->getWholesalegoodsExtendInfo($wholesalegoods_info);
  134. return $wholesalegoods_info;
  135. }
  136. /**
  137. * 更新
  138. * @access public
  139. * @author csdeshang
  140. * @param type $update 数据更新
  141. * @param type $condition 条件
  142. * @return type
  143. */
  144. public function editWholesalegoods($update, $condition) {
  145. $result = Db::name('wholesalegoods')->where($condition)->update($update);
  146. if ($result) {
  147. $wholesalegoods_list = $this->getWholesalegoodsList($condition, null, '', 'goods_id');
  148. if (!empty($wholesalegoods_list)) {
  149. foreach ($wholesalegoods_list as $val) {
  150. // 删除商品批发缓存
  151. $this->_dGoodsWholesaleCache($val['goods_id']);
  152. }
  153. }
  154. }
  155. return $result;
  156. }
  157. /**
  158. * 删除
  159. * @access public
  160. * @author csdeshang
  161. * @param type $condition 条件
  162. * @return type
  163. */
  164. public function delWholesalegoods($condition) {
  165. $wholesalegoods_list = $this->getWholesalegoodsList($condition, null, '', 'goods_id,goods_commonid');
  166. $result = Db::name('wholesalegoods')->where($condition)->delete();
  167. if ($result) {
  168. if (!empty($wholesalegoods_list)) {
  169. foreach ($wholesalegoods_list as $val) {
  170. $this->_unlockGoods($val['goods_commonid'],$val['goods_id']);
  171. // 删除商品批发缓存
  172. $this->_dGoodsWholesaleCache($val['goods_id']);
  173. }
  174. }
  175. }
  176. return $result;
  177. }
  178. /**
  179. * 获取批发商品扩展信息
  180. * @access public
  181. * @author csdeshang
  182. * @param type $wholesale_info 信息
  183. * @return string
  184. */
  185. public function getWholesalegoodsExtendInfo($wholesale_info) {
  186. $wholesale_info['goods_url'] = (string)url('/home/Goods/index', array('goods_id' => $wholesale_info['goods_id']));
  187. $wholesale_info['image_url'] = goods_cthumb($wholesale_info['goods_image'], 60, $wholesale_info['store_id']);
  188. $wholesale_info['wholesalegoods_price'] = unserialize($wholesale_info['wholesalegoods_price']);
  189. return $wholesale_info;
  190. }
  191. /**
  192. * 获取推荐批发商品
  193. * @access public
  194. * @author csdeshang
  195. * @param type $count 推荐数量
  196. * @return type
  197. */
  198. public function getWholesalegoodsCommendList($count = 4) {
  199. $condition = array();
  200. $condition[] = array('wholesalegoods_state', '=', self::WHOLESALE_GOODS_STATE_NORMAL);
  201. $condition[] = array('wholesale_starttime', '<', TIMESTAMP);
  202. $condition[] = array('wholesale_end_time', '>', TIMESTAMP);
  203. $wholesale_list = $this->getWholesalegoodsExtendList($condition, null, '', '*', $count);
  204. return $wholesale_list;
  205. }
  206. /**
  207. * 锁定商品
  208. * @access private
  209. * @author csdeshang
  210. * @param type $goods_commonid 商品编号
  211. */
  212. private function _lockGoods($goods_commonid,$goods_id)
  213. {
  214. $condition = array();
  215. $condition[] = array('goods_commonid','=',$goods_commonid);
  216. $goods_model = model('goods');
  217. $goods_model->editGoodsCommonLock($condition);
  218. $condition = array();
  219. $condition[] = array('goods_id','=',$goods_id);
  220. $goods_model->editGoodsLock($condition);
  221. }
  222. /**
  223. * 解锁商品
  224. * @access private
  225. * @author csdeshang
  226. * @param type $goods_commonid 商品编号ID
  227. */
  228. private function _unlockGoods($goods_commonid,$goods_id)
  229. {
  230. $goods_model = model('goods');
  231. $goods_model->editGoodsUnlock(array('goods_id' => $goods_id));
  232. if(!$goods_model->getGoodsCount(array('goods_commonid' => $goods_commonid,'goods_lock'=>1))){
  233. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  234. }
  235. }
  236. /**
  237. * 根据商品编号查询是否有可用批发活动,如果有返回批发活动,没有返回null
  238. * @access public
  239. * @author csdeshang
  240. * @param type $goods_id 商品id
  241. * @return array
  242. */
  243. public function getWholesalegoodsInfoByGoodsID($goods_id) {
  244. $info = $this->_rGoodsWholesaleCache($goods_id);
  245. if (empty($info)) {
  246. $condition = array();
  247. $condition[] = array('wholesalegoods_state','=',self::WHOLESALE_GOODS_STATE_NORMAL);
  248. $condition[] = array('wholesale_end_time','>',TIMESTAMP);
  249. $condition[] = array('goods_id','=',$goods_id);
  250. $wholesalegoods_list = $this->getWholesalegoodsExtendList($condition, null, 'wholesale_starttime asc', '*', 1);
  251. $info['info'] = isset($wholesalegoods_list[0]) ? serialize($wholesalegoods_list[0]) : serialize("");
  252. $this->_wGoodsWholesaleCache($goods_id, $info);
  253. }
  254. $wholesalegoods_info = unserialize($info['info']);
  255. if (!empty($wholesalegoods_info) && ($wholesalegoods_info['wholesale_starttime'] > TIMESTAMP || $wholesalegoods_info['wholesale_end_time'] < TIMESTAMP)) {
  256. $wholesalegoods_info = array();
  257. }
  258. return $wholesalegoods_info;
  259. }
  260. /**
  261. * 根据商品编号查询是否有可用批发活动,如果有返回批发活动,没有返回null
  262. * @access public
  263. * @author csdeshang
  264. * @param type $goods_string 商品编号字符串,例:'11,22,33'
  265. * @return type
  266. */
  267. public function getWholesalegoodsListByGoodsString($goods_string) {
  268. $wholesalegoods_list = $this->_getWholesalegoodsListByGoods($goods_string);
  269. $wholesalegoods_list = array_under_reset($wholesalegoods_list, 'goods_id');
  270. return $wholesalegoods_list;
  271. }
  272. /**
  273. * 根据商品编号查询是否有可用批发活动,如果有返回批发活动,没有返回null
  274. * @access public
  275. * @author csdeshang
  276. * @param type $goods_id_string 商品编号字符串
  277. * @return type
  278. */
  279. private function _getWholesalegoodsListByGoods($goods_id_string) {
  280. $condition = array();
  281. $condition[] = array('wholesalegoods_state','=',self::WHOLESALE_GOODS_STATE_NORMAL);
  282. $condition[] = array('wholesale_starttime','<',TIMESTAMP);
  283. $condition[] = array('wholesale_end_time','>',TIMESTAMP);
  284. $condition[] = array('goods_id','in', $goods_id_string);
  285. $wholesalegoods_list = $this->getWholesalegoodsExtendList($condition, null, 'wholesalegoods_id desc', '*');
  286. return $wholesalegoods_list;
  287. }
  288. /**
  289. * 读取商品批发缓存
  290. * @access public
  291. * @author csdeshang
  292. * @param type $goods_id 商品id
  293. * @return type
  294. */
  295. private function _rGoodsWholesaleCache($goods_id) {
  296. return rcache($goods_id, 'goods_wholesale');
  297. }
  298. /**
  299. * 写入商品批发缓存
  300. * @access public
  301. * @author csdeshang
  302. * @param int $goods_id 商品id
  303. * @param array $info 信息
  304. * @return boolean
  305. */
  306. private function _wGoodsWholesaleCache($goods_id, $info) {
  307. return wcache($goods_id, $info, 'goods_wholesale');
  308. }
  309. /**
  310. * 删除商品批发缓存
  311. * @access public
  312. * @author csdeshang
  313. * @param type $goods_id 商品id
  314. * @return bool
  315. */
  316. private function _dGoodsWholesaleCache($goods_id) {
  317. return dcache($goods_id, 'goods_wholesale');
  318. }
  319. }