package com.sptg.permissions.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.entity.VideoCallAccount;
import com.sptg.estate.api.feigns.VideoCallAccountFeign;
import com.sptg.permissions.api.dto.AccountDto;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import com.sptg.permissions.api.feigns.DeviceAdminUserFeign;
import com.sptg.permissions.biz.service.DeviceAdminUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * title: DeviceAdminUserController
 * description:
 *
 *
 *
 */
@RestController
@RequiredArgsConstructor
public class DeviceAdminUserController implements DeviceAdminUserFeign {

    private final DeviceAdminUserService deviceAdminUserService;
    private final VideoCallAccountFeign videoCallAccountFeign;

    @Override
    @GetMapping(value = "/xq/device/admin/user/getUserById")
    public DeviceAdminUser getUserById(@RequestParam(value = "userId")Long userId) {
        return this.deviceAdminUserService.selectById(userId);
    }

    @Override
    @PostMapping(value = "/xq/device/admin/user/pagingQuery")
    public ResultBean<Page> pagingQuery(@RequestBody PageBean<DeviceAdminUser, AccountDto> pageBean) {
        Page<DeviceAdminUser> page = this.deviceAdminUserService.pagingQuery(pageBean);
        return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @Override
    @PostMapping(value = "/xq/device/admin/user/addEntity")
    public ResultBean<Boolean> addEntity(@RequestBody DeviceAdminUser user) {
        //  判断登录名是否已存在
        boolean isExist = this.deviceAdminUserService.checkLoginNameIsExist(user.getLoginName());
        Assert.isTrue(!isExist,"登录名已存在");
        //  限制一个小区只能有最多10个账号
        Wrapper<DeviceAdminUser> wrapper = new EntityWrapper<>();
        wrapper.eq("xq_id",user.getXqId());
        int count = this.deviceAdminUserService.selectCount(wrapper);
        Assert.isTrue(count < 10,"每个小区最多只能有10个管理机账号");
        //  生成Id
        user.setId(IdWorker.getId());
        //  密码加密
        user.setLoginPwd(this.passwordEncrypt(user.getLoginPwd()));
        user.setIsEnable(1);
        user.setCreateTime(new Date());
        //  创建管理机账号
        boolean isInsert = this.deviceAdminUserService.insert(user);
        if (isInsert){
            //  创建视频账号
            this.videoCallAccountFeign.addEntityByAdminUser(user);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,isInsert);
    }

    private final static String PWD_SALT = "device_manage_account";

    private String passwordEncrypt(String password) {
        return DigestUtils.md5Hex(PWD_SALT + password);
    }

    @Override
    @PostMapping(value = "/xq/device/admin/user/login")
    public ResultBean<DeviceAdminUser> login(@RequestBody DeviceAdminUser user) {
        DeviceAdminUser dbUser = this.deviceAdminUserService.selectByLoginName(user.getLoginName());
        Assert.notNull(dbUser,"用户名不存在");
        //  校验密码
        String password = this.passwordEncrypt(user.getLoginPwd());
        Assert.isTrue(password.equals(dbUser.getLoginPwd()),"用户名或密码错误");
        //  获取视频通话账号信息
        VideoCallAccount account = this.videoCallAccountFeign.getCallAccountByUser(dbUser.getId());
        Assert.notNull(account,"该账户未绑定视频对讲账号");
        dbUser.setVideoCallAccountId(account.getAccountId());
        dbUser.setVideoCallAccountPwd(account.getAccountPwd());
        return new ResultBean<>(ResultEnum.SUCCESS,dbUser);
    }

}
