package com.starry.admin.modules.personnel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.personnel.module.entity.PlayPersonnelAdminInfoEntity;
import com.starry.admin.modules.personnel.module.vo.PlayPersonnelAdminInfoEditAddInfoVo;
import com.starry.admin.modules.personnel.module.vo.PlayPersonnelAdminInfoEditBaseInfoVo;
import com.starry.admin.modules.personnel.module.vo.PlayPersonnelAdminInfoQueryVo;
import com.starry.admin.modules.personnel.module.vo.PlayPersonnelAdminInfoReturnVo;
import com.starry.admin.modules.personnel.service.IPlayPersonnelAdminInfoService;
import com.starry.admin.modules.system.entity.SysUserEntity;
import com.starry.admin.modules.system.service.SysRoleService;
import com.starry.admin.modules.system.service.SysUserRoleService;
import com.starry.admin.modules.system.service.SysUserService;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
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.time.LocalDateTime;

/**
 * 管理员管理Controller
 *
 * @author admin
 * @since 2024-06-14
 */
@RestController
@RequestMapping("/personnel/admin")
public class PlayPersonnelAdminInfoController {
    @Resource
    private IPlayPersonnelAdminInfoService playPersonnelAdminInfoService;

    @Resource
    private SysUserService sysUserService;


    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysUserRoleService userRoleService;


    /**
     * 查询管理员管理信息列表
     */
    @PostMapping("/listByPage")
    public R listByPage(@Validated @RequestBody PlayPersonnelAdminInfoQueryVo vo) {
        IPage<PlayPersonnelAdminInfoReturnVo> list = playPersonnelAdminInfoService.selectByPage(vo);
        return R.ok(list);
    }


    /**
     * 获取管理员管理详细信息
     */
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id) {
        return R.ok(playPersonnelAdminInfoService.selectPlayPersonnelAdminInfoById(id));
    }

    /**
     * 新增管理员管理信息
     */
    @Log(title = "管理员管理信息", businessType = BusinessType.INSERT)
    @PostMapping("/createBaseInfo")
    public R createBaseInfo(@Validated @RequestBody PlayPersonnelAdminInfoEditAddInfoVo vo) {
        //校验对应用户是否存在
        SysUserEntity sysUserEntity = sysUserService.selectUserById(vo.getSysUserId());
        //校验当前用户是否已经是组长
        PlayPersonnelAdminInfoEntity groupInfoEntity = playPersonnelAdminInfoService.selectByUserId(vo.getSysUserId());
        if (groupInfoEntity != null) {
            throw new CustomException("当前账号已经是管理员,无法重复添加");
        }
        //添加管理员
        PlayPersonnelAdminInfoEntity entity = ConvertUtil.entityToVo(vo, PlayPersonnelAdminInfoEntity.class);
        entity.setSysUserCode(sysUserEntity.getUserCode());
        entity.setAddTime(LocalDateTime.now());
        boolean success = playPersonnelAdminInfoService.create(entity);
        if (success) {
            //新建管理员后，添加对应的用户角色权限
            userRoleService.addUserRole(vo.getSysUserId(), sysRoleService.selectByRoleKey("operator").getRoleId());
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改管理员管理信息
     */
    @Log(title = "管理员管理信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateBaseInfo")
    public R updateBaseInfo(@Validated @RequestBody PlayPersonnelAdminInfoEditBaseInfoVo vo) {

        //校验对应用户是否存在
        SysUserEntity sysUserEntity = sysUserService.selectUserById(vo.getSysUserId());
        //校验当前用户是否已经是组长
        PlayPersonnelAdminInfoEntity adminInfoEntity = playPersonnelAdminInfoService.selectByUserId(vo.getSysUserId());
        if (adminInfoEntity != null && !adminInfoEntity.getId().equals(vo.getId())) {
            throw new CustomException("当前账号已经是管理员,无法重复添加");
        }
        PlayPersonnelAdminInfoEntity entity = ConvertUtil.entityToVo(vo, PlayPersonnelAdminInfoEntity.class);
        entity.setSysUserCode(sysUserEntity.getUserCode());
        boolean success = playPersonnelAdminInfoService.update(entity);
        if (success) {
            //新建管理员后，添加对应的用户角色权限
            userRoleService.addUserRole(vo.getSysUserId(), sysRoleService.selectByRoleKey("operator").getRoleId());
        }
        return R.error("修改失败");
    }

    /**
     * 删除管理员管理
     */
    @PreAuthorize("@customSs.hasPermission('play:info:remove')")
    @Log(title = "管理员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        Long ruleId = sysRoleService.selectByRoleKey("operator").getRoleId();
        for (String id : ids) {
            // 判断数据是否存在
            PlayPersonnelAdminInfoEntity entity = playPersonnelAdminInfoService.selectPlayPersonnelAdminInfoById(id);
            //删除管理员
            playPersonnelAdminInfoService.deletePlayPersonnelAdminInfoById(id);
            //删除对应的权限信息
            userRoleService.delUserRole(entity.getSysUserId(), ruleId);
        }
        return R.ok();
    }
}
