package com.ruoyi.web.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.BaseDepartment;
import com.ruoyi.web.base.service.IBaseDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2026-02-02
 */
@RestController
@RequestMapping("/base-department")
@Api("部门管理")
public class BaseDepartmentController {

    @Autowired
    private IBaseDepartmentService baseDepartmentService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("部门管理添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BaseDepartment baseDepartment, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (baseDepartmentService.count(new QueryWrapper<BaseDepartment>().eq("org_id",loginOrgId).eq("department_num", baseDepartment.getDepartmentNum())) >0 ){
            throw new Exception("该编号已存在");
        }
        baseDepartment.setOrgId(loginOrgId);
        baseDepartment.setCreateBy(getUsername());
        return success(baseDepartmentService.save(baseDepartment));
    }

    @ApiOperation("部门管理修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BaseDepartment baseDepartment, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (baseDepartmentService.count(new QueryWrapper<BaseDepartment>().ne("id",baseDepartment.getId())
                .eq("org_id",loginOrgId).eq("department_num", baseDepartment.getDepartmentNum())) >0 ){
            throw new Exception("该编号已存在");
        }
        baseDepartment.setUpdateBy(getUsername());
        return success(baseDepartmentService.updateById(baseDepartment));
    }

    @ApiOperation("部门管理删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            baseDepartmentService.removeById(s);
        }
        return success();
    }

    @ApiOperation("部门管理列表")
    @PostMapping("/list")
    public AjaxResult listAll( HttpServletRequest request){
        return success(baseDepartmentService.list(new QueryWrapper<BaseDepartment>().eq("org_id",tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("部门管理分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,HttpServletRequest request
          ) {
        return success(baseDepartmentService.page(new Page<BaseDepartment>(pageNum,pageSize),new QueryWrapper<BaseDepartment>().eq("org_id",tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("部门管理详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        return success(baseDepartmentService.getById(id));
    }

}
