package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.service.PrintCommonService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.enums.OrgTypeEnum;
import com.qmth.teachcloud.common.enums.userPush.SpecialPrivilegeEnum;
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 io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

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

    @Autowired
    private SysOrgService sysOrgService;

    @Resource
    PrintCommonService printCommonService;

    /**
     * 查询机构树
     *
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "specialPrivilege", required = false) SpecialPrivilegeEnum specialPrivilege) {
        return ResultUtil.ok(sysOrgService.listOrgTree(specialPrivilege));
    }

    /**
     * 新增/修改
     *
     * @param org
     * @return
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@Valid @RequestBody SysOrg org, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(sysOrgService.saveOrg(org));
    }

    /**
     * 启用/禁用
     *
     * @param org
     * @return
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result enable(@RequestBody SysOrg org) {
        return ResultUtil.ok(printCommonService.enable(org));
    }

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

    /**
     * 根据机构类型查询机构
     *
     * @return 结果
     */
    @ApiOperation(value = "根据类型查询机构")
    @RequestMapping(value = "/find_by_type", method = RequestMethod.POST)
    public Result findByType(@ApiParam(value = "机构类型") @RequestParam OrgTypeEnum orgType,
                             @ApiParam(value = "某机构下的集合") @RequestParam(required = false) String orgId,
                             @ApiParam(value = "是否使用当前用户所在机构的数据权限") @RequestParam(required = false) boolean auth) {
        return ResultUtil.ok(sysOrgService.findDeepByOrgIdAndType(orgType, SystemConstant.convertIdToLong(orgId),auth));
    }

}

