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