package com.kiisoo.ic.department.controller;

import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.department.entity.DepartmentDO;
import com.kiisoo.ic.department.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 部门管理controller
 * @Auther: yechenhao
 * @Date: 2020/4/7 0002 10:06
 * @Version: v1
 */
@Controller
@RequestMapping("/department")
@Slf4j
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询部门
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listDepartment(){
        try {
            List<DepartmentDO> departments = departmentService.listDepartment();
            return data(departments);
        }catch (Exception e){
            log.error("添加部门失败",e);
            return fail();
        }
    }

    /**
     * 增加部门
     * @param departmentDO 部门实体类
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addDepartment(@RequestBody DepartmentDO departmentDO){
        try {
            Boolean hasAdd = departmentService.addDepartment(departmentDO);
            return data(hasAdd);
        }catch (Exception e){
            log.error("添加部门失败",e);
            return fail();
        }
    }

    /**
     * 修改部门
     * @param departmentDO 部门实体类
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateDepartment(@RequestBody DepartmentDO departmentDO){
        try {
            Boolean hasUpdate = departmentService.updateDepartment(departmentDO);
            return data(hasUpdate);
        }catch (Exception e){
            log.error("修改部门失败",e);
            return fail();
        }
    }

    /**
     * 删除部门
     * @param departmentId 部门实体id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteDepartment(@RequestParam("departmentId") Long departmentId){
        try {
            Boolean hasDel = departmentService.deleteDepartment(departmentId);
            return data(hasDel);
        }catch (Exception e){
            log.error("删除部门失败",e);
            return fail();
        }
    }

    @RequestMapping(value = "sync",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> syncDepartment(){
        try {
            Boolean hasSync = departmentService.syncDepartment();
            return data(hasSync);
        }catch (Exception e){
            log.error("添加部门失败",e);
            return fail();
        }
    }
}
