package com.cf.imes.module.system.controller.admin.organ;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.system.controller.admin.organ.vo.organ.OrganPageReqVO;
import com.cf.imes.module.system.controller.admin.organ.vo.organ.OrganRespVO;
import com.cf.imes.module.system.controller.admin.organ.vo.organ.OrganSaveReqVO;
import com.cf.imes.module.system.controller.admin.organ.vo.organ.OrganSimpleRespVO;
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
import com.cf.imes.module.system.service.organ.OrganService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 组织")
@RestController
@RequestMapping("/system/organ")
public class OrganController {

    @Resource
    private OrganService organService;

    @GetMapping("/get-id-by-name")
    @PermitAll
    @Operation(summary = "使用组织名，获得组织编号", description = "登录界面，根据用户的组织名，获得组织编号")
    @Parameter(name = "name", description = "组织名", required = true, example = "1024")
    public CommonResult<Long> getOrganIdByName(@RequestParam("name") String name) {
        OrganizationDO organizationDO = organService.getOrganByName(name);
        return success(organizationDO != null ? organizationDO.getId() : null);
    }

    @GetMapping("/get-by-website")
    @PermitAll
    @Operation(summary = "使用域名，获得组织信息", description = "登录界面，根据用户的域名，获得组织信息")
    @Parameter(name = "website", description = "域名", required = true, example = "www.cf.com")
    public CommonResult<OrganSimpleRespVO> getOrganByWebsite(@RequestParam("website") String website) {
        OrganizationDO organizationDO = organService.getOrganByWebsite(website);
        return success(BeanUtils.toBean(organizationDO, OrganSimpleRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建组织")
    @PreAuthorize("@ss.hasPermission('system:organ:create')")
    public CommonResult<Long> createOrgan(@Valid @RequestBody OrganSaveReqVO createReqVO) {
        return success(organService.createOrgan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织")
    @PreAuthorize("@ss.hasPermission('system:organ:update')")
    public CommonResult<Boolean> updateOrgan(@Valid @RequestBody OrganSaveReqVO updateReqVO) {
        organService.updateOrgan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:organ:delete')")
    public CommonResult<Boolean> deleteOrgan(@RequestParam("id") Long id) {
        organService.deleteOrgan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:organ:query')")
    public CommonResult<OrganRespVO> getOrgan(@RequestParam("id") Long id) {
        OrganizationDO organ = organService.getOrgan(id);
        return success(BeanUtils.toBean(organ, OrganRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织分页")
    @PreAuthorize("@ss.hasPermission('system:organ:query')")
    public CommonResult<PageResult<OrganRespVO>> getOrganPage(@Valid OrganPageReqVO pageVO) {
        PageResult<OrganizationDO> pageResult = organService.getOrganPage(pageVO);
        return success(BeanUtils.toBean(pageResult, OrganRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织 Excel")
    @PreAuthorize("@ss.hasPermission('system:organ:export')")
    @OperateLog(type = EXPORT)
    public void exportOrganExcel(@Valid OrganPageReqVO exportReqVO,
                                  HttpServletResponse response) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrganizationDO> list = organService.getOrganPage(exportReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组织.xls", "数据", OrganRespVO.class,
                BeanUtils.toBean(list, OrganRespVO.class));
    }

}
