| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116 |
- <?php
- namespace addon\giftcard\model;
- use addon\supermember\model\MemberCard;
- use app\model\BaseModel;
- use app\model\member\Member;
- use app\model\member\MemberAccount;
- use app\model\message\Message;
- use app\model\order\Config;
- use addon\giftcard\model\GiftCard as GiftCardModel;
- use app\model\system\Cron;
- use app\model\system\Pay;
- use think\facade\Cache;
- use app\model\order\StoreOrder;
- use app\model\order\LocalOrder;
- use app\model\order\VirtualOrder;
- use app\model\order\Order;
- use app\model\upload\Upload;
- class GiftCard extends BaseModel
- {
- private $account_content = [
- 'point' => [ 'relate_tag' => "兑换积分", 'remark' => "礼品卡兑换" ],
- 'growth' => [ 'relate_tag' => "兑换成长值", 'remark' => "礼品卡兑换" ],
- 'balance' => [ 'relate_tag' => "兑换储值余额", 'remark' => "礼品卡兑换" ],
- 'balance_meney' => [ 'relate_tag' => "兑换现金余额", 'remark' => "礼品卡兑换" ]
- ];
- private $goods_money = 0;//商品金额
- private $balance_money = 0;//余额
- private $delivery_money = 0;//配送费用
- private $coupon_money = 0;//优惠券金额
- private $adjust_money = 0;//调整金额
- private $invoice_money = 0;//发票费用
- private $promotion_money = 0;//优惠金额
- private $order_money = 0;//订单金额
- private $pay_money = 0;//支付总价
- private $is_virtual = 0; //是否是虚拟类订单
- private $order_name = ''; //订单详情
- private $goods_num = 0; //商品种数
- private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
- private $pay_type = 'ONLINE_PAY';//支付方式
- private $invoice_delivery_money = 0;
- private $error = 0; //是否有错误
- private $error_msg = ''; //错误描述
- /**
- * 兑换卡列表
- * @param array $condition
- * @param int $page
- * @param int $page_size
- * @param string $order
- * @param string $field
- * @return array
- */
- public function getGiftCardPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
- {
- $bargain_list = model("giftcard")->pageList($condition, $field, $order, $page, $page_size, $alias, $join, $group, $limit);
- if ($bargain_list[ 'list' ]) {
- $bargain_info[ 'goods_list' ] = [];
- foreach ($bargain_list[ 'list' ] as $key => $val) {
- $giftcard_list = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $val[ 'id' ] ], [ 'site_id', '=', $val[ 'site_id' ] ] ]);
- $bargain_list[ 'list' ][ $key ][ 'goods_num' ] = count($giftcard_list) ?? 0;
- if ($giftcard_list) {
- $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 1;
- foreach ($giftcard_list as $k => $v) {
- $goods_list = model('goods_sku')->getInfo(
- [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
- 'goods_id,sku_id,sku_name,price,sku_image,stock'
- );
- $goods_list[ 'num' ] = $v[ 'num' ];
- $bargain_list[ 'list' ][ $key ][ 'goods_list' ][] = $goods_list;
- }
- } else {
- $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 0;
- }
- }
- }
- return $this->success($bargain_list);
- }
- public function giftcardSort($id, $sort)
- {
- $res = model('giftcard')->update([ 'sort' => $sort ], [ [ 'id', '=', $id ] ]);
- return $this->success($res);
- }
- /**
- * api兑换卡列表
- * @param array $condition
- * @param int $page
- * @param int $page_size
- * @param string $order
- * @param string $field
- * @return array
- */
- public function getGiftCardApiPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
- {
- if ($order == 2) {
- $order_by = 'selling_price desc';
- } else if ($order == 3) {
- $order_by = 'selling_price asc';
- } else {
- $order_by = 'sort asc,create_time desc';
- }
- $bargain_list = model("giftcard")->pageList($condition, $field, $order_by, $page, $page_size, $alias, $join, $group, $limit);
- if ($bargain_list[ 'list' ]) {
- $time = time();
- $bargain_info[ 'goods_list' ] = [];
- foreach ($bargain_list[ 'list' ] as $key => $val) {
- $giftcard_list = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $val[ 'id' ] ], [ 'site_id', '=', $val[ 'site_id' ] ] ]);
- $bargain_list[ 'list' ][ $key ][ 'goods_num' ] = count($giftcard_list) ?? 0;
- if ($giftcard_list) {
- $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 1;
- foreach ($giftcard_list as $k => $v) {
- $goods_list = model('goods_sku')->getInfo(
- [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
- 'goods_id,sku_id,sku_name,price,sku_image,stock'
- );
- $goods_list[ 'num' ] = $v[ 'num' ];
- $bargain_list[ 'list' ][ $key ][ 'goods_list' ][] = $goods_list;
- }
- } else {
- $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 0;
- }
- if ($val[ 'time_type' ] == 2 && $val[ 'youxiao_time' ] < $time) {
- unset($bargain_list[ 'list' ][ $key ]);
- }
- }
- }
- $money = [];
- foreach ($bargain_list[ 'list' ] as $key => $row) {
- $money[ $key ] = $row[ 'selling_price' ];
- }
- if ($order == 2) {
- array_multisort($money, SORT_DESC, $bargain_list[ 'list' ]);
- } else if ($order == 3) {
- array_multisort($money, SORT_ASC, $bargain_list[ 'list' ]);
- } else {
- array_multisort($bargain_list[ 'list' ]);
- }
- return $this->success($bargain_list);
- }
- public function getGiftCardList($condition = [], $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
- {
- $list = model("giftcard")->getList($condition, $field, $order, $alias, $join, $group, $limit);
- return $this->success($list);
- }
- public function getGiftCardInfo($condition = [], $field = '*', $alias = "a", $join = [])
- {
- $data = model("giftcard")->getInfo($condition, $field, $alias, $join);
- return $this->success($data);
- }
- public function getGiftCardGoodsInfo($condition = [], $field = '*', $alias = "a", $join = [])
- {
- $bargain_info = model("giftcard")->getInfo($condition, $field, 'a', $join);
- $bargain_info[ 'goods_list' ] = [];
- if ($bargain_info) {
- $giftcard_info = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $bargain_info[ 'id' ] ], [ 'site_id', '=', $bargain_info[ 'site_id' ] ] ]);
- $bargain_info[ 'goods_num' ] = count($giftcard_info) ?? 0;
- if ($giftcard_info) {
- $bargain_info[ 'is_electronic' ] = 1;
- foreach ($giftcard_info as $k => $v) {
- $goods_list = model('goods_sku')->getInfo(
- [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
- 'goods_id,sku_id,sku_name,price,sku_image,stock'
- );
- if($goods_list){
- $goods_list[ 'num' ] = $v[ 'num' ];
- $bargain_info[ 'goods_list' ][] = $goods_list;
- }
- }
- } else {
- $bargain_info[ 'is_electronic' ] = 0;
- }
- $bargain_info[ 'goods_list_count' ] = count($bargain_info[ 'goods_list' ]);
- }
- return $this->success($bargain_info);
- }
- public function getGiftCardRecordInfo($condition = [], $field = '*', $alias = 'a', $join = [])
- {
- $bargain_info = model("giftcard_record")->getInfo($condition, $field, $alias, $join);
- if ($bargain_info) {
- $giftcard_info = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $bargain_info[ 'giftcard_id' ] ], [ 'site_id', '=', $bargain_info[ 'site_id' ] ] ]);
- $bargain_info[ 'goods_list' ] = [];
- $bargain_info[ 'goods_num' ] = count($giftcard_info) ?? 0;
- if ($giftcard_info) {
- foreach ($giftcard_info as $k => $v) {
- $goods_list = model('goods_sku')->getInfo(
- [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
- 'goods_id,sku_id,sku_name,price,sku_image,stock'
- );
- if($goods_list) {
- $goods_list['num'] = $v['num'];
- $bargain_info['goods_list'][] = $goods_list;
- }
- }
- }
- }
- return $this->success($bargain_info);
- }
- /**
- * 获取卡密详细列表
- * @param array $condition
- * @param int $page
- * @param int $page_size
- * @param string $order
- * @param string $field
- * @param string $alias
- * @param array $join
- * @param null $group
- * @param null $limit
- * @return array
- */
- public function recordPagelist($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
- {
- $bargain_list = model("giftcard_record")->pageList($condition, $field, $order, $page, $page_size, $alias, $join, $group, $limit);
- if ($bargain_list[ 'list' ]) {
- $bargain_info[ 'goods_list' ] = [];
- foreach ($bargain_list[ 'list' ] as $key => $val) {
- $giftcard_list = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $val[ 'giftcard_id' ] ], [ 'site_id', '=', $val[ 'site_id' ] ] ]);
- $bargain_list[ 'list' ][ $key ][ 'goods_num' ] = count($giftcard_list) ?? 0;
- if ($giftcard_list) {
- $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 1;
- foreach ($giftcard_list as $k => $v) {
- $goods_list = model('goods_sku')->getInfo(
- [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
- 'goods_id,sku_id,sku_name,price,sku_image,stock'
- );
- $goods_list[ 'num' ] = $v[ 'num' ];
- $bargain_list[ 'list' ][ $key ][ 'goods_list' ][] = $goods_list;
- }
- } else {
- $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 0;
- }
- }
- }
- return $this->success($bargain_list);
- }
- /**
- * 批量激活/批量禁用
- * @param $id
- * @param $status
- * @return array
- */
- public function editStatus($id, $status)
- {
- $data = [ 'status' => $status ];
- $condition = [
- [ 'id', '=', $id ],
- ];
- $condition_record = [
- [ 'giftcard_id', '=', $id ],
- ];
- model('giftcard')->startTrans();
- try {
- model('giftcard')->update($data, $condition);
- if ($status == 1) {
- $record_data[ 'activation_time' ] = time();
- } else {
- $record_data[ 'activation_time' ] = 0;
- }
- if ($status == 1) {
- $record_data[ 'entity_status' ] = 1;
- $condition_record[] = [ 'entity_status', '=', 0 ];
- }
- if ($status == 2) {
- $condition_record[] = [ 'entity_status', 'in', [ 0, 1 ] ];
- $record_data[ 'entity_status' ] = 2;
- }
- model('giftcard_record')->update($record_data, $condition_record);
- model('giftcard')->commit();
- return $this->success();
- } catch (\Exception $e) {
- model('giftcard')->rollback();
- return $this->error($e->getMessage() . $e->getLine());
- }
- }
- public function editeCordStatus($id, $status)
- {
- $data = [ 'entity_status' => $status ];
- if ($status) {
- $data[ 'activation_time' ] = time();
- } else {
- $data[ 'activation_time' ] = 0;
- }
- $condition = [ [ 'id', '=', $id ] ];
- model('giftcard_record')->update($data, $condition);
- return $this->success();
- }
- public function updateGiftCard($id, $data)
- {
- $condition = [ [ 'id', '=', $id ] ];
- $gift_info = model('giftcard')->getInfo($condition);
- if (!empty($gift_info[ 'card_cover' ]) && !empty($data[ 'card_cover' ]) && $gift_info[ 'card_cover' ] != $data[ 'card_cover' ]) {
- $upload_model = new Upload();
- $upload_model->deletePic($gift_info[ 'card_cover' ], $gift_info[ 'site_id' ]);
- }
- model('giftcard')->update($data, $condition);
- return $this->success();
- }
- public function updateGoodsGiftCard($id, $data, $site_id)
- {
- model('goods_giftcard')->delete([ [ 'giftcard_id', '=', $id ] ]);
- if ($data) {
- foreach ($data as $k => $v) {
- $data_goods = [
- 'site_id' => $site_id,
- 'sku_id' => $v[ 'sku_id' ] ?? 0,
- 'num' => $v[ 'num' ] ?? 1,
- 'giftcard_id' => $id,
- ];
- $condition = [ [ 'id', '=', $id ] ];
- model('goods_giftcard')->add($data_goods, $condition);
- }
- }
- return $this->success();
- }
- /**
- * 添加电子卡
- * @param $data
- * @return array
- */
- public function addElectGiftCard($data)
- {
- $data[ 'code' ] = $this->createCode(8, 'G');
- $gift_id = model('giftcard')->add($data);
- return $this->success($gift_id);
- }
- /**
- * 添加实体卡
- * @param
- * @return array
- */
- public function addGiftCard($data)
- {
- $card_count = $data[ 'card_count' ];
- $carmichael_length = $data[ 'carmichael_length' ] ?? 6;
- $carmichael_prefix = $data[ 'carmichael_prefix' ] ?? '';
- $carmichael_suffix = $data[ 'carmichael_suffix' ] ?? '';
- $carmichael_type = $data[ 'carmichael_type' ] ?? [];
- $time = time();
- $data[ 'status' ] = 0;
- $data[ 'create_time' ] = $time;
- $data[ 'carmichael_type' ] = implode(',', $carmichael_type);
- $arecord_list = [];
- $data[ 'code' ] = $this->createCode(8, 'G');
- model("giftcard")->startTrans();
- try {
- $giftcard_id = model('giftcard')->add($data);
- if ($data[ 'time_type' ] == 2) {
- $cron = new Cron();
- //增加定时更改活动状态自动事件
- if (!empty($data[ 'youxiao_time' ])) {
- $cron->addCron(1, 0, "实体卡作废", "GiftCardChangeStatus", $data[ 'youxiao_time' ], $giftcard_id);
- }
- }
- $carmichael_lists = model('giftcard_record')->getColumn([], 'carmichael_text');
- for ($i = 0; $i < $card_count; $i++) {
- while (true) {
- $carmichael_text = $this->createCarmichael($carmichael_length, $carmichael_prefix, $carmichael_suffix, $carmichael_type);
- if (!in_array($carmichael_text, $carmichael_lists)) {
- break;
- }
- }
- $arecord_list[ $i ][ 'entity_bianhao' ] = $this->createEntityBianhao(9, 'F', $i + 1);
- $arecord_list[ $i ][ 'giftcard_id' ] = $giftcard_id;
- $arecord_list[ $i ][ 'status' ] = $data[ 'status' ];
- $arecord_list[ $i ][ 'create_time' ] = $time;
- $arecord_list[ $i ][ 'carmichael_text' ] = $carmichael_text;
- $arecord_list[ $i ][ 'site_id' ] = $data[ 'site_id' ];
- $arecord_list[ $i ][ 'goods_ids' ] = $data[ 'goods_ids' ];
- }
- model('giftcard_record')->addList($arecord_list);
- model("giftcard")->commit();
- return $this->success($giftcard_id);
- } catch (\Exception $e) {
- model("giftcard")->rollback();
- return $this->error($e->getMessage() . $e->getLine());
- }
- }
- /**
- * 兑换码兑换奖励
- * @param $carmichael_text
- * @param $member_id
- * @param $site_id
- * @return array
- */
- public function exchange($carmichael_text, $member_id, $site_id, $data)
- {
- $message = "领取成功";
- $condition = [
- [ 'carmichael_text', '=', $carmichael_text ]
- ];
- $alias = "er";
- $field = "er.*,e.point,e.growth,e.balance,e.balance_money,e.coupon";
- $join = [
- [ 'giftcard e', 'er.giftcard_id = e.id', 'inner' ]
- ];
- $carmichael_record_info = model('giftcard_record')->getInfo($condition, $field, $alias, $join);
- if ($carmichael_record_info[ 'entity_bianhao' ] == "" && $member_id != $carmichael_record_info[ 'use_member_id' ]) {
- return $this->error('', '未查到该礼品卡');
- }
- if ($carmichael_record_info[ 'entity_bianhao' ]) {
- switch ( $carmichael_record_info[ 'entity_status' ] ) {
- case 0:
- return $this->error('', '该礼品卡未激活');
- break;
- case 2:
- return $this->error('', '该礼品卡已作废');
- break;
- case 3:
- return $this->error('', '该礼品卡已使用');
- break;
- case 4:
- return $this->error('', '该礼品卡已过期');
- break;
- }
- } else {
- switch ( $carmichael_record_info[ 'elect_status' ] ) {
- case 1:
- return $this->error('', '该礼品卡已使用');
- break;
- case 2:
- return $this->error('', '该礼品卡已过期');
- break;
- case 3:
- return $this->error('', '该卡密已使用');
- break;
- }
- }
- //领取优惠卷
- // if (!empty($carmichael_record_info['coupon'])) {
- // $coupon_type_id = $carmichael_record_info['coupon'];
- // $coupon_model = new CouponModel();
- // $res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $member_id, 2);
- // if($res['code']<0){
- // $message = '优惠卷已领完';
- // }
- // }
- //领取账户有关奖励 积分/成长值/储值余额/现金余额
- $member_account = [
- 'point' => $carmichael_record_info[ 'point' ],
- 'balance' => $carmichael_record_info[ 'balance' ],
- 'growth' => $carmichael_record_info[ 'growth' ],
- 'balance_money' => $carmichael_record_info[ 'balance_money' ]
- ];
- $member_account_model = new MemberAccount();
- foreach ($member_account as $k => $v) {
- if ($v > 0) {
- $relate_tag = $this->account_content[ $k ][ 'relate_tag' ] . $v;
- $remark = $this->account_content[ $k ][ 'remark' ] . $v;
- $member_account_model->addMemberAccount($site_id, $member_id, $k, $v, 'giftcard', $relate_tag, $remark);
- }
- }
- if ($carmichael_record_info[ 'entity_bianhao' ]) {
- model('giftcard_record')->update([ 'entity_status' => 3, 'use_member_id' => $member_id, 'use_time' => time() ], [ [ 'carmichael_text', '=', $carmichael_text ] ]);
- } else {
- model('giftcard_record')->update([ 'elect_status' => 1, 'use_member_id' => $member_id, 'use_time' => time() ], [ [ 'carmichael_text', '=', $carmichael_text ] ]);
- }
- model('giftcard_record')->startTrans();
- try {
- model('giftcard_record')->commit();
- $giftcar_create = new GiftcardOrderCreate();
- if (strlen($data[ 'sku_id' ]) > 1) {
- $giftcar_create->create($data);
- }
- // 组合返回信息的信息文字
- $msg = '';
- if (!empty($carmichael_record_info[ 'balance' ]) && $carmichael_record_info[ 'balance' ] != '0.00') $msg .= '余额' . $carmichael_record_info[ 'balance' ];
- if (!empty($carmichael_record_info[ 'point' ])) $msg .= ( empty($msg) ? '' : '+' ) . '积分' . $carmichael_record_info[ 'point' ];
- $message = '获得' . $msg;
- return $this->success($message);
- } catch (\Exception $e) {
- model('giftcard_record')->rollback();
- return $this->error($e->getMessage());
- }
- }
- /**
- * 兑换码兑换奖励
- * @param $carmichael_text
- * @param $member_id
- * @param $site_id
- * @return array
- */
- public function useGiftCard($record_id, $member_id, $site_id, $data)
- {
- $message = "使用成功";
- $condition = [
- [ 'er.id', '=', $record_id ],
- [ 'er.site_id', '=', $site_id ],
- [ 'er.use_member_id', '=', $member_id ]
- ];
- $alias = "er";
- $field = "er.*,e.point,e.growth,e.balance,e.balance_money,e.coupon";
- $join = [
- [ 'giftcard e', 'er.giftcard_id = e.id', 'inner' ]
- ];
- $carmichael_record_info = model('giftcard_record')->getInfo($condition, $field, $alias, $join);
- if ($carmichael_record_info[ 'entity_bianhao' ] == "" && $member_id != $carmichael_record_info[ 'use_member_id' ]) {
- return $this->error('', '未查到该礼品卡');
- }
- if ($carmichael_record_info[ 'entity_bianhao' ]) {
- switch ( $carmichael_record_info[ 'entity_status' ] ) {
- case 0:
- return $this->error('', '该卡密未激活');
- break;
- case 2:
- return $this->error('', '该卡密已作废');
- break;
- case 3:
- return $this->error('', '该卡密已使用');
- break;
- case 4:
- return $this->error('', '该卡密已过期');
- break;
- }
- } else {
- switch ( $carmichael_record_info[ 'elect_status' ] ) {
- case 1:
- return $this->error('', '该卡密已使用');
- break;
- case 2:
- return $this->error('', '该卡密已过期');
- break;
- case 3:
- return $this->error('', '该卡密已使用');
- break;
- }
- }
- //领取优惠卷
- // if (!empty($carmichael_record_info['coupon'])) {
- // $coupon_type_id = $carmichael_record_info['coupon'];
- // $coupon_model = new CouponModel();
- // $res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $member_id, 2);
- // if($res['code']<0){
- // $message = '优惠卷已领完';
- // }
- // }
- //领取账户有关奖励 积分/成长值/储值余额/现金余额
- $member_account = [
- 'point' => $carmichael_record_info[ 'point' ],
- 'balance' => $carmichael_record_info[ 'balance' ],
- 'growth' => $carmichael_record_info[ 'growth' ],
- 'balance_money' => $carmichael_record_info[ 'balance_money' ]
- ];
- $member_account_model = new MemberAccount();
- foreach ($member_account as $k => $v) {
- if ($v > 0) {
- $relate_tag = $this->account_content[ $k ][ 'relate_tag' ] . $v;
- $remark = $this->account_content[ $k ][ 'remark' ] . $v;
- $member_account_model->addMemberAccount($site_id, $member_id, $k, $v, 'giftcard', $relate_tag, $remark);
- }
- }
- if ($carmichael_record_info[ 'entity_bianhao' ]) {
- model('giftcard_record')->update([ 'entity_status' => 3, 'use_member_id' => $member_id, 'use_time' => time() ], [ [ 'id', '=', $record_id ], [ 'site_id', '=', $site_id ], ]);
- } else {
- model('giftcard_record')->update([ 'elect_status' => 1, 'use_member_id' => $member_id, 'use_time' => time() ], [ [ 'id', '=', $record_id ], [ 'site_id', '=', $site_id ], ]);
- }
- model('giftcard_record')->startTrans();
- try {
- if ($data[ 'sku_id' ]) {
- $giftcar_create = new GiftcardOrderCreate();
- $giftcar_create->create($data);
- }
- model('giftcard_record')->commit();
- return $this->success($message);
- } catch (\Exception $e) {
- model('giftcard_record')->rollback();
- return $this->error($e->getMessage());
- }
- }
- /**
- * 生成卡密
- * @param $length
- * @param string $prefix
- * @param string $suffix
- * @param array $type
- * @return string
- */
- private function createCarmichael($length, $prefix = '', $suffix = '', $type = [ '0-9' ])
- {
- $chars = [];
- $number = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
- $small = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ];
- $large = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
- if (in_array('0-9', $type)) {
- $chars = array_merge($chars, $number);
- }
- if (in_array('a-z', $type)) {
- $chars = array_merge($chars, $small);
- }
- if (in_array('A-Z', $type)) {
- $chars = array_merge($chars, $large);
- }
- $charsLen = count($chars) - 1;
- shuffle($chars);
- $output = $prefix;
- $length = $length - strlen($prefix) + strlen($suffix);
- for ($i = 0; $i < $length; $i++) {
- $output .= $chars[ mt_rand(0, $charsLen) ];
- }
- $output .= $suffix;
- return $output;
- }
- /**
- * 创建卡编号
- * @param int $length
- * @param string $prefix
- * @return string
- */
- private function createCode($length = 8, $prefix = '')
- {
- $last_data = model('giftcard')->getList([], 'id', 'id DESC', '', '', '', 1);
- if (!empty($last_data)) {
- $number = $last_data[ 0 ][ 'id' ] + 1;
- } else {
- $number = 1;
- }
- $length = $length - (int) strlen($prefix);
- $number = sprintf("%0" . $length . "d", $number);
- return $prefix . $number;
- }
- /**
- * 创建实体卡卡密编号
- * @param int $length
- * @param string $prefix
- * @return string
- */
- private function createEntityBianhao($length = 8, $prefix = '', $i = 1)
- {
- $last_data = model('giftcard_record')->getList([], 'id', 'id DESC', '', '', '', 1);
- if (!empty($last_data)) {
- $number = $last_data[ 0 ][ 'id' ] + $i;
- } else {
- $number = $i++;
- }
- $length = $length - (int) strlen($prefix);
- $number = sprintf("%0" . $length . "d", $number);
- return $prefix . $number;
- }
- /**
- * 订单创建
- * @param $data
- * @return unknown|array|mixed
- */
- public function create($data)
- {
- //查询出会员相关信息
- $calculate_data = $this->calculate($data);
- if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
- return $calculate_data;
- if ($this->error > 0) {
- return $this->error([ 'error_code' => $this->error ], $this->error_msg);
- }
- if (!empty($calculate_data[ 'invoice_type' ])) {
- if ($calculate_data[ 'invoice_type' ] == 1 && $calculate_data[ 'invoice_full_address' ] == "") {
- //物流,同城
- if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "express" || $calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "local") {
- $calculate_data[ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
- $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
- }
- //门店
- if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "store") {
- $delivery_store_info = json_decode($calculate_data[ 'shop_goods_list' ][ 'delivery_store_info' ], true);
- $calculate_data[ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
- $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
- }
- }
- }
- $pay = new Pay();
- $out_trade_no = $pay->createOutTradeNo($data[ 'member_id' ]);
- model("giftcard_order")->startTrans();
- model("giftcard_record")->startTrans();
- //循环生成多个订单
- try {
- //订单主表
- $order_no = $this->createOrderNo($data[ 'site_id' ], $data[ 'member_id' ]);
- $data_order = [
- 'order_number' => $order_no,
- 'site_id' => $data[ 'site_id' ],
- 'out_trade_no' => $out_trade_no,
- 'member_id' => $data[ 'member_id' ],
- 'buyer_ip' => request()->ip(),
- 'card_price' => $data[ 'card_price' ],
- 'card_id' => $data[ 'card_id' ],
- 'num' => $data[ 'num' ],
- 'order_total_price' => $data[ 'order_total_price' ],
- 'create_time' => time(),
- 'order_from' => $data[ 'order_from' ],
- 'order_from_name' => $data[ 'order_from_name' ],
- 'is_invoice' => $data[ 'is_invoice' ],
- 'invoice_type' => $data[ 'invoice_type' ],
- 'invoice_title' => $data[ 'invoice_title' ],
- 'taxpayer_number' => $data[ 'taxpayer_number' ],
- 'invoice_content' => $data[ 'invoice_content' ],
- 'invoice_full_address' => $data[ 'invoice_full_address' ],
- 'is_tax_invoice' => $data[ 'is_tax_invoice' ],
- 'invoice_email' => $data[ 'invoice_email' ],
- 'invoice_title_type' => $data[ 'invoice_title_type' ],
- ];
- $order_id = model("giftcard_order")->add($data_order);
- //验证库存
- $card_stock_model = new CardStock();
- $check_stock = $card_stock_model->checkStock($data);
- if ($check_stock[ 'code' ] < 0) {
- model("giftcard_order")->rollback();
- return $check_stock;
- }
- $config_model = new Config();
- $balance_config = $config_model->getBalanceConfig($data[ 'site_id' ]);
- //扣除余额(统一扣除)
- if ($calculate_data[ "balance_money" ] > 0 && $balance_config[ 'data' ][ 'value' ][ 'balance_show' ] == 1) {
- $this->pay_type = "BALANCE";
- $calculate_data[ 'order_id' ] = $order_id;
- $balance_result = $this->useBalance($calculate_data, $data[ 'site_id' ]);
- if ($balance_result[ "code" ] < 0) {
- model("giftcard_order")->rollback();
- return $balance_result;
- }
- } else {
- $this->pay_money = $data[ 'order_total_price' ];
- }
- //生成支付单据
- $pay_body = '购买礼品卡';
- $pay->addPay($data[ 'site_id' ], $out_trade_no, $this->pay_type, $pay_body, $pay_body, $this->pay_money, '', 'GiftCardOrderPayNotify', '');
- $this->addOrderCronClose($order_id, $data[ 'site_id' ]);//增加关闭订单自动事件
- //订单生成的消息
- $message_model = new Message();
- $message_model->sendMessage([ 'keywords' => "ORDER_CREATE", 'order_id' => $order_id, 'site_id' => $data[ 'site_id' ] ]);
- model("giftcard_record")->commit();
- model("giftcard_order")->commit();
- return $this->success($out_trade_no);
- } catch (\Exception $e) {
- model("giftcard_order")->rollback();
- model("giftcard_record")->rollback();
- return $this->error('', $e->getMessage() . $e->getFile() . $e->getLine());
- }
- }
- /**
- * 增加订单自动关闭事件
- * @param $order_id
- */
- public function addOrderCronClose($order_id, $site_id)
- {
- //计算订单自动关闭时间
- $config_model = new Config();
- $order_config_result = $config_model->getOrderEventTimeConfig($site_id);
- $order_config = $order_config_result[ "data" ];
- $now_time = time();
- if ($order_config[ "value" ][ "auto_close" ] > 0) {
- $execute_time = $now_time + $order_config[ "value" ][ "auto_close" ] * 60; //自动关闭时间
- $cron_model = new Cron();
- $cron_model->addCron(1, 0, "订单自动关闭", "GiftCardOrderClose", $execute_time, $order_id);
- }
- }
- /**
- * 生成订单编号
- *
- * @param array $site_id
- */
- public function createOrderNo($site_id, $member_id = 0)
- {
- $time_str = date('YmdHi');
- $max_no = Cache::get($site_id . "_" . $member_id . "_" . $time_str);
- if (!isset($max_no) || empty($max_no)) {
- $max_no = 1;
- } else {
- $max_no = $max_no + 1;
- }
- $order_no = $time_str . $member_id . sprintf("%03d", $max_no);
- Cache::set($site_id . "_" . $member_id . "_" . $time_str, $max_no);
- return $order_no;
- }
- /**
- * 获取购卡订单列表
- * @param array $condition
- * @param int $page
- * @param int $page_size
- * @param string $order
- * @param string $field
- * @param string $alias
- * @param array $join
- * @param null $group
- * @param null $limit
- * @return array
- */
- public function orderPagelist($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
- {
- $list = model("giftcard_order")->pageList($condition, $field, $order, $page, $page_size, $alias, $join, $group, $limit);
- return $this->success($list);
- }
- /**
- * 订单计算
- * @param unknown $data
- */
- public function calculate($data)
- {
- $giftcard_model = new GiftCardModel();
- $data = $this->initMemberAccount($data);//初始化会员账户
- $giftcard_data = $giftcard_model->getGiftCardInfo([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'id', '=', $data[ 'card_id' ] ], [ 'elect_is_use', '=', 1 ] ])[ 'data' ] ?? [];
- if (empty($giftcard_data)) {
- return $this->error([], "礼品卡已下架");
- }
- $data[ "giftcard_info" ] = $giftcard_data;
- //余额付款
- if ($data[ 'is_balance' ] > 0 && $giftcard_data[ 'is_balance' ] == 1) {
- $this->member_balance_money = $data[ "member_account" ][ "balance_total" ] ?? 0;
- $balance_money = $data[ 'member_account' ][ 'balance' ] - $data[ 'order_total_price' ];
- if ($balance_money > 0) {
- $this->balance_money = $data[ 'order_total_price' ];
- } else {
- $this->balance_money = $data[ 'member_account' ][ 'balance' ];
- }
- }
- // 限购
- if ($giftcard_data[ 'max_buy' ] > 0) {
- if (( $data[ 'num' ] ) > $giftcard_data[ 'max_buy' ]) {
- $this->error = 1;
- $this->error_msg = "该商品每人限购{$giftcard_data['max_buy']}张";
- }
- }
- //总结计算
- $data[ 'delivery_money' ] = $this->delivery_money;
- $data[ 'coupon_money' ] = $this->coupon_money;
- $data[ 'adjust_money' ] = $this->adjust_money;
- $data[ 'invoice_money' ] = $this->invoice_money;
- $data[ 'invoice_delivery_money' ] = $this->invoice_delivery_money;
- $data[ 'promotion_money' ] = $this->promotion_money;
- $data[ 'order_money' ] = $this->order_money;
- $data[ 'balance_money' ] = $this->balance_money;
- $data[ 'pay_money' ] = $this->pay_money;
- $data[ 'goods_money' ] = $this->goods_money;
- $data[ 'goods_num' ] = $this->goods_num;
- return $data;
- }
- /**
- * 初始化会员账户
- * @param $data
- * @return mixed
- */
- public function initMemberAccount($data)
- {
- $member_model = new Member();
- $member_info_result = $member_model->getMemberDetail($data[ "member_id" ], $data[ "site_id" ]);
- $member_info = $member_info_result[ "data" ];
- if (!empty($member_info)) {
- if (!empty($member_info[ "pay_password" ])) {
- $is_pay_password = 1;
- } else {
- $is_pay_password = 0;
- }
- unset($member_info[ "pay_password" ]);
- $member_info[ "is_pay_password" ] = $is_pay_password;
- $data[ 'member_account' ] = $member_info;
- // 查询推荐会员卡
- if ($member_info[ 'member_level_type' ] == 0 && addon_is_exit('supermember', $data[ 'site_id' ])) {
- $member_card_model = new MemberCard();
- $recommend_member_card = $member_card_model->getRecommendMemberCard($data[ 'site_id' ]);
- if (!empty($recommend_member_card[ 'data' ])) {
- $recommend_member_card[ 'data' ][ 'discount_money' ] = 0;
- $recommend_member_card[ 'data' ][ 'charge_rule' ] = json_decode($recommend_member_card[ 'data' ][ 'charge_rule' ], true);
- $this->recommend_member_card = $recommend_member_card[ 'data' ];
- }
- }
- }
- return $data;
- }
- /**
- * 使用余额
- * @param $data
- * @param $site_id
- * @param string $from_type
- * @return array
- */
- public function useBalance($data, $site_id, $from_type = 'order')
- {
- $this->pay_type = "BALANCE";
- $member_model = new Member();
- // $result = $member_model->checkPayPassword($data["member_id"], $data["pay_password"]);
- // if ($result["code"] >= 0) {
- $balance_money = $data[ "member_account" ][ "balance_money" ]; //现金余额
- $balance = $data[ "member_account" ][ "balance" ]; //储值余额
- $member_account_model = new MemberAccount();
- $surplus_banance = $data[ "balance_money" ];
- //优先扣除储值余额
- if ($balance > 0) {
- if ($balance >= $surplus_banance) {
- $real_balance = $surplus_banance;
- } else {
- $real_balance = $balance;
- }
- $result = $member_account_model->addMemberAccount($site_id, $data[ "member_id" ], "balance", -$real_balance, $from_type, $data[ 'order_id' ], "订单消费扣除");
- $surplus_banance -= $real_balance;
- }
- // if($balance_money > 0){
- // if($balance_money > $surplus_banance){
- // $real_balance_money = $surplus_banance;
- // }else{
- // $real_balance_money = $balance_money;
- // }
- // $result = $member_account_model->addMemberAccount($data["member_id"], "balance", -$real_balance, "order", "余额抵扣","订单余额抵扣,扣除储值余额:".$real_balance);
- // }
- if ($surplus_banance > 0) {
- $result = $member_account_model->addMemberAccount($site_id, $data[ "member_id" ], "balance_money", -$surplus_banance, $from_type, $data[ 'order_id' ], "订单消费扣除");
- }
- $this->pay_money = $data[ "balance_money" ] - ( $real_balance + $surplus_banance ); //计算出实际支付金额
- return $result;
- // } else {
- // return $result;
- // }
- }
- /**
- * 关闭礼品卡
- * @param $order_id
- * @return array|\multitype
- */
- public function cronCloseGiftCard($record_id)
- {
- $giftcard_info = model('giftcard_record')->getInfo([ [ 'id', '=', $record_id ] ], 'elect_status');
- if (!empty($giftcard_info)) {
- if ($giftcard_info[ 'elect_status' ] == 0) {
- $res = model('giftcard_record')->update([ 'elect_status' => 2 ], [ [ 'id', '=', $record_id ] ]);
- return $this->success($res);
- } else {
- return $this->error("", "该礼品卡已使用或关闭");
- }
- } else {
- return $this->error("", "该礼品卡不存在");
- }
- }
- /**
- * 订单线上支付
- * @param unknown $out_trade_no
- */
- public function orderOnlinePay($data)
- {
- model('order')->startTrans();
- try {
- $out_trade_no = $data[ "out_trade_no" ];
- $order_list = model("order")->getList([ [ 'out_trade_no', '=', $out_trade_no ] ], '*');
- $message_list = [];
- //订单支付消息
- foreach ($order_list as $k => $order) {
- if ($order[ 'order_status' ] == -1) {
- continue;
- }
- switch ( $order[ 'order_type' ] ) {
- case 1:
- $order_model = new Order();
- break;
- case 2:
- $order_model = new StoreOrder();
- break;
- case 3:
- $order_model = new LocalOrder();
- break;
- case 4:
- $order_model = new VirtualOrder();
- break;
- }
- if (isset($data[ 'log_data' ])) {
- $order_model->orderPay($order, $data[ "pay_type" ], $data[ "log_data" ]);
- } else {
- $order_model->orderPay($order, $data[ "pay_type" ]);
- }
- $is_enable_refund = array (
- "is_enable_refund" => 0,
- );
- //赠送商品禁止退货
- model("order")->update($is_enable_refund, [ [ "order_id", "=", $order[ "order_id" ] ] ]);
- //同时将用户表的order_money和order_num更新
- model('member')->setInc([ [ 'member_id', '=', $order[ 'member_id' ] ] ], 'order_money', $order[ 'order_money' ]);
- model('member')->setInc([ [ 'member_id', '=', $order[ 'member_id' ] ] ], 'order_num');
- //支付后商品增加销量
- $order_goods_list = model("order_goods")->getList([ [ "order_id", "=", $order[ "order_id" ] ] ], "sku_id,num");
- $message_list[] = $order;
- $order = model("order")->getInfo([ [ 'order_id', '=', $order[ "order_id" ] ] ], '*');
- $message_model = new Message();
- // 发送消息
- $param = [ "keywords" => "ORDER_PAY" ];
- $param = array_merge($param, $order);
- $message_model->sendMessage($param);
- //商家消息
- $param = [ "keywords" => "BUYER_PAY" ];
- $param = array_merge($param, $order);
- $message_model->sendMessage($param);
- }
- model('order')->commit();
- return $this->success();
- } catch (\Exception $e) {
- model('order')->rollback();
- return $this->error('', $e->getMessage());
- }
- }
- /**
- * @param $id
- * @return int
- * 实体卡作废
- */
- public function cronGiftCardChangeStatus($id)
- {
- $res = model('giftcard')->update([ 'status' => 2 ], [ [ 'id', '=', $id ] ]);
- model('giftcard_record')->update([ 'elect_status' => 2, 'entity_status' => 2 ], [ [ 'giftcard_id', '=', $id ] ]);
- return $res;
- }
- /**
- * 限制错误次数
- */
- public function cardErrorNum($carmichael_text, $entity_bianhao, $member_id)
- {
- $session_card_num = Cache::get("giftcard_num_" . $member_id . "_" . $entity_bianhao);
- $card_info = model("giftcard_record")->getInfo([ [ 'entity_bianhao', '=', $entity_bianhao ] ], "*", "", "");
- $card_time_d = 0;//最后一次错误时间
- $time_d = 0;//当前错误时间
- if (!empty($session_card_num)) {
- $card_time_d = date("d", $session_card_num[ 'time' ]);//最后一次错误时间
- $time_d = date('d', time());//当前错误时间
- if ($time_d <= $card_time_d && $session_card_num[ 'num' ] == 5 && $card_info) {
- return $this->error("", "您多次输入卡号卡密错误,该卡今日不再支持兑换");
- }
- }
- $info = $this->getGiftCardRecordInfo([ [ 'carmichael_text', '=', $carmichael_text ], [ 'entity_bianhao', '=', $entity_bianhao ] ]);
- if ($info[ 'code' ] >= 0 && $info[ 'data' ]) {
- model('giftcard_record')->update([ 'use_member_id' => $member_id ], [ [ 'id', '=', $info[ 'data' ][ 'id' ] ], [ 'site_id', '=', $info[ 'data' ][ 'site_id' ] ], [ 'entity_status', '=', 1 ] ]);
- return $info;
- }
- if ($card_info) {
- if (empty($session_card_num)) {
- Cache::set("giftcard_num_" . $member_id . "_" . $entity_bianhao, [ 'time' => time(), 'num' => 1 ], 86400);
- return $this->error("", "当前兑换码卡密错误,还可尝试4次");
- } else {
- if ($time_d > $card_time_d) {
- Cache::set("giftcard_num_" . $member_id . "_" . $entity_bianhao, [ 'time' => time(), 'num' => 1 ], 86400);
- return $this->error("", "当前兑换码卡密错误,还可尝试4次");
- } else if ($session_card_num[ 'num' ] < 5) {
- Cache::set("giftcard_num_" . $member_id . "_" . $entity_bianhao, [ 'time' => time(), 'num' => $session_card_num[ 'num' ] + 1 ], 86400);
- $num = 5 - $session_card_num[ 'num' ] - 1;
- return $this->error("", "当前兑换码卡密错误,还可尝试" . $num . "次");
- }
- }
- }
- return $info;
- }
- }
|