Pointprod.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 数据层模型
  15. */
  16. class Pointprod extends BaseModel
  17. {
  18. public $page_info;
  19. /**
  20. * 获取兑换商品的展示状态
  21. * @access public
  22. * @author csdeshang
  23. * @return array
  24. */
  25. public function getPgoodsShowState()
  26. {
  27. $pgoodsshowstate_arr = array('unshow' => array(0, '下架'), 'show' => array(1, '上架'));
  28. return $pgoodsshowstate_arr;
  29. }
  30. /**
  31. * 获取兑换商品的开启状态
  32. * @access public
  33. * @author csdeshang
  34. * @return array
  35. */
  36. public function getPgoodsOpenState()
  37. {
  38. $pgoodsopenstate_arr = array('open' => array(0, '开启'), 'close' => array(1, '禁售'));
  39. return $pgoodsopenstate_arr;
  40. }
  41. /**
  42. * @access public
  43. * @author csdeshang
  44. * 获取兑换商品的推荐状态
  45. * @return array
  46. */
  47. public function getPgoodsRecommendState()
  48. {
  49. $pgoodsrecommendstate_arr = array('uncommend' => array(0, '未推荐'), 'commend' => array(1, '已推荐'));
  50. return $pgoodsrecommendstate_arr;
  51. }
  52. /**
  53. * 礼品保存
  54. * @access public
  55. * @author csdeshang
  56. * @param type $data 商品数据
  57. * @return boolean
  58. */
  59. public function addPointgoods($data)
  60. {
  61. if (empty($data)) {
  62. return false;
  63. }
  64. $insert_id = Db::name('pointsgoods')->insertGetId($data);
  65. if ($insert_id) {
  66. return $insert_id;
  67. }
  68. else {
  69. return false;
  70. }
  71. }
  72. /**
  73. * 礼品信息列表
  74. * @access public
  75. * @author csdeshang
  76. * @param array $where 条件
  77. * @param string $field 字段
  78. * @param string $order 排序
  79. * @param int $limit 限制
  80. * @param int $pagesize 分页
  81. * @return array
  82. */
  83. public function getPointProdList($where = '', $field = '*', $order = '', $limit = 0, $pagesize = '')
  84. {
  85. if (empty($order)) {
  86. $order = 'pgoods_sort asc';
  87. }
  88. if ($pagesize) {
  89. $res = Db::name('pointsgoods')->field($field)->where($where)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  90. $this->page_info = $res;
  91. $list = $res->items();
  92. }else {
  93. $list = Db::name('pointsgoods')->field($field)->where($where)->order($order)->limit($limit)->select()->toArray();
  94. }
  95. if (is_array($list) && count($list) > 0) {
  96. foreach ($list as $k => $v) {
  97. $v['pgoods_image_old'] = $v['pgoods_image'];
  98. $v['pgoods_image_small'] = pointprod_thumb($v['pgoods_image'], 'small');
  99. $v['pgoods_image'] = pointprod_thumb($v['pgoods_image'], 'normal');
  100. $v['ex_state'] = $this->getPointProdExstate($v);
  101. //处理限制的会员等级
  102. if (isset($v['pgoods_limitmgrade'])) {
  103. $limitgrade = intval($v['pgoods_limitmgrade']);
  104. if ($limitgrade > 0) {
  105. $membergrade_arr = model('member')->getMemberGradeArr();
  106. $v['pgoods_limitgradename'] = $membergrade_arr[$limitgrade]['level_name'];
  107. }
  108. }
  109. //获得浏览次数
  110. if (isset($v['pgoods_view'])) {
  111. $v['pgoods_view'] = $this->getPointProdViewNum($v['pgoods_id'], $v['pgoods_view']);
  112. }
  113. $list[$k] = $v;
  114. }
  115. }
  116. return $list;
  117. }
  118. /**
  119. * 查询出售中的兑换礼品列表
  120. * @access public
  121. * @author csdeshang
  122. * @param type $where 条件
  123. * @param type $field 字段
  124. * @param type $order 排序
  125. * @param type $limit 限制
  126. * @param type $pagesize 分页
  127. * @return type
  128. */
  129. public function getOnlinePointProdList($where = '', $field = '*', $order = '', $limit = 0, $pagesize = '')
  130. {
  131. $pgoodsshowstate_arr = $this->getPgoodsShowState();
  132. $pgoodsopenstate_arr = $this->getPgoodsOpenState();
  133. $where[]=array('pgoods_show','=',$pgoodsshowstate_arr['show'][0]);
  134. $where[]=array('pgoods_state','=',$pgoodsopenstate_arr['open'][0]);
  135. $list = $this->getPointProdList($where, $field, $order, $limit, $pagesize);
  136. return $list;
  137. }
  138. /**
  139. * 礼品信息单条
  140. * @access public
  141. * @author csdeshang
  142. * @param type $where 条件
  143. * @param type $field 字段
  144. * @return type
  145. */
  146. public function getPointProdInfo($where = '', $field = '*')
  147. {
  148. $prodinfo = Db::name('pointsgoods')->where($where)->find();
  149. if (!empty($prodinfo)) {
  150. $prodinfo['pgoods_image_old'] = $prodinfo['pgoods_image'];
  151. $prodinfo['pgoods_image_max'] = pointprod_thumb($prodinfo['pgoods_image']);
  152. $prodinfo['pgoods_image_small'] = pointprod_thumb($prodinfo['pgoods_image'], 'small');
  153. $prodinfo['pgoods_image'] = pointprod_thumb($prodinfo['pgoods_image'], 'normal');
  154. $prodinfo['ex_state'] = $this->getPointProdExstate($prodinfo);
  155. //处理兑换时间限制
  156. if ($prodinfo['pgoods_islimittime'] == 1 && $prodinfo['ex_state'] == 'going') {
  157. $timediff = intval($prodinfo['pgoods_endtime']) - TIMESTAMP;
  158. $prodinfo['timediff']['diff_day'] = intval($timediff / 86400);
  159. $prodinfo['timediff']['diff_hour'] = intval($timediff % 86400 / 3600);
  160. $prodinfo['timediff']['diff_mins'] = intval($timediff % 86400 % 3600 / 60);
  161. $prodinfo['timediff']['diff_secs'] = intval($timediff % 86400 % 3600 % 60);
  162. }
  163. //处理限制的会员等级
  164. if (isset($prodinfo['pgoods_limitmgrade'])) {
  165. $limitgrade = intval($prodinfo['pgoods_limitmgrade']);
  166. if ($limitgrade > 0) {
  167. $membergrade_arr = model('member')->getMemberGradeArr();
  168. $prodinfo['pgoods_limitgradename'] = $membergrade_arr[$limitgrade]['level_name'];
  169. }
  170. }
  171. //获得浏览次数
  172. if (isset($prodinfo['pgoods_view'])) {
  173. $prodinfo['pgoods_view'] = $this->getPointProdViewNum($prodinfo['pgoods_id'], $prodinfo['pgoods_view']);
  174. }
  175. }
  176. return $prodinfo;
  177. }
  178. /**
  179. * 查询出售中的兑换礼品
  180. * @access public
  181. * @author csdeshang
  182. * @param type $where 条件
  183. * @param type $field 字段
  184. * @return type
  185. */
  186. public function getOnlinePointProdInfo($where = array(), $field = '*')
  187. {
  188. $pgoodsshowstate_arr = $this->getPgoodsShowState();
  189. $pgoodsopenstate_arr = $this->getPgoodsOpenState();
  190. $where[]=array('pgoods_show','=',$pgoodsshowstate_arr['show'][0]);
  191. $where[]=array('pgoods_state','=',$pgoodsopenstate_arr['open'][0]);
  192. $prodinfo = $this->getPointProdInfo($where, $field);
  193. return $prodinfo;
  194. }
  195. /**
  196. * 获得兑换礼品的浏览次数
  197. * @access public
  198. * @author csdeshang
  199. * @param type $prod_id 兑换礼品ID
  200. * @param type $pgoods_view 浏览
  201. * @return int
  202. */
  203. public function getPointProdViewNum($prod_id, $pgoods_view = '')
  204. {
  205. $prod_id = intval($prod_id);
  206. if ($prod_id <= 0) {
  207. return 0;
  208. }
  209. $is_data = true;//是否从数据库读取
  210. if (config('ds_config.cache_open')) {//如果开启缓存,则读取缓存的浏览次数
  211. $prod_info = rcache($prod_id, 'pointprod');
  212. if ($prod_info) {
  213. $is_data = false;
  214. }
  215. }
  216. if ($is_data) {//从数据库读取
  217. if ($pgoods_view === '') {//如果已经获得浏览次数则直接返回,否则查询数据库中的浏览次数
  218. $prod_info = Db::name('pointsgoods')->field('pgoods_view')->where(array('pgoods_id' => $prod_id))->find();
  219. $pgoods_view = intval($prod_info['pgoods_view']);
  220. }
  221. }
  222. else {
  223. $pgoods_view = intval($prod_info['pgoods_view']);
  224. }
  225. return $pgoods_view;
  226. }
  227. /**
  228. * 获得礼品兑换状态
  229. * @access public
  230. * @author csdeshang
  231. * @param type $prodinfo 礼品信息
  232. * @return string
  233. */
  234. public function getPointProdExstate($prodinfo)
  235. {
  236. $datetime = TIMESTAMP;
  237. $ex_state = 'end';//兑换按钮的可用状态
  238. if ($prodinfo['pgoods_islimittime'] == 1) {
  239. //即将开始
  240. if ($prodinfo['pgoods_starttime'] > $datetime && $prodinfo['pgoods_storage'] > 0) {
  241. $ex_state = 'willbe';
  242. }
  243. //时间进行中
  244. if ($prodinfo['pgoods_starttime'] <= $datetime && $datetime < $prodinfo['pgoods_endtime'] && $prodinfo['pgoods_storage'] > 0) {
  245. $ex_state = 'going';
  246. }
  247. }
  248. else {
  249. if ($prodinfo['pgoods_storage'] > 0) {
  250. $ex_state = 'going';
  251. }
  252. }
  253. return $ex_state;
  254. }
  255. /**
  256. * 删除礼品信息
  257. * @access public
  258. * @author csdeshang
  259. * @param type $pg_id 礼品ID
  260. * @return boolean
  261. */
  262. public function delPointProdById($pg_id)
  263. {
  264. //$pg_id 为整形或者数组
  265. $result = Db::name('pointsgoods')->where('pgoods_id','in',$pg_id)->delete();
  266. //删除积分礼品下的图片信息
  267. if ($result) {
  268. //删除积分礼品下的图片信息
  269. $upload_model = model('upload');
  270. $condition = array();
  271. $condition[] = array('upload_type','in',array('5','6'));
  272. $condition[] = array('item_id','in',$pg_id);
  273. $upload_list = $upload_model->getUploadList($condition);
  274. if (is_array($upload_list) && count($upload_list) > 0) {
  275. $upload_idarr = array();
  276. foreach ($upload_list as $v) {
  277. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_POINTPROD . DIRECTORY_SEPARATOR . $v['file_name']);
  278. $upload_idarr[] = $v['upload_id'];
  279. }
  280. //删除图片
  281. $condition = array();
  282. $condition[] = array('upload_id','in',$upload_idarr);
  283. $upload_model->delUpload($condition);
  284. }
  285. }
  286. return $result;
  287. }
  288. /**
  289. * 编辑积分礼品信息
  290. * @access public
  291. * @author csdeshang
  292. * @param type $update_arr 更新数据
  293. * @param type $where 条件
  294. * @return boolean
  295. */
  296. public function editPointProd($update_arr, $where)
  297. {
  298. if (empty($update_arr)) {
  299. return true;
  300. }
  301. $result = Db::name('pointsgoods')->where($where)->update($update_arr);
  302. return $result;
  303. }
  304. /**
  305. * 获得推荐的热门兑换商品列表
  306. * @access public
  307. * @author csdeshang
  308. * @param type $num
  309. * @return type
  310. */
  311. public function getRecommendPointProd($num)
  312. {
  313. $where = array();
  314. $where[] = array('pgoods_show','=',1);
  315. $where[] = array('pgoods_state','=',0);
  316. $where[] = array('pgoods_commend','=',1);
  317. $recommend_pointsprod = $this->getPointProdList($where, '*', 'pgoods_sort asc,pgoods_id desc', $num);
  318. if ($recommend_pointsprod && is_array($recommend_pointsprod)) {
  319. foreach ($recommend_pointsprod as $k => $v) {
  320. //处理限制的会员等级
  321. $limitgrade = intval($v['pgoods_limitmgrade']);
  322. if ($limitgrade > 0) {
  323. $membergrade_arr = model('member')->getMemberGradeArr();
  324. $v['pgoods_limitgradename'] = $membergrade_arr[$limitgrade]['level_name'];
  325. $recommend_pointsprod[$k] = $v;
  326. }
  327. }
  328. }
  329. return $recommend_pointsprod;
  330. }
  331. /**
  332. * 更新礼品浏览次数
  333. * @access public
  334. * @author csdeshang
  335. * @param type $prod_id 礼品ID
  336. * @return int
  337. */
  338. public function editPointProdViewnum($prod_id)
  339. {
  340. if (intval($prod_id) <= 0) {
  341. return array('state' => false, 'msg' => '参数错误');
  342. }
  343. $viewnum = 0;//最新浏览次数
  344. $cache_arr = array();
  345. $tmptime = TIMESTAMP;
  346. if (!config('ds_config.cache_open')) {//直接更新数据库浏览次数
  347. $this->editPointProd(array('pgoods_view' => Db::raw('pgoods_view+1')), array('pgoods_id' => $prod_id));
  348. }
  349. else {//通过缓存记录浏览次数
  350. $prod_info = rcache($prod_id, 'pointprod');
  351. if (empty($prod_info)) {//如果兑换礼品的浏览次数缓存不存在,则查询兑换礼品数据库信息,建立缓存
  352. //查询兑换礼品信息
  353. $prod_info = $this->getPointProdInfo(array('pgoods_id' => $prod_id), 'pgoods_view');
  354. $viewnum = intval($prod_info['pgoods_view']) + 1;
  355. wcache($prod_id, array('pgoods_view' => $viewnum, 'view_updatetime' => $tmptime), 'pointprod');
  356. }
  357. else {
  358. $viewnum = intval($prod_info['pgoods_view']) + 1;
  359. if (($prod_info['view_updatetime'] + 3600) < $tmptime) {//如果缓存时间超出1小时,则将更新进入数据库,时间初始为当前时间
  360. $this->editPointProd(array('pgoods_view' => $viewnum), array('pgoods_id' => $prod_id));
  361. wcache($prod_id, array('pgoods_view' => $viewnum, 'view_updatetime' => $tmptime), 'pointprod');
  362. }
  363. else {//如果缓存时间未超出1小时,则更新浏览次数
  364. wcache($prod_id, array('pgoods_view' => $viewnum), 'pointprod');
  365. }
  366. }
  367. }
  368. return array('state' => true);
  369. }
  370. }