Voucher.php 33 KB

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