package com.qmth.teachcloud.report.api;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.bean.dto.OrgDto;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 学校组织架构 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "组织架构Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.sys}/org")
public class SysOrgController {

    @Resource
    SysOrgService sysOrgService;

    /**
     * 查询机构树
     *
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list() {
        List<OrgDto> orgDtoList = sysOrgService.listOrgTree(null);
        return ResultUtil.ok(orgDtoList);
    }

    /**
     * 新增/修改
     *
     * @param org
     * @return
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody SysOrg org) {
        return ResultUtil.ok(sysOrgService.saveReportOrg(org));
    }

    /**
     * 启用/禁用
     *
     * @param org
     * @return
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result enable(@RequestBody SysOrg org) {
        UpdateWrapper<SysOrg> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(SysOrg::getEnable, org.getEnable()).eq(SysOrg::getId, org.getId());
        return ResultUtil.ok(sysOrgService.update(updateWrapper));
    }

    /**
     * 删除
     *
     * @param org
     * @return
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestBody SysOrg org) {
        return ResultUtil.ok(sysOrgService.removeById(org.getId()));
    }

}

