package com.zcloud.basic.info.web;


import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.system.client.dept.facade.DeptFacade;
import com.zcloud.basic.info.api.DepartmentServiceI;
import com.zcloud.basic.info.dto.*;
import com.zcloud.basic.info.dto.clientobject.DepartmentCO;
import com.zcloud.basic.info.dto.clientobject.DepartmentInfoCO;
import com.zcloud.basic.info.dto.clientobject.DepartmentTreeInfoCO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * web-adapter
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:03:19
 */
@Api(tags = "部门管理")
@RequestMapping("/${application.gateway}/department")
@RestController
@AllArgsConstructor
public class DepartmentController {
    private final DepartmentServiceI departmentService;

    @DubboReference
    private DeptFacade deptFacade;

    @ApiOperation("新增部门")
    @PostMapping("/save")
    public SingleResponse<DepartmentCO> add(@Validated @RequestBody DepartmentAddCmd cmd) {
        return departmentService.add(cmd);
    }

    @ApiOperation("查询部门分页")
    @PostMapping("/list")
    @PreAuthorize("@pms.hasAnyPermission('jgzzjg','zzjgqyd','bmglxgf','zzjg')")
    public PageResponse<DepartmentCO> page(@Validated @RequestBody DepartmentPageQry qry) {
        return departmentService.listPage(qry);
    }

    @ApiOperation("部门详情")
    @PostMapping("/info/{id}")
    public SingleResponse<DepartmentInfoCO> getInfoById(@PathVariable("id") Long id) {
        return SingleResponse.of(departmentService.info(id));
    }

    @ApiOperation("部门删除")
    @PostMapping("/remove/{id}")
    public Response remove(@PathVariable("id") Long id) {
        departmentService.remove(id);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("删除多个")
    @PostMapping("/ids")
    public Response removeBatch(@RequestParam Long[] ids) {
        departmentService.removeBatch(ids);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("部门修改")
    @PostMapping("/edit")
    public SingleResponse edit(@Validated @RequestBody DepartmentUpdateCmd departmentUpdateCmd) {
        departmentService.edit(departmentUpdateCmd);
        return SingleResponse.buildSuccess();
    }
    @ApiOperation("企业下部门树状所有数据")
    @PostMapping("/listTree")
    public MultiResponse<DepartmentTreeInfoCO> listTree(@Validated @RequestBody DepartmentTreeQry qry) {
        return MultiResponse.of(departmentService.listTree(qry));
    }

    @ApiOperation("企业下部门树状所有数据,免token")
    @PostMapping("/listTreeNopermission")
    public MultiResponse<DepartmentTreeInfoCO> listTreeNopermission(@Validated @RequestBody DepartmentTreeQry qry) {
        return MultiResponse.of(departmentService.listTreeNopermission(qry));
    }

    @ApiOperation("所有企业下部门树状所有数据")
    @PostMapping("/listAllTree")
    public MultiResponse<DepartmentTreeInfoCO> listAllTree() {
        return MultiResponse.of(departmentService.listAllTree());
    }

    @ApiOperation("所有企业下部门树状所有数据根据企业类型")
    @PostMapping("/listAllTreeByCorpType")
    public MultiResponse<DepartmentTreeInfoCO> listAllTreeByCorpType( @RequestBody DepartmentTreeQry qry) {
        return MultiResponse.of(departmentService.listAllTreeByCorpType(qry));
    }


}

