GiftCardLogic.php 6.3 KB

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