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