AttendanceClass.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class AttendanceClass extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'name' => input('name','','trim'),
  13. 'content' => input('content','','trim'),
  14. 'org_id' => input('orgId/d',0),
  15. ];
  16. $result = validate('AttendanceClass')->check($data,[],'');
  17. if(true !== $result){
  18. HelpHander::error(validate('AttendanceClass')->getError());
  19. }
  20. $id = $data['id'];
  21. unset($data['id']);
  22. Db::startTrans();
  23. try{
  24. if($id > 0){
  25. $data['update_time'] = date('Y-m-d H:i:s');
  26. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  27. }else{
  28. $data['create_time'] = date('Y-m-d H:i:s');
  29. $ret = $this->allowField(true)->save($data);
  30. }
  31. if(!$ret){
  32. \exception('操作失败');
  33. }
  34. Db::commit();
  35. }catch (Exception $e){
  36. Db::rollback();
  37. HelpHander::error($e->getMessage());
  38. }
  39. return true;
  40. }
  41. public function info($id){
  42. $info = $this->where('id',$id)->find();
  43. if(!$info){
  44. HelpHander::error('数据不存在');
  45. }
  46. $data = $info->toArray();
  47. $data['content'] = json_decode($data['content'],true);
  48. return $data;
  49. }
  50. public function getNameById($id){
  51. $info = $this->where('id',$id)->find();
  52. if(!$info){
  53. HelpHander::error('数据不存在');
  54. }
  55. $data = $info->toArray();
  56. return $this->formatClassName($data);
  57. }
  58. public function formatClassName($info){
  59. $info['content'] = json_decode($info['content'],true);
  60. $names = [];
  61. foreach ($info['content']['dates'] as $k=>$v){
  62. $str = '';
  63. if ($v['snext'] == 1) {
  64. $str .= '次日'.$v['stime'];
  65. } else {
  66. $str .= $v['stime'];
  67. }
  68. $str .= '-';
  69. if ($v['enext'] === 1) {
  70. $str .= '次日'.$v['etime'];
  71. } else {
  72. $str .= $v['etime'];
  73. }
  74. $names[] = $str;
  75. }
  76. return $info['name'].' '.implode(',',$names);
  77. }
  78. public function lists($page,$size,$name,$orgId){
  79. $map[] = ['del','=',0];
  80. $map[] = ['org_id','=',$orgId];
  81. if($name != ''){
  82. $map[] = ['name','like','%'.$name.'%'];
  83. }
  84. $lists = $this
  85. ->where($map)
  86. ->page($page,$size)
  87. ->order('id desc')
  88. ->select();
  89. $lists = $lists?$lists->toArray():[];
  90. foreach ($lists as $k=>$v){
  91. $lists[$k]['content'] = json_decode($v['content'],true);
  92. }
  93. $total = $this->where($map)->count();
  94. $data = [
  95. 'total' => $total,
  96. 'list' => $lists
  97. ];
  98. return $data;
  99. }
  100. public function del($id){
  101. $ret = $this->where('id',$id)->setField('del',1);
  102. if(!$ret){
  103. HelpHander::error('删除失败');
  104. }
  105. return true;
  106. }
  107. public function all($orgId){
  108. $map[] = ['del','=',0];
  109. $map[] = ['org_id','=',$orgId];
  110. $lists = $this
  111. ->where($map)
  112. ->order('id desc')
  113. ->select();
  114. $lists = $lists?$lists->toArray():[];
  115. foreach ($lists as $k=>$v){
  116. $lists[$k]['content'] = json_decode($v['content'],true);
  117. }
  118. return $lists;
  119. }
  120. }