SpecialExchange.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\special;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use app\wap\model\store\StorePink;
  15. use app\wap\model\topic\TestPaperObtain;
  16. use app\wap\model\material\DataDownloadBuy;
  17. /**专题兑换
  18. * Class SpecialExchange
  19. * @package app\wap\model\special
  20. */
  21. class SpecialExchange extends ModelBasic
  22. {
  23. use ModelTrait;
  24. /**提交兑换
  25. * @param $uid
  26. * @param $code
  27. */
  28. public static function userExchangeSubmit($uid, $special_id, $code)
  29. {
  30. if (!$uid) return self::setErrorInfo('参数错误!');
  31. $exchange = SpecialExchange::where(['exchange_code' => $code, 'special_id' => $special_id])->find();
  32. if (!$exchange) return self::setErrorInfo('兑换码不存在或活动和兑换码不匹配!');
  33. if (!$exchange['status']) return self::setErrorInfo('兑换码已结束!');
  34. if ($exchange['use_uid'] > 0 || $exchange['use_time'] > 0) return self::setErrorInfo('兑换码已兑换!');
  35. $batch = SpecialBatch::where('id', $exchange['card_batch_id'])->find();
  36. if (!$batch['status']) return self::setErrorInfo('活动已结束!');
  37. self::beginTrans();
  38. $pinkId = StorePink::where(['cid' => $special_id, 'status' => '1', 'uid' => $uid])->order('add_time desc')->value('id');
  39. if ($pinkId) {
  40. return self::setErrorInfo('您正在拼团,无法兑换!', true);
  41. }
  42. $res = self::edit(['use_uid' => $uid, 'use_time' => time()], $exchange['id'], 'id');
  43. if ($res && $batch) {
  44. $res1 = SpecialBatch::edit(['use_num' => bcadd($batch['use_num'], 1, 0)], $exchange['card_batch_id'], 'id');
  45. if (!$res1) {
  46. return self::setErrorInfo('数据修改有误!', true);
  47. }
  48. $special = Special::PreWhere()->where('id', $special_id)->field('id,is_light,type,money,pay_type,validity')->find();
  49. if (!$special) {
  50. return self::setErrorInfo('兑换码关联的专题不存在!', true);
  51. }
  52. if (in_array($special['money'], [0, 0.00]) || in_array($special['pay_type'], [PAY_NO_MONEY, PAY_PASSWORD])) {
  53. $isPay = 1;
  54. } else {
  55. $isPay = (!$uid || $uid == 0) ? false : SpecialBuy::PaySpecial($special_id, $uid);
  56. }
  57. if ($isPay) {
  58. return self::setErrorInfo('该专题是免费的或者已购买,无需兑换!', true);
  59. }
  60. if ($special['type'] == SPECIAL_COLUMN) {
  61. $special_source = SpecialSource::getSpecialSource($special['id']);
  62. if ($special_source) {
  63. foreach ($special_source as $k => $v) {
  64. $task_special = Special::PreWhere()->where(['id'=>$v['source_id']])->find();
  65. if ($task_special['is_show'] == 1) {
  66. SpecialBuy::setBuySpecial('', $uid, $v['source_id'], 4, $task_special['validity'], $special_id);
  67. }
  68. }
  69. }
  70. }
  71. SpecialBuy::setBuySpecial('', $uid, $special_id, 4, $special['validity']);
  72. TestPaperObtain::setTestPaper('', $uid, $special_id, 2);
  73. DataDownloadBuy::setDataDownload('', $uid, $special_id, 1);
  74. self::commitTrans();
  75. $data['id'] = $special['id'];
  76. $data['is_light'] = $special['is_light'];
  77. return $data;
  78. } else {
  79. return self::setErrorInfo('数据修改有误!', true);
  80. }
  81. }
  82. }