package com.sptg.gateway.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.UserFlagConstant;
import com.sptg.common.core.entity.DispatchTask;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.estate.api.dto.ApplyUserDto;
import com.sptg.estate.api.dto.UserDto;
import com.sptg.estate.api.dto.UserInfoDto;
import com.sptg.estate.api.entity.ApplyUser;
import com.sptg.estate.api.entity.Member;
import com.sptg.estate.api.entity.MemberAndUser;
import com.sptg.estate.api.entity.User;
import com.sptg.estate.api.feigns.ApplyUserFeign;
import com.sptg.estate.api.feigns.MemberAndUserFeign;
import com.sptg.estate.api.feigns.MemberFeign;
import com.sptg.estate.api.feigns.UserFeign;
import com.sptg.gateway.entity.UserApi;
import com.sptg.gateway.util.DataCheck;
import com.sptg.gateway.util.PojoToApi;
import com.sptg.permissions.api.entity.ApiLog;
import com.sptg.permissions.api.entity.Conf;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import com.sptg.permissions.api.feigns.SysApiLogFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * title: UserController
 * package: com.sptg.estate.biz.security
 * description: 住户管理控制器
 *
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/user")
public class UserController {

    private final OrgFeign orgFeign;
    private final MemberAndUserFeign memberAndUserFeign;
    private final UserFeign userFeign;
    private final ConfFeign confFeign;
    private final MemberFeign memberFeign;
    private final ApplyUserFeign applyUserFeign;
    private final ApiDockingConfFeign apiDockingConfFeign;
    private final FastFileStorageClient fastFileStorageClient;
    private final DeviceFeign deviceFeign;

