UserAnnualLog.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use app\Request;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Model;
  8. class UserAnnualLog extends Model
  9. {
  10. public function add(){
  11. $data = [
  12. 'user_id' => input('uid/d',0),
  13. 'create_user_id' => input('userId/d',0),
  14. 'annual_leave' => input('annualLeave/f',0),
  15. 'org_id' => input('orgId/d',0),
  16. 'remark' => input('remark','','trim'),
  17. 'create_time' => date('Y-m-d H:i:s')
  18. ];
  19. $result = validate('UserAnnualLog')->check($data,[],'');
  20. if(true !== $result){
  21. HelpHander::error(validate('UserAnnualLog')->getError());
  22. }
  23. Db::startTrans();
  24. try{
  25. $ret = $this->allowField(true)->save($data);
  26. if(!$ret){
  27. \exception('操作失败');
  28. }
  29. $res = Db::name('user_info')->where('user_id',$data['user_id'])->setField('annual_leave',$data['annual_leave']);
  30. if(!$res){
  31. \exception('操作失败');
  32. }
  33. Db::commit();
  34. }catch (Exception $e){
  35. Db::rollback();
  36. HelpHander::error('操作失败'.$e->getMessage());
  37. }
  38. return true;
  39. }
  40. public function lists($page,$size,$orgId){
  41. $map[] = ['org_id','=',$orgId];
  42. $lists = Db::name('user_annual_log')
  43. ->where($map)
  44. ->page($page,$size)
  45. ->order('id desc')
  46. ->select();
  47. $lists = $lists?$lists:[];
  48. foreach ($lists as $k=>$v){
  49. $lists[$k]['user_name'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  50. $lists[$k]['create_user_name'] = Db::name('user_info')->where('user_id',$v['create_user_id'])->value('name');
  51. }
  52. $total = Db::name('user_annual_log')->where($map)->count();
  53. $data = [
  54. 'total' => $total,
  55. 'list' => $lists
  56. ];
  57. return $data;
  58. }
  59. }