package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Dept;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 部门管理控制器
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/admin/dept")
@AllArgsConstructor
@Api(tags = "部门管理接口")
public class DeptController {

    private final DeptService deptService;

    /**
     * 获取部门列表
     *
     * @return 部门列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取部门列表", notes = "获取所有部门信息")
    public R<List<Dept>> list() {
        List<Dept> deptList = deptService.listAllDept();
        return R.SUCCESS(deptList);
    }

    /**
     * 分页获取部门列表
     *
     * @param page 分页参数
     * @param dept 查询条件
     * @return 部门分页数据
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页获取部门列表", notes = "分页获取部门信息")
    public R<IPage<Dept>> page(PageParam<Dept> page, Dept dept) {
        IPage<Dept> deptPage = deptService.page(page, null);
        return R.SUCCESS(deptPage);
    }

    /**
     * 根据ID获取部门详情
     *
     * @param deptId 部门ID
     * @return 部门详情
     */
    @GetMapping("/{deptId}")
    @ApiOperation(value = "根据ID获取部门详情", notes = "根据部门ID获取部门详细信息")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, dataType = "Long", paramType = "path")
    public R<Dept> info(@PathVariable("deptId") Long deptId) {
        Dept dept = deptService.getById(deptId);
        return R.SUCCESS(dept);
    }

    /**
     * 添加部门
     *
     * @param dept 部门信息
     * @return 添加结果
     */
    @PostMapping
    @ApiOperation(value = "添加部门", notes = "添加新的部门信息")
    public R<Boolean> save(@Valid @RequestBody Dept dept) {
        Boolean result = deptService.saveDept(dept);
        return R.SUCCESS(result);
    }

    /**
     * 修改部门
     *
     * @param dept 部门信息
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改部门", notes = "修改部门信息")
    public R<Boolean> update(@Valid @RequestBody Dept dept) {
        Boolean result = deptService.updateDept(dept);
        return R.SUCCESS(result);
    }



    /**
     * 删除部门
     *
     * @param deptId 部门ID
     * @return 删除结果
     */
    @DeleteMapping("/{deptId}")
    @ApiOperation(value = "删除部门", notes = "根据部门ID删除部门")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, dataType = "Long", paramType = "path")
    public R<Boolean> delete(@PathVariable("deptId") Long deptId) {
        Boolean result = deptService.deleteDept(deptId);
        return R.SUCCESS(result);
    }

    /**
     * 获取部门树结构
     *
     * @return 部门树结构
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获取部门树结构", notes = "获取部门树形结构数据")
    public R<List<Dept>> tree() {
        List<Dept> tree = deptService.treeDept();
        return R.SUCCESS(tree);
    }

    /**
     * 根据父级部门ID获取子部门列表
     *
     * @param parentId 父级部门ID
     * @return 子部门列表
     */
    @GetMapping("/listByParentId/{parentId}")
    @ApiOperation(value = "根据父级部门ID获取子部门列表", notes = "根据父级部门ID获取子部门列表")
    @ApiImplicitParam(name = "parentId", value = "父级部门ID", required = true, dataType = "Long", paramType = "path")
    public R<List<Dept>> listByParentId(@PathVariable("parentId") Long parentId) {
        List<Dept> deptList = deptService.listByParentId(parentId);
        return R.SUCCESS(deptList);
    }


}
