package com.qmth.paper.library.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.business.bean.result.EditResult;
import com.qmth.paper.library.business.service.PaperLibraryCommonService;
import com.qmth.paper.library.business.templete.execute.AsyncSysOrgImportService;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.SysOrg;
import com.qmth.paper.library.common.entity.SysUser;
import com.qmth.paper.library.common.entity.TBTask;
import com.qmth.paper.library.common.enums.TaskTypeEnum;
import com.qmth.paper.library.common.service.SysOrgService;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import com.qmth.paper.library.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.validation.Valid;
import java.util.Map;
import java.util.Objects;

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

    @Resource
    private SysOrgService sysOrgService;

    @Resource
    private PaperLibraryCommonService paperLibraryCommonService;

    @Resource
    private AsyncSysOrgImportService asyncSysOrgImportService;

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

    /**
     * 新增/修改
     *
     * @param org 机构对象
     */
    @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 机构对象
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result enable(@RequestBody SysOrg org) {
        return ResultUtil.ok(paperLibraryCommonService.enable(org));
    }

    /**
     * 删除
     *
     * @param ids ids
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestParam String ids) {
        return ResultUtil.ok(paperLibraryCommonService.sysOrgRemove(ids));
    }

    /**
     * 根据机构类型查询机构
     *
     * @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)})
    public Result sysOrgImportAsync(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file) throws Exception {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        Map<String, Object> map = paperLibraryCommonService.saveTask(file, TaskTypeEnum.ORG_IMPORT);
        map.put(SystemConstant.SYS_USER, sysUser);

        asyncSysOrgImportService.importTask(map);
        TBTask tbTask = Objects.nonNull(map.get(SystemConstant.TASK)) ? (TBTask) map.get(SystemConstant.TASK) : null;
        return Objects.nonNull(tbTask) ? ResultUtil.ok(tbTask.getId()) : ResultUtil.error("创建任务失败");
    }
}

