Wholesalegoods.php 13 KB

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