package com.mosty.common.core.business.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.exception.Asserts;
import com.mosty.common.base.util.IpUtil;
import com.mosty.common.core.business.entity.SysDept;
import com.mosty.common.core.business.entity.vo.*;
import com.mosty.common.core.business.service.SysDeptService;
import com.mosty.common.token.JwtSysUser;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author kevin
 * @since 2022-02-12
 */
@Slf4j
@SuppressWarnings("unused")
@RestController
@AllArgsConstructor
@RequestMapping("/sysDept")
@Api(tags = "部门信息接口")
public class SysDeptController {

    private final SysDeptService sysDeptService;

    @PostMapping("addSysDept")
    @JwtSysUser
    @ApiOperation(value = "添加部门", httpMethod = "POST", response = Boolean.class)
    @Log(title = "部门功能-添加部门", businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> addSysDept(@RequestBody @Valid AddDeptVO addDeptVO, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysDeptService.saveInfo(addDeptVO, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    @PostMapping("updateSysDept")
    @JwtSysUser
    @Log(title = "部门功能-修改部门", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改部门", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> updateSysDept(@RequestBody @Valid SysDeptVO sysDept, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysDeptService.updateInfo(sysDept, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    @Log(title = "部门功能-删除部门信息", businessType = BusinessType.DELETE)
    @PostMapping("deleteSysDept")
    @JwtSysUser
    @ApiOperation(value = "删除部门信息", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> deleteSysDept(@RequestBody DeleteDeptVO deleteDeptVO, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        UserInfo userInfo = UserInfoManager.get();
        sysDeptService.delete(deleteDeptVO, ipAddress, userInfo);
        return ResponseResult.success(Boolean.TRUE);
    }

    @GetMapping("selectPage")
    @ApiOperation(value = "分页查询部门信息", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<IPage<SysDept>> selectPage(DeptPage deptPage) {

        IPage<SysDept> deptIPage = sysDeptService.selectByPage(deptPage);
        return ResponseResult.success(deptIPage);
    }

    @PostMapping("getChildDeptList")
    @ApiOperation(value = "查询子部门集合", httpMethod = "POST", response = SysDeptVO.class)
    public ResponseResult<List<SysDeptVO>> getChildDeptList(Long deptId) {
        List<SysDept> list = sysDeptService.getChildDeptList(deptId);

        List<SysDeptVO> resultList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(list)) {
            resultList = BeanUtil.copyToList(resultList, SysDeptVO.class);
        }
        return ResponseResult.success(resultList);
    }

    @GetMapping("getChildDeptList")
    @ApiOperation(value = "查询子部门集合", httpMethod = "POST", response = SysDeptVO.class)
    public ResponseResult<SysDeptVO> getDeptByOrgCode(@RequestParam("orgCode") String orgCode) {
        Asserts.check(StringUtils.isBlank(orgCode), "查询的组织机构编码不能为空！");
        SysDept sysDept = sysDeptService.getDeptByOrgCode(orgCode);
        Asserts.check(sysDept == null, "组织机构编码对应的部门不存在！");
        SysDeptVO sysDeptVO = BeanUtil.copyProperties(sysDept, SysDeptVO.class);
        return ResponseResult.success(sysDeptVO);
    }

    @JwtSysUser
    @PostMapping("getRootDept")
    @ApiOperation(value = "获取当前token对应的部门信息", httpMethod = "POST", response = SysDeptVO.class)
    public ResponseResult<SysDeptVO> getRootDept(/*Long deptId*/) {
        UserInfo userInfo = UserInfoManager.get();
        SysDept sysDept = sysDeptService.getById(userInfo.getDeptId());
        Asserts.check(sysDept == null || DeletedEnum.DELETED.code.equals(sysDept.getXtZxbz()),
                "部门不存在，请重新登陆");
        SysDeptVO sysDeptVO = BeanUtil.copyProperties(sysDept, SysDeptVO.class);
        return ResponseResult.success(sysDeptVO);
    }

    @JwtSysUser
    // @PostMapping("getAllChildDeptList")
    @ApiOperation(value = "查询所有子部门树", httpMethod = "POST", response = SysDeptVO.class)
    public ResponseResult<SysDeptVO> getAllChildDeptList(@RequestBody DeptTreeVO deptTreeVO) {
        UserInfo userInfo = UserInfoManager.get();
        SysDept sysDept = sysDeptService.getById(userInfo.getDeptId());
        Asserts.check(sysDept == null || DeletedEnum.DELETED.code.equals(sysDept.getXtZxbz()),
                "部门不存在，请求重新登陆");
        SysDeptVO rootSysDeptVo = sysDeptService.getAllChildDeptList(sysDept, deptTreeVO);
        return ResponseResult.success(rootSysDeptVo);
    }

    @GetMapping("selectUserPageByDept")
    @ApiOperation(value = "分页查询部门下用户信息", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<IPage<UserDeptVO>> selectUserPageByDept(UserByDeptPage userByDeptPage) {
        IPage<UserDeptVO> deptIPage = sysDeptService.selectUserPageByDept(userByDeptPage);
        return ResponseResult.success(deptIPage);
    }

    @GetMapping("selectRolePageByDept")
    @ApiOperation(value = "分页查询部门下角色信息", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<IPage<RoleDeptVO>> selectRolePageByDept(RoleByDeptPage roleByDeptPage) {
        IPage<RoleDeptVO> deptIPage = sysDeptService.selectRolePageByDept(roleByDeptPage);
        return ResponseResult.success(deptIPage);
    }

    @PostMapping("saveUserDeptInfo")
    @JwtSysUser
    @Log(title = "部门功能", businessType = BusinessType.GRANT)
    @ApiOperation(value = "添加用户部门关联关系", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> saveUserDeptInfo(@RequestBody AddUserDeptVO addUserDeptVO) {
        sysDeptService.saveUserDeptInfo(addUserDeptVO);
        return ResponseResult.success(Boolean.TRUE);
    }

    @PostMapping("saveRoleDeptInfo")
    @JwtSysUser
    @Log(title = "部门功能", businessType = BusinessType.GRANT)
    @ApiOperation(value = "添加角色部门关联关系", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> saveRoleDeptInfo(@RequestBody AddRoleDeptVO roleDeptVO) {
        sysDeptService.saveRoleDeptInfo(roleDeptVO);
        return ResponseResult.success(Boolean.TRUE);
    }

    @PostMapping("deleteUserDeptInfo")
    @JwtSysUser
    @Log(title = "部门功能", businessType = BusinessType.OTHER)
    @ApiOperation(value = "删除用户部门关联关系", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> deleteUserDeptInfo(@RequestBody DeleteUserDeptVO deleteUserDeptVO) {
        sysDeptService.deleteUserDeptInfo(deleteUserDeptVO);
        return ResponseResult.success(Boolean.TRUE);
    }

    @JwtSysUser
    @GetMapping("selectDept")
    @ApiOperation(value = "查询部门列表信息", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<List<SysDept>> selectDept(DeptQuery dto) {
        return ResponseResult.success(sysDeptService.selectDept(dto));
    }


//    @PostMapping("deleteRoleDeptInfo")
//    @JwtSysUser
//    @Log(title = "部门功能", businessType = BusinessType.OTHER)
//    @ApiOperation(value = "删除角色部门关联关系", httpMethod = "POST", response = Boolean.class)
//    public ResponseResult<Boolean> deleteRoleDeptInfo(@RequestBody DeleteRoleDeptVO deleteRoleDeptVO) {
//        sysDeptService.deleteRoleDeptInfo(deleteRoleDeptVO);
//        return ResponseResult.success(Boolean.TRUE);
//    }
//
//    @JwtSysUser
//    @GetMapping("selectNextPage")
//    @ApiOperation(value = "根据部门编码分页查询当前及下级部门信息", httpMethod = "GET", response = SysDept.class)
//    public ResponseResult<IPage<SysDept>> selectNextPage(DeptPage deptPage) {
//        IPage<SysDept> deptIPage = sysDeptService.selectNextPage(deptPage);
//        return ResponseResult.success(deptIPage);
//    }

    @GetMapping("getDeptList")
    @ApiOperation(value = "根据部门编码分页查询当前及下级部门信息--第三方接口", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<IPage<SysDept>> getDeptList(DeptPage deptPage) {
        IPage<SysDept> deptIPage = sysDeptService.getDeptList(deptPage);
        return ResponseResult.success(deptIPage);
    }

    @GetMapping("deptSys")
    @ApiOperation(value = "部门信息同步", httpMethod = "GET", response = Boolean.class)
    public ResponseResult<Boolean> deptSys() {
        Boolean bool = sysDeptService.deptSys();
        return ResponseResult.success(bool);
    }

    @ApiOperation("查询部门下的所有子部门信息")
    @GetMapping("getChildDept/{deptid}")
    public ResponseResult<List<String>> getChildDept(@PathVariable("deptid") String deptid) {
        return ResponseResult.success(this.sysDeptService.getChildDept(deptid));
    }

    @ApiOperation("查询部门下的所有子部门信息")
    @GetMapping("getChildDeptByOrgCode/{orgcode}")
    public ResponseResult<List<String>> getChildDeptByOrgCode(@PathVariable("orgcode") String orgcode) {
        return ResponseResult.success(this.sysDeptService.getChildDeptByOrgCode(orgcode));
    }

}

