package com.sptg.gateway.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.estate.api.dto.AccessRecordsDto;
import com.sptg.estate.api.entity.AccessRecords;
import com.sptg.estate.api.entity.MemberAndUser;
import com.sptg.estate.api.feigns.AccessRecordsFeign;
import com.sptg.estate.api.feigns.MemberAndUserFeign;
import com.sptg.gateway.entity.AccessRecordsApi;
import com.sptg.gateway.util.DataCheck;
import com.sptg.gateway.util.PojoToApi;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import io.protostuff.Request;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * title: AccessRecordsController
 * package: com.sptg.estate.biz.security
 * description: 住户出入记录理控制器
 *
 *               
 *                     
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/accessRecords")
public class AccessRecordsController {

    private final OrgFeign orgFeign;
    private final AccessRecordsFeign accessRecordsFeign;
    private final MemberAndUserFeign memberAndUserFeign;
    private final ApiDockingConfFeign apiDockingConfFeign;


    /**
     * 功能描述: 通过会员id获取用户密码授权的开门记录
     * @param map {
     *             member_id     :   会员id,
     *             current       :   分页参数--当前页   不必填
     *             size          :   查询条数  不必填
     *             org_id        :   机构授权org_id，机构id
     *             token         :   对接令牌
     *            }
     * @return ResultBean<hashMap> 返回结果
     *               
     *                     
     */
    @PostMapping(value = "/getListByMemberId")
    public Map<String,Object> getListByMemberId(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "查询数据失败");
        try {
            String member_id = map.get("member_id");
            String org_id =  map.get("org_id");
            String token = map.get("token");
            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", "会员id不能为空");
                return hashMap;
            }
            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", "没有查询到与会员对应的住户信息");
            }else {
                List<Long> userIds = new ArrayList<>();
                for(MemberAndUser mau:list){
                    userIds.add(mau.getUserId());
                }
                ResultBean<AccessRecords> resultBean = this.accessRecordsFeign.getListByUserId(userIds);
                if(resultBean.getCode().equals("200")){
                    hashMap.put("result_code", 1);
                    List<AccessRecords> accessRecords = resultBean.getList();
                    List<AccessRecordsApi> apis = new ArrayList<>();
                    for(AccessRecords a:accessRecords){
                        apis.add(PojoToApi.accessRecordsToApi(a));
                    }
                    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 accessRecords
     * 描述：根据memberId查询出入记录列表
     * @return ResultBean
     * @author lianghanqiang
     * @date
     */
    @PostMapping("/getAccessRecordPage")
    public ResultBean getAccessRecords(@RequestBody AccessRecordsDto accessRecords){
        return accessRecordsFeign.accessRecordsListByMemberId(accessRecords);
    }
}
