TeamItemController.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. /*
  5. 团队和项目的绑定关系
  6. */
  7. class TeamItemController extends BaseController {
  8. //添加和编辑
  9. public function save(){
  10. $login_user = $this->checkLogin();
  11. $uid = $login_user['uid'] ;
  12. $item_id = I("item_id");
  13. $team_id = I("team_id/d");
  14. $item_id = \SQLite3::escapeString($item_id) ;
  15. $teamInfo = D("Team")->where(" id = '$team_id' and uid = '$login_user[uid]' ")->find();
  16. if (!$teamInfo) {
  17. $this->sendError(10209,"无此团队或者你无管理此团队的权限");
  18. return ;
  19. }
  20. $item_id_array = explode(",", $item_id);
  21. foreach ($item_id_array as $key => $value) {
  22. $item_id = intval($value) ;
  23. if(!$this->checkItemManage($uid , $item_id)){
  24. $this->sendError(10303);
  25. return ;
  26. }
  27. if (D("TeamItem")->where(" team_id = '$team_id' and item_id = '$item_id' ")->find()) {
  28. continue ; //如果该项目已经加入团队了,则结束当前一次循环。
  29. }
  30. $data = array() ;
  31. $data['item_id'] = $item_id ;
  32. $data['team_id'] = $team_id ;
  33. $data['addtime'] = time() ;
  34. $id = D("TeamItem")->add($data);
  35. //获取该团队的所有成员并加入项目
  36. $teamMembers = D("TeamMember")->where(" team_id = '$team_id' ")->select() ;
  37. if ($teamMembers) {
  38. foreach ($teamMembers as $key => $value) {
  39. $data= array(
  40. "team_id"=>$team_id,
  41. "member_uid"=>$value['member_uid'],
  42. "member_username"=>$value['member_username'],
  43. "item_id"=>$item_id,
  44. "member_group_id"=>1, //默认添加的权限为1,即编辑权限
  45. "addtime"=>time()
  46. );
  47. D("TeamItemMember")->add($data);
  48. }
  49. }
  50. }
  51. $return = D("TeamItem")->where(" id = '$id' ")->find();
  52. if (!$return) {
  53. $return['error_code'] = 10103 ;
  54. $return['error_message'] = 'request fail' ;
  55. }
  56. $this->sendResult($return);
  57. }
  58. //根据项目来获取其绑定的团队列表
  59. public function getList(){
  60. $login_user = $this->checkLogin();
  61. $uid = $login_user['uid'] ;
  62. $item_id = I("item_id/d");
  63. if(!$this->checkItemManage($uid , $item_id)){
  64. $this->sendError(10303);
  65. return ;
  66. }
  67. $sql = "select team.*,team_item.team_id , team_item.id as id from team left join team_item on team.id = team_item.team_id where team_item.item_id = '$item_id' ";
  68. $ret = D("TeamItem")->query($sql);
  69. if ($ret) {
  70. foreach ($ret as $key => &$value) {
  71. $value['addtime'] = date("Y-m-d H:i:s" , $value['addtime']);
  72. }
  73. $this->sendResult($ret);
  74. }else{
  75. $this->sendResult(array());
  76. }
  77. }
  78. //根据团队来获取项目列表
  79. public function getListByTeam(){
  80. $login_user = $this->checkLogin();
  81. $uid = $login_user['uid'] ;
  82. $team_id = I("team_id/d");
  83. $teamInfo = D("Team")->where(" id = '$team_id' and uid = '$login_user[uid]' ")->find();
  84. if (!$teamInfo) {
  85. $this->sendError(10209,"无此团队或者你无管理此团队的权限");
  86. return ;
  87. }
  88. $sql = "select item.*,team_item.team_id , team_item.id as id from item left join team_item on item.item_id = team_item.item_id where team_item.team_id = '$team_id' and item.is_del = 0 ";
  89. $ret = D("Item")->query($sql);
  90. if ($ret) {
  91. foreach ($ret as $key => &$value) {
  92. $value['addtime'] = date("Y-m-d H:i:s" , $value['addtime']);
  93. }
  94. $this->sendResult($ret);
  95. }else{
  96. $this->sendResult(array());
  97. }
  98. }
  99. //删除
  100. public function delete(){
  101. $login_user = $this->checkLogin();
  102. $uid = $login_user['uid'] ;
  103. $id = I("id/d")? I("id/d") : 0;
  104. $teamItemInfo = D("TeamItem")->where(" id = '$id' ")->find();
  105. $item_id = $teamItemInfo['item_id'] ;
  106. $team_id = $teamItemInfo['team_id'] ;
  107. if(!$this->checkItemManage($uid , $item_id)){
  108. $this->sendError(10303);
  109. return ;
  110. }
  111. $ret = D("TeamItemMember")->where(" item_id = '$item_id' and team_id = '$team_id' ")->delete();
  112. $ret = D("TeamItem")->where(" id = '$id' ")->delete();
  113. if ($ret) {
  114. $this->sendResult($ret);
  115. }else{
  116. $return['error_code'] = 10103 ;
  117. $return['error_message'] = 'request fail' ;
  118. $this->sendResult($return);
  119. }
  120. }
  121. }