ItgOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * ItgOrder.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/7 22:42
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\shop;
  13. use huo\controller\agent\AgentWallet;
  14. use huo\controller\common\Base;
  15. use huo\controller\finance\AoRequest;
  16. use huo\controller\gift\Gift;
  17. use huo\controller\integral\MemIntegral;
  18. use huo\controller\member\MemCache;
  19. use huo\logic\posts\ShareActLogic;
  20. use huo\logic\shop\ItgOrderLogic;
  21. use huo\model\agent\AgentOrderModel;
  22. use huo\model\shop\ItgOrderModel;
  23. use huo\model\user\UserModel;
  24. use huolib\constant\MemItgConst;
  25. use huolib\constant\SettleConst;
  26. use huolib\constant\WalletConst;
  27. use huolib\status\CommonStatus;
  28. use huolib\status\GiftStatus;
  29. use huolib\status\IntegralStatus;
  30. use huolib\status\MemberStatus;
  31. use huolib\status\SettleStatus;
  32. use huolib\status\ShopStatus;
  33. use huolib\tool\StrUtils;
  34. class ItgOrder extends Base {
  35. /**
  36. * 兑换商品
  37. *
  38. * @param $mem_id
  39. * @param $goods_id
  40. *
  41. * @param array $address
  42. *
  43. * @param $act_id
  44. * @param int $cost_integral
  45. *
  46. * @return array
  47. */
  48. public function exchange($mem_id, $goods_id, $address = [], $act_id = 0, $cost_integral = 0) {
  49. if (empty($mem_id)) {
  50. $_code = MemberStatus::LOGIN_IS_OUT;
  51. return $this->huoError($_code, MemberStatus::getMsg($_code));
  52. }
  53. if (empty($goods_id)) {
  54. $_code = ShopStatus::GOODS_ID_EMPTY;
  55. return $this->huoError($_code, ShopStatus::getMsg($_code));
  56. }
  57. $_mc_class = MemCache::ins();
  58. $_me_data = $_mc_class->getMeInfoById($mem_id);
  59. if (empty($_me_data)) {
  60. $_code = IntegralStatus::ITG_NOT_ENOUGH;
  61. return $this->huoSuccess($_code, IntegralStatus::getMsg($_code));
  62. }
  63. $_goods_cache = GoodsCache::ins();
  64. $_goods_data = $_goods_cache->getInfoByGoodsId($goods_id);
  65. if (empty($_goods_data)) {
  66. $_code = ShopStatus::GOODS_NOT_EXISTS;
  67. return $this->huoSuccess($_code, ShopStatus::getMsg($_code));
  68. }
  69. /* 判断余量 */
  70. if ($_goods_data['remain_cnt'] <= 0) {
  71. $_code = ShopStatus::GOODS_NOT_ENOUGH;
  72. return $this->huoSuccess($_code, ShopStatus::getMsg($_code));
  73. }
  74. $_my_integral = $_me_data['my_integral'];
  75. $_cost_integral = $cost_integral;
  76. if (empty($_cost_integral)) {
  77. $_cost_integral = $_goods_data['integral'];
  78. }
  79. if ($_my_integral < $_cost_integral) {
  80. $_code = IntegralStatus::ITG_NOT_ENOUGH;
  81. return $this->huoError($_code, IntegralStatus::getMsg($_code));
  82. }
  83. $_goods_data['remain_cnt']--;
  84. $_goods_cache->updateGoods($goods_id, $_goods_data);
  85. $shipping_status = MemItgConst::SHIP_STATUS_NOT;
  86. if (MemItgConst::SHOP_FLAG_RP == $_goods_data['flag']) {
  87. $shipping_status = MemItgConst::SHIP_STATUS_SUC;
  88. }
  89. $_order_id = $this->createOrder(
  90. $mem_id, $_goods_data, $act_id, $_cost_integral, $shipping_status, $address, $_goods_data['flag']
  91. );
  92. if (!$_order_id) {
  93. return $this->huoError(
  94. ShopStatus::GOODS_EXCHANGE_ERROR, ShopStatus::getMsg(ShopStatus::GOODS_EXCHANGE_ERROR)
  95. );
  96. }
  97. $_mitg_class = new MemIntegral();
  98. $_ia_name = '兑换'.$_goods_data['goods_name'];
  99. $_rs = IntegralStatus::NO_ERROR;
  100. if ($_cost_integral > 0) {
  101. /* 减少此次积分 */
  102. $_rs = $_mitg_class->setMemItg(
  103. $mem_id, $_cost_integral, MemItgConst::MEM_ITG_DEDUCT, 0, 0, "", 0, $_ia_name
  104. );
  105. if (IntegralStatus::NO_ERROR == $_rs && MemItgConst::SHOP_FLAG_RP == $_goods_data['flag']) { //兑换红包
  106. /* 获得红包 */
  107. $_agent_id = (new UserModel())->getIdByMemId($mem_id);
  108. $_code = (new AgentWallet())->updateWallet(
  109. $_agent_id, $_goods_data['market_price'], SettleConst::SETTLE_WALLET_ADD
  110. );
  111. if (SettleStatus::NO_ERROR != $_code) {
  112. return $this->huoError($_code, SettleStatus::NO_ERROR);
  113. }
  114. /* 添加到agentOrder 渠道余额记录*/
  115. $_agent_id = (new UserModel())->getIdByMemId($mem_id);
  116. $_ao_data = [
  117. 'order_id' => StrUtils::genOrderId(
  118. $_agent_id, $_agent_id, $mem_id, WalletConst::WALLET_ORDER_PREFIX_EG
  119. ),
  120. 'agent_id' => $_agent_id,
  121. 'agent_gain' => $_goods_data['market_price'],
  122. 'flag' => AoRequest::FLAG_EXCHANGE,
  123. 'status' => 2,//成功
  124. 'remark' => $_ia_name
  125. ];
  126. (new AgentOrderModel())->createOrder($_ao_data);
  127. }
  128. }
  129. if (IntegralStatus::NO_ERROR == $_rs && $_goods_data['gain_integral'] > 0) {
  130. /* 获得的积分 */
  131. $_rs = $_mitg_class->setMemItg(
  132. $mem_id, $_goods_data['gain_integral'], MemItgConst::MEM_ITG_ADD, 0, 0, "", 0,
  133. MemItgConst::ITG_DEDUCT_LOTTERY
  134. );
  135. }
  136. if (IntegralStatus::NO_ERROR != $_rs) {
  137. return $this->huoSuccess($_rs, IntegralStatus::getMsg($_rs));
  138. }
  139. $_code = ShopStatus::NO_ERROR;
  140. $_rdata['order_id'] = $_order_id;
  141. return $this->huoSuccess($_code, ShopStatus::getMsg($_code), $_rdata);
  142. }
  143. /**
  144. * 抽奖兑换商品
  145. *
  146. * @param $mem_id
  147. * @param $goods_id
  148. *
  149. * @param array $address
  150. *
  151. * @param $act_id
  152. * @param int $cost_integral
  153. *
  154. * @return array
  155. */
  156. public function drawExchange($mem_id, $goods_id, $address = [], $act_id = 0, $cost_integral = 0) {
  157. if (empty($mem_id)) {
  158. $_code = MemberStatus::LOGIN_IS_OUT;
  159. return $this->huoError($_code, MemberStatus::getMsg($_code));
  160. }
  161. if (empty($goods_id)) {
  162. $_code = ShopStatus::GOODS_ID_EMPTY;
  163. return $this->huoError($_code, ShopStatus::getMsg($_code));
  164. }
  165. $_mc_class = MemCache::ins();
  166. $_me_data_ext = $_mc_class->getMeInfoById($mem_id);
  167. if (empty($_me_data_ext)) {
  168. $_code = MemberStatus::UID_INVALID;
  169. return $this->huoSuccess($_code, MemberStatus::getMsg($_code));
  170. }
  171. // $_me_data = $_mc_class->getInfoById($mem_id);
  172. // if (empty($_me_data['mobile'])) {
  173. // $_code = ShopStatus::PHONE_NOT_BIND;
  174. //
  175. // return $this->huoSuccess($_code, ShopStatus::getMsg($_code));
  176. // }
  177. $_goods_cache = GoodsCache::ins();
  178. $_goods_data = $_goods_cache->getInfoByGoodsId($goods_id);
  179. if (empty($_goods_data) || $_goods_data['flag'] != MemItgConst::SHOP_FLAG_LOTTERY) {
  180. $_code = ShopStatus::GOODS_NOT_EXISTS;
  181. return $this->huoSuccess($_code, ShopStatus::getMsg($_code));
  182. }
  183. $_my_integral = $_me_data_ext['my_integral'];
  184. $_cost_integral = $cost_integral;
  185. //判断是否有免费次数
  186. $_share_act_logic = new ShareActLogic();
  187. $_share_act = $_share_act_logic->getMemActModel($mem_id, $act_id);
  188. if ($_share_act['free_cnt'] - 1 >= 0) {
  189. $_cost_integral = 0;
  190. $_mem_act_data['free_cnt'] = $_share_act['free_cnt'] - 1;
  191. $_share_act_logic->updateMemAct($_share_act['id'], $_mem_act_data);
  192. } else if ($_share_act['today_cnt'] - 1 >= 0) {
  193. $_cost_integral = 0;
  194. $_mem_act_data['today_cnt'] = $_share_act['today_cnt'] - 1;
  195. $_share_act_logic->updateMemAct($_share_act['id'], $_mem_act_data);
  196. }
  197. if ($_my_integral < $_cost_integral) {
  198. $_code = IntegralStatus::ITG_NOT_ENOUGH;
  199. return $this->huoError($_code, IntegralStatus::getMsg($_code));
  200. }
  201. //是积分礼包
  202. if ($_goods_data['object_name'] == MemItgConst::GOODS_IS_GIFT) {
  203. //需要给玩家发放积分礼包
  204. $_gift_class = new Gift();
  205. $_rs = $_gift_class->setGift($mem_id, $_goods_data['object_name']['object_id']);
  206. if (GiftStatus::NO_ERROR != $_rs['code']) {
  207. return $this->huoReturn($_rs);
  208. }
  209. $shipping_status = MemItgConst::SHIP_STATUS_SUC;
  210. } else if ($_goods_data['object_name'] == MemItgConst::GOODS_IS_INTEGRAL
  211. || $_goods_data['object_name'] == MemItgConst::GOODS_IS_REDPACKET) {
  212. //红包 积分商品,标记为发货成功,下方有增加积分
  213. $shipping_status = MemItgConst::SHIP_STATUS_SUC;
  214. } else {
  215. $shipping_status = MemItgConst::SHIP_STATUS_WAIT_GET;
  216. }
  217. $_order_id = $this->createOrder(
  218. $mem_id, $_goods_data, $act_id, $_cost_integral, $shipping_status, $address, $_goods_data['flag']
  219. );
  220. if (!$_order_id) {
  221. return $this->huoError(
  222. ShopStatus::GOODS_EXCHANGE_ERROR, ShopStatus::getMsg(ShopStatus::GOODS_EXCHANGE_ERROR)
  223. );
  224. }
  225. $_mitg_class = new MemIntegral();
  226. $_ia_name = "抽奖";
  227. $_rs = IntegralStatus::NO_ERROR;
  228. if ($_cost_integral > 0) {
  229. /* 减少此次积分 */
  230. $_rs = $_mitg_class->setMemItg(
  231. $mem_id, $_cost_integral, MemItgConst::MEM_ITG_DEDUCT, 0, 0, "", 0, $_ia_name
  232. );
  233. }
  234. if (IntegralStatus::NO_ERROR == $_rs && $_goods_data['gain_integral'] > 0
  235. && $_goods_data['object_name'] == MemItgConst::GOODS_IS_INTEGRAL) {
  236. /* 获得的积分 */
  237. $_rs = $_mitg_class->setMemItg(
  238. $mem_id, $_goods_data['gain_integral'], MemItgConst::MEM_ITG_ADD, 0, 0, "", 0,
  239. MemItgConst::ITG_DEDUCT_LOTTERY
  240. );
  241. }
  242. if (IntegralStatus::NO_ERROR == $_rs && $_goods_data['object_name'] == MemItgConst::GOODS_IS_REDPACKET) {
  243. /* 获得红包 */
  244. $_agent_id = (new UserModel())->getIdByMemId($mem_id);
  245. $_code = (new AgentWallet())->updateWallet(
  246. $_agent_id, $_goods_data['market_price'], SettleConst::SETTLE_WALLET_ADD
  247. );
  248. if (SettleStatus::NO_ERROR != $_code) {
  249. return $this->huoError($_code, SettleStatus::NO_ERROR);
  250. }
  251. /* 添加到agentOrder 渠道余额记录*/
  252. $_agent_id = (new UserModel())->getIdByMemId($mem_id);
  253. $_ao_data = [
  254. 'order_id' => StrUtils::genOrderId(
  255. $_agent_id, $_agent_id, $mem_id, WalletConst::WALLET_ORDER_PREFIX_LT
  256. ),
  257. 'agent_id' => $_agent_id,
  258. 'agent_gain' => $_goods_data['market_price'],
  259. 'flag' => AoRequest::FLAG_LOTTERY,
  260. 'status' => 2,//成功
  261. 'remark' => $_ia_name
  262. ];
  263. (new AgentOrderModel())->createOrder($_ao_data);
  264. }
  265. if (IntegralStatus::NO_ERROR != $_rs) {
  266. return $this->huoSuccess($_rs, IntegralStatus::getMsg($_rs));
  267. }
  268. $_code = ShopStatus::NO_ERROR;
  269. $_rdata['order_id'] = $_order_id;
  270. return $this->huoSuccess($_code, ShopStatus::getMsg($_code), $_rdata);
  271. }
  272. /**
  273. * 创建订单
  274. *
  275. * @param $mem_id
  276. * @param $goods_data
  277. * @param $address
  278. * @param int $flag
  279. *
  280. * @return int
  281. */
  282. public function createOrder(
  283. $mem_id, $goods_data, $act_id, $_cost_integral, $shipping_status, $address = [], $flag = 0
  284. ) {
  285. $_data['order_id'] = StrUtils::genOrderId(0, 0, $mem_id, 'itg');
  286. $_data['mem_id'] = $mem_id;
  287. $_data['flag'] = $flag;
  288. $_data['act_id'] = $act_id;
  289. $_data['shipping_status'] = $shipping_status;
  290. $_data['goods_id'] = $goods_data['goods_id'];
  291. $_data['gain_integral'] = $goods_data['gain_integral'];
  292. $_data['integral'] = $_cost_integral;
  293. $_data['status'] = MemItgConst::PAY_STATUS_SUC;
  294. $_data['consignee'] = get_val($address, 'consignee', '');
  295. $_data['country'] = get_val($address, 'country', 0);
  296. $_data['province'] = get_val($address, 'province', 0);
  297. $_data['city'] = get_val($address, 'city', 0);
  298. $_data['district'] = get_val($address, 'district', 0);
  299. $_data['town'] = get_val($address, 'town', 0);
  300. $_data['address'] = get_val($address, 'address', '');
  301. $_data['zipcode'] = get_val($address, 'zipcode', '');
  302. $_data['mobile'] = get_val($address, 'mobile', '');
  303. $_data['email'] = get_val($address, 'email', '');
  304. $_data['email'] = get_val($address, 'email', '');
  305. $_ito_id = (new ItgOrderModel())->createOrder($_data);
  306. if ($_ito_id) {
  307. return $_data['order_id'];
  308. }
  309. return false;
  310. }
  311. public function getDetail($_order_id) {
  312. $_where['order_id'] = $_order_id;
  313. $_itg_order = (new ItgOrderLogic())->getDetail($_where);
  314. if (empty($_itg_order)) {
  315. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  316. } else {
  317. $_code = CommonStatus::NO_ERROR;
  318. }
  319. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_itg_order);
  320. }
  321. public function getDayMobileDrawCount($mobile, $act_id) {
  322. $_model = new ItgOrderModel();
  323. $_where['memMobile.mobile'] = $mobile;
  324. $_where['itg_order_model.create_time'] = ['gt', strtotime(date('Y-m-d'))];
  325. $_where['itg_order_model.act_id'] = $act_id;
  326. $_count = $_model->useGlobalScope(false)->with("memMobile")->where($_where)->count();
  327. return $_count;
  328. }
  329. }