package com.sptg.video.cloud.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.video.cloud.api.beans.ApiResultBean;
import com.sptg.video.cloud.api.dto.VCAccountDto;
import com.sptg.video.cloud.api.entity.VCAccount;
import com.sptg.video.cloud.api.entity.VCDevice;
import com.sptg.video.cloud.api.feigns.VCAccountFeign;
import com.sptg.video.cloud.biz.service.VCAccountServiceInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zwx
 * @create 2020/10/17 11:49
 */
@RequiredArgsConstructor
@RestController
@Slf4j
public class VCAccountController implements VCAccountFeign {

    private final VCAccountServiceInterface vcAccountServiceInterface;

    @Override
    @PostMapping(value = "/vcAccount/login")
    public ResultBean<String> login(@RequestBody VCAccount account) {
        String accountName = account.getAccountName();
        ApiResultBean apiResultBean = this.vcAccountServiceInterface.login(accountName, null);
        String returnCode = apiResultBean.getReturnCode();
        if(returnCode.equals(OperationConstant.ONE + "")){
            return new ResultBean<>(ResultEnum.SUCCESS, "登录成功");
        }else{
            return new ResultBean(ResultEnum.FAILURE, apiResultBean.getMsg());
        }
    }

    /**
     * 分页展示账户列表
     * @param pageBean 用户业务对象（含分页及查询条件）
     * @return
     */
    @Override
    @PostMapping(value = "/vcAccount/pagingQuery")
    public ResultBean<Page<VCAccount>> pagingQuery(@RequestBody PageBean<VCAccount, VCAccountDto> pageBean) {
        Page<VCAccount> accountPage = this.vcAccountServiceInterface.pagingQuery(pageBean);
        return new ResultBean<>(ResultEnum.SUCCESS, accountPage);
    }

    @Override
    @GetMapping(value = "/vcAccount/getList")
    public ResultBean<List<VCAccount>> getList() {
        List<VCAccount> list = this.vcAccountServiceInterface.getList();
        return new ResultBean<List<VCAccount>>(ResultEnum.SUCCESS, list);
    }

    @Override
    @PostMapping(value = "/vcAccount/updateBatch")
    public ResultBean<String> updateBatch(@RequestBody List<VCAccount> vcAccountList) {
        boolean b = this.vcAccountServiceInterface.updateBatchById(vcAccountList);
        if(b){
            return new ResultBean<>(ResultEnum.SUCCESS, "视频云账号更新成功");
        }else{
            return new ResultBean<>(ResultEnum.FAILURE, "视频云账号更新失败");
        }
    }
}
