123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Exception;
- class WorkerBalance extends Base {
- // 保存余额变化记录
- public function saveWorkerBalance($orgId,$money,$userId,$workerId,$type,$content,$fromId=0,$logId=0){
- $this->startTrans();
- try{
- $data = [
- 'org_id' => $orgId,
- 'user_id' => $userId,
- 'worker_id' => $workerId,
- 'money' => $money,
- 'type' => $type,
- 'remark' => $content,
- 'from_id' => $fromId,
- 'settlement_log_id' => $logId,
- 'create_time' => date('Y-m-d H:i:s'),
- ];
- $ret = Db::name('worker_balance')->insert($data);
- if(!$ret){
- \exception('余额记录保存失败');
- }
- $workerInfo = Db::name('worker')
- ->where('id',$workerId)
- ->find();
- if($money>0){
- $a = [
- 'cumulative_money'=>$workerInfo['cumulative_money']+$money,
- 'balance'=>$workerInfo['balance']+$money,
- ];
- }else{
- if($workerInfo['balance'] <abs($money)){
- \exception('余额不足');
- }
- $a = [
- 'balance'=>$workerInfo['balance']-abs($money),
- ];
- }
- $ret = Db::name('worker')
- ->where('id',$workerId)
- ->update($a);
- if(!$ret){
- \exception('余额更新失败');
- }
- $this->commit();
- return true;
- }catch (Exception $e){
- $this->error = $e->getMessage();
- $this->rollback();
- return false;
- }
- }
- public function lists($workerId,$page,$size){
- $start = ($page - 1) * $size;
- $map[] = ['worker_id','=',$workerId];
- $list = $this->where($map)
- ->limit($start,$size)
- ->order('id','desc')
- ->select()
- ->toArray();
- foreach ($list as $k=>$v){
- $list[$k]['date'] = date('Y-m-d',strtotime($v['create_time']));
- $list[$k]['time'] = date('H:i',strtotime($v['create_time']));
- }
- return $list;
- }
- }
|