Orderinviter.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Orderinviter extends BaseModel
  12. {
  13. /**
  14. * 支付给钱
  15. * @access public
  16. * @author csdeshang
  17. * @param type $order_id 订单编号
  18. */
  19. public function giveMoney($order_id, $type)
  20. {
  21. $orderinviter_list = Db::name('orderinviter')->where(array('orderinviter_order_id' => $order_id, 'orderinviter_valid' => 0, 'orderinviter_order_type' => $type))->lock(true)->select()->toArray();
  22. if ($orderinviter_list) {
  23. $predeposit_model = model('predeposit');
  24. foreach ($orderinviter_list as $val) {
  25. //如果是被清退的分销员,则得不到分销佣金,对冲分销员分销完商品后,退款的情况
  26. $inviter = Db::name('inviter')->where(array('inviter_id' => $val['orderinviter_member_id'], 'inviter_state' => 1))->lock(true)->find();
  27. if ($inviter) {
  28. $data = array();
  29. $data['member_id'] = $val['orderinviter_member_id'];
  30. $data['member_name'] = $val['orderinviter_member_name'];
  31. $data['amount'] = $val['orderinviter_money'];
  32. $data['order_sn'] = $val['orderinviter_order_sn'];
  33. $data['lg_desc'] = $val['orderinviter_remark'];
  34. $predeposit_model->changePd('order_inviter', $data);
  35. $goodscommon = Db::name('goodscommon')->where('goods_commonid=' . $val['orderinviter_goods_commonid'])->lock(true)->find();
  36. if ($goodscommon) {
  37. $goodscommon_data = array();
  38. if (!Db::name('orderinviter')->where(array(array('orderinviter_order_id', '=', $order_id), array('orderinviter_goods_commonid', '=', $val['orderinviter_goods_commonid']), array('orderinviter_valid', '<>', 0)))->lock(true)->find()) {
  39. //更新商品的分销情况
  40. $goodscommon_data['inviter_total_quantity'] = $goodscommon['inviter_total_quantity'] + $val['orderinviter_goods_quantity'];
  41. $goodscommon_data['inviter_total_amount'] = bcadd($goodscommon['inviter_total_amount'], $val['orderinviter_goods_amount'], 2);
  42. }
  43. if ($val['orderinviter_money'] > 0) {
  44. $goodscommon_data['inviter_amount'] = bcadd($goodscommon['inviter_amount'], $val['orderinviter_money'], 2);
  45. }
  46. if (!empty($goodscommon_data)) {
  47. $mysql_flag = Db::name('goodscommon')->where('goods_commonid=' . $val['orderinviter_goods_commonid'])->update($goodscommon_data);
  48. if (!$mysql_flag) {
  49. throw new \think\Exception('[订单id:' . $order_id . ']商品分销信息更新失败', 10006);
  50. }
  51. }
  52. }
  53. $inviter_data = array(
  54. 'inviter_goods_quantity' => $inviter['inviter_goods_quantity'] + $val['orderinviter_goods_quantity'],
  55. 'inviter_goods_amount' => bcadd($inviter['inviter_goods_amount'], $val['orderinviter_goods_amount'], 2),
  56. 'inviter_total_amount' => bcadd($inviter['inviter_total_amount'], $val['orderinviter_money'], 2),
  57. );
  58. //更新分销员的分销情况
  59. $mysql_flag = Db::name('inviter')->where(array('inviter_id' => $val['orderinviter_member_id']))->update($inviter_data);
  60. if (!$mysql_flag) {
  61. throw new \think\Exception('[订单id:' . $order_id . ']分销员分销信息更新失败', 10006);
  62. }
  63. $mysql_flag = Db::name('orderinviter')->where('orderinviter_id', $val['orderinviter_id'])->update(['orderinviter_valid' => 1]);
  64. if (!$mysql_flag) {
  65. throw new \think\Exception('[订单id:' . $order_id . ']分销佣金状态更新失败', 10006);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }