package com.sptg.gateway.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.estate.api.dto.BuildingDto;
import com.sptg.estate.api.dto.GuestRecordDto;
import com.sptg.estate.api.dto.PratiqueDto;
import com.sptg.estate.api.dto.UnitDto;
import com.sptg.estate.api.entity.*;
import com.sptg.estate.api.feigns.*;
import com.sptg.gateway.entity.BuildingApi;
import com.sptg.gateway.entity.ResidentialApi;
import com.sptg.gateway.entity.RoomApi;
import com.sptg.gateway.entity.UnitApi;
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.entity.Organization;
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 com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.bytedeco.opencv.presets.opencv_core;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * title: EstateController
 * package: com.sptg.gateway.controller
 * description: 小区管理控制器--查询小区信息接口
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/pratique")
public class PratiqueController {

    private final PratiqueFeign pratiqueFeign;

    /**
     * 功能描述: 通过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 {
            Pratique pratique = this.pratiqueFeign.findPratiqueById(id);
            if (null != pratique) {
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "查询数据成功");
                hashMap.put("pratique", pratique);
            } 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,
     *      *            }
     * @return ResultBean<hashMap> 返回结果
     */
    @PostMapping(value = "/getListByMemberId")
    public Map<String, Object> getListByMemberId(@RequestBody Map<String, String> map) {
        log.warn("============================小程序通过会员id获取防疫通行记录(/app/pratique/getListByMemberId)请求数据==============================");
        log.warn(JsonUtils.objectToJson(map));
        Map<String, Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "查询数据失败");
        try {
            PratiqueDto pratiqueDto = new PratiqueDto();
            String member_id = map.get("memberId");
            String createDate = map.get("createDate");
            String current = map.get("current");
            String size = map.get("size");
            String residentialId = map.get("residentialId");
            String residentialName = map.get("residentialName");
            if (StringUtils.isBlank(member_id)) {
                hashMap.put("result_msg", "会员id不能为空");
                return hashMap;
            }
            if(StringUtils.isNotBlank(createDate)){
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                df.setTimeZone(TimeZone.getTimeZone("Etc/GMT-8"));
                pratiqueDto.setMemberId(Long.valueOf(member_id));
                pratiqueDto.setCreateDate(df.parse(createDate));
            }

            Page<Pratique> page = new Page<>();
            if(StringUtils.isNotBlank(current)){
                page.setCurrent(Integer.valueOf(current));
            }else {
                page.setCurrent(1);
            }

            if(StringUtils.isNotBlank(size)){
                page.setSize(Integer.valueOf(size));
            }else {
                page.setSize(10);
            }

            pratiqueDto.setPage(page);

            if(StringUtils.isNotBlank(residentialId)){
                pratiqueDto.setResidentialId(Long.valueOf(residentialId));
            }
            if(StringUtils.isNotBlank(residentialName)){
                pratiqueDto.setResidentialName(residentialName);
            }
            ResultBean<Page<Pratique>> pageResultBean = this.pratiqueFeign.showPratiqueList(pratiqueDto);
            if (pageResultBean != null) {
                hashMap.put("result_code", 1);
                List<Pratique> pratiques = pageResultBean.getData().getRecords();
                Integer total = pratiques.size();
                hashMap.put("list", pratiques);
                hashMap.put("total", total);
                if (pratiques.size() == 0) {
                    hashMap.put("result_msg", "没有查询到相关数据");
                } else {
                    hashMap.put("result_msg", "获取相关数据成功");
                }
            }

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


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

        ResultBean<Pratique> resultBean = this.pratiqueFeign.addPratique(pratique);
        if (resultBean != null && resultBean.getData() != null) {
            hashMap.put("result_code", 1);
            hashMap.put("result_msg", "添加成功");
            hashMap.put("pratique",resultBean.getData());
        }


        return hashMap;
    }

//    @PostMapping(value = "/updateGuestRecord")
//    public Map<String, Object> updateGuestRecord(@RequestBody GuestRecordDto 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) {
//        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;
//    }

}
