GiftCardLogic.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\gift_card;
  20. use app\common\logic\BaseLogic;
  21. use app\common\model\GiftCardInfo;
  22. use app\common\model\GiftCard;
  23. use app\common\service\ConfigService;
  24. use app\common\service\FileService;
  25. use app\common\service\GiftCardQrCodeService;
  26. use think\facade\Db;
  27. class GiftCardLogic extends BaseLogic
  28. {
  29. /**
  30. * @notes 新增礼品卡
  31. * @param $params
  32. * @return bool|string
  33. * @author ljj
  34. * @date 2022/3/31 2:29 下午
  35. */
  36. public function add($params)
  37. {
  38. Db::startTrans();
  39. try {
  40. $num = $params['num'];
  41. $money = $params['money'];
  42. $gift_card_max_no = GiftCardInfo::where(['type'=>1])->max('card_no');
  43. if(empty($gift_card_max_no)) {
  44. $start_no = 100000001;
  45. }else{
  46. $start_no = $gift_card_max_no+1;
  47. }
  48. $end_no = $start_no + $num -1;
  49. $nowtime = time();
  50. $todaytime = date('Y-m-d',$nowtime);
  51. $where = [];
  52. $where[]=['create_time','>',strtotime($todaytime)];
  53. $card_info = GiftCard::where($where)->order('create_time desc')->find();
  54. if($card_info){
  55. $date = $card_info['date'] +1;
  56. }else{
  57. $date =date('Ymd',$nowtime).'00001';
  58. }
  59. //添加批次信息
  60. $card_data = [
  61. 'date' =>$date,
  62. 'type' =>1,
  63. 'card_remark' => $start_no.'~'.$end_no,
  64. 'card_money' => $money,
  65. 'total_num' => $num,
  66. 'create_time' => time(),
  67. ];
  68. $gift_card = GiftCard::create($card_data);
  69. if(!$gift_card){
  70. Db::rollback();
  71. self::setError('插入批次信息错误!');
  72. }
  73. $gc_id = $gift_card['id'];
  74. $gcidata = [];
  75. $saveData = [];
  76. $saveData['gc_id'] = $gc_id;
  77. $saveData['type'] = 1;
  78. $saveData['card_money'] = $money;
  79. $saveData['is_used'] = 0;
  80. $saveData['create_time'] = time();
  81. for($i = 1;$i<=$num;$i++){
  82. $saveData['card_no'] = $start_no;
  83. $pass = gift_card_pass();
  84. $saveData['card_pass'] = $pass;
  85. // // 生成小程序二维码并上传到七牛云
  86. $qrCodePath = GiftCardQrCodeService::generateAndUploadQrCode($start_no, $pass);
  87. $saveData['qr_code_path'] = $qrCodePath ?: '';
  88. $start_no ++;
  89. $gcidata[]=$saveData;
  90. }
  91. if(!empty($gcidata)){
  92. $gCobj = new GiftCardInfo();
  93. $card_add_info = $gCobj->saveAll($gcidata);
  94. if(!$card_add_info){
  95. Db::rollback();
  96. self::setError('插入礼品卡信息错误,请重试!');
  97. }
  98. }
  99. // 提交事务
  100. Db::commit();
  101. return true;
  102. } catch (\Exception $e) {
  103. // 回滚事务
  104. Db::rollback();
  105. return $e->getMessage();
  106. }
  107. }
  108. /**
  109. * @notes 删除批次信息
  110. * @param $params
  111. * @return array
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. * @author ljj
  116. * @date 2022/3/31 2:37 下午
  117. */
  118. public function deleteGiftCard($params)
  119. {
  120. Db::startTrans();
  121. try {
  122. // $res = GiftCard::destroy($params['id']);
  123. $res = GiftCard::where(['id'=>$params['id']])->update(['delete_time'=>time()]);
  124. // 提交事务
  125. if(!$res){
  126. Db::rollback();
  127. self::setError('删除批次信息错误!');
  128. }
  129. $list = GiftCardInfo::where(['gc_id'=>$params['id'],'is_used'=>0])->findOrEmpty();
  130. if(!$list->isEmpty()){
  131. $res2 = GiftCardInfo::where(['gc_id'=>$params['id'],'is_used'=>0])->update(['delete_time'=>time()]);
  132. if(!$res2){
  133. Db::rollback();
  134. self::setError('删除礼品卡信息错误!');
  135. }
  136. }
  137. // 提交事务
  138. Db::commit();
  139. return true;
  140. } catch (\Exception $e) {
  141. // 回滚事务
  142. Db::rollback();
  143. return $e->getMessage();
  144. }
  145. }
  146. /**
  147. * @notes 删除礼品卡信息
  148. * @param $params
  149. * @return array
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. * @author ljj
  154. * @date 2022/3/31 2:37 下午
  155. */
  156. public function deleteGiftCardInfo($params)
  157. {
  158. Db::startTrans();
  159. try {
  160. $res = GiftCardInfo::destroy($params['id']);
  161. if(!$res){
  162. Db::rollback();
  163. self::setError('删除礼品卡信息错误!');
  164. }
  165. // 提交事务
  166. Db::commit();
  167. return true;
  168. } catch (\Exception $e) {
  169. // 回滚事务
  170. Db::rollback();
  171. return $e->getMessage();
  172. }
  173. }
  174. }