Wholesalegoods.php 12 KB

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