| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop开源商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
- // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | likeshop团队版权所有并拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshop.cn.team
- // +----------------------------------------------------------------------
- namespace app\adminapi\logic\gift_card;
- use app\common\logic\BaseLogic;
- use app\common\model\GiftCardInfo;
- use app\common\model\GiftCard;
- use app\common\service\ConfigService;
- use app\common\service\FileService;
- use app\common\service\GiftCardQrCodeService;
- use think\facade\Db;
- class GiftCardLogic extends BaseLogic
- {
- /**
- * @notes 新增礼品卡
- * @param $params
- * @return bool|string
- * @author ljj
- * @date 2022/3/31 2:29 下午
- */
- public function add($params)
- {
- Db::startTrans();
- try {
- $num = $params['num'];
- $money = $params['money'];
- $gift_card_max_no = GiftCardInfo::where(['type'=>1])->max('card_no');
- if(empty($gift_card_max_no)) {
- $start_no = 100000001;
- }else{
- $start_no = $gift_card_max_no+1;
- }
- $end_no = $start_no + $num -1;
- $nowtime = time();
- $todaytime = date('Y-m-d',$nowtime);
- $where = [];
- $where[]=['create_time','>',strtotime($todaytime)];
- $card_info = GiftCard::where($where)->order('create_time desc')->find();
- if($card_info){
- $date = $card_info['date'] +1;
- }else{
- $date =date('Ymd',$nowtime).'00001';
- }
- //添加批次信息
- $card_data = [
- 'date' =>$date,
- 'type' =>1,
- 'card_remark' => $start_no.'~'.$end_no,
- 'card_money' => $money,
- 'total_num' => $num,
- 'create_time' => time(),
- ];
- $gift_card = GiftCard::create($card_data);
- if(!$gift_card){
- Db::rollback();
- self::setError('插入批次信息错误!');
- }
- $gc_id = $gift_card['id'];
- $gcidata = [];
- $saveData = [];
- $saveData['gc_id'] = $gc_id;
- $saveData['type'] = 1;
- $saveData['card_money'] = $money;
- $saveData['is_used'] = 0;
- $saveData['create_time'] = time();
- for($i = 1;$i<=$num;$i++){
- $saveData['card_no'] = $start_no;
- $pass = gift_card_pass();
- $saveData['card_pass'] = $pass;
- // // 生成小程序二维码并上传到七牛云
- $qrCodePath = GiftCardQrCodeService::generateAndUploadQrCode($start_no, $pass);
- $saveData['qr_code_path'] = $qrCodePath ?: '';
- $start_no ++;
- $gcidata[]=$saveData;
- }
- if(!empty($gcidata)){
- $gCobj = new GiftCardInfo();
- $card_add_info = $gCobj->saveAll($gcidata);
- if(!$card_add_info){
- Db::rollback();
- self::setError('插入礼品卡信息错误,请重试!');
- }
- }
- // 提交事务
- Db::commit();
- return true;
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return $e->getMessage();
- }
- }
- /**
- * @notes 删除批次信息
- * @param $params
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author ljj
- * @date 2022/3/31 2:37 下午
- */
- public function deleteGiftCard($params)
- {
- Db::startTrans();
- try {
- // $res = GiftCard::destroy($params['id']);
- $res = GiftCard::where(['id'=>$params['id']])->update(['delete_time'=>time()]);
- // 提交事务
- if(!$res){
- Db::rollback();
- self::setError('删除批次信息错误!');
- }
- $list = GiftCardInfo::where(['gc_id'=>$params['id'],'is_used'=>0])->findOrEmpty();
- if(!$list->isEmpty()){
- $res2 = GiftCardInfo::where(['gc_id'=>$params['id'],'is_used'=>0])->update(['delete_time'=>time()]);
- if(!$res2){
- Db::rollback();
- self::setError('删除礼品卡信息错误!');
- }
- }
- // 提交事务
- Db::commit();
- return true;
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return $e->getMessage();
- }
- }
- /**
- * @notes 删除礼品卡信息
- * @param $params
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author ljj
- * @date 2022/3/31 2:37 下午
- */
- public function deleteGiftCardInfo($params)
- {
- Db::startTrans();
- try {
- $res = GiftCardInfo::destroy($params['id']);
- if(!$res){
- Db::rollback();
- self::setError('删除礼品卡信息错误!');
- }
- // 提交事务
- Db::commit();
- return true;
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- return $e->getMessage();
- }
- }
- }
|