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.PlayPersonnelWaiterInfoEntity;
import com.starry.admin.modules.personnel.module.vo.PlayPersonnelWaiterInfoEditAddInfoVo;
import com.starry.admin.modules.personnel.module.vo.PlayPersonnelWaiterInfoEditBaseInfoVo;
import com.starry.admin.modules.personnel.module.vo.PlayPersonnelWaiterInfoQueryVo;
import com.starry.admin.modules.personnel.module.vo.PlayPersonnelWaiterInfoReturnVo;
import com.starry.admin.modules.personnel.service.IPlayPersonnelWaiterInfoService;
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.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/waiter")
public class PlayPersonnelWaiterInfoController {
    @Resource
    private IPlayPersonnelWaiterInfoService playClerkWaiterInfoService;

    @Resource
    private SysUserService sysUserService;


    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysUserRoleService userRoleService;


    /**
     * 查询客服信息列表
     */
    @PostMapping("/listByPage")
    public R listByPage(@Validated @RequestBody PlayPersonnelWaiterInfoQueryVo vo) {
        IPage<PlayPersonnelWaiterInfoReturnVo> list = playClerkWaiterInfoService.selectByPage(vo);
        return R.ok(list);
    }

    /**
     * 新增客服信息列表
     */
    @Log(title = "客服管理信息", businessType = BusinessType.INSERT)
    @PostMapping("/createBaseInfo")
    public R createBaseInfo(@Validated @RequestBody PlayPersonnelWaiterInfoEditAddInfoVo vo) {
        //校验对应用户是否存在
        SysUserEntity sysUserEntity = sysUserService.selectUserById(vo.getSysUserId());
        //校验当前用户是否已经是组长
        PlayPersonnelWaiterInfoEntity waiterInfoEntity = playClerkWaiterInfoService.selectByUserId(vo.getSysUserId());
        if (waiterInfoEntity != null) {
            throw new CustomException("当前账号已经是客服,无法重复添加");
        }

        PlayPersonnelWaiterInfoEntity entity = ConvertUtil.entityToVo(vo, PlayPersonnelWaiterInfoEntity.class);
        entity.setSysUserCode(sysUserEntity.getUserCode());
        entity.setAddTime(LocalDateTime.now());
        boolean success = playClerkWaiterInfoService.create(entity);
        if (success) {
            //新建客服后，添加对应的用户角色权限
            userRoleService.addUserRole(vo.getSysUserId(), sysRoleService.selectByRoleKey("waiter").getRoleId());
            return R.ok();
        }
        return R.error("添加失败");
    }

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

        //校验对应用户是否存在
        SysUserEntity sysUserEntity = sysUserService.selectUserById(vo.getSysUserId());
        //校验当前用户是否已经是组长
        PlayPersonnelWaiterInfoEntity waiterInfoEntity = playClerkWaiterInfoService.selectByUserId(vo.getSysUserId());
        if (waiterInfoEntity != null && !waiterInfoEntity.getId().equals(vo.getId())) {
            throw new CustomException("当前账号已经是客服,无法重复添加");
        }


        PlayPersonnelWaiterInfoEntity entity = ConvertUtil.entityToVo(vo, PlayPersonnelWaiterInfoEntity.class);
        entity.setSysUserCode(sysUserEntity.getUserCode());
        boolean success = playClerkWaiterInfoService.update(entity);
        if (success) {
            //新建客服后，添加对应的用户角色权限
            userRoleService.addUserRole(vo.getSysUserId(), sysRoleService.selectByRoleKey("waiter").getRoleId());
            return R.ok();
        }
        return R.error("修改失败");
    }


    /**
     * 删除客服信息
     */
    @Log(title = "客服信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        Long ruleId = sysRoleService.selectByRoleKey("operator").getRoleId();
        for (String id : ids) {
            // 判断数据是否存在
            PlayPersonnelWaiterInfoEntity entity = playClerkWaiterInfoService.selectPlayClerkWaiterInfoById(id);
            //删除客服
            playClerkWaiterInfoService.deletePlayClerkWaiterInfoById(id);
            //删除对应的权限信息
            userRoleService.delUserRole(entity.getSysUserId(), ruleId);

        }
        return R.ok();
    }
}
