SalaryYear.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 SalaryYear extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'user_id' => input('userId/d',0),
  13. 'have' => input('have/d',0),
  14. 'year' => input('year','','trim'),
  15. 'salary' => input('salary/f',0),
  16. 'bonus' => input('bonus/f',0),
  17. 'other' => input('other/f',0),
  18. 'total' => input('total/f',0),
  19. 'givelectures' => input('givelectures/f',0),
  20. 'writing' => input('writing/f',0),
  21. 'consult' => input('consult/f',0),
  22. 'review' => input('review/f',0),
  23. 'painting' => input('painting/f',0),
  24. 'other2' => input('other2/f',0),
  25. 'total2' => input('total2/f',0),
  26. ];
  27. $result = validate('SalaryYear')->check($data,[],'');
  28. if(true !== $result){
  29. HelpHander::error(validate('SalaryYear')->getError());
  30. }
  31. $id = $data['id'];
  32. unset($data['id']);
  33. Db::startTrans();
  34. try{
  35. if($id > 0){
  36. $data['update_time'] = date('Y-m-d H:i:s');
  37. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  38. }else{
  39. Db::name('salary_year')->where('user_id',$data['user_id'])->update(['default'=>0]);
  40. $data['default'] = 1;
  41. $data['create_time'] = date('Y-m-d H:i:s');
  42. $ret = $this->allowField(true)->save($data);
  43. }
  44. if(!$ret){
  45. \exception('操作失败');
  46. }
  47. Db::commit();
  48. }catch (Exception $e){
  49. Db::rollback();
  50. }
  51. return true;
  52. }
  53. public function info($id){
  54. $info = $this->where('id',$id)->where('del',0)->find();
  55. if(!$info){
  56. HelpHander::error('数据不存在');
  57. }
  58. $data = $info->toArray();
  59. return $data;
  60. }
  61. public function lists($page,$size,$name,$year,$userId = 0){
  62. if($name != ''){
  63. $map[] = ['ui.name','like','%'.$name.'%'];
  64. }
  65. if($year != ''){
  66. $map[] = ['sy.year','=',$year];
  67. }
  68. if($userId > 0){
  69. $map[] = ['sy.user_id','=',$userId];
  70. }
  71. $map[] = ['del','=',0];
  72. $lists = Db::name('salary_year')
  73. ->alias('sy')
  74. ->join('user_info ui','ui.user_id = sy.user_id')
  75. ->where($map)
  76. ->page($page,$size)
  77. ->field('sy.*,ui.name')
  78. ->order('sy.id desc')
  79. ->select();
  80. $total = Db::name('salary_year')
  81. ->alias('sy')
  82. ->join('user_info ui','ui.user_id = sy.user_id')
  83. ->count();
  84. $data = [
  85. 'total' => $total,
  86. 'list' => $lists?$lists:[]
  87. ];
  88. return $data;
  89. }
  90. public function lists2($page,$size,$name,$year,$userId = 0){
  91. if($name != ''){
  92. $map[] = ['ui.name','like','%'.$name.'%'];
  93. }
  94. if($year != ''){
  95. $map[] = ['sy.year','=',$year];
  96. }
  97. if($userId > 0){
  98. $map[] = ['sy.user_id','=',$userId];
  99. }
  100. $map[] = ['del','=',0];
  101. $map[] = ['default','=',1];
  102. $lists = Db::name('salary_year')
  103. ->alias('sy')
  104. ->join('user_info ui','ui.user_id = sy.user_id')
  105. ->where($map)
  106. ->page($page,$size)
  107. ->field('sy.*,ui.name')
  108. ->order('sy.id desc')
  109. ->select();
  110. $total = Db::name('salary_year')
  111. ->alias('sy')
  112. ->join('user_info ui','ui.user_id = sy.user_id')
  113. ->count();
  114. $data = [
  115. 'total' => $total,
  116. 'list' => $lists?$lists:[]
  117. ];
  118. return $data;
  119. }
  120. public function del($id){
  121. $ret = Db::name('salary_year')->where('id',$id)->update(['del'=>1]);
  122. if(!$ret){
  123. HelpHander::error('删除失败');
  124. }
  125. return true;
  126. }
  127. public function export($name,$year){
  128. if($name != ''){
  129. $map[] = ['ui.name','like','%'.$name.'%'];
  130. }
  131. if($year != ''){
  132. $map[] = ['sy.year','=',$year];
  133. }
  134. $map[] = ['del','=',0];
  135. $map[] = ['default','=',1];
  136. $lists = Db::name('salary_year')
  137. ->alias('sy')
  138. ->join('user_info ui','ui.user_id = sy.user_id')
  139. ->where($map)
  140. ->field('sy.*,ui.name')
  141. ->order('sy.id desc')
  142. ->select();
  143. foreach ($lists as $k=>$v){
  144. if($v['have'] == 1){
  145. $lists[$k]['have_text'] = '无';
  146. }else{
  147. $lists[$k]['have_text'] = '有';
  148. }
  149. }
  150. $columns[] = ["title" => "姓名","key" => "name"];
  151. $columns[] = ["title" => "年份","key" => "year"];
  152. $columns[] = ["title" => "工资","key" => "salary"];
  153. $columns[] = ["title" => "奖金","key" => "bonus"];
  154. $columns[] = ["title" => "其他","key" => "other"];
  155. $columns[] = ["title" => "合计","key" => "total"];
  156. $columns[] = ["title" => "有无劳务所得","key" => "haveText"];
  157. $columns[] = ["title" => "讲学","key" => "givelectures"];
  158. $columns[] = ["title" => "写作","key" => "writing"];
  159. $columns[] = ["title" => "咨询","key" => "consult"];
  160. $columns[] = ["title" => "审稿","key" => "review"];
  161. $columns[] = ["title" => "书画","key" => "painting"];
  162. $columns[] = ["title" => "其他","key" => "other2"];
  163. $columns[] = ["title" => "合计","key" => "total2"];
  164. $columns[] = ["title" => "创建日期","key" => "createTime"];
  165. $data = [
  166. 'columns' => $columns,
  167. 'list' => $lists?$lists:[]
  168. ];
  169. return $data;
  170. }
  171. }