Voucher.php 32 KB

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