Pointprod.php 14 KB

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