0
0

WorkerBalance.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class WorkerBalance extends Base {
  6. // 保存余额变化记录
  7. public function saveWorkerBalance($orgId,$money,$userId,$workerId,$type,$content,$fromId=0,$logId=0){
  8. $this->startTrans();
  9. try{
  10. $data = [
  11. 'org_id' => $orgId,
  12. 'user_id' => $userId,
  13. 'worker_id' => $workerId,
  14. 'money' => $money,
  15. 'type' => $type,
  16. 'remark' => $content,
  17. 'from_id' => $fromId,
  18. 'settlement_log_id' => $logId,
  19. 'create_time' => date('Y-m-d H:i:s'),
  20. ];
  21. $ret = Db::name('worker_balance')->insert($data);
  22. if(!$ret){
  23. \exception('余额记录保存失败');
  24. }
  25. $workerInfo = Db::name('worker')
  26. ->where('id',$workerId)
  27. ->find();
  28. if($money>0){
  29. $a = [
  30. 'cumulative_money'=>$workerInfo['cumulative_money']+$money,
  31. 'balance'=>$workerInfo['balance']+$money,
  32. ];
  33. }else{
  34. if($workerInfo['balance'] <abs($money)){
  35. \exception('余额不足');
  36. }
  37. $a = [
  38. 'balance'=>$workerInfo['balance']-abs($money),
  39. ];
  40. }
  41. $ret = Db::name('worker')
  42. ->where('id',$workerId)
  43. ->update($a);
  44. if(!$ret){
  45. \exception('余额更新失败');
  46. }
  47. $this->commit();
  48. return true;
  49. }catch (Exception $e){
  50. $this->error = $e->getMessage();
  51. $this->rollback();
  52. return false;
  53. }
  54. }
  55. public function lists($workerId,$page,$size){
  56. $start = ($page - 1) * $size;
  57. $map[] = ['worker_id','=',$workerId];
  58. $list = $this->where($map)
  59. ->limit($start,$size)
  60. ->order('id','desc')
  61. ->select()
  62. ->toArray();
  63. foreach ($list as $k=>$v){
  64. $list[$k]['date'] = date('Y-m-d',strtotime($v['create_time']));
  65. $list[$k]['time'] = date('H:i',strtotime($v['create_time']));
  66. }
  67. return $list;
  68. }
  69. }