package com.codvision.auth.controller.manager;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codvision.auth.entity.Org;
import com.codvision.auth.entity.dto.SysOrgQueryDTO;
import com.codvision.auth.entity.excel.OrgExcel;
import com.codvision.auth.entity.vo.OrgVO;
import com.codvision.auth.entity.vo.SysUserVO;
import com.codvision.auth.service.IOrgService;
import com.codvision.auth.support.UserContext;
import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.base.support.EasyExcelUtil;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.annotation.ApiSecret;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.util.PageUtil;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 组织信息 管理模块
 *
 * @author lingee
 * @date 2023-11-10
 */
@RestController
@RequestMapping("/v1/manager/org")
@Api(tags = "平台管理端-组织管理模块")
public class ManagerOrgController {

    @Autowired
    private IOrgService orgService;

    @ApiOperation(value = "获取组织信息分页列表", notes = "获取组织信息分页列表接口")
    @PostMapping("/all")
    public ResponseEntity<IPage<OrgVO>> findAll(@RequestParam(required = false) String root,
                                                @RequestParam(required = false) String type,
                                                @RequestParam(required = false) String keyword,
                                                @ApiParam(value = "组织属性id") @RequestParam(value = "orgPropertyId", required = false) Integer orgPropertyId,
                                                @ApiParam(value = "最大level") @RequestParam(required = false) Integer maxLevel,
                                                @ApiParam(value = "是否忽略wkt") @RequestParam(required = false, defaultValue = "false") Boolean isIgnoreWkt,
                                                @ApiParam(value = "是否查询子组织") @RequestParam(required = false, defaultValue = "false") Boolean subQuery,
                                                @RequestParam(required = false, defaultValue = "false") Boolean isAll,
                                                @RequestParam(required = false, defaultValue = "1") int page,
                                                @RequestParam(required = false, defaultValue = "20") int size) {
        SysOrgQueryDTO queryDTO = new SysOrgQueryDTO(root, type, keyword, orgPropertyId, maxLevel, isIgnoreWkt, subQuery);
        IPage<OrgVO> pageResult;
        if (ObjectUtil.isNotEmpty(isAll) && isAll) {
            pageResult = new Page<>();
            List<OrgVO> list = orgService.findAll(queryDTO);
            pageResult.setRecords(list);
        } else {
            QueryPage<Org> queryPage = new QueryPage<>(page, size);
            pageResult = orgService.findAll(queryPage, queryDTO);
        }
        return new ResponseEntity<IPage<OrgVO>>().ok().data(pageResult);
    }

    @ApiOperation(value = "获取组织信息详情", notes = "获取组织信息详情接口")
    @PostMapping("/detail")
    public ResponseEntity<OrgVO> getOrgDetail(@ApiParam(value = "组织信息ID", required = true) @RequestParam Integer oid) {
        OrgVO vo = orgService.getDetail(oid);
        return new ResponseEntity<OrgVO>().ok().data(vo);
    }

    @ApiOperation(value = "新增组织信息", notes = "新增组织信息接口")
    @PostMapping("/add")
    @RequiresPermissions("sys_org_add")
    public ResponseEntity<OrgVO> addOrg(@Valid @RequestBody Org org) {
        OrgVO vo = orgService.addOrg(org);
        return new ResponseEntity<OrgVO>().ok().data(vo).message("添加成功");
    }

    @ApiOperation(value = "删除组织信息", notes = "删除组织信息接口")
    @PostMapping("/del")
    @RequiresPermissions("sys_org_del")
    public ResponseEntity<String> deleteById(@ApiParam(value = "组织信息ID", required = true) @RequestParam Integer oid) {
        orgService.logicDel(oid);
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "编辑组织信息", notes = "编辑组织信息接口")
    @PostMapping("/update")
    @RequiresPermissions("sys_org_edit")
    public ResponseEntity<OrgVO> updateOrg(@RequestBody Org org) {
        if (ObjectUtil.isEmpty(org) && ObjectUtil.isEmpty(org.getOid())) {
            throw new BusinessException(30002);
        }
        OrgVO vo = orgService.updateOrg(org);

        return new ResponseEntity<OrgVO>().ok().data(vo).message("编辑成功");
    }

    @PostMapping("/tree")
    @ApiOperation(value = "组织树状展示", notes = "组织树状展示接口")
    public ResponseEntity<OrgVO> getTree(@RequestParam(value = "root", required = false) Integer root) {
        return new ResponseEntity<OrgVO>().data(orgService.getTree(root));
    }

    @ApiSecret(ignore = true)
    @ApiOperation(value = "下载组织导入模板", notes = "下载组织导入模板接口")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        EasyExcelUtil.downloadTemplate("组织模板表", "组织", OrgExcel.class, response);
    }

    @ApiSecret(ignore = true)
    @ApiOperation(value = "导入组织", notes = "导入组织接口")
    @PostMapping("/import")
    public ResponseEntity<String> excelImport(@RequestPart("file") MultipartFile file) {
        return new ResponseEntity<String>().data(orgService.excelImport(file));
    }

    @ApiSecret(ignore = true)
    @ApiOperation(value = "组织导出", notes = "组织导出接口")
    @GetMapping("/export")
    public ResponseEntity<String> excelExport(@RequestParam(required = false) String keyword,
                                              @RequestParam(required = false) String root,
                                              @RequestParam(required = false) String type) {
        SysOrgQueryDTO queryDTO = new SysOrgQueryDTO();
        queryDTO.setKeyword(keyword);
        if (StrUtil.isNotBlank(root)) {
            queryDTO.setRoot(root);
        } else {
            SysUserVO user = UserContext.get();
            queryDTO.setRoot(String.valueOf(user.getActiveOid()));
        }
        queryDTO.setType(type);
        queryDTO.setSubQuery(true);

        orgService.excelExport(queryDTO);
        return new ResponseEntity<String>().ok();
    }
}
