/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.base.api.entity.User;
import com.tofly.base.entity.Department;
import com.tofly.base.service.DepartmentService;
import com.tofly.base.service.UserService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.update;

/**
 * 部门表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@RestController
@AllArgsConstructor
@RequestMapping("/department")
@Api(tags="部门管理")
public class DepartmentController {

    private final  DepartmentService departmentService;

    private final UserService userService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param department 部门表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getDepartmentPage(Page page, @ModelAttribute Department department) {
      return ResultRespone.success(departmentService.page(page, Wrappers.query(department)));
    }

    /**
     * 部门列表
     * @param department 部门表
     * @return
     */
    @GetMapping("/list" )
    @ApiOperation(tags = {"用户管理","部门管理"},value = "部门列表")
    public ResultRespone getDepartmentList(@ModelAttribute Department department) {
        return ResultRespone.success(departmentService.list(Wrappers.query(department)));
    }


    /**
     * 通过id查询部门表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(departmentService.getById(id));
    }

    /**
     * 新增部门表
     * @param department 部门表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增部门表" )
    @ApiOperation(value = "新增部门表")
    @PostMapping
    public ResultRespone save(@RequestBody Department department) {
      return ResultRespone.success(departmentService.save(department));
    }

    /**
     * 修改部门表
     * @param department 部门表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改部门表" )
    @ApiOperation(value = "修改部门表")
    @PutMapping
    public ResultRespone updateById(@RequestBody Department department) {
      return ResultRespone.success(departmentService.updateById(department));
    }

    /**
     * 通过id删除部门表
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除部门表" )
    @ApiOperation(value = "通过ID删除部门表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
        //非传统意义删除，修改禁用标志
        Department department=new Department();
        department.setId(id);
        department.setStatusFlag("0");

      return ResultRespone.success(update(department));
    }

    /**
     * 通过id删除单位
     * @param ids id数组
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除单位" )
    @ApiOperation(value = "通过ID删除单位")
    @DeleteMapping("/removeByIds" )
    public ResultRespone removeByIds(@ApiParam(value = "通过ID拼接的字符串,以逗号隔开.如,'1,2,3,4,5,6,7",name = "ids",required = true) String ids) {
        String[] idss=ids.split(",");
        List<Long> list=new ArrayList();
        boolean flag=true;
        StringBuffer sb=new StringBuffer();
        for(String id:idss){
            //查询是否存在子级部门
           List<Department> departmentList=departmentService.list(Wrappers.<Department>lambdaQuery()
           .eq(Department::getParentId,Long.valueOf(id)));

           if(departmentList.size()>0){
               flag=false;
               sb.append("id:").append(id).append(",对应子级单位还存在").append(departmentList.size())
                       .append("条数据;");
               break;

           }

           List<User> userList = userService.list(Wrappers.<User>lambdaQuery()
                   .eq(User::getDepartmentId,Long.valueOf(id)));
            if(userList.size()>0){
                flag=false;
                sb.append("id:").append(id).append(",对应用户还存在").append(departmentList.size())
                        .append("条数据;");
                break;

            }
            list.add(Long.valueOf(id));
        }
        if(flag){
            return ResultRespone.success(departmentService.removeByIds(list));
        }else {
            return ResultRespone.failed("不能删除："+sb.toString());
        }
    }

    /**
     * 通过单位ID查询资源列表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/listSourceById" )
    @ApiOperation(value = "通过单位ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone listSourceById(Long id) {
        return ResultRespone.success(departmentService.listSourceByDept(id));
    }



    /**
     * 分页查询
     * @return
     */
    @GetMapping("/subList" )
    @ApiOperation(value = "查询部门所有下属部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "returnSelf", value = "是否包括自己", required = false, dataType = "String")
    })
    public ResultRespone getDepartmentPage(@RequestParam("id") Long id,@RequestParam(name="returnSelf",required = false) String returnSelf) {

        return ResultRespone.success(departmentService.subList(id,returnSelf));
    }

}
