Voucher.php 33 KB

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