package com.tuoheng.admin.controller;

import com.alibaba.fastjson.JSON;
import com.tuoheng.admin.request.dept.AddDeptRequest;
import com.tuoheng.admin.request.dept.EditDeptRequest;
import com.tuoheng.admin.request.dept.QueryDeptChildPageListRequest;
import com.tuoheng.admin.service.dept.IDeptService;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 部门前端控制器
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-16
 */
@Slf4j
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    /**
     * 查询部门树形列表
     */
    @GetMapping("/list/tree")
    public JsonResult getListTree() {
//        log.info("进入获取部门列表接口");
        return deptService.getListTree();
    }

    /**
     * 根据当前租户获取部门列表
     */
    @GetMapping("/list")
    public JsonResult getListByTenantId(){
        return deptService.getListByTenantId();
    }

    /**
     * 查询当前部门树形列表
     */
    @GetMapping("/list/tree/{deptId}")
    public JsonResult getListTree(@PathVariable(value="deptId", required=false) String deptId) {
//        log.info("进入获取部门列表接口");
        return deptService.getListTreeByDeptId(deptId);
    }

    /**
     * 根据id查询子部门列表
     */
    @GetMapping(value= {"/child/list", "/child/list/{id}"})
    public JsonResult getChildList(@PathVariable(value="id", required=false) String id) {
//        log.info("进入获取子部门分页列表列表接口, id:{}", id);
        return deptService.getChildList(id);
    }

    /**
     * 获取部门详细信息
     */
    @GetMapping(value = "/{id}")
    public JsonResult getInfo(@PathVariable("id") String id) {
//        log.info("进入获取部门信息接口");
        return deptService.getDeptInfo(id);
    }

    /**
     * 新增部门
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody AddDeptRequest addDeptRequest) {
//        log.info("进入新增部门接口");
        return deptService.insert(addDeptRequest);
    }

    /**
     * 修改部门
     */
    @PutMapping("/edit")
    public JsonResult update(@RequestBody Map<String, Object> param) {
//        log.info("进入修改部门接口");
        EditDeptRequest oldEditDeptRequest = JSON.parseObject(JSON.toJSONString(param.get("oldEditDeptRequest")), EditDeptRequest.class);
        EditDeptRequest newEditDeptRequest = JSON.parseObject(JSON.toJSONString(param.get("newEditDeptRequest")), EditDeptRequest.class);
        return deptService.update(oldEditDeptRequest, newEditDeptRequest);
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/delete/{id}")
    public JsonResult delete(@PathVariable("id") String id) {
//        log.info("进入删除部门接口");
        return deptService.deleteById(id);
    }
}
