ItemVariableController.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ItemVariableController extends BaseController {
  5. //保存
  6. public function save(){
  7. $item_id = I("item_id/d");
  8. $env_id = I("env_id/d");
  9. $var_name = I("var_name");
  10. $var_value = I("var_value");
  11. $login_user = $this->checkLogin();
  12. $uid = $login_user['uid'] ;
  13. if(!$this->checkItemEdit($uid , $item_id)){
  14. $this->sendError(10303);
  15. return ;
  16. }
  17. $id = 0 ;
  18. $res = D("ItemVariable")->where(" item_id = '{$item_id}' and env_id = '{$env_id}' and var_name = '%s' " ,array($var_name) )->find() ;
  19. if($res){
  20. $id = $res['id'] ;
  21. D("ItemVariable")->where(" id = '{$id}' ")->save(array("var_value"=>$var_value));
  22. }else{
  23. $data = array() ;
  24. $data['var_name'] = $var_name ;
  25. $data['uid'] = $uid ;
  26. $data['var_value'] = $var_value ;
  27. $data['item_id'] = $item_id ;
  28. $data['env_id'] = $env_id ;
  29. $data['addtime'] = time() ;
  30. $id = D("ItemVariable")->add($data);
  31. }
  32. if (!$id) {
  33. $this->sendError(10101);
  34. }else{
  35. $this->sendResult($id);
  36. }
  37. }
  38. //获取列表
  39. public function getList(){
  40. $item_id = I("item_id/d");
  41. $env_id = I("env_id/d");
  42. $login_user = $this->checkLogin();
  43. $uid = $login_user['uid'] ;
  44. if(!$this->checkItemEdit($uid , $item_id)){
  45. $this->sendError(10303);
  46. return ;
  47. }
  48. if ($item_id > 0 ) {
  49. $where = "item_id = '$item_id'";
  50. if($env_id){
  51. $where .= " and env_id = '$env_id'";
  52. }
  53. $ret = D("ItemVariable")->where($where)->order(" addtime asc ")->select();
  54. }
  55. if ($ret) {
  56. foreach ($ret as $key => &$value) {
  57. $value['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
  58. }
  59. }
  60. $this->sendResult($ret);
  61. }
  62. //删除
  63. public function delete(){
  64. $item_id = I("item_id/d");
  65. $id = I("id/d");
  66. $login_user = $this->checkLogin();
  67. $uid = $login_user['uid'] ;
  68. if(!$this->checkItemEdit($uid , $item_id)){
  69. $this->sendError(10303);
  70. return ;
  71. }
  72. $ret = D("ItemVariable")->where(" item_id = '%d' and id = '%d' ",array($item_id,$id))->delete();
  73. if ($ret) {
  74. $this->sendResult($ret);
  75. }else{
  76. $this->sendError(10101);
  77. }
  78. }
  79. //根据name删除
  80. public function deleteByName(){
  81. $item_id = I("item_id/d");
  82. $env_id = I("env_id/d");
  83. $var_name = I("var_name");
  84. $login_user = $this->checkLogin();
  85. $uid = $login_user['uid'] ;
  86. if(!$this->checkItemEdit($uid , $item_id)){
  87. $this->sendError(10303);
  88. return ;
  89. }
  90. $ret = D("ItemVariable")->where(" item_id = '%d' and env_id = '%d' and var_name = '%s' ",array($item_id,$env_id,$var_name))->delete();
  91. if ($ret) {
  92. $this->sendResult($ret);
  93. }else{
  94. $this->sendError(10101);
  95. }
  96. }
  97. }