package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.service.PrintCommonService;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.RoleTypeEnum;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.service.TeachcloudCommonService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Set;

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

    @Resource
    private SysOrgService sysOrgService;

    @Resource
    private PrintCommonService printCommonService;
    @Resource
    private TeachcloudCommonService teachcloudCommonService;

    /**
     * 查询机构树
     *
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "specialPrivilege", required = false) RoleTypeEnum specialPrivilege,
                       @RequestParam(value = "withoutPrintingRoom", required = false) boolean withoutPrintingRoom,
                       @ApiParam(value = "科目编码") @RequestParam(required = false) String courseCode,
                       @ApiParam(value = "父机构ID") @RequestParam(required = false) Long orgId) {
        Set<Long> longSet = teachcloudCommonService.listSubOrgIds(orgId);
        return ResultUtil.ok(sysOrgService.listOrgTree(specialPrivilege, withoutPrintingRoom, courseCode, longSet));
    }

    /**
     * 新增/修改
     *
     * @param org
     * @return
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.SAVE, detail = "新增/修改操作，机构ID：{{org.id}}，机构名称：{{org.name}}")
    public Result save(@Valid @RequestBody SysOrg org, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(sysOrgService.saveOrg(org, sysUser));
    }

    /**
     * 启用/禁用
     *
     * @param org
     * @return
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.ENABLE, detail = "启用/禁用操作，机构ID：{{org.id}}，启用/禁用状态：{{org.enable}}")
    public Result enable(@RequestBody SysOrg org) {
        return ResultUtil.ok(printCommonService.enable(org));
    }

    /**
     * 删除
     *
     * @param org
     * @return
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @OperationLogDetail(operationType = OperationTypeEnum.DELETE, detail = "删除操作，机构ID：{{org.id}}")
    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(required = false) String orgType) {
        return ResultUtil.ok(sysOrgService.findDeepByOrgIdAndType(orgType));
    }

    @ApiOperation(value = "机构管理-批量导入")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    @OperationLogDetail(operationType = OperationTypeEnum.IMPORT)
    public Result sysOrgImportAsync(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file) throws Exception {
        sysOrgService.executeImportSysOrgLogic(file);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "机构管理-导出")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    @OperationLogDetail(operationType = OperationTypeEnum.EXPORT)
    public void export(HttpServletResponse response) throws Exception {
        sysOrgService.exportOrgData(response, false);
    }
}

