package com.sptg.gateway.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.estate.api.dto.GuestRecordDto;
import com.sptg.estate.api.entity.GuestRecord;
import com.sptg.estate.api.entity.MemberAndUser;

import com.sptg.estate.api.feigns.GuestRecordFeign;
import com.sptg.estate.api.feigns.MemberAndUserFeign;
import com.sptg.estate.api.feigns.UserFeign;

import com.sptg.gateway.util.DataCheck;

import com.sptg.permissions.api.entity.ApiLog;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
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.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.swing.text.StyledEditorKit;
import java.util.*;

/**
 * title: GuestRecordController
 * package: com.sptg.estate.biz.security
 * description: 访客登记信息记录控制器
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/guestRecord")
public class GuestRecordController {

    private final OrgFeign orgFeign;
    private final GuestRecordFeign guestRecordFeign;
    private final MemberAndUserFeign memberAndUserFeign;
    private final ApiDockingConfFeign apiDockingConfFeign;


    /**
     * 功能描述: 通过id获取访客登记信息记录详情
     *
     * @param
     * @return ResultBean<hashMap> 返回结果
     */
    @PostMapping(value = "/getById/{id}")
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        Map<String, Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "查询数据失败");
        try {
            GuestRecord GuestRecord = this.guestRecordFeign.findGuestRecordById(id);
            if(null != GuestRecord){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "查询数据成功");
                hashMap.put("GuestRecord",GuestRecord);
            }else{
                hashMap.put("result_code", 0);
                hashMap.put("result_msg", "授权记录不存在");

            }

        } catch (Exception e) {
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述: 通过会员id获取访客登记信息记录
     *
     * @param map {
     *            member_id     :   会员id,
     *            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 {
            GuestRecordDto guestRecordDto = new GuestRecordDto();
            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());

                }
                guestRecordDto.setUserIds(userIds);
                String current = map.get("current");//当前页
                String size = map.get("size");
                String id = map.get("id");
                String guestName = map.get("guestName");
                String guestTel = map.get("guestTel");
                String auditStatus = map.get("auditStatus");
                Page<GuestRecord> page = new Page<>();

                if(StringUtils.isNotBlank(current)&&StringUtils.isNotBlank(size)){
                    page.setCurrent(Integer.valueOf(current));
                    page.setSize(Integer.valueOf(size));

                }else{//默认第一页十条
                    page.setCurrent(1);
                    page.setSize(10);
                }
                guestRecordDto.setPage(page);
                if(StringUtils.isNotBlank(id)){
                    guestRecordDto.setId(Long.valueOf(id));
                }

                if(StringUtils.isNotBlank(guestName)){
                    guestRecordDto.setGuestName(guestName);
                }

                if(StringUtils.isNotBlank(guestTel)){
                    guestRecordDto.setGuestTel(guestTel);
                }

                if(StringUtils.isNotBlank(auditStatus)){
                    guestRecordDto.setAuditStatus(Integer.valueOf(auditStatus));
                }

//                ResultBean<GuestRecord> resultBean = this.guestRecordFeign.getListByUserId(userIds);

                Page<GuestRecord> guestRecordPage = this.guestRecordFeign.showGuestRecordList(guestRecordDto);
                if (guestRecordPage!=null) {
                    hashMap.put("result_code", 1);
                    List<GuestRecord> guestRecords = guestRecordPage.getRecords();
                    Integer total = guestRecords.size();
                    hashMap.put("list",guestRecords);
                    hashMap.put("total",total);
                    if (guestRecords.size() == 0) {
                        hashMap.put("result_msg", "没有查询到相关数据");
                    } else {
                        hashMap.put("result_msg", "获取相关数据成功");
                    }
                }
            }
        } catch (Exception e) {
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }


    @PostMapping(value = "/addGuestRecord")
    public Map<String, Object> addGuestRecord(@RequestBody GuestRecord guestRecord) {
        log.warn("============================小程序添加访客记录(/app/guestRecord/addGuestRecord)请求数据==============================");
        log.warn(JsonUtils.objectToJson(guestRecord));
        Map<String, Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "添加失败");

        ResultBean<GuestRecord> resultBean = this.guestRecordFeign.addGuestRecord(guestRecord);
        if(resultBean != null && resultBean.getData() !=null){
            hashMap.put("result_code", 1);
            hashMap.put("result_msg", "添加成功");
            hashMap.put("guestRecordId",resultBean.getData().getId());
        }


        return hashMap;
    }

    @PostMapping(value = "/updateGuestRecord")
    public Map<String, Object> updateGuestRecord(@RequestBody GuestRecordDto guestRecord) {
        log.warn("============================小程序更新访客记录(/app/guestRecord/updateGuestRecord)请求数据==============================");
        log.warn(JsonUtils.objectToJson(guestRecord));
        Map<String, Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "修改失败");
        if(guestRecord== null || guestRecord.getId()==null){
            hashMap.put("result_msg","修改的参数对象必须有id");
            return hashMap;
        }
        ResultBean<Boolean> resultBean = this.guestRecordFeign.updateGuestRecord(guestRecord);
        if(resultBean != null && resultBean.getData() !=null){
            hashMap.put("result_code", 1);
            hashMap.put("result_msg", "修改成功");
            hashMap.put("guestRecordId",guestRecord.getId());
        }


        return hashMap;
    }


    /**
     * 功能描述: 通过访客授权id和openId获取访客登记信息记录
     *
     * @param
     * @return ResultBean<hashMap> 返回结果
     */
    @PostMapping(value = "/getByCondition")
    public Map<String, Object> getByCondition(@RequestBody GuestRecord guestRecord) {
        log.warn("============================小程序通过访客授权id和openId获取访客登记信息记录(/app/guestRecord/getByCondition)请求数据==============================");
        log.warn(JsonUtils.objectToJson(guestRecord));
        Map<String, Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "查询数据失败");
        try {
            List<GuestRecord> guestRecords = this.guestRecordFeign.findByCondition(guestRecord);
            if(null != guestRecords&&guestRecords.size()>0){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "查询数据成功");
                hashMap.put("guestRecords",guestRecords);
            }else{
                hashMap.put("result_code", 0);
                hashMap.put("result_msg", "记录不存在");

            }

        } catch (Exception e) {
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

}
