| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020-05-07
- *
- *
- */
- namespace app\common\repositories\user;
- use app\common\dao\BaseDao;
- use app\common\dao\shared\SharedProsperityBalanceLogDao;
- use app\common\dao\user\UserBillDao;
- use app\common\enum\CommonEnum;
- use app\common\enum\shared\SharedProsperityBalanceLogEnum;
- use app\common\enum\user\UserBillEnum;
- use app\common\enum\user\UserCertificationEnum;
- use app\common\model\user\UserBill;
- use app\common\repositories\BaseRepository;
- use app\common\repositories\gzc\GzcLogRepository;
- use app\common\repositories\shared\SharedProsperityUserRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\entity\data\shared\SharedProsperityBalanceLogEntity;
- use app\entity\data\shared\SharedProsperityUserEntity;
- use app\entity\data\user\UserBillEntity;
- use app\entity\data\user\UserCertificationEntity;
- use app\entity\data\user\UserEntity;
- use app\utils\ArrayUtil;
- use think\db\exception\DbException;
- use think\exception\ValidateException;
- use think\facade\Cache;
- use think\facade\Db;
- use think\Model;
- /**
- * Class UserBillRepository
- * @package app\common\repositories\user
- * @author xaboy
- * @day 2020-05-07
- * @mixin UserBillDao
- */
- class UserBillRepository extends BaseRepository
- {
- /**
- * @var UserBillDao
- */
- protected $dao;
- /**
- * UserBillRepository constructor.
- * @param UserBillDao $dao
- */
- public function __construct(UserBillDao $dao)
- {
- $this->dao = $dao;
- }
- public function userList($where, $uid, $page, $limit)
- {
- $where['uid'] = $uid;
- $query = $this->dao->search($where)->order('create_time DESC')
- ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
- $query->whereLike('status', $where['status']);
- });
- $count = $query->count();
- $list = $query->setOption('field', [])->field('bill_id,pm,title,number,balance,mark,create_time,status,order_sn,mer_id')->page($page, $limit)->select();
- return compact('count', 'list');
- }
- public function getList($where, $page, $limit)
- {
- if ($where["city"]){
- if ($where["city"] == '直辖区') {
- $city_id = Db::name("user_city")->where("city_name", $where["province"])->value("city_id");
- } else {
- $city_id = Db::name("user_city")->where("city_name",$where["city"])->value("city_id");
- }
- $where['city_id'] = $city_id;
- unset($where["city"]);
- }
- $query = $this->dao->searchJoin($where)->order('a.create_time DESC');
- $count = $query->count();
- $list = $query->page($page, $limit)->select();
- $order_type = 1;
- // dump($list);
- // return 1;
- $souxufee = systemConfig('yanglao');
- foreach ($list as &$v){
- if ($v["source"]==1) {
- $vorder_merchant=Db::name("order_merchant")->where(["out_trade_no"=>$v["order_sn"]])->find();
- $v["mer_id"]=$vorder_merchant["mer_id"];
- //消费金额
- $v["total_price"]=$vorder_merchant["money"];
- //可分佣金总额
- $v["extension_one"]=$vorder_merchant["available"];
- //分账给服务商的钱
- $v["service_money"]=$vorder_merchant["service_money"];
- //养老金
- $v["pension"]=$vorder_merchant["pension"];
- //刷卡手续费
- $v["swipe_fee"]=$vorder_merchant["swipe_fee"];
- $v["uid"]=$vorder_merchant["uid"];
- if ($vorder_merchant["uid"] > 0) {
- $users=Db::name("user")->where("uid",$vorder_merchant["uid"])->field("nickname,phone")->find();
- $v["nickname"]=$users["nickname"];
- $v["phone"]=$users["phone"];
- } else {
- $v["nickname"]=$vorder_merchant["nickname"]."【".($vorder_merchant["trade_type"]=="WX"?"微信用户":"支付宝用户")."】";
- $v["phone"]=$vorder_merchant["pay_user_id"];
- }
- } else {
- if(strpos($v['order_sn'],'channel') !== false || strpos($v['order_sn'],'sIC') !== false ){
- //渠道商
- $channel_order = Db::name('channel_order') -> where('order_no',$v['order_sn']) -> find();
- $v["mer_id"]='';
- $v["total_price"]=$channel_order["pay_price"];
- $v["extension_one"]=0;
- $users=Db::name("user")->where("uid",$channel_order["uid"])->field("nickname,phone")->find();
- $v["nickname"]=$users["nickname"];
- $v["phone"]=$users["phone"];
- $v["uid"]=$channel_order["uid"];
- $v['mer_name'] = '开通渠道商';
- $order_type = 2;
- }elseif (strpos($v['order_sn'],'annual') !== false || strpos($v['order_sn'],'sIM') !== false ){
- //商户
- $annual_cost_order=Db::name('merchant_annual_cost_order')-> where('order_no',$v['order_sn'])->find();
- $v["mer_id"]='';
- $v["total_price"]=$annual_cost_order["pay_price"];
- $v["extension_one"]=0;
- $users=Db::name("user")->where("uid",$annual_cost_order["uid"])->field("nickname,phone")->find();
- $v["nickname"]=$users["nickname"];
- $v["phone"]=$users["phone"];
- $v["uid"]=$annual_cost_order["uid"];
- $v['mer_name'] = '开通商户';
- $order_type = 2;
- }else{
- if(strpos($v['order_sn'],'sIO') !== false){
- $v["mer_id"]='';
- $v["total_price"]=strpos($v['mark'],'金卡') !== false ? '999' : '399';
- $order_type = 2;
- }else{
- $store_order=Db::name("store_order")->where("order_sn",$v["order_sn"])->find();
- $v["mer_id"]=$store_order["mer_id"];
- $v["total_price"]=$store_order["total_price"];
- $v["extension_one"]=$store_order["extension_one"];
- $users=Db::name("user")->where("uid",$store_order["uid"])->field("nickname,phone")->find();
- $v["nickname"]=$users["nickname"];
- $v["phone"]=$users["phone"];
- $v["uid"]=$store_order["uid"];
- }
- }
- $v["pension"]=Db::name('user_bill')
- ->where('commission_type',5)
- ->where("order_sn",$v["order_sn"])
- ->value('number');
- $v["swipe_fee"]=Db::name('admin_bill')
- ->where('type',1)
- ->where('status',1)
- ->where("order_sn",$v["order_sn"])
- ->value('number');
- //服务商分账的钱
- $service_money = bcsub($v["extension_one"], $v["pension"], 2);
- // $service_money=bcmul($v["total_price"],0.05,2);
- $v["service_money"]=$service_money;
- }
- $user_bill=Db::name("user_bill")
- ->alias('a')
- ->join('User b', 'a.uid = b.uid')
- ->field('a.bill_id,a.pm,a.title,a.number,a.balance,a.mark,a.create_time,a.status,b.nickname,b.phone,a.uid,a.order_sn,a.source,identity')
- ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
- ->select();
- $admin_bill=Db::name("admin_bill")->where("order_sn",$v["order_sn"])->select();
- foreach ($admin_bill as $q){
- $q["nickname"]="平台";
- $q["phone"]="---------";
- $user_bill[]=$q;
- }
- if ($v["uid"]==0){
- $user_bill[]=[
- "nickname"=>"注:该用户未绑定app,未给到养老金",
- "mark"=>$v["pension"]
- ];
- }
- $v["datalist"]=$user_bill;
- $mer =Db::name("merchant")->where("mer_id",$v["mer_id"])->find();
- $v["mer_name"]=$mer["mer_name"];
- if($order_type == 1){
- $mer_mory=bcsub(bcsub($v["total_price"],$v["extension_one"],2),$v["swipe_fee"],2);
- $user_bill[]=[
- "identity"=>4,
- "nickname"=>$mer["mer_name"],
- "phone"=>$mer["mer_phone"],
- "number"=>$mer_mory,
- "mark"=>"用户到店支付成功消费".$v["total_price"]."元,商家货款".$mer_mory
- ];
- }
- if($order_type == 1){
- $countcommission=Db::name("user_bill")
- ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
- ->where("commission_type","<>",5)
- ->sum("number");
- $jieyu=bcsub($v["service_money"],$countcommission,2);
- $user_bill[]=[
- "nickname"=>"平台",
- "phone"=>"----------",
- "number"=>$jieyu,
- "mark"=>"分账给服务商的钱".$v["service_money"]."元,结余".$jieyu
- ];
- $v['pingtai'] = $jieyu;
- }else{
- $countcommission=Db::name("user_bill")
- ->where(["type"=>"commission","order_sn"=>$v["order_sn"]])
- ->sum("number");
- $v["extension_one"] = $countcommission;
- $jieyu=bcsub($v["total_price"],$countcommission,2);
- $user_bill[]=[
- "nickname"=>"平台",
- "phone"=>"----------",
- "number"=>$jieyu,
- "mark"=>"总佣金+养老金:".$countcommission."元,结余".$jieyu
- ];
- $v['pingtai'] = $jieyu;
- //服务商分账的钱
- $service_money = bcsub($v["extension_one"], $v["pension"], 2);
- $v["service_money"]=$service_money;
- }
- }
- unset($v);
- $c = UserBill::getDB() -> count();
- $userBill = json_decode(Cache::get('userBill'),true);
- // dump($userBill);
- if(isset($userBill['count']) && $userBill['count'] == $c){
- $total1_query = [];
- $cardLists = $userBill['query'];
- }else{
- $total1_query = UserBill::getDB()
- ->alias('a')
- ->join('order_merchant b', 'a.order_sn = b.out_trade_no')
- ->join('merchant_intention c', 'a.mer_id = c.mer_id')
- ->field("b.money,b.available,b.service_money,b.pension,b.swipe_fee")
- ->where('a.source', 1)
- ->where("a.status",1)
- ->where("a.type_shop",0)
- ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
- $query->where('a.type', $where['type']);
- })
- ->when(isset($where['user_time']) && $where['user_time'] !== '', function ($query) use ($where) {
- getModelTime($query, $where['user_time'], 'a.create_time');
- })
- ->when(isset($where['city_id']) && $where['city_id'], function ($query) use ($where) {
- $query->where('c.city_id', $where['city_id']);
- })
- ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
- $query->whereLike('c.mer_name', "%{$where['keyword']}%");
- })
- ->group("a.order_sn")
- ->cursor();
- $total1_price = 0;
- $total1_extension_one = 0;
- $total1_service_money = 0;
- $total1_pension = 0;
- $total1_swipe_fee = 0;
- foreach ($total1_query as $total1) {
- $total1_price += $total1['money'];
- $total1_extension_one += $total1['available'];
- $total1_service_money += $total1['service_money'];
- $total1_pension += $total1['pension'];
- $total1_swipe_fee += $total1['swipe_fee'];
- }
- $cardLists=[
- ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_price, 0, 2), 'field'=>'元', 'name'=> '消费总额' ],
- ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_extension_one, 0, 2), 'field'=>'元', 'name'=> '可分佣总额' ],
- ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_service_money, 0, 2), 'field'=>'元', 'name'=> '分给服务商的金额' ],
- ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_pension, 0, 2), 'field'=>'元', 'name'=> '养老金' ],
- ['className'=> 'el-icon-s-finance', 'count'=> bcadd($total1_swipe_fee, 0, 2), 'field'=>'元', 'name'=> '刷卡手续费' ],
- ];
- $userBill1['count'] = $c;
- $userBill1['query'] = $cardLists;
- Cache::set('userBill',json_encode($userBill1));
- }
- return compact('count', 'list', 'cardLists', 'total1_query');
- }
- /*平台收益记录*/
- public function getAdminList($where, $page, $limit)
- {
- $query = Db::name("admin_bill")->alias('a')->join('User b', 'a.link_id = b.uid')
- ->field('a.*,b.nickname,b.account')
- ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
- $query->where('a.type', $where['type']);
- })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
- getModelTime($query, $where['date'], 'a.create_time');
- })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
- $query->whereLike('a.order_sn|b.nickname|b.account', "%{$where['keyword']}%");
- });
- $query = $query->order('a.create_time DESC');
- $count = $query->count();
- $list = $query->page($page, $limit)->select();
- $cardLists=[];
- // $cardLists=[
- // ['className'=> 'el-icon-s-goods', 'count'=>$count1, 'field'=>'个', 'name'=> '平台收入' ],
- // ['className'=> 'el-icon-s-goods', 'count'=>$count2, 'field'=>'个', 'name'=> '审核中' ],
- // ['className'=> 'el-icon-s-goods', 'count'=>$count3, 'field'=>'个', 'name'=> '未通过' ],
- // ];
- return compact('count', 'list','cardLists');
- }
- /**
- * @param int $uid
- * @param string $category
- * @param string $type
- * @param int $pm
- * @param array $data
- * @return BaseDao|Model
- * @author xaboy
- * @day 2020-05-07
- */
- public function bill(int $uid, string $category, string $type, int $pm, array $data)
- {
- $data['category'] = $category;
- $data['type'] = $type;
- $data['uid'] = $uid;
- $data['pm'] = $pm;
- $bill = $this->dao->create($data);
- // if($category == 'now_money'){
- // $tempData = ['tempCode' => 'USER_BALANCE_CHANGE','id' => $bill->bill_id];
- // Queue::push(SendTemplateMessageJob::class,$tempData);
- // }
- return $bill;
- }
- /**
- * @param int $uid
- * @param string $category
- * @param string $type
- * @param array $data
- * @return BaseDao|Model
- * @author xaboy
- * @day 2020-05-07
- */
- public function incBill(int $uid, string $category, string $type, array $data)
- {
- return $this->bill($uid, $category, $type, 1, $data);
- }
- /**
- * @param int $uid
- * @param string $category
- * @param string $type
- * @param array $data
- * @return BaseDao|Model
- * @author xaboy
- * @day 2020-05-07
- */
- public function decBill(int $uid, string $category, string $type, array $data)
- {
- return $this->bill($uid, $category, $type, 0, $data);
- }
- /**
- * 添加 bill 数据
- * @param int $uid
- * @param int $commissionType
- * @param int $pm
- * @param float $number
- * @param string $category
- * @param string $type
- * @param string $source
- * @param int $orderType
- * @param int $typeShop
- * @param int $orderId
- * @param string $mark
- * @param string $title
- * @param string $orderSn
- * @return UserBillEntity
- */
- public function addUserBill(int $uid, int $commissionType, int $pm, float $number, string $category, string $type, string $source, int $orderType, int $typeShop = 0, int $orderId = 0, string $mark = '', string $title = '', string $orderSn = ''): UserBillEntity
- {
- $merId = 0;
- if ($typeShop == UserBillEnum::TYPE_SHOP['Platform']['code']) {
- /** @var StoreOrderRepository $storeOrderRepository */
- $storeOrderRepository = app()->make(StoreOrderRepository::class);
- $storeOrderEntity = $storeOrderRepository->getStoreOrderEntityByOrderId($orderId);
- if (empty($storeOrderEntity->getOrderId())) {
- throw new ValidateException('未查询到订单信息');
- }
- $orderId = $storeOrderEntity->getOrderId();
- $orderSn = $storeOrderEntity->getOrderSn();
- $merId = $storeOrderEntity->getMerId();
- }
- /** @var UserBillEntity $userBillEntity */
- $userBillEntity = UserBillEntity::newInstance();
- $userBillEntity->setUid($uid)
- ->setLinkId($orderId)
- //UserBillEnum::PM['INCOME']['code']
- ->setPm($pm)
- ->setTitle($title ?: ArrayUtil::getEnumNameByCode($commissionType, UserBillEnum::COMMISSION_TYPE))
- ->setCategory($category)
- ->setType($type)
- ->setNumber($number)
- ->setMark($mark)
- ->setStatus(UserBillEnum::STATUS['PENDING']['code'])
- ->setCommissionType($commissionType)
- ->setOrderSn($orderSn)
- ->setTypeShop($typeShop)
- ->setMerId($merId)
- // UserBillEnum::SOURCE['ONLINE']['code']
- ->setSource($source)
- ->setOrderType($orderType)
- ->setIsRedBrokerage(UserBillEnum::IS_RED_BROKERAGE['NORMAL']['code'])
- ->setGzc(UserBillEnum::GZC_STATUS['NOT_ENTERED']['code'])
- ->setIsGzc(UserBillEnum::IS_GZC['NOT_STARTED']['code']);
- return $this->createByEntity($userBillEntity);
- }
- /**
- * 写入 数据
- * @param UserBillEntity $userBillEntity
- * @return UserBillEntity
- */
- public function createByEntity(UserBillEntity $userBillEntity): UserBillEntity
- {
- $result = $this->dao->create($userBillEntity->toUnderlineArray())->toArray();
- /** @var UserBillEntity $entity */
- $entity = UserBillEntity::newInstance($result);
- return $entity;
- }
- /**
- * @param int $billId
- * @return UserBillEntity
- */
- public function getUserBillEntityByBillId(int $billId): UserBillEntity
- {
- $entityList = $this->getUserBillEntityListByBillIdList([$billId]);
- $entity = array_shift($entityList);
- if ($entity instanceof UserBillEntity) {
- return $entity;
- } else {
- /** @var UserBillEntity */
- return UserBillEntity::newInstance();
- }
- }
- /**
- * 获取Bill实体集合 根据 Id集合
- * @param array $billIdList
- * @return UserBillEntity[]
- */
- public function getUserBillEntityListByBillIdList(array $billIdList): array
- {
- return $this->dao->getUserBillEntityListByBillIdList($billIdList);
- }
- /**
- * 获取Bill实体集合 根据 订单类型 订单Id集合 待结算状态
- * @param string $typeShop
- * @param array $orderIdList
- * @return UserBillEntity[]
- */
- public function getUserBillEntityListByOrderIdListAndPending(string $typeShop, array $orderIdList): array
- {
- return $this->dao->getUserBillEntityListByOrderIdListAndPending($typeShop, $orderIdList);
- }
- /**
- * @param string $typeShop
- * @param string $orderSn
- * @return UserBillEntity
- */
- public function getUserBillEntityByTypeShopAndOrderSn(string $typeShop, string $orderSn): UserBillEntity
- {
- return $this->dao->getUserBillEntityByTypeShopAndOrderSn($typeShop, $orderSn);
- }
- /**
- * @param $dataList
- * @return array
- */
- public function batchUpdateUserBillDataByDataList($dataList): array
- {
- return $this->dao->saveAll($dataList);
- }
- /**
- * 将 账单 记录设置为生效状态 并对用户的 对应 字段进行增减
- * @param array $idList
- * @return array
- */
- public function executeByIdList(array $idList): array
- {
- $userBillEntityList = $this->getUserBillEntityListByBillIdList($idList);
- // 忽略 已经生效的 记录
- $userBillEntityList = array_filter($userBillEntityList, function($entity) {
- return $entity->getStatus() != UserBillEnum::STATUS['VALID']['code'];
- });
- if (!empty($userBillEntityList)) {
- $pensionUidList = [];
- // 获取所有记录的 归属人
- $userIdList = array_map(function ($userBillEntity) use (&$pensionUidList) {
- // 获取 养老金记录
- if ($userBillEntity->getCommissionType() == UserBillEnum::COMMISSION_TYPE['PENSION']['code']) {
- $pensionUidList[] = $userBillEntity->getUid();
- }
- return $userBillEntity->getUid();
- }, $userBillEntityList);
- // 获取用户 映射信息
- /** @var UserRepository $userRepository */
- $userRepository = app()->make(UserRepository::class);
- /** @var UserEntity[] $userEntityMapByUid */
- $userEntityMapByUid = [];
- if (!empty($userIdList)) {
- $userEntityMapByUid = $userRepository->getUserEntityMapByUidList($userIdList);
- }
- // 获取所有用户是否有 共富身份
- /** @var SharedProsperityUserRepository $sharedProsperityUserRepository */
- $sharedProsperityUserRepository = app()->make(SharedProsperityUserRepository::class);
- $sharedProsperityUserEntityMapUserId = [];
- if (!empty($sharedProsperityUserIdList)) {
- $sharedProsperityUserEntityMapUserId = $sharedProsperityUserRepository->getSharedProsperityUserEntityMapByUserIdList($userIdList);
- }
- // 获取实名认证 映射信息 (养老金记录 结算时 需要确定 所属人是否已经实名认证 如果实名认证了 需要推到养老金公证处明细表里 并修改养老金记录的推送状态)
- /** @var UserCertificationEntity[] $userCertificationEntityMapByUid */
- $userCertificationEntityMapByUid = [];
- if (!empty($pensionUidList)) {
- /** @var UserCertificationRepository $userCertificationRepository */
- $userCertificationRepository = app()->make(UserCertificationRepository::class);
- $userCertificationEntityMapByUid = $userCertificationRepository->getUserCertificationEntityMapByUidListApproved($pensionUidList);
- }
- /** @var GzcLogRepository $gzcLogRepository */
- $gzcLogRepository = app()->make(GzcLogRepository::class);
- // 需要更新的 用户信息-基础信息- 会员身份信息
- $updateUserEntityList = [];
- // 需要更新的 共富体系身份信息
- $updateSharedProsperityUserEntityList = [];
- // 共富区 佣金收入支出记录
- /** @var SharedProsperityBalanceLogEntity[] $insertSharedProsperityBalanceLogEntityList */
- $insertSharedProsperityBalanceLogEntityList = [];
- $updateUserBillEntityList = [];
- foreach ($userBillEntityList as $userBillEntity) {
- if (empty($userBillEntity->getBillId()) || empty($userBillEntity->getUid())) {
- // throw new ValidateException('无效的账单记录');
- continue;
- }
- if (!empty($userBillEntity->getTakeTime())) {
- throw new ValidateException("该账单记录已更新,请勿重复操作({$userBillEntity->getBillId()})");
- }
- // 判断用户是否存在
- $userEntity = $userEntityMapByUid[$userBillEntity->getUid()];
- if (empty($userEntity) || empty($userEntity->getUid())) {
- throw new ValidateException("未查询到用户信息({$userBillEntity->getBillId()})");
- }
- // 要更新的用户信息
- $updateUserEntity = null;
- // 要更新的 共富用户信息
- $updateSharedProsperityUserEntity = null;
- // 更新 账单 记录
- /** @var UserBillEntity $updateUserBillEntity */
- $updateUserBillEntity = $updateUserBillEntityList[$userBillEntity->getBillId()] ?? UserBillEntity::newInstance();
- // 更新 账单记录 状态
- $updateUserBillEntity
- ->setBillId($userBillEntity->getBillId())
- ->setStatus(UserBillEnum::STATUS['VALID']['code'])
- ->setTakeTime(time());
- // 直推佣金 锁客佣金 平台奖励佣金 分享佣金
- if (in_array($userBillEntity->getCommissionType(), [
- UserBillEnum::COMMISSION_TYPE['DIRECT_REFERRAL']['code'],
- UserBillEnum::COMMISSION_TYPE['LOCKED_CUSTOMER']['code'],
- UserBillEnum::COMMISSION_TYPE['PLATFORM_REWARD']['code'],
- UserBillEnum::COMMISSION_TYPE['SHARE_EARNINGS']['code']
- ])) {
- // 首先判断 是否有共富会员身份
- $sharedProsperityUserEntity = $sharedProsperityUserEntityMapUserId[$userBillEntity->getUid()] ?? null;
- // 如果拥有共富身份 并且非 会员专区 专属奖励 则发放到 共富身份中
- if (
- $userBillEntity->getMerId() != CommonEnum::DESIGN_MERCHANT_ID['MemberZone']['code']
- && !empty($sharedProsperityUserEntity)
- && $sharedProsperityUserEntity instanceof SharedProsperityUserEntity
- && !empty($sharedProsperityUserEntity->getId())
- ) {
- // 更新 共富用户信息
- /** @var SharedProsperityUserEntity $updateSharedProsperityUserEntity */
- $updateSharedProsperityUserEntity = $updateSharedProsperityUserEntityList[$userBillEntity->getUid()] ?? SharedProsperityUserEntity::newInstance();
- $updateSharedProsperityUserEntity
- ->setId($sharedProsperityUserEntity->getId())
- ->setUserId($sharedProsperityUserEntity->getUserId());
- // 佣金
- if (is_null($updateSharedProsperityUserEntity->getBalance())) {
- $updateSharedProsperityUserEntity->setBalance($sharedProsperityUserEntity->getBalance());
- }
- switch ($userBillEntity->getCommissionType()) {
- case UserBillEnum::COMMISSION_TYPE['DIRECT_REFERRAL']['code']:
- $sharedProsperityBalanceLogType = SharedProsperityBalanceLogEnum::TYPE['DIRECT_REFERRAL']['code'];
- break;
- case UserBillEnum::COMMISSION_TYPE['LOCKED_CUSTOMER']['code']:
- $sharedProsperityBalanceLogType = SharedProsperityBalanceLogEnum::TYPE['LOCKED_CUSTOMER']['code'];
- break;
- case UserBillEnum::COMMISSION_TYPE['PLATFORM_REWARD']['code']:
- $sharedProsperityBalanceLogType = SharedProsperityBalanceLogEnum::TYPE['PLATFORM_REWARD']['code'];
- break;
- case UserBillEnum::COMMISSION_TYPE['SHARE_EARNINGS']['code']:
- $sharedProsperityBalanceLogType = SharedProsperityBalanceLogEnum::TYPE['SHARE_EARNINGS']['code'];
- break;
- default:
- $sharedProsperityBalanceLogType = null;
- break;
- }
- $sharedProsperityBalanceLogEntity = SharedProsperityBalanceLogEntity::newInstance()
- ->setUserId($sharedProsperityUserEntity->getUserId())
- ->setType($sharedProsperityBalanceLogType)
- ->setLevel($sharedProsperityUserEntity->getPartnerLevel())
- ->setNumber($userBillEntity->getNumber())
- ->setStatus(SharedProsperityBalanceLogEnum::STATUS['VALID']['code'])
- ;
- // 更新用户 佣金 信息
- if ($userBillEntity->getPm() == UserBillEnum::PM['EXPEND']['code']) {
- if ($updateSharedProsperityUserEntity->getBalance() < $userBillEntity->getNumber()) {
- throw new ValidateException('用户佣金不足');
- }
- $updateSharedProsperityUserEntity->setBalance(bcsub($updateSharedProsperityUserEntity->getBalance(), $userBillEntity->getNumber(), 2));
- $sharedProsperityBalanceLogEntity->setPm(SharedProsperityBalanceLogEnum::PM['EXPEND']['code']);
- } else {
- $updateSharedProsperityUserEntity->setBalance(bcadd($updateSharedProsperityUserEntity->getBalance(), $userBillEntity->getNumber(), 2));
- $sharedProsperityBalanceLogEntity->setPm(SharedProsperityBalanceLogEnum::PM['INCOME']['code']);
- }
- // 插入 共富区 佣金记录 变动表
- $insertSharedProsperityBalanceLogEntityList[] = $sharedProsperityBalanceLogEntity
- ->setAfter($updateSharedProsperityUserEntity->getBalance());
- } else {
- // 更新 用户信息
- /** @var UserEntity $updateUserEntity */
- $updateUserEntity = $updateUserEntityList[$userEntity->getUid()] ?? UserEntity::newInstance();
- $updateUserEntity->setUid($userEntity->getUid());
- // 直推奖
- if (is_null($updateUserEntity->getBrokeragePrice())) {
- $updateUserEntity->setBrokeragePrice($userEntity->getBrokeragePrice());
- }
- // 更新用户 佣金 信息
- if ($userBillEntity->getPm() == UserBillEnum::PM['EXPEND']['code']) {
- if ($updateUserEntity->getBrokeragePrice() < $userBillEntity->getNumber()) {
- throw new ValidateException('用户佣金不足');
- }
- $updateUserEntity->setBrokeragePrice(bcsub($updateUserEntity->getBrokeragePrice(), $userBillEntity->getNumber(), 2));
- } else {
- $updateUserEntity->setBrokeragePrice(bcadd($updateUserEntity->getBrokeragePrice(), $userBillEntity->getNumber(), 2));
- }
- }
- } else if ($userBillEntity->getCommissionType() == UserBillEnum::COMMISSION_TYPE['PENSION']['code']) {
- if ($userBillEntity->getGzc() == UserBillEnum::GZC_STATUS['NOT_ENTERED']['code']) {
- if (!empty($userCertificationEntityMapByUid[$userBillEntity->getUid()])) {
- $userCertificationEntity = $userCertificationEntityMapByUid[$userBillEntity->getUid()];
- // 创建公证处记录
- $gzcLogRepository->addGzcLog(
- $userCertificationEntity->getUid(),
- $userCertificationEntity->getUserName(),
- $userCertificationEntity->getMobile(),
- $userCertificationEntity->getUserCard(),
- $userBillEntity->getTypeShop(),
- $userBillEntity->getLinkId(),
- $userBillEntity->getNumber()
- );
- // 更新 用户信息
- /** @var UserEntity $updateUserEntity */
- $updateUserEntity = $updateUserEntityList[$userEntity->getUid()] ?? UserEntity::newInstance();
- $updateUserEntity->setUid($userEntity->getUid());
- // 养老金
- if (is_null($updateUserEntity->getAnnuity())) {
- $updateUserEntity->setAnnuity($userEntity->getAnnuity());
- }
- $updateUserEntity->setAnnuity(bcadd($updateUserEntity->getAnnuity(), $userBillEntity->getNumber(), 2));
- // 几笔养老金
- if (is_null($updateUserEntity->getAnnuityNum())) {
- $updateUserEntity->setAnnuityNum($userEntity->getAnnuityNum());
- }
- $updateUserEntity->setAnnuityNum(bcadd($updateUserEntity->getAnnuityNum(), 1));
- // 更新佣金记录
- $updateUserBillEntity
- ->setGzc(UserBillEnum::GZC_STATUS['ENTERED']['code'])
- ->setIsGzc(UserBillEnum::IS_GZC['NOT_STARTED']['code']);
- }
- }
- }
- if (!empty($updateUserEntity)) {
- $updateUserEntityList[$userEntity->getUid()] = $updateUserEntity;
- }
- if (!empty($updateSharedProsperityUserEntity)) {
- $updateSharedProsperityUserEntityList[$updateSharedProsperityUserEntity->getUserId()] = $updateSharedProsperityUserEntity;
- }
- $updateUserBillEntityList[$userBillEntity->getBillId()] = $updateUserBillEntity;
- }
- if (!empty($updateUserEntityList)) {
- $userRepository->batchUpdateUserDataByDataList(
- array_map(function (UserEntity $updateUserEntity) {
- return $updateUserEntity->toUnderlineArray();
- }, $updateUserEntityList)
- );
- }
- if (!empty($updateSharedProsperityUserEntityList)) {
- $sharedProsperityUserRepository->batchUpdateSharedProsperityUserDataByDataList(
- array_map(function (SharedProsperityUserEntity $updateSharedProsperityUserEntity) {
- return $updateSharedProsperityUserEntity->toUnderlineArray();
- }, $updateSharedProsperityUserEntityList)
- );
- }
- if (!empty($insertSharedProsperityBalanceLogEntityList)) {
- /** @var SharedProsperityBalanceLogDao $balanceDao */
- $balanceDao = app()->make(SharedProsperityBalanceLogDao::class);
- $balanceDao->insertAll(array_map(function ($insertSharedProsperityBalanceLogEntity) {
- return $insertSharedProsperityBalanceLogEntity->toUnderlineArray();
- }, $insertSharedProsperityBalanceLogEntityList));
- }
- if (!empty($updateUserBillEntityList)) {
- $this->batchUpdateUserBillDataByDataList(
- array_map(function (UserBillEntity $updateUserBillEntity) {
- return $updateUserBillEntity->toUnderlineArray();
- }, $updateUserBillEntityList)
- );
- }
- }
- return $idList;
- }
- /**
- * @param $start
- * @param $end
- * @return array
- * @author 史晨
- * @date 2026/2/11 14:29
- * 共富感恩奖佣金查询
- */
- public function getListForSharedProsperityThank($start, $end)
- {
- $status = UserBillEnum::STATUS['VALID']['code'];
- $merId = [
- CommonEnum::DESIGN_MERCHANT_ID['WonderfulLiving']['code'],
- CommonEnum::DESIGN_MERCHANT_ID['Repurchase']['code'],
- CommonEnum::DESIGN_MERCHANT_ID['SharedProsperity']['code'],
- ];
- $commission_type = [
- CommonEnum::COMMISSION_TYPE['DIRECT_REFERRAL']['code'],
- CommonEnum::COMMISSION_TYPE['SHARE_EARNINGS']['code']
- ];
- $params = [
- 'start' => $start,
- 'end' => $end,
- 'status' => $status,
- 'merId' => $merId,
- 'commissionType' => $commission_type
- ];
- return $this->dao->getListData($params);
- }
- }
|