Orderinviter.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 数据层模型
  15. */
  16. class Orderinviter extends BaseModel {
  17. /**
  18. * 支付给钱
  19. * @access public
  20. * @author csdeshang
  21. * @param type $order_id 订单编号
  22. */
  23. public function giveMoney($order_id, $type) {
  24. $orderinviter_list = Db::name('orderinviter')->where(array('orderinviter_order_id' => $order_id, 'orderinviter_valid' => 0, 'orderinviter_order_type' => $type))->lock(true)->select()->toArray();
  25. if ($orderinviter_list) {
  26. $predeposit_model = model('predeposit');
  27. foreach ($orderinviter_list as $val) {
  28. //如果是被清退的分销员,则得不到分销佣金,对冲分销员分销完商品后,退款的情况
  29. $inviter = Db::name('inviter')->where(array('inviter_id' => $val['orderinviter_member_id'], 'inviter_state' => 1))->lock(true)->find();
  30. if ($inviter) {
  31. $data = array();
  32. $data['member_id'] = $val['orderinviter_member_id'];
  33. $data['member_name'] = $val['orderinviter_member_name'];
  34. $data['amount'] = $val['orderinviter_money'];
  35. $data['order_sn'] = $val['orderinviter_order_sn'];
  36. $data['lg_desc'] = $val['orderinviter_remark'];
  37. $predeposit_model->changePd('order_inviter', $data);
  38. $goodscommon = Db::name('goodscommon')->where('goods_commonid=' . $val['orderinviter_goods_commonid'])->lock(true)->find();
  39. if ($goodscommon) {
  40. $goodscommon_data = array();
  41. 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()) {
  42. //更新商品的分销情况
  43. $goodscommon_data['inviter_total_quantity'] = $goodscommon['inviter_total_quantity'] + $val['orderinviter_goods_quantity'];
  44. $goodscommon_data['inviter_total_amount'] = bcadd($goodscommon['inviter_total_amount'], $val['orderinviter_goods_amount'], 2);
  45. }
  46. if ($val['orderinviter_money'] > 0) {
  47. $goodscommon_data['inviter_amount'] = bcadd($goodscommon['inviter_amount'], $val['orderinviter_money'], 2);
  48. }
  49. if (!empty($goodscommon_data)) {
  50. $mysql_flag = Db::name('goodscommon')->where('goods_commonid=' . $val['orderinviter_goods_commonid'])->update($goodscommon_data);
  51. if (!$mysql_flag) {
  52. throw new \think\Exception('[订单id:' . $order_id . ']商品分销信息更新失败', 10006);
  53. }
  54. }
  55. }
  56. $inviter_data = array(
  57. 'inviter_goods_quantity' => $inviter['inviter_goods_quantity'] + $val['orderinviter_goods_quantity'],
  58. 'inviter_goods_amount' => bcadd($inviter['inviter_goods_amount'], $val['orderinviter_goods_amount'], 2),
  59. 'inviter_total_amount' => bcadd($inviter['inviter_total_amount'], $val['orderinviter_money'], 2),
  60. );
  61. //更新分销员的分销情况
  62. $mysql_flag = Db::name('inviter')->where(array('inviter_id' => $val['orderinviter_member_id']))->update($inviter_data);
  63. if (!$mysql_flag) {
  64. throw new \think\Exception('[订单id:' . $order_id . ']分销员分销信息更新失败', 10006);
  65. }
  66. $mysql_flag = Db::name('orderinviter')->where('orderinviter_id', $val['orderinviter_id'])->update(['orderinviter_valid' => 1]);
  67. if (!$mysql_flag) {
  68. throw new \think\Exception('[订单id:' . $order_id . ']分销佣金状态更新失败', 10006);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }