package com.sptg.gateway.controller.auth;

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.constant.OperationConstant;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.dto.ApplyUserDto;
import com.sptg.estate.api.entity.ApplyUser;
import com.sptg.estate.api.feigns.ApplyUserFeign;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * title: UserAuditController
 * description:
 *
 *                  
 *            
 */
@RestController
@RequiredArgsConstructor
public class UserAuditController {

    private final ApplyUserFeign applyUserFeign;
    private final SecurityServiceInterface<DeviceAdminUser> securityServiceInterface;

    /**
     * 获取待审核用户列表
     * @param pageBean 分页查询条件
     * @return ResultBean<Page>
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/user/audit/pagingQuery")
    public ResultBean<Page> pagingQuery(@RequestBody PageBean<ApplyUser, ApplyUserDto> pageBean){
        ApplyUserDto dto = pageBean.getDto();
        dto = null == dto ? new ApplyUserDto() : dto;
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setResidentialId(onLineUser.getXqId());
        dto.setCheckState(0);
        pageBean.setDto(dto);
        return this.applyUserFeign.getApplyUserPage(pageBean);
    }

    /**
     * 认证审核
     * @return ResultBean
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/user/audit")
    public ResultBean<Boolean> audit(@RequestBody ApplyUser applyUser){
        Assert.notNull(applyUser.getId(),"Id不能为空");
        Assert.notNull(applyUser.getCheckState(),"审核状态不能为空");
        if (applyUser.getCheckState().equals(OperationConstant.TWO)){
            Assert.hasText(applyUser.getCheckOpinion(),"审核不通过原因必填");
        }
        return this.applyUserFeign.updateCheckState(applyUser);
    }



}
