AttendanceLeaveType.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class AttendanceLeaveType extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'name' => input('name','','trim'),
  12. 'org_id' => input('orgId/d',0),
  13. 'limit' => input('limit/d',0),
  14. 'enable' => input('enable/d',0),
  15. 'type' => input('type/d',0),
  16. ];
  17. $result = validate('AttendanceLeaveType')->check($data,[],'');
  18. if(true !== $result){
  19. HelpHander::error(validate('AttendanceLeaveType')->getError());
  20. }
  21. $id = $data['id'];
  22. unset($data['id']);
  23. if($id > 0){
  24. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  25. }else{
  26. $ret = $this->allowField(true)->save($data);
  27. }
  28. if(!$ret){
  29. HelpHander::error('操作失败');
  30. }
  31. return true;
  32. }
  33. public function info($id){
  34. $info = $this->where('id',$id)->where('del',0)->find();
  35. if(!$info){
  36. HelpHander::error('数据不存在');
  37. }
  38. return $info->toArray();
  39. }
  40. public function lists($enable,$userId,$orgId){
  41. $map[] = ['del','=',0];
  42. $map[] = ['org_id','=',$orgId];
  43. if($enable != ''){
  44. $map[] = ['enable','=',$enable];
  45. }
  46. $lists = $this
  47. ->where($map)
  48. ->order('id asc')
  49. ->select();
  50. $lists = $lists?$lists->toArray():[];
  51. $startTime = date('Y').'-00-01 00:00:00';
  52. foreach ($lists as $k=>$v){
  53. $usedays = Db::name('attendance_leave')
  54. ->where('user_id',$userId)
  55. ->where('leave_type',$v['id'])
  56. ->where('start_time','>=',$startTime)
  57. ->where('status',1)
  58. ->sum('cur_days');
  59. $lists[$k]['useDay'] = $usedays;
  60. $lists[$k]['lastDay'] = -1;
  61. if($v['id'] == 3){ // 年假
  62. $annual_leave = Db::name('user_info')->where('user_id',$userId)->value('annual_leave');
  63. $lists[$k]['lastDay'] = $annual_leave?round($annual_leave,1):0;
  64. }
  65. }
  66. return $lists;
  67. }
  68. public function del($id){
  69. $ret = $this->where('id',$id)->setField('del',1);
  70. if(!$ret){
  71. HelpHander::error('删除失败');
  72. }
  73. return true;
  74. }
  75. public function changeStatus($id,$enable){
  76. $ret = $this->where('id',$id)->setField('enable',$enable);
  77. if(!$ret){
  78. HelpHander::error('操作失败');
  79. }
  80. return true;
  81. }
  82. }