Pointprod.php 14 KB

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