GiftCardLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 think\facade\Db;
  26. class GiftCardLogic extends BaseLogic
  27. {
  28. /**
  29. * @notes 新增礼品卡
  30. * @param $params
  31. * @return bool|string
  32. * @author ljj
  33. * @date 2022/3/31 2:29 下午
  34. */
  35. public function add($params)
  36. {
  37. Db::startTrans();
  38. try {
  39. $num = $params['num'];
  40. $money = $params['money'];
  41. $gift_card_max_no = GiftCardInfo::where(['type'=>1])->max('card_no');
  42. if(empty($gift_card_max_no)) {
  43. $start_no = 100000001;
  44. }else{
  45. $start_no = $gift_card_max_no+1;
  46. }
  47. $end_no = $start_no + $num -1;
  48. $nowtime = time();
  49. $todaytime = date('Y-m-d',$nowtime);
  50. $where = [];
  51. $where[]=['create_time','>',strtotime($todaytime)];
  52. $card_info = GiftCard::where($where)->order('create_time desc')->find();
  53. if($card_info){
  54. $date = $card_info['date'] +1;
  55. }else{
  56. $date =date('Ymd',$nowtime).'00001';
  57. }
  58. //添加批次信息
  59. $card_data = [
  60. 'date' =>$date,
  61. 'type' =>1,
  62. 'card_remark' => $start_no.'~'.$end_no,
  63. 'card_money' => $money,
  64. 'total_num' => $num,
  65. 'create_time' => time(),
  66. ];
  67. $gift_card = GiftCard::create($card_data);
  68. if(!$gift_card){
  69. Db::rollback();
  70. self::setError('插入批次信息错误!');
  71. }
  72. $gc_id = $gift_card['id'];
  73. $gcidata = [];
  74. $saveData = [];
  75. $saveData['gc_id'] = $gc_id;
  76. $saveData['type'] = 1;
  77. $saveData['card_money'] = $money;
  78. $saveData['is_used'] = 0;
  79. $saveData['create_time'] = time();
  80. for($i = 1;$i<=$num;$i++){
  81. $saveData['card_no'] = $start_no;
  82. $pass = gift_card_pass();
  83. $saveData['card_pass'] = $pass;
  84. $start_no ++;
  85. $gcidata[]=$saveData;
  86. }
  87. if(!empty($gcidata)){
  88. $gCobj = new GiftCardInfo();
  89. $card_add_info = $gCobj->saveAll($gcidata);
  90. if(!$card_add_info){
  91. Db::rollback();
  92. self::setError('插入礼品卡信息错误,请重试!');
  93. }
  94. }
  95. // 提交事务
  96. Db::commit();
  97. return true;
  98. } catch (\Exception $e) {
  99. // 回滚事务
  100. Db::rollback();
  101. return $e->getMessage();
  102. }
  103. }
  104. /**
  105. * @notes 删除批次信息
  106. * @param $params
  107. * @return array
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @author ljj
  112. * @date 2022/3/31 2:37 下午
  113. */
  114. public function deleteGiftCard($params)
  115. {
  116. Db::startTrans();
  117. try {
  118. $res = GiftCard::destroy($params['id']);
  119. // 提交事务
  120. if(!$res){
  121. Db::rollback();
  122. self::setError('删除批次信息错误!');
  123. }
  124. $res2 = GiftCardInfo::where(['gc_id'=>$params['id'],'is_used'=>0])->update(['delete_time'=>time()]);
  125. if(!$res2){
  126. Db::rollback();
  127. self::setError('删除礼品卡信息错误!');
  128. }
  129. // 提交事务
  130. Db::commit();
  131. return true;
  132. } catch (\Exception $e) {
  133. // 回滚事务
  134. Db::rollback();
  135. return $e->getMessage();
  136. }
  137. }
  138. /**
  139. * @notes 删除礼品卡信息
  140. * @param $params
  141. * @return array
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @author ljj
  146. * @date 2022/3/31 2:37 下午
  147. */
  148. public function deleteGiftCardInfo($params)
  149. {
  150. Db::startTrans();
  151. try {
  152. $res = GiftCardInfo::destroy($params['id']);
  153. if(!$res){
  154. Db::rollback();
  155. self::setError('删除礼品卡信息错误!');
  156. }
  157. // 提交事务
  158. Db::commit();
  159. return true;
  160. } catch (\Exception $e) {
  161. // 回滚事务
  162. Db::rollback();
  163. return $e->getMessage();
  164. }
  165. }
  166. }