You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 line
4.8 KiB

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