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.beans.ResultEnum;
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.UserDto;
import com.sptg.estate.api.entity.Room;
import com.sptg.estate.api.entity.Unit;
import com.sptg.estate.api.entity.User;
import com.sptg.estate.api.feigns.RoomFeign;
import com.sptg.estate.api.feigns.UnitFeign;
import com.sptg.estate.api.feigns.UserFeign;
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;

import java.util.List;

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

    private final UserFeign userFeign;
    private final UnitFeign unitFeign;
    private final RoomFeign roomFeign;
    private final SecurityServiceInterface<DeviceAdminUser> securityServiceInterface;


    /**
     * 分页获取住户列表
     * @param pageBean 分页查询条件
     * @return ResultBean<Page>
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/user/pagingQuery")
    public ResultBean<Page> pagingQuery(@RequestBody PageBean<User, UserDto> pageBean){
        UserDto dto = pageBean.getDto();
        dto = null == dto ? new UserDto() : dto;
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setResidentialId(onLineUser.getXqId());
        pageBean.setDto(dto);
        return this.userFeign.getUserPage(pageBean);
    }

    /**
     * @return 获取单元列表
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/user/getUnitList")
    public ResultBean<Unit> getUnitList(){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        List<Unit> unitList = this.unitFeign.getListByXqId(onLineUser.getXqId());
        return new ResultBean<>(ResultEnum.SUCCESS,unitList);
    }

    /**
     * 根据单元Id 获取房间列表
     * @param room 查询条件
     * @return ResultBean<Room>
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/user/getRoomByUnitId")
    public ResultBean<Room> getRoomByUnitId(@RequestBody Room room){
        Assert.notNull(room.getUnitId(),"单元Id不能为空");
        return this.roomFeign.getByUnitId(room.getUnitId());
    }

    /**
     * 新增或修改人脸图片
     * @param dto 用户信息
     * @return ResultBean
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/user/modifyUserFace")
    public ResultBean modifyUserFace(@RequestBody UserDto dto){
        Assert.notNull(dto.getId(),"住户Id不能为空");
        Assert.hasText(dto.getImageUri(),"人脸图片地址不能为空");
        Assert.hasText(dto.getFaceCode(),"人脸图片MD5码不能为空");
        return this.userFeign.addOrUpdateUserFaceImage(dto);
    }
}
