package org.springblade.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springblade.core.tool.api.R;
import org.springblade.estate.dto.PratiqueDTO;
import org.springblade.estate.entity.Pratique;
import org.springblade.estate.feign.IPratiqueClient;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
 * Created By lidexi in 2021/5/10
 **/
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/pratique")
@Slf4j
public class PratiqueController {
	private final IPratiqueClient pratiqueClient;

	/**
	 * 功能描述: 通过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 searchEntity = new Pratique();
			searchEntity.setId(id);
			Pratique pratique = this.pratiqueClient.detail(searchEntity);
			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) {

		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;
			}
			pratiqueDto.setMemberId(Long.valueOf(member_id));

			if(StringUtils.isNotBlank(createDate)){
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				df.setTimeZone(TimeZone.getTimeZone("Etc/GMT-8"));
				pratiqueDto.setCreateTime(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);
			}
			R<Page<Pratique>> pageR = this.pratiqueClient.page(pratiqueDto);
			if (pageR != null &&pageR.getData()!=null) {
				hashMap.put("result_code", 1);
				List<Pratique> pratiques = pageR.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) {

		Map<String, Object> hashMap = new HashMap<>();
		//result_code:0-失败  1-成功
		hashMap.put("result_code", 0);
		hashMap.put("result_msg", "添加失败");

		Pratique pratique1 = this.pratiqueClient.save(pratique);
		if (pratique1.getId() != null) {
			hashMap.put("result_code", 1);
			hashMap.put("result_msg", "添加成功");
			hashMap.put("pratique",pratique);
		}


		return hashMap;
	}

}
