package com.jwipc.sys.controller.pc;

import com.jwipc.dto.ZTreeDTO;
import com.jwipc.dto.ZTreeDataDTO;
import com.jwipc.page.PageView;
import com.jwipc.sys.dto.OrgAddOrEditDTO;
import com.jwipc.sys.logic.OrgLogic;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

@RequestMapping(MODULE_ROOT + "/org")
@RequiredArgsConstructor
@RestController
public class OrgRestController {
    private final OrgLogic orgLogic;

    /**
     * 查询节点树
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("treegrid")
    @ResponseBody
    public PageView findOrgByParentId(@Param("parentId") String parentId) throws Exception {
        return orgLogic.findForTreegridByParentId(parentId);
    }

    @GetMapping("tree")
    public ResponseEntity tree(Long userId, String searchName) {
        List<ZTreeDTO<ZTreeDataDTO>> list = orgLogic.getAllOrgTree(searchName, userId);
        return ResponseEntity.ok(list);
    }

    @PostMapping("saveOrUpdate")
    public ResponseEntity saveOrUpdate(OrgAddOrEditDTO orgAddOrEditDTO) {
        orgLogic.saveOrUpdate(orgAddOrEditDTO);
        return ResponseEntity.ok().build();
    }

    @PostMapping("remove")
    public ResponseEntity remove(Long id) {
        orgLogic.remove(id);
        return ResponseEntity.ok().build();
    }
}