    /**
     * 功能描述: 获取房屋成员 By  memberId
     * @param map {
     *            member_id     :   会员id
     *            pageNum       :   分页页数
     *            pageSize      :   分页大小
     *            org_id        :   机构授权org_id，机构id,
     *            token         :   对接令牌
     *            }
     * @return ResultBean<hashMap> 返回结果
     */
    @PostMapping(value = "/getAllUserByMemberId")
    public Map<String,Object> getAllUserByMemberId(@RequestBody Map<String,String> map){
        log.warn("============================小程序通过会员ID获取所有住户(/app/user/getAllUserByMemberId)请求数据==============================");
        log.warn(JsonUtils.objectToJson(map));
        Map<String,Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取列表失败");
        String org_id = map.get("org_id");
        String member_id = map.get("member_id");
        String pageNum = map.get("pageNum");
        String pageSize = map.get("pageSize");
        String token = map.get("token");
        String roomId = map.get("roomId");
        try {
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            if(StringUtils.isBlank(member_id)){
                hashMap.put("result_msg", "member_id不能为空");
                return hashMap;
            }
            if (StringUtils.isEmpty(pageNum)) {
                hashMap.put("result_msg", "分页页码不能为空");
                return hashMap;
            }
            if (StringUtils.isEmpty(pageSize)) {
                hashMap.put("result_msg", "分页大小不能为空");
                return hashMap;
            }
            //查询会员对应的user
            MemberAndUser memberAndUser = new MemberAndUser();
            memberAndUser.setMemberId(Long.valueOf(member_id));
            List<MemberAndUser> list = this.memberAndUserFeign.getByCondition(memberAndUser);
            if(list == null || list.size()==0){
                hashMap.put("result_msg", "没有查询到与会员对应的住户信息");
                return hashMap;
            }else{
                List<Long> roomIds = new ArrayList<>();
                if(null != roomId){
                    roomIds.add(Long.valueOf(roomId));
                }else {
                    for(MemberAndUser mu:list){
                        roomIds.add(mu.getRoomId());
                    }
                }

                UserDto userDto = new UserDto();
                userDto.setRoomIds(roomIds);
                userDto.setPage(new Page<>(Integer.parseInt(pageNum),Integer.parseInt(pageSize)));
                /*Page<User> page = new Page<>();
                page.setSize(Integer.parseInt(pageSize));
                page.setCurrent(Integer.parseInt(pageNum));
                userDto.setPage(page);*/
                Page<User> userPage = this.userFeign.pagingByRoomId(userDto);
                Long totalPageNum = (userPage.getTotal() + userPage.getSize() -1) / userPage.getSize();
                List<User> users = userPage.getRecords();
                if (users == null || users.size() == 0){
                    hashMap.put("result_msg", "没有查询到与会员对应的住户信息");
                    return hashMap;
                }
                hashMap.put("result_code",1);
                List<UserApi> apis = new ArrayList<>();
                String fastDfsServer = this.confFeign.getFileServerHost();
                for(User user:users){
                    if(StringUtils.isNotBlank(user.getImageUri())){
                        user.setImageUri(fastDfsServer + user.getImageUri());
                    }
                    if(StringUtils.isNotBlank(user.getPassportImgUri())){
                        user.setPassportImgUri(fastDfsServer + user.getPassportImgUri());
                    }
                    if(StringUtils.isNotBlank(user.getEntryImgUri())){
                        user.setEntryImgUri(fastDfsServer + user.getEntryImgUri());
                    }
                    UserApi userApi = PojoToApi.userToApi(user);
                    userApi.setFaceBeginDate(user.getFaceBeginDate());
                    userApi.setFaceEndDate(user.getFaceEndDate());
                    if(user.getFaceEndDate() != null){
                        Conf conf = this.confFeign.getEntityByToken("expire.notice.days");
                        if(conf != null){
                            Integer days = Integer.parseInt(conf.getConfValue());
                            int compare = DateUtil.compare(DateUtil.offsetDay(new Date(), days), user.getFaceEndDate());
                            userApi.setFaceValid(compare > 0 ? false : true);
                        }
                    }
                    apis.add(userApi);
                }
                hashMap.put("list",apis);
                hashMap.put("pages",totalPageNum);
                if(apis.size()==0){
                    hashMap.put("result_msg", "没有相关数据");
                }else{
                    hashMap.put("result_msg", "查询数据成功");
                }
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }


    /**
     * 功能描述: 删除User
     * @param map {
     *            member_id     :   会员id
     *            user_id       :   要删除的住户id
     *            org_id        :   机构授权org_id，机构id,
     *            token         :   对接令牌
     *            }
     * @return ResultBean<hashMap> 返回结果
     */
    @PostMapping(value = "/deleteUserById")
    public Map<String,Object> deleteUserById(@RequestBody Map<String,String> map){
        log.warn("============================小程序删除住户(/app/user/deleteUserById)请求数据==============================");
        log.warn(JsonUtils.objectToJson(map));
        Map<String,Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "删除失败");
        String org_id = map.get("org_id");
        String user_id = map.get("user_id");
        String member_id = map.get("member_id");
        String token = map.get("token");
        try {
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            if(StringUtils.isBlank(user_id)){
                hashMap.put("result_msg", "user_id不能为空");
                return hashMap;
            }
            if(StringUtils.isBlank(member_id)){
                hashMap.put("result_msg", "member_id不能为空");
                return hashMap;
            }
            MemberAndUser memberAndUser = new MemberAndUser();
            memberAndUser.setMemberId(Long.valueOf(member_id));
            memberAndUser.setUserId(Long.valueOf(user_id));
            //判断会员和住户是否存在关系
            ResultBean<Boolean> resultBean = this.memberAndUserFeign.checkRelationship(memberAndUser);
            //会员和住户存在关系
            if(resultBean.getData()){
                hashMap.put("result_msg", "不能删除自身住户信息");
                return hashMap;
            }
            //校验权限
            Member member = this.memberFeign.getMemberByMemberId(Long.valueOf(member_id));
            if(member==null){
                hashMap.put("result_msg", "操作的会员不存在");
                return hashMap;
            }
            User user = this.userFeign.findUserById(Long.valueOf(user_id));
            if(user==null){
                hashMap.put("result_msg", "要删除的住户不存在");
                return hashMap;
            }
            //业主---不能删除
            if(user.getType()==OperationConstant.ZERO){
                hashMap.put("result_msg", "该住户身份为业主不能删除");
                return hashMap;
            }
            //从新拼凑条件
            memberAndUser.setUserId(null);
            List<MemberAndUser> list = this.memberAndUserFeign.getByCondition(memberAndUser);
            if(list==null || list.size()==0){
                hashMap.put("result_msg", "没有查询到与会员对应的住户信息");
                return hashMap;
            }else {
                boolean result = false;
                for(MemberAndUser mau:list){
                    //会员也是房屋下的成员
                    if(mau.getRoomId().equals(user.getRoomId())){
                        User memberUser = this.userFeign.findUserById(mau.getUserId());
                        //会员在要删除的住户房屋下是业主身份
                        if(memberUser.getType().equals(OperationConstant.ZERO)){
                            result = true;
                        }
                        break;
                    }
                }
                if(!result){
                    hashMap.put("result_msg", "会员无权限删除该住户");
                    return hashMap;
                }
            }
            UserDto userDto = new UserDto();
            userDto.setId(user.getId());
            userDto.setMemberId(member.getId());
            //删除user，而且删除与会员的关系，修改会员的状态
            ApplyUserDto applyUserDto = new ApplyUserDto();
            applyUserDto.setUserId(user.getId());
            boolean result = this.applyUserFeign.deleteApplyUser(applyUserDto);
            if(result){
                resultBean = this.userFeign.deleteUserAndRelationship(userDto);
                if(resultBean.getData()){
                    hashMap.put("result_code", 1);
                    hashMap.put("result_msg", "删除成功");
                }
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 根据房屋id查询房屋下面的成员
     * @param map {
     *            room_id       :   房屋id
     *            pageNum       :   分页页数
     *            pageSize      :   分页大小
     *            org_id        :   机构授权org_id，机构id,
     *            token         :   对接令牌
     *            }
     * @return ResultBean<hashMap> 返回结果
     */
    @PostMapping(value = "/getUserByRoomId")
    public Map<String,Object> getUserByRoomId(@RequestBody Map<String,String> map){
        log.warn("============================小程序通过房间ID获取房间下的所有住户(/app/user/getUserByRoomId)请求数据==============================");
        log.warn(JsonUtils.objectToJson(map));
        Map<String,Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取列表失败");
        String org_id = map.get("org_id");
        String room_id = map.get("room_id");
        String pageNum = map.get("pageNum");
        String pageSize = map.get("pageSize");
        String token = map.get("token");
        try {
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            if(room_id==null){
                hashMap.put("result_msg", "room_id不能为空");
                return hashMap;
            }
            if (StringUtils.isEmpty(pageNum)) {
                hashMap.put("result_msg", "分页页码不能为空");
                return hashMap;
            }
            if (StringUtils.isEmpty(pageSize)) {
                hashMap.put("result_msg", "分页大小不能为空");
                return hashMap;
            }
            List<Long> roomIds = new ArrayList<>(1);
            roomIds.add(Long.valueOf(room_id));
            UserDto userDto = new UserDto();
            userDto.setRoomIds(roomIds);
            userDto.setPage(new Page<>(Integer.parseInt(pageNum),Integer.parseInt(pageSize)));
            Page<User> page = this.userFeign.pagingByRoomId(userDto);
            Long totalPageNum = (page.getTotal() + page.getSize() -1) / page.getSize();
            List<User> users = page.getRecords();
            hashMap.put("result_code", 1);
            List<UserApi> apis = new ArrayList<>();
            String fastDfsServer = this.confFeign.getFileServerHost();
            for(User user:users){
                if(StringUtils.isNotBlank(user.getImageUri())){
                    user.setImageUri(fastDfsServer+user.getImageUri());
                }
                UserApi userApi = PojoToApi.userToApi(user);
                if(user.getFaceEndDate() != null){
                    Conf conf = this.confFeign.getEntityByToken("expire.notice.days");
                    if(conf != null){
                        Integer days = Integer.parseInt(conf.getConfValue());
                        int compare = DateUtil.compare(DateUtil.offsetDay(new Date(), days), user.getFaceEndDate());
                        userApi.setFaceValid(compare > 0 ? false : true);
                    }
                }
                apis.add(userApi);
            }
            hashMap.put("list", apis);
            hashMap.put("pages",totalPageNum);
            if (apis.size() == 0) {
                hashMap.put("result_msg", "没有相关房屋信息");
            }else {
                hashMap.put("result_msg", "获取列表成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 修改我的家人
     * @param map {
     *            org_id                :   机构授权org_id，机构id,
     *            token                 :   对接令牌
     *            user_id               :   修改的住户id
     *            type                  :   住户类型,['业主','成员','租户'],
     *            id_card               :   身份证号，
     *            sex                   :   性别，
     *            nationality           :   国籍， 0 中国大陆 1 中国香港/澳门 2 中国台湾 3 海外
     *            image_uri             :   脸部照片url（没有域名）
     *            face_code             :   根据图片生成的faceId--图片的md5
     *            passport_img_uri      :   护照照片url（没有域名，nationality = 3时,国籍为海外）
     *            entry_img_uri         :   入境证明url（没有域名，nationality = 3时,国籍为海外）
     *            tel                   :   电话，
     *            member_id             :   会员id
     *            create_type           :   申请来源0-小程序添加 1-后台添加  2-后台导入 3-app添加
     *            face_image_base64     :   人脸图片的base64字符串，
     *            }
     * @return ResultBean<hashMap> 返回结果
     */
    @PostMapping(value = "/editUser")
    public Map<String,Object> editUser(@RequestBody Map<String,String> map){
        log.warn("/app/user/editUser?user_id=" + map.get("user_id"));

        HashMap<Object, Object> mapTemp = new HashMap<>();
        BeanUtil.copyProperties(map, mapTemp);
        mapTemp.remove("face_image_base64");
        log.warn("============================小程序会员编辑家人(/app/user/editUser)请求数据==============================");
        log.warn(JsonUtils.objectToJson(mapTemp));

        Map<String,Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "提交修改失败");
        String org_id = map.get("org_id");
        String token = map.get("token");
        String user_id = map.get("user_id");
        String type = map.get("type");
        String nationality = map.get("nationality");
        String image_uri = map.get("image_uri");
        String face_code = map.get("face_code");
        String passport_img_uri = map.get("passport_img_uri");
        String entry_img_uri = map.get("entry_img_uri");
        String id_card = map.get("id_card");
        String sex = map.get("sex");
        String tel = map.get("tel");
        String member_id = map.get("member_id");
        String create_type = map.get("create_type");
        String face_image_base64 = map.get("face_image_base64");
        String operUserType = map.get("oper_user_type");
        try {
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            if(StringUtils.isBlank(type)){
                hashMap.put("result_msg", "住户身份类型（user_id）不能为空");
                return hashMap;
            }
            /*if(Integer.valueOf(type)==0 && StringUtils.isBlank(id_card)){
                hashMap.put("result_msg", "（id_card）业主身份时证件号不能为空");
                return hashMap;
            }*/
            if(StringUtils.isBlank(sex)){
                hashMap.put("result_msg", "住户性别（sex）不能为空");
                return hashMap;
            }
            if(StringUtils.isBlank(tel)){
                hashMap.put("result_msg", "电话（tel）不能为空");
                return hashMap;
            }
            if(StringUtils.isBlank(member_id)){
                hashMap.put("result_msg", "会员id（member_id）不能为空");
                return hashMap;
            }
            if(StringUtils.isBlank(create_type)){
                hashMap.put("result_msg", "数据申请来源（create_type）不能为空");
                return hashMap;
            }
            if(StringUtils.isBlank(user_id)){
                hashMap.put("result_msg", "user_id不能为空");
                return hashMap;
            }else {
                User user = this.userFeign.findUserById(Long.valueOf(user_id));
                if(user==null){
                    hashMap.put("result_msg", "修改的住户不存在");
                    return hashMap;
                }else{
                    //一个房间下面同一个名字只能一条待审核的申请记录
                    if(operUserType != null){
                        if(OperationConstant.ZERO == Integer.parseInt(operUserType) && OperationConstant.ZERO != Integer.parseInt(type)){
                            //如果不是审核的业主信息，就直接通过
                            if (StringUtils.isNotBlank(image_uri)){
                                user.setImageUri(image_uri);
                            }
                            if (StringUtils.isNotBlank(face_code)){
                                user.setFaceCode(face_code);
                            }
                            user.setIdCard(id_card);
                            user.setSex(Integer.parseInt(sex));
//                        user.setNationality(Integer.parseInt(nationality));
//                        user.setPassportImgUri(passport_img_uri);
//                        user.setEntryImgUri(entry_img_uri);
                            user.setTel(tel);
                            user.setCreateType(Integer.parseInt(create_type));
                            user.setType(Integer.parseInt(type));
                            //上传了人脸
                            if (StringUtils.isNotBlank(face_image_base64)){
                                Map<String, String> newFaceMap = DataCheck.uploadFace(face_image_base64, this.fastFileStorageClient);
                                if(newFaceMap != null){
                                    user.setImageUri(newFaceMap.get("imageUrl"));
                                    user.setFaceCode(newFaceMap.get("faceCode"));
                                }
                            }
                            boolean b = userFeign.updateEntity(user);
                            if(b){
                                if (user.getImageUri() != null && image_uri != null && !user.getImageUri().equals(image_uri)){
                                    //新旧人脸不一致，要下发人脸
                                    pushFace(user, OperationConstant.ONE);
                                }

                                hashMap.put("result_code", 1);
                                hashMap.put("result_msg", "修改成功");
                            }else{
                                hashMap.put("result_code",0);
                                hashMap.put("result_msg", "修改失败");
                            }
                            return hashMap;
                        }
                    }
                    ApplyUser applyUser = new ApplyUser();
                    //有bug这个地方
                    applyUser.setName(user.getName());
                    applyUser.setRoomId(user.getRoomId());
                    ResultBean<Boolean> resultBean = this.applyUserFeign.checkByNameAndRoomId(applyUser);
                    if(resultBean.getData()){
                        hashMap.put("result_msg", "名字"+applyUser.getName()+"已有一条申请记录处于待审核状态，请勿重复提交，等待物业审核");
                        return hashMap;
                    }
                    //查询用户之前是否有申请过的数据，有就把人脸拿出来set到旧人脸，没有旧人脸为空
                    ApplyUserDto applyUserDto = new ApplyUserDto();
                    applyUserDto.setMemberId(Long.valueOf(member_id));
                    List<ApplyUser> applyUserList = applyUserFeign.getApplyUserByCondition(applyUserDto);
                    ApplyUser user1 = applyUserList.get(0);
                    //上传了人脸
                    ResultBean resultBean1 = DataCheck.uploadFace(face_image_base64, applyUser, this.fastFileStorageClient, confFeign);
                    if (resultBean1 != null){
                        hashMap.put("result_msg", resultBean1.getMsg());
                    }
                    applyUser.setUserId(Long.valueOf(user_id));
                    applyUser.setRoomName(user.getRoomName());
                    //申请记录审核状态：0-待审核 1-审核通过 2-审核不通过
                    applyUser.setCheckState(OperationConstant.ZERO);
                    applyUser.setUnitId(user.getUnitId());
                    applyUser.setUnitName(user.getUnitName());
                    applyUser.setBuildingName(user.getBuildingName());
                    applyUser.setBuildingId(user.getBuildingId());
                    applyUser.setResidentialName(user.getResidentialName());
                    applyUser.setResidentialId(user.getResidentialId());
                    applyUser.setOrgPosition(user.getOrgPosition());
                    applyUser.setEstatePosition(user.getEstatePosition());
                    applyUser.setRootOrgId(user.getRootOrgId());
                    applyUser.setCreateDate(new Date());
                    applyUser.setCreateType(Integer.valueOf(create_type));
                    applyUser.setRecordType(OperationConstant.TWO);
                    //修改的属性**********开始*******
                    if (StringUtils.isNotEmpty(user1.getImageUri())){
                        applyUser.setOldImageUri(user1.getImageUri());
                    }
                    applyUser.setId(Long.valueOf(user_id));
                    applyUser.setType(Integer.valueOf(type));
                    if(StringUtils.isNotBlank(id_card)){
                        applyUser.setIdCard(id_card);
                    }
                    applyUser.setSex(Integer.valueOf(sex));
                    if(StringUtils.isBlank(nationality)){
                    	applyUser.setNationality(user.getNationality());
                    } else {
                    	applyUser.setNationality(NumberUtils.INTEGER_ZERO);
                    }
                    //applyUser.setNationality(Integer.valueOf(nationality));
                    if(StringUtils.isNotBlank(image_uri)){
                        applyUser.setImageUri(image_uri);
                        applyUser.setFaceCode(face_code);
                    }
                    if(StringUtils.isNotBlank(passport_img_uri)){
                        applyUser.setPassportImgUri(passport_img_uri);
                    }
                    if(StringUtils.isNotBlank(entry_img_uri)){
                        applyUser.setEntryImgUri(entry_img_uri);
                    }
                    applyUser.setTel(tel);
                    applyUser.setMemberId(Long.valueOf(member_id));
                    //申请的记录类型 0--认证类型 1--新增类型 2--编辑类型
                    applyUser.setRecordType(2);
                    //修改的属性**********结束*******
                    resultBean = this.applyUserFeign.addApplyUser(applyUser);
                    if(resultBean.getData()){
                        hashMap.put("result_code", 1);
                        hashMap.put("result_msg", "提交修改成功，请等待物业审核");
                    }
                }
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 获取会员用户自己的住户信息 By  memberId
     * @param map {
     *            member_id     :   会员id
     *            org_id        :   机构授权org_id，机构id,
     *            token        :   对接令牌
     *            }
     * @return ResultBean<hashMap> 返回结果
     */
    @PostMapping(value = "/getMyUserByMemberId")
    public Map<String,Object> getMyUserByMemberId(@RequestBody Map<String,String> map){
        log.warn("============================小程序获取会员用户自己的住户信息(/app/user/getMyUserByMemberId)请求数据==============================");
        log.warn(JsonUtils.objectToJson(map));
        Map<String,Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "查询数据失败");
        String org_id = map.get("org_id");
        String member_id = map.get("member_id");
        String token = map.get("token");
        try {
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            if(StringUtils.isBlank(member_id)){
                hashMap.put("result_msg", "member_id不能为空");
                return hashMap;
            }
            //查询会员对应的user
            MemberAndUser memberAndUser = new MemberAndUser();
            memberAndUser.setMemberId(Long.valueOf(member_id));
            List<MemberAndUser> list = this.memberAndUserFeign.getByCondition(memberAndUser);
            if(list == null || list.size()==0){
                hashMap.put("result_msg", "没有查询到与会员对应的住户信息");
                return hashMap;
            }else{
                List<Long> userId = new ArrayList<>();
                for(MemberAndUser mau : list){
                    userId.add(mau.getUserId());
                }
                List<User> users = this.userFeign.getListByUserId(userId);
                hashMap.put("result_code", 1);
                List<UserApi> apis = new ArrayList<>();
                String fastDfsServer = this.confFeign.getFileServerHost();
                for(User user:users){
                    if(StringUtils.isNotBlank(user.getImageUri())){
                        user.setImageUri(fastDfsServer+user.getImageUri());
                    }
                    apis.add(PojoToApi.userToApi(user));
                }
                hashMap.put("list", apis);
                if(apis.size()==0){
                    hashMap.put("result_msg", "没有查询到相关住户信息");
                }else {
                    hashMap.put("result_msg", "查询数据成功");
                }
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 下发人脸方法
     * @param user
     * @param operType
     */
    private void pushFace(User user, Integer operType) {
        if (StringUtils.isNotBlank(user.getImageUri())) {

            Map<String, String> logMap = new HashMap<>();
            //小区
            if (!StringUtils.isEmpty(user.getResidentialName())) {
                logMap.put("xqName", user.getResidentialName());
            } else {
                logMap.put("xqName", null);
            }
            //单元
            if (!StringUtils.isEmpty(user.getUnitName())) {
                logMap.put("unitName", user.getUnitName());
            } else {
                logMap.put("unitName", null);
            }
            //房间
            if (!StringUtils.isEmpty(user.getRoomName())) {
                logMap.put("roomName", user.getRoomName());
            } else {
                logMap.put("roomName", null);
            }
            //用户名
            if (!StringUtils.isEmpty(user.getName())) {
                logMap.put("userName", user.getName());
            } else {
                logMap.put("userName", null);
            }

            String imgHost = this.confFeign.getFileServerHost();
            List<Map<String, Object>> userFaceList = new ArrayList<>(1);
            Map<String, Object> map = new HashMap<>(3);
            map.put("imgMd5", user.getFaceCode());
            map.put("imgSrc", imgHost + user.getImageUri());
            userFaceList.add(map);
            //  下发人脸-到通用设备
            UserInfoDto dto = new UserInfoDto();
            dto.setId(UserFlagConstant.USER_NUMBER_FLAG + user.getId());
            dto.setFaceCodeList(userFaceList);
            dto.setBeginDate(user.getFaceBeginDate());
            dto.setEndDate(user.getFaceEndDate());
            DispatchTask task = new DispatchTask();
            task.setUnitId(user.getUnitId());
            task.setXqId(user.getResidentialId());
            task.setData(dto);
            this.deviceFeign.pushUserFaceToUniversalDevices(task, operType, logMap);
        }
    }

}
