123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- /**
- * ItgOrder.php UTF-8
- *
- *
- * @date : 2018/5/7 22:42
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\controller\shop;
- use huo\controller\agent\AgentWallet;
- use huo\controller\common\Base;
- use huo\controller\finance\AoRequest;
- use huo\controller\gift\Gift;
- use huo\controller\integral\MemIntegral;
- use huo\controller\member\MemCache;
- use huo\logic\posts\ShareActLogic;
- use huo\logic\shop\ItgOrderLogic;
- use huo\model\agent\AgentOrderModel;
- use huo\model\shop\ItgOrderModel;
- use huo\model\user\UserModel;
- use huolib\constant\MemItgConst;
- use huolib\constant\SettleConst;
- use huolib\constant\WalletConst;
- use huolib\status\CommonStatus;
- use huolib\status\GiftStatus;
- use huolib\status\IntegralStatus;
- use huolib\status\MemberStatus;
- use huolib\status\SettleStatus;
- use huolib\status\ShopStatus;
- use huolib\tool\StrUtils;
- class ItgOrder extends Base {
- /**
- * 兑换商品
- *
- * @param $mem_id
- * @param $goods_id
- *
- * @param array $address
- *
- * @param $act_id
- * @param int $cost_integral
- *
- * @return array
- */
- public function exchange($mem_id, $goods_id, $address = [], $act_id = 0, $cost_integral = 0) {
- if (empty($mem_id)) {
- $_code = MemberStatus::LOGIN_IS_OUT;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- if (empty($goods_id)) {
- $_code = ShopStatus::GOODS_ID_EMPTY;
- return $this->huoError($_code, ShopStatus::getMsg($_code));
- }
- $_mc_class = MemCache::ins();
- $_me_data = $_mc_class->getMeInfoById($mem_id);
- if (empty($_me_data)) {
- $_code = IntegralStatus::ITG_NOT_ENOUGH;
- return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
- }
- $_goods_cache = GoodsCache::ins();
- $_goods_data = $_goods_cache->getInfoByGoodsId($goods_id);
- if (empty($_goods_data)) {
- $_code = ShopStatus::GOODS_NOT_EXISTS;
- return $this->huoSuccess($_code, ShopStatus::getMsg($_code));
- }
- /* 判断余量 */
- if ($_goods_data['remain_cnt'] <= 0) {
- $_code = ShopStatus::GOODS_NOT_ENOUGH;
- return $this->huoSuccess($_code, ShopStatus::getMsg($_code));
- }
- $_my_integral = $_me_data['my_integral'];
- $_cost_integral = $cost_integral;
- if (empty($_cost_integral)) {
- $_cost_integral = $_goods_data['integral'];
- }
- if ($_my_integral < $_cost_integral) {
- $_code = IntegralStatus::ITG_NOT_ENOUGH;
- return $this->huoError($_code, IntegralStatus::getMsg($_code));
- }
- $_goods_data['remain_cnt']--;
- $_goods_cache->updateGoods($goods_id, $_goods_data);
- $shipping_status = MemItgConst::SHIP_STATUS_NOT;
- if (MemItgConst::SHOP_FLAG_RP == $_goods_data['flag']) {
- $shipping_status = MemItgConst::SHIP_STATUS_SUC;
- }
- $_order_id = $this->createOrder(
- $mem_id, $_goods_data, $act_id, $_cost_integral, $shipping_status, $address, $_goods_data['flag']
- );
- if (!$_order_id) {
- return $this->huoError(
- ShopStatus::GOODS_EXCHANGE_ERROR, ShopStatus::getMsg(ShopStatus::GOODS_EXCHANGE_ERROR)
- );
- }
- $_mitg_class = new MemIntegral();
- $_ia_name = '兑换'.$_goods_data['goods_name'];
- $_rs = IntegralStatus::NO_ERROR;
- if ($_cost_integral > 0) {
- /* 减少此次积分 */
- $_rs = $_mitg_class->setMemItg(
- $mem_id, $_cost_integral, MemItgConst::MEM_ITG_DEDUCT, 0, 0, "", 0, $_ia_name
- );
- if (IntegralStatus::NO_ERROR == $_rs && MemItgConst::SHOP_FLAG_RP == $_goods_data['flag']) { //兑换红包
- /* 获得红包 */
- $_agent_id = (new UserModel())->getIdByMemId($mem_id);
- $_code = (new AgentWallet())->updateWallet(
- $_agent_id, $_goods_data['market_price'], SettleConst::SETTLE_WALLET_ADD
- );
- if (SettleStatus::NO_ERROR != $_code) {
- return $this->huoError($_code, SettleStatus::NO_ERROR);
- }
- /* 添加到agentOrder 渠道余额记录*/
- $_agent_id = (new UserModel())->getIdByMemId($mem_id);
- $_ao_data = [
- 'order_id' => StrUtils::genOrderId(
- $_agent_id, $_agent_id, $mem_id, WalletConst::WALLET_ORDER_PREFIX_EG
- ),
- 'agent_id' => $_agent_id,
- 'agent_gain' => $_goods_data['market_price'],
- 'flag' => AoRequest::FLAG_EXCHANGE,
- 'status' => 2,//成功
- 'remark' => $_ia_name
- ];
- (new AgentOrderModel())->createOrder($_ao_data);
- }
- }
- if (IntegralStatus::NO_ERROR == $_rs && $_goods_data['gain_integral'] > 0) {
- /* 获得的积分 */
- $_rs = $_mitg_class->setMemItg(
- $mem_id, $_goods_data['gain_integral'], MemItgConst::MEM_ITG_ADD, 0, 0, "", 0,
- MemItgConst::ITG_DEDUCT_LOTTERY
- );
- }
- if (IntegralStatus::NO_ERROR != $_rs) {
- return $this->huoSuccess($_rs, IntegralStatus::getMsg($_rs));
- }
- $_code = ShopStatus::NO_ERROR;
- $_rdata['order_id'] = $_order_id;
- return $this->huoSuccess($_code, ShopStatus::getMsg($_code), $_rdata);
- }
- /**
- * 抽奖兑换商品
- *
- * @param $mem_id
- * @param $goods_id
- *
- * @param array $address
- *
- * @param $act_id
- * @param int $cost_integral
- *
- * @return array
- */
- public function drawExchange($mem_id, $goods_id, $address = [], $act_id = 0, $cost_integral = 0) {
- if (empty($mem_id)) {
- $_code = MemberStatus::LOGIN_IS_OUT;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- if (empty($goods_id)) {
- $_code = ShopStatus::GOODS_ID_EMPTY;
- return $this->huoError($_code, ShopStatus::getMsg($_code));
- }
- $_mc_class = MemCache::ins();
- $_me_data_ext = $_mc_class->getMeInfoById($mem_id);
- if (empty($_me_data_ext)) {
- $_code = MemberStatus::UID_INVALID;
- return $this->huoSuccess($_code, MemberStatus::getMsg($_code));
- }
- // $_me_data = $_mc_class->getInfoById($mem_id);
- // if (empty($_me_data['mobile'])) {
- // $_code = ShopStatus::PHONE_NOT_BIND;
- //
- // return $this->huoSuccess($_code, ShopStatus::getMsg($_code));
- // }
- $_goods_cache = GoodsCache::ins();
- $_goods_data = $_goods_cache->getInfoByGoodsId($goods_id);
- if (empty($_goods_data) || $_goods_data['flag'] != MemItgConst::SHOP_FLAG_LOTTERY) {
- $_code = ShopStatus::GOODS_NOT_EXISTS;
- return $this->huoSuccess($_code, ShopStatus::getMsg($_code));
- }
- $_my_integral = $_me_data_ext['my_integral'];
- $_cost_integral = $cost_integral;
- //判断是否有免费次数
- $_share_act_logic = new ShareActLogic();
- $_share_act = $_share_act_logic->getMemActModel($mem_id, $act_id);
- if ($_share_act['free_cnt'] - 1 >= 0) {
- $_cost_integral = 0;
- $_mem_act_data['free_cnt'] = $_share_act['free_cnt'] - 1;
- $_share_act_logic->updateMemAct($_share_act['id'], $_mem_act_data);
- } else if ($_share_act['today_cnt'] - 1 >= 0) {
- $_cost_integral = 0;
- $_mem_act_data['today_cnt'] = $_share_act['today_cnt'] - 1;
- $_share_act_logic->updateMemAct($_share_act['id'], $_mem_act_data);
- }
- if ($_my_integral < $_cost_integral) {
- $_code = IntegralStatus::ITG_NOT_ENOUGH;
- return $this->huoError($_code, IntegralStatus::getMsg($_code));
- }
- //是积分礼包
- if ($_goods_data['object_name'] == MemItgConst::GOODS_IS_GIFT) {
- //需要给玩家发放积分礼包
- $_gift_class = new Gift();
- $_rs = $_gift_class->setGift($mem_id, $_goods_data['object_name']['object_id']);
- if (GiftStatus::NO_ERROR != $_rs['code']) {
- return $this->huoReturn($_rs);
- }
- $shipping_status = MemItgConst::SHIP_STATUS_SUC;
- } else if ($_goods_data['object_name'] == MemItgConst::GOODS_IS_INTEGRAL
- || $_goods_data['object_name'] == MemItgConst::GOODS_IS_REDPACKET) {
- //红包 积分商品,标记为发货成功,下方有增加积分
- $shipping_status = MemItgConst::SHIP_STATUS_SUC;
- } else {
- $shipping_status = MemItgConst::SHIP_STATUS_WAIT_GET;
- }
- $_order_id = $this->createOrder(
- $mem_id, $_goods_data, $act_id, $_cost_integral, $shipping_status, $address, $_goods_data['flag']
- );
- if (!$_order_id) {
- return $this->huoError(
- ShopStatus::GOODS_EXCHANGE_ERROR, ShopStatus::getMsg(ShopStatus::GOODS_EXCHANGE_ERROR)
- );
- }
- $_mitg_class = new MemIntegral();
- $_ia_name = "抽奖";
- $_rs = IntegralStatus::NO_ERROR;
- if ($_cost_integral > 0) {
- /* 减少此次积分 */
- $_rs = $_mitg_class->setMemItg(
- $mem_id, $_cost_integral, MemItgConst::MEM_ITG_DEDUCT, 0, 0, "", 0, $_ia_name
- );
- }
- if (IntegralStatus::NO_ERROR == $_rs && $_goods_data['gain_integral'] > 0
- && $_goods_data['object_name'] == MemItgConst::GOODS_IS_INTEGRAL) {
- /* 获得的积分 */
- $_rs = $_mitg_class->setMemItg(
- $mem_id, $_goods_data['gain_integral'], MemItgConst::MEM_ITG_ADD, 0, 0, "", 0,
- MemItgConst::ITG_DEDUCT_LOTTERY
- );
- }
- if (IntegralStatus::NO_ERROR == $_rs && $_goods_data['object_name'] == MemItgConst::GOODS_IS_REDPACKET) {
- /* 获得红包 */
- $_agent_id = (new UserModel())->getIdByMemId($mem_id);
- $_code = (new AgentWallet())->updateWallet(
- $_agent_id, $_goods_data['market_price'], SettleConst::SETTLE_WALLET_ADD
- );
- if (SettleStatus::NO_ERROR != $_code) {
- return $this->huoError($_code, SettleStatus::NO_ERROR);
- }
- /* 添加到agentOrder 渠道余额记录*/
- $_agent_id = (new UserModel())->getIdByMemId($mem_id);
- $_ao_data = [
- 'order_id' => StrUtils::genOrderId(
- $_agent_id, $_agent_id, $mem_id, WalletConst::WALLET_ORDER_PREFIX_LT
- ),
- 'agent_id' => $_agent_id,
- 'agent_gain' => $_goods_data['market_price'],
- 'flag' => AoRequest::FLAG_LOTTERY,
- 'status' => 2,//成功
- 'remark' => $_ia_name
- ];
- (new AgentOrderModel())->createOrder($_ao_data);
- }
- if (IntegralStatus::NO_ERROR != $_rs) {
- return $this->huoSuccess($_rs, IntegralStatus::getMsg($_rs));
- }
- $_code = ShopStatus::NO_ERROR;
- $_rdata['order_id'] = $_order_id;
- return $this->huoSuccess($_code, ShopStatus::getMsg($_code), $_rdata);
- }
- /**
- * 创建订单
- *
- * @param $mem_id
- * @param $goods_data
- * @param $address
- * @param int $flag
- *
- * @return int
- */
- public function createOrder(
- $mem_id, $goods_data, $act_id, $_cost_integral, $shipping_status, $address = [], $flag = 0
- ) {
- $_data['order_id'] = StrUtils::genOrderId(0, 0, $mem_id, 'itg');
- $_data['mem_id'] = $mem_id;
- $_data['flag'] = $flag;
- $_data['act_id'] = $act_id;
- $_data['shipping_status'] = $shipping_status;
- $_data['goods_id'] = $goods_data['goods_id'];
- $_data['gain_integral'] = $goods_data['gain_integral'];
- $_data['integral'] = $_cost_integral;
- $_data['status'] = MemItgConst::PAY_STATUS_SUC;
- $_data['consignee'] = get_val($address, 'consignee', '');
- $_data['country'] = get_val($address, 'country', 0);
- $_data['province'] = get_val($address, 'province', 0);
- $_data['city'] = get_val($address, 'city', 0);
- $_data['district'] = get_val($address, 'district', 0);
- $_data['town'] = get_val($address, 'town', 0);
- $_data['address'] = get_val($address, 'address', '');
- $_data['zipcode'] = get_val($address, 'zipcode', '');
- $_data['mobile'] = get_val($address, 'mobile', '');
- $_data['email'] = get_val($address, 'email', '');
- $_data['email'] = get_val($address, 'email', '');
- $_ito_id = (new ItgOrderModel())->createOrder($_data);
- if ($_ito_id) {
- return $_data['order_id'];
- }
- return false;
- }
- public function getDetail($_order_id) {
- $_where['order_id'] = $_order_id;
- $_itg_order = (new ItgOrderLogic())->getDetail($_where);
- if (empty($_itg_order)) {
- $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
- } else {
- $_code = CommonStatus::NO_ERROR;
- }
- return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_itg_order);
- }
- public function getDayMobileDrawCount($mobile, $act_id) {
- $_model = new ItgOrderModel();
- $_where['memMobile.mobile'] = $mobile;
- $_where['itg_order_model.create_time'] = ['gt', strtotime(date('Y-m-d'))];
- $_where['itg_order_model.act_id'] = $act_id;
- $_count = $_model->useGlobalScope(false)->with("memMobile")->where($_where)->count();
- return $_count;
- }
- }
|