Voucher.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 Voucher extends BaseModel
  16. {
  17. const VOUCHER_STATE_UNUSED = 1;
  18. const VOUCHER_STATE_USED = 2;
  19. const VOUCHER_STATE_EXPIRE = 3;
  20. public $page_info;
  21. private $voucher_state_array = array(
  22. self::VOUCHER_STATE_UNUSED => '未使用', self::VOUCHER_STATE_USED => '已使用', self::VOUCHER_STATE_EXPIRE => '已过期',
  23. );
  24. const VOUCHER_GETTYPE_DEFAULT = 'points'; //默认领取方式
  25. private $voucher_gettype_array = array(
  26. 'points' => array('sign' => 1, 'name' => '积分兑换'), 'pwd' => array('sign' => 2, 'name' => '卡密兑换'),
  27. 'free' => array('sign' => 3, 'name' => '免费领取')
  28. );
  29. private $quotastate_arr;
  30. private $templatestate_arr;
  31. /**
  32. * 构造函数
  33. * @access public
  34. * @author csdeshang
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. //套餐状态
  40. $this->quotastate_arr = array(
  41. 'activity' => array(1, lang('voucher_quotastate_activity')),
  42. 'cancel' => array(2, lang('voucher_quotastate_cancel')),
  43. 'expire' => array(3, lang('voucher_quotastate_expire'))
  44. );
  45. //代金券模板状态
  46. $this->templatestate_arr = array('usable' => array(1, '有效'), 'disabled' => array(2, '失效'));
  47. }
  48. /**
  49. * 获取代金券模板状态
  50. * @access public
  51. * @author csdeshang
  52. * @return type
  53. */
  54. public function getTemplateState()
  55. {
  56. return $this->templatestate_arr;
  57. }
  58. /**
  59. * 领取的代金券状态
  60. * @access public
  61. * @author csdeshang
  62. * @return type
  63. */
  64. public function getVoucherState()
  65. {
  66. return array(
  67. 'unused' => array(1, lang('voucher_voucher_state_unused')),
  68. 'used' => array(2, lang('voucher_voucher_state_used')),
  69. 'expire' => array(3, lang('voucher_voucher_state_expire'))
  70. );
  71. }
  72. /**
  73. * 返回当前可用的代金券列表,每种类型(模板)的代金券里取出一个代金券码(同一个模板所有码面额和到期时间都一样)
  74. * @access public
  75. * @author csdeshang
  76. * @param array $condition 条件
  77. * @param int $goods_total 商品总金额
  78. * @return string
  79. */
  80. public function getCurrentAvailableVoucher($condition = array(), $goods_total = 0)
  81. {
  82. $condition[] = array('voucher_state', '=', 1);
  83. $condition[] = array('voucher_enddate', '>', TIMESTAMP);
  84. $voucher_list = Db::name('voucher')->where($condition)->select()->toArray();
  85. if(!empty($voucher_list)){
  86. $voucher_list = ds_change_arraykey($voucher_list,'vouchertemplate_id');
  87. }
  88. foreach ($voucher_list as $key => $voucher) {
  89. if ($goods_total < $voucher['voucher_limit']) {
  90. unset($voucher_list[$key]);
  91. }
  92. else {
  93. $voucher_list[$key]['desc'] = sprintf('面额%s元 有效期至 %s ', $voucher['voucher_price'], date('Y-m-d', $voucher['voucher_enddate']));
  94. if ($voucher['voucher_limit'] > 0) {
  95. $voucher_list[$key]['desc'] .= sprintf(' 消费满%s可用', $voucher['voucher_limit']);
  96. }
  97. }
  98. }
  99. return $voucher_list;
  100. }
  101. /**
  102. * 取得当前有效代金券数量
  103. * @access public
  104. * @author csdeshang
  105. * @param type $member_id 会员ID
  106. * @return type
  107. */
  108. public function getCurrentAvailableVoucherCount($member_id)
  109. {
  110. $info = rcache($member_id, 'm_voucher');
  111. if (empty($info)) {
  112. $condition = array();
  113. $condition[] = array('voucher_owner_id','=',$member_id);
  114. $condition[] = array('voucher_enddate','>',TIMESTAMP);
  115. $condition[] = array('voucher_state','=',1);
  116. $voucher_count = Db::name('voucher')->where($condition)->count();
  117. $voucher_count = intval($voucher_count);
  118. wcache($member_id, array('voucher_count' => $voucher_count), 'm_voucher');
  119. }
  120. else {
  121. $voucher_count = intval($info['voucher_count']);
  122. }
  123. return $voucher_count;
  124. }
  125. /**
  126. * 查询当前可用的套餐
  127. * @access public
  128. * @author csdeshang
  129. * @param type $store_id 店铺ID
  130. * @return boolean|array
  131. */
  132. public function getVoucherquotaCurrent($store_id)
  133. {
  134. $store_id = intval($store_id);
  135. if ($store_id <= 0) {
  136. return false;
  137. }
  138. $condition = array();
  139. $condition[] = array('voucherquota_storeid', '=', $store_id);
  140. $condition[] = array('voucherquota_endtime', '>', TIMESTAMP);
  141. $info = Db::name('voucherquota')->where($condition)->find();
  142. return $info;
  143. }
  144. /**
  145. * 获得代金券列表
  146. * @access public
  147. * @author csdeshang
  148. * @param type $where 条件
  149. * @param type $field 字段
  150. * @return type
  151. */
  152. public function getVoucherList($where, $field = '*')
  153. {
  154. $voucher_list = array();
  155. $voucher_list = Db::name('voucher')->field($field)->where($where)->select()->toArray();
  156. return $voucher_list;
  157. }
  158. /**
  159. * 获取失效代金卷列表
  160. * @access public
  161. * @author csdeshang
  162. * @param array $where 条件
  163. * @param type $field 字段
  164. * @return type
  165. */
  166. public function getVoucherUnusedList($where, $field = '*')
  167. {
  168. $where[] = array('voucher_state','=',1);
  169. return $this->getVoucherList($where, $field);
  170. }
  171. /**
  172. * 查询可兑换代金券模板详细信息,包括店铺信息
  173. * @access public
  174. * @author csdeshang
  175. * @param type $vid
  176. * @param type $member_id 会员id
  177. * @param type $store_id 店铺id
  178. * @return type
  179. */
  180. public function getCanChangeTemplateInfo($vid, $member_id, $store_id = 0,$no_point=false)
  181. {
  182. if ($vid <= 0 || $member_id <= 0) {
  183. return array('state' => false, 'msg' => '参数错误');
  184. }
  185. //查询可用代金券模板
  186. $where = array();
  187. $where[] = array('vouchertemplate_id','=',$vid);
  188. $where[] = array('vouchertemplate_state','=',$this->templatestate_arr['usable'][0]);
  189. $where[] = array('vouchertemplate_enddate','>',TIMESTAMP);
  190. $template_info = $this->getVouchertemplateInfo($where);
  191. if (empty($template_info) || $template_info['vouchertemplate_total'] <= $template_info['vouchertemplate_giveout']) {//代金券不存在或者已兑换完
  192. return array('state' => false, 'msg' => '代金券已兑换完');
  193. }
  194. //验证是否为店铺自己
  195. if ($store_id > 0 && $store_id == $template_info['vouchertemplate_store_id']) {
  196. return array('state' => false, 'msg' => '不可以兑换自己店铺的代金券');
  197. }
  198. $member_model = model('member');
  199. $member_info = $member_model->getMemberInfoByID($member_id);
  200. if (empty($member_info)) {
  201. return array('state' => false, 'msg' => '参数错误');
  202. }
  203. if(!$no_point){
  204. //验证会员积分是否足够
  205. if ($template_info['vouchertemplate_gettype'] == $this->voucher_gettype_array['points']['sign'] && $template_info['vouchertemplate_points'] > 0) {
  206. if (intval($member_info['member_points']) < intval($template_info['vouchertemplate_points'])) {
  207. return array('state' => false, 'msg' => '您的积分不足,暂时不能兑换该代金券');
  208. }
  209. }
  210. }
  211. //验证会员级别
  212. /*
  213. $member_currgrade = $member_model->getOneMemberGrade(intval($member_info['member_exppoints']));
  214. $member_info['member_currgrade'] = $member_currgrade ? $member_currgrade['level'] : 0;
  215. if ($member_info['member_currgrade'] < intval($template_info['vouchertemplate_mgradelimit'])) {
  216. return array('state' => false, 'msg' => '您的会员级别不够,暂时不能兑换该代金券');
  217. }
  218. */
  219. //查询代金券对应的店铺信息
  220. $store_info = model('store')->getStoreInfoByID($template_info['vouchertemplate_store_id']);
  221. if (empty($store_info)) {
  222. return array('state' => false, 'msg' => '代金券已兑换完');
  223. }
  224. //整理代金券信息
  225. $template_info = array_merge($template_info, $store_info);
  226. //查询代金券列表
  227. $where = array();
  228. $where[] = array('voucher_owner_id','=',$member_id);
  229. $where[] = array('voucher_store_id','=',$template_info['vouchertemplate_store_id']);
  230. $voucher_list = $this->getVoucherList($where);
  231. //halt($voucher_list);
  232. if (!empty($voucher_list)) {
  233. if(!$no_point){
  234. $voucher_count = 0; //在该店铺兑换的代金券数量
  235. $voucherone_count = 0; //该张代金券兑换的数量
  236. foreach ($voucher_list as $k => $v) {
  237. //如果代金券未用且未过期
  238. if ($v['voucher_state'] == 1 && $v['voucher_enddate'] > TIMESTAMP) {
  239. $voucher_count += 1;
  240. }
  241. if ($v['vouchertemplate_id'] == $template_info['vouchertemplate_id']) {
  242. $voucherone_count += 1;
  243. }
  244. }
  245. //买家最多只能拥有同一个店铺尚未消费抵用的店铺代金券最大数量的验证
  246. if ($voucher_count >= intval(config('ds_config.voucher_buyertimes_limit'))) {
  247. $message = sprintf('您的可用代金券已有%s张,不可再兑换了', config('ds_config.voucher_buyertimes_limit'));
  248. return array('state' => false, 'msg' => $message);
  249. }
  250. //同一张代金券最多能兑换的次数
  251. if (!empty($template_info['vouchertemplate_eachlimit']) && $voucherone_count >= $template_info['vouchertemplate_eachlimit']) {
  252. $message = sprintf('该代金券您已兑换%s次,不可再兑换了', $template_info['vouchertemplate_eachlimit']);
  253. return array('state' => false, 'msg' => $message);
  254. }
  255. }
  256. }
  257. return array('state' => true, 'info' => $template_info);
  258. }
  259. /**
  260. * 获取代金券编码
  261. * @access public
  262. * @author csdeshang
  263. * @staticvar int $num
  264. * @param type $member_id 会员id
  265. * @return type
  266. */
  267. public function getVoucherCode($member_id = 0)
  268. {
  269. static $num = 1;
  270. $sign_arr = array();
  271. $sign_arr[] = sprintf('%02d', mt_rand(10, 99));
  272. $sign_arr[] = sprintf('%03d', (float)microtime() * 1000);
  273. $sign_arr[] = sprintf('%010d', TIMESTAMP - 946656000);
  274. if ($member_id) {
  275. $sign_arr[] = sprintf('%03d', (int)$member_id % 1000);
  276. }
  277. else {
  278. //自增变量
  279. $tmpnum = 0;
  280. if ($num > 99) {
  281. $tmpnum = substr($num, -1, 2);
  282. }
  283. else {
  284. $tmpnum = $num;
  285. }
  286. $sign_arr[] = sprintf('%02d', $tmpnum);
  287. $sign_arr[] = mt_rand(1, 9);
  288. }
  289. $code = implode('', $sign_arr);
  290. $num += 1;
  291. return $code;
  292. }
  293. /**
  294. * 生成代金券卡密
  295. * @staticvar int $num
  296. * @param type $vouchertemplate_id 卡密ID编号
  297. * @return boolean|array
  298. */
  299. public function createVoucherPwd($vouchertemplate_id)
  300. {
  301. if ($vouchertemplate_id <= 0) {
  302. return false;
  303. }
  304. static $num = 1;
  305. $sign_arr = array();
  306. //时间戳
  307. $time_tmp = uniqid('', true);
  308. $time_tmp = explode('.', $time_tmp);
  309. $sign_arr[] = substr($time_tmp[0], -1, 4) . $time_tmp[1];
  310. //自增变量
  311. $tmpnum = 0;
  312. if ($num > 999) {
  313. $tmpnum = substr($num, -1, 3);
  314. }
  315. else {
  316. $tmpnum = $num;
  317. }
  318. $sign_arr[] = sprintf('%03d', $tmpnum);
  319. //代金券模板ID
  320. if ($vouchertemplate_id > 9999) {
  321. $vouchertemplate_id = substr($num, -1, 4);
  322. }
  323. $sign_arr[] = sprintf('%04d', $vouchertemplate_id);
  324. //随机数
  325. $sign_arr[] = sprintf('%04d', rand(1, 9999));
  326. $pwd = implode('', $sign_arr);
  327. $num += 1;
  328. return array(md5($pwd), ds_encrypt($pwd));
  329. }
  330. /**
  331. * 生成代金券卡密
  332. * @access public
  333. * @author csdeshang
  334. * @param type $pwd 卡密
  335. * @return string
  336. */
  337. public function getVoucherPwd($pwd)
  338. {
  339. if (!$pwd) {
  340. return '';
  341. }
  342. $pwd = ds_decrypt($pwd);
  343. $pattern = "/^([0-9]{20})$/i";
  344. if (preg_match($pattern, $pwd)) {
  345. return $pwd;
  346. }
  347. else {
  348. return '';
  349. }
  350. }
  351. /**
  352. * 更新代金券信息
  353. * @param type $data 数据
  354. * @param type $condition 条件
  355. * @param type $member_id 会员id
  356. * @return type
  357. */
  358. public function editVoucher($data, $condition, $member_id = 0)
  359. {
  360. $result = Db::name('voucher')->where($condition)->update($data);
  361. if ($result && $member_id > 0) {
  362. wcache($member_id, array('voucher_count' => null), 'm_voucher');
  363. }
  364. return $result;
  365. }
  366. /**
  367. * 返回代金券状态数组
  368. * @access public
  369. * @author csdeshang
  370. * @return array
  371. */
  372. public function getVoucherStateArray()
  373. {
  374. return $this->voucher_state_array;
  375. }
  376. /**
  377. * 返回代金券领取方式数组
  378. * @access public
  379. * @author csdeshang
  380. * @return array
  381. */
  382. public function getVoucherGettypeArray()
  383. {
  384. return $this->voucher_gettype_array;
  385. }
  386. /**
  387. * 获取买家代金券列表
  388. * @access public
  389. * @author csdeshang
  390. * @param int $member_id 用户编号
  391. * @param int $voucher_state 代金券状态
  392. * @param int $pagesize 分页数
  393. * @return array
  394. */
  395. public function getMemberVoucherList($member_id, $voucher_state, $pagesize = null)
  396. {
  397. if (empty($member_id)) {
  398. return false;
  399. }
  400. //更新过期代金券状态
  401. $this->_checkVoucherExpire($member_id);
  402. $where = array();
  403. $where[] = array('voucher_owner_id','=',$member_id);
  404. $voucher_state = intval($voucher_state);
  405. if (intval($voucher_state) > 0 && array_key_exists($voucher_state, $this->voucher_state_array)) {
  406. $where[] = array('voucher_state','=',$voucher_state);
  407. }
  408. if($pagesize){
  409. $list = Db::name('voucher')->alias('v')->join('store s','s.store_id=v.voucher_store_id')->join('vouchertemplate t','v.vouchertemplate_id=t.vouchertemplate_id')->where($where)->order('voucher_id desc')->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  410. $this->page_info=$list;
  411. $list=$list->items();
  412. }else{
  413. $list=Db::name('voucher')->alias('v')->join('store s','s.store_id=v.voucher_store_id')->join('vouchertemplate t','v.vouchertemplate_id=t.vouchertemplate_id')->where($where)->order('voucher_id desc')->select()->toArray();
  414. }
  415. if (!empty($list) && is_array($list)) {
  416. foreach ($list as $key => $val) {
  417. //代金券图片
  418. if (empty($val['vouchertemplate_customimg'])) {
  419. $list[$key]['vouchertemplate_customimg'] = ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  420. }
  421. else {
  422. $list[$key]['vouchertemplate_customimg'] = ds_get_pic( ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $val['store_id'] ,$val['vouchertemplate_customimg']);
  423. }
  424. //代金券状态文字
  425. $list[$key]['voucher_state_text'] = $this->voucher_state_array[$val['voucher_state']];
  426. $list[$key]['voucher_end_date_text'] = $val['voucher_enddate'] ? @date('Y.m.d', $val['voucher_enddate']) : '';
  427. }
  428. }
  429. return $list;
  430. }
  431. /**
  432. * 更新过期代金券状态
  433. * @access public
  434. * @author csdeshang
  435. * @param type $member_id 会员ID
  436. * @return type
  437. */
  438. private function _checkVoucherExpire($member_id)
  439. {
  440. $condition = array();
  441. $condition[] = array('voucher_owner_id','=',$member_id);
  442. $condition[] = array('voucher_state','=',self::VOUCHER_STATE_UNUSED);
  443. $condition[] = array('voucher_enddate','<', TIMESTAMP);
  444. Db::name('voucher')->where($condition)->update(array('voucher_state' => self::VOUCHER_STATE_EXPIRE));
  445. }
  446. /**
  447. * 查询代金券模板列表
  448. * @access public
  449. * @author csdeshang
  450. * @param type $where 条件
  451. * @param type $field 字段
  452. * @param type $limit 限制
  453. * @param type $pagesize 分页
  454. * @param type $order 排序
  455. * @return type
  456. */
  457. public function getVouchertemplateList($where, $field = '*', $limit = 0, $pagesize = 0, $order = '')
  458. {
  459. $voucher_list = array();
  460. if ($pagesize) {
  461. $result = Db::name('vouchertemplate')->field($field)->where($where)->order($order)->paginate(['list_rows'=>10,'query' => request()->param()],false);
  462. $voucher_list = $result->items();
  463. $this->page_info = $result;
  464. } else {
  465. $voucher_list = Db::name('vouchertemplate')->field($field)->where($where)->limit($limit)->order($order)->select()->toArray();
  466. }
  467. //查询店铺分类
  468. $store_class = rkcache('storeclass', true);
  469. if (!empty($voucher_list) && is_array($voucher_list)) {
  470. foreach ($voucher_list as $k => $v) {
  471. if (!empty($v['vouchertemplate_customimg'])) {
  472. $v['vouchertemplate_customimg'] = ds_get_pic( ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $v['vouchertemplate_store_id'] , $v['vouchertemplate_customimg']);
  473. }
  474. else {
  475. $v['vouchertemplate_customimg'] = ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  476. }
  477. $v['vouchertemplate_sc_name'] = isset($store_class[$v['vouchertemplate_sc_id']])?$store_class[$v['vouchertemplate_sc_id']]['storeclass_name']:'';
  478. //领取方式
  479. if ($v['vouchertemplate_gettype']) {
  480. foreach ($this->voucher_gettype_array as $gtype_k => $gtype_v) {
  481. if ($v['vouchertemplate_gettype'] == $gtype_v['sign']) {
  482. $v['vouchertemplate_gettype_key'] = $gtype_k;
  483. $v['vouchertemplate_gettype_text'] = $gtype_v['name'];
  484. }
  485. }
  486. }
  487. //状态
  488. if ($v['vouchertemplate_state']) {
  489. foreach ($this->templatestate_arr as $tstate_k => $tstate_v) {
  490. if ($v['vouchertemplate_state'] == $tstate_v[0]) {
  491. $v['vouchertemplate_state_text'] = $tstate_v[1];
  492. }
  493. }
  494. }
  495. //会员级别
  496. /*
  497. $member_grade = model('member')->getMemberGradeArr();
  498. $v['vouchertemplate_mgradelimittext'] = isset($v['vouchertemplate_mgradelimit']) ? $member_grade[$v['vouchertemplate_mgradelimit']]['level_name'] : '';
  499. */
  500. $voucher_list[$k] = $v;
  501. }
  502. }
  503. return $voucher_list;
  504. }
  505. /**
  506. * 更新代金券模板信息
  507. * @access public
  508. * @author csdeshang
  509. * @param type $where 条件
  510. * @param type $data 数据
  511. * @return type
  512. */
  513. public function editVouchertemplate($where, $data)
  514. {
  515. return Db::name('vouchertemplate')->where($where)->update($data);
  516. }
  517. /**
  518. * 获得代金券面额列表
  519. * @access public
  520. * @author csdeshang
  521. * @return type
  522. */
  523. public function getVoucherPriceList($pagesize='',$order='voucherprice asc')
  524. {
  525. if($pagesize){
  526. $voucherprice_list = Db::name('voucherprice')->order('voucherprice asc')->paginate(['list_rows'=>10,'query' => request()->param()],false);
  527. $this->page_info = $voucherprice_list;
  528. return $voucherprice_list->items();
  529. } else {
  530. return Db::name('voucherprice')->order('voucherprice asc')->select()->toArray();
  531. }
  532. }
  533. /**
  534. * 获得推荐的热门代金券列表
  535. * @access public
  536. * @author csdeshang
  537. * @param int $num 查询条数
  538. * @return array
  539. */
  540. public function getRecommendTemplate($num)
  541. {
  542. //查询推荐的热门代金券列表
  543. $where = array();
  544. $where[] = array('vouchertemplate_recommend','=',1);
  545. $where[] = array('vouchertemplate_state','=',$this->templatestate_arr['usable'][0]);
  546. //领取方式为积分兑换
  547. $where[] = array('vouchertemplate_gettype','=',$this->voucher_gettype_array['points']['sign']);
  548. $where[] = array('vouchertemplate_enddate','>',TIMESTAMP);
  549. $recommend_voucher = $this->getVouchertemplateList($where, $field = '*', $num, 0, 'vouchertemplate_recommend desc,vouchertemplate_id desc');
  550. return $recommend_voucher;
  551. }
  552. /**
  553. * 积分兑换代金券
  554. * @access public
  555. * @author csdeshang
  556. * @param type $template_info 信息模板
  557. * @param type $member_id 会员ID
  558. * @param type $member_name 会员名
  559. * @return type
  560. */
  561. public function exchangeVoucher($template_info, $member_id, $member_name = '',$no_point=false)
  562. {
  563. if (intval($member_id) <= 0 || empty($template_info)) {
  564. return array('state' => false, 'msg' => '参数错误');
  565. }
  566. //查询会员信息
  567. if (!$member_name) {
  568. $member_info = model('member')->getMemberInfoByID($member_id);
  569. if (empty($template_info)) {
  570. return array('state' => false, 'msg' => '参数错误');
  571. }
  572. $member_name = $member_info['member_name'];
  573. }
  574. //添加代金券信息
  575. $insert_arr = array();
  576. $insert_arr['voucher_code'] = $this->getVoucherCode($member_id);
  577. $insert_arr['vouchertemplate_id'] = $template_info['vouchertemplate_id'];
  578. $insert_arr['voucher_title'] = $template_info['vouchertemplate_title'];
  579. $insert_arr['voucher_desc'] = $template_info['vouchertemplate_desc'];
  580. $insert_arr['voucher_startdate'] = TIMESTAMP;
  581. $insert_arr['voucher_enddate'] = $template_info['vouchertemplate_enddate'];
  582. $insert_arr['voucher_price'] = $template_info['vouchertemplate_price'];
  583. $insert_arr['voucher_limit'] = $template_info['vouchertemplate_limit'];
  584. $insert_arr['voucher_store_id'] = $template_info['vouchertemplate_store_id'];
  585. $insert_arr['voucher_state'] = 1;
  586. $insert_arr['voucher_activedate'] = TIMESTAMP;
  587. $insert_arr['voucher_owner_id'] = $member_id;
  588. $insert_arr['voucher_owner_name'] = $member_name;
  589. $result = Db::name('voucher')->insertGetId($insert_arr);
  590. if (!$result) {
  591. return array('state' => false, 'msg' => '兑换失败');
  592. }
  593. if(!$no_point){
  594. //扣除会员积分
  595. if ($template_info['vouchertemplate_points'] > 0 && $template_info['vouchertemplate_gettype'] == $this->voucher_gettype_array['points']['sign']) {
  596. $points_arr['pl_memberid'] = $member_id;
  597. $points_arr['pl_membername'] = $member_name;
  598. $points_arr['pl_points'] = -$template_info['vouchertemplate_points'];
  599. $points_arr['point_ordersn'] = $insert_arr['voucher_code'];
  600. $points_arr['pl_desc'] = lang('home_voucher') . $insert_arr['voucher_code'] . lang('points_pointorderdesc');
  601. $result = model('points')->savePointslog('app', $points_arr, true);
  602. if (!$result) {
  603. return array('state' => false, 'msg' => '兑换失败');
  604. }
  605. }
  606. }
  607. if ($result) {
  608. //代金券模板的兑换数增加
  609. $result = $this->editVouchertemplate(array('vouchertemplate_id' => $template_info['vouchertemplate_id']), array(
  610. 'vouchertemplate_giveout' => Db::raw('vouchertemplate_giveout+1')
  611. ));
  612. if (!$result) {
  613. return array('state' => false, 'msg' => '兑换失败');
  614. }
  615. return array('state' => true, 'msg' => '兑换成功');
  616. }
  617. else {
  618. return array('state' => false, 'msg' => '兑换失败');
  619. }
  620. }
  621. /**
  622. * 批量增加代金券
  623. * @access public
  624. * @author csdeshang
  625. * @param type $insert_arr 参数数据
  626. * @return type
  627. */
  628. public function addVoucherBatch($insert_arr)
  629. {
  630. return Db::name('voucher')->insertAll($insert_arr);
  631. }
  632. /**
  633. * 获得代金券模板总数量
  634. * @access public
  635. * @author csdeshang
  636. * @param type $where 条件
  637. * @return int
  638. */
  639. public function getVouchertemplateCount($where)
  640. {
  641. return Db::name('vouchertemplate')->where($where)->count();
  642. }
  643. /**
  644. * 获得代金券总数量
  645. * @access public
  646. * @author csdeshang
  647. * @param type $where 条件
  648. * @return int
  649. */
  650. public function getVoucherCount($where)
  651. {
  652. return Db::name('voucher')->where($where)->count();
  653. }
  654. /**
  655. * 获得代金券模板详情
  656. * @access public
  657. * @author csdeshang
  658. * @param type $where 条件
  659. * @param type $field 字段
  660. * @param type $order 排序
  661. * @return array
  662. */
  663. public function getVouchertemplateInfo($where = array(), $field = '*', $order = '')
  664. {
  665. $info = Db::name('vouchertemplate')->where($where)->field($field)->order($order)->find();
  666. if (!$info) {
  667. return array();
  668. }
  669. if ($info['vouchertemplate_gettype']) {
  670. foreach ($this->voucher_gettype_array as $k => $v) {
  671. if ($info['vouchertemplate_gettype'] == $v['sign']) {
  672. $info['vouchertemplate_gettype_key'] = $k;
  673. $info['vouchertemplate_gettype_text'] = $v['name'];
  674. }
  675. }
  676. }
  677. if ($info['vouchertemplate_state']) {
  678. foreach ($this->templatestate_arr as $k => $v) {
  679. if ($info['vouchertemplate_state'] == $v[0]) {
  680. $info['vouchertemplate_state_text'] = $v[1];
  681. }
  682. }
  683. }
  684. //查询店铺分类
  685. if ($info['vouchertemplate_sc_id']) {
  686. $store_class = rkcache('storeclass', true);
  687. $info['vouchertemplate_sc_name'] = $store_class[$info['vouchertemplate_sc_id']]['storeclass_name'];
  688. }
  689. if (!empty($info['vouchertemplate_customimg'])) {
  690. $info['vouchertemplate_customimg'] = ds_get_pic( ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $info['vouchertemplate_store_id'] , $info['vouchertemplate_customimg']);
  691. }
  692. else {
  693. $info['vouchertemplate_customimg'] = ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  694. }
  695. //会员等级
  696. /*
  697. $member_grade = model('member')->getMemberGradeArr();
  698. $info['vouchertemplate_mgradelimittext'] = isset($info['vouchertemplate_mgradelimit'])?$member_grade[$info['vouchertemplate_mgradelimit']]['level_name']:'';
  699. */
  700. return $info;
  701. }
  702. /**
  703. * 获得代金券详情
  704. * @access public
  705. * @author csdeshang
  706. * @param type $where 条件
  707. * @param type $field 字段
  708. * @param type $order 排序
  709. * @return type
  710. */
  711. public function getVoucherInfo($where = array(), $field = '*', $order = '')
  712. {
  713. $info = Db::name('voucher')->where($where)->field($field)->order($order)->find();
  714. if ($info['voucher_state']) {
  715. $info['voucher_state_text'] = $this->voucher_state_array[$info['voucher_state']];
  716. }
  717. return $info;
  718. }
  719. /**
  720. * 获取单个代金券
  721. * @param type $where
  722. * @return type
  723. */
  724. public function getOneVoucherprice($condition = array()){
  725. return Db::name('voucherprice')->where($condition)->find();
  726. }
  727. /**
  728. * 增加代金券面额
  729. * @param type $data
  730. * @return type
  731. */
  732. public function addVoucherprice($data){
  733. return Db::name('voucherprice')->insert($data);
  734. }
  735. /**
  736. * 修改代金券面额
  737. * @param type $where
  738. * @param type $data
  739. * @return type
  740. */
  741. public function editVoucherprice($condition,$data){
  742. return Db::name('voucherprice')->where($condition)->update($data);
  743. }
  744. /**
  745. * 删除代金券
  746. * @param type $condition
  747. * @return type
  748. */
  749. public function delVoucherprice($condition){
  750. return Db::name('voucherprice')->where($condition)->delete();
  751. }
  752. /**
  753. * 更新代金券
  754. * @param type $condition
  755. * @param type $data
  756. * @return type
  757. */
  758. public function editVoucherquota($condition,$data){
  759. return Db::name('voucherquota')->where($condition)->update($data);
  760. }
  761. /**
  762. * 获取代金券套餐列表
  763. * @param type $condition
  764. * @param type $pagesize
  765. * @param type $order
  766. * @return type
  767. */
  768. public function getVoucherquotaList($condition,$pagesize,$order){
  769. if($pagesize){
  770. $voucherquota_list = Db::name('voucherquota')->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  771. $this->page_info = $voucherquota_list;
  772. return $voucherquota_list->items();
  773. } else {
  774. $voucherquota_list = Db::name('voucherquota')->where($condition)->order($order)->select()->toArray();
  775. return $voucherquota_list;
  776. }
  777. }
  778. /**
  779. * 更新使用的代金券状态
  780. * @param $input_voucher_list
  781. * @throws Exception
  782. */
  783. public function editVoucherState($voucher_list)
  784. {
  785. $send = new \sendmsg\sendMemberMsg();
  786. foreach ($voucher_list as $store_id => $voucher_info) {
  787. $update = $this->editVoucher(array('voucher_state' => 2), array('voucher_id' => $voucher_info['voucher_id']), $voucher_info['voucher_owner_id']);
  788. if ($update) {
  789. $this->editVouchertemplate(array('vouchertemplate_id' => $voucher_info['vouchertemplate_id']),array('vouchertemplate_used'=>Db::raw('vouchertemplate_used+1')));
  790. // 发送用户店铺消息
  791. $send->set('member_id', $voucher_info['voucher_owner_id']);
  792. $send->set('code', 'voucher_use');
  793. $ali_param = array();
  794. $ali_param['voucher_code'] = $voucher_info['voucher_code'];
  795. $ten_param=array($voucher_info['voucher_code']);
  796. $param=$ali_param;
  797. $param['voucher_url'] = HOME_SITE_URL .'/Membervoucher/index';
  798. $weixin_param = array(
  799. 'url' => config('ds_config.h5_site_url').'/pages/member/voucher/VoucherList',
  800. 'data'=>array(
  801. "keyword1" => array(
  802. "value" => $voucher_info['voucher_code'],
  803. "color" => "#333"
  804. ),
  805. "keyword2" => array(
  806. "value" => date('Y-m-d H:i'),
  807. "color" => "#333"
  808. )
  809. ),
  810. );
  811. $send->send($param,$weixin_param,$ali_param,$ten_param);
  812. }
  813. else {
  814. return ds_callback(false, '更新代金券状态失败vcode:' . $voucher_info['voucher_code']);
  815. }
  816. }
  817. return ds_callback(true);
  818. }
  819. }
  820. ?>