Orderinviter.php 4.6 KB

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