package com.boman.system.controller;

import com.boman.common.core.utils.SecurityUtils;
import com.boman.common.core.web.controller.BaseController;
import com.boman.common.log.annotation.Log;
import com.boman.common.log.enums.BusinessType;
import com.boman.common.security.annotation.PreAuthorize;
import com.boman.domain.BomanGroupUser;
import com.boman.domain.SysUser;
import com.boman.domain.constant.UserConstants;
import com.boman.domain.dto.AjaxResult;
import com.boman.domain.BomanGroup;
import com.boman.system.service.IBomanGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 自定义组
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/group")
public class BomanGroupController extends BaseController
{
    @Autowired
    private IBomanGroupService bomanGroupService;

    /**
     * 获取自定义组
     */
//    @PreAuthorize(hasPermi = "system:group:list")
    @GetMapping("/list")
    public AjaxResult list(BomanGroup bomanGroup)
    {
        List<BomanGroup> bomanGroups = bomanGroupService.selectGroupList(bomanGroup);
        return AjaxResult.success(bomanGroups);
    }


    /**
     * 根据组织编号获取详细信息
     */
//    @PreAuthorize(hasPermi = "system:group:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return AjaxResult.success(bomanGroupService.selectGroupById(id));
    }

    /**
     * 新增组织
     */
//    @PreAuthorize(hasPermi = "system:group:add")
    @Log(title = "组织管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BomanGroup bomanGroup)
    {
        if (UserConstants.NOT_UNIQUE.equals(bomanGroupService.checkGroupNameUnique(bomanGroup)))
        {
            return AjaxResult.error("新增组织'" + bomanGroup.getGroupName() + "'失败，组织名称已存在");
        }
        bomanGroup.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bomanGroupService.insertGroup(bomanGroup));
    }

    /**
     * 修改组织
     */
//    @PreAuthorize(hasPermi = "system:group:edit")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody BomanGroup bomanGroup)
    {
        if (UserConstants.NOT_UNIQUE.equals(bomanGroupService.checkGroupNameUnique(bomanGroup)))
        {
            return AjaxResult.error("修改组织'" + bomanGroup.getGroupName() + "'失败，组织名称已存在");
        }
        bomanGroup.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(bomanGroupService.updateGroup(bomanGroup));
    }

    /**
     * 删除组织
     */
//    @PreAuthorize(hasPermi = "system:group:remove")
    @Log(title = "组织管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if (bomanGroupService.checkGroupExistUser(ids))
        {
            return AjaxResult.error("组织存在用户,不允许删除");
        }
        return toAjax(bomanGroupService.deleteGroupId(ids));
    }

    /**
     * 给组添加用户/修改组中的用户
     * @param groupId
     * @param userIds
     * @return
     */
    @GetMapping("/addGroupUser/{groupId}/{userIds}")
    public AjaxResult addGroupUser(@PathVariable Long groupId,@PathVariable Long[] userIds){
        return bomanGroupService.insertUserGroup(groupId,userIds);
    }

    /**
     * 给组删除用户
     * @param groupId
     * @param userIds
     * @return
     */
    @GetMapping("/deleteGroupUser/{groupId}/{userIds}")
    public AjaxResult deleteGroupUser(@PathVariable Long groupId,@PathVariable long[] userIds){
        return bomanGroupService.deleteGroupUser(groupId,userIds);
    }

    /**
     * 根据组织编号获取对应组织所有用户的用户id,用户名,部门名称,角色名
     */
//    @PreAuthorize(hasPermi = "system:role:user")
    @GetMapping(value = "/user/{groupId}")
    public AjaxResult getUserInfo(@PathVariable Long groupId)
    {
        List<SysUser> sysUsers = bomanGroupService.selectUserByGroupId(groupId);
        return AjaxResult.success(sysUsers);
    }


}
