package org.springblade.datamigration.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.HttpConstant;
import org.springblade.community.entity.Room;
import org.springblade.community.feign.IRoomClient;
import org.springblade.core.tool.api.R;
import org.springblade.datamigration.constant.UrlConstants;
import org.springblade.datamigration.util.TokenUtil;
import org.springblade.datamigration.util.WuyeSystemHttpUtil;
import org.springblade.device.entity.DoorDevice;
import org.springblade.estate.entity.ApplyUser;
import org.springblade.estate.feign.IApplyUserClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lidexi
 * @date 2021/6/15 5:46
 *
 * 物业数据迁移
 */
@RestController
@RequestMapping("/data/migration/estate")
@AllArgsConstructor
public class EstateController {

	private final RestTemplate restTemplate = new RestTemplate();
	private final IApplyUserClient applyUserClient;
	private final IRoomClient roomClient;

	@PostMapping("/applyuser")
	public R applyuser(){

		String token = TokenUtil.getWuyeSystemToken();
		Map<String, Object> map = new HashMap<>();
		map.put("size", Integer.MAX_VALUE);
		map.put("current",1);
		R r = WuyeSystemHttpUtil.postQuery(JSONUtil.toJsonStr(map), UrlConstants.estateService.APPLYUSER.getUrl(), token);
		Map data = (Map) r.getData();
		data = (Map) data.get("data");
		JSONArray records = JSONUtil.parseArray(data.get("records"));
		records.forEach(item -> {
			JSONObject jsonObject = JSONUtil.parseObj(item);
			Long oldId = Long.valueOf(jsonObject.get("id").toString());
			ApplyUser isExist = applyUserClient.getById(oldId);
			ApplyUser applyUser = this.buildApplyUser(jsonObject);
			if (isExist == null) {//新增

				applyUserClient.save(applyUser);
			} else {
				applyUserClient.update(applyUser);
			}
		});
		return R.data(true);
	}

	private ApplyUser buildApplyUser(JSONObject jsonObject) {
		ApplyUser applyUser = new ApplyUser();

		applyUser.setId(jsonObject.getLong("id"));
		applyUser.setUserId(jsonObject.getLong("userId"));
		applyUser.setMemberId(jsonObject.getLong("memberId"));
		applyUser.setName(jsonObject.getStr("name"));
		applyUser.setRoomId(jsonObject.getLong("roomId"));
		applyUser.setRoomName(jsonObject.getStr("roomName"));
		Room room = roomClient.getById(applyUser.getRoomId());
		if(room != null){
			applyUser.setUnitId(room.getUnitId());
			applyUser.setUnitName(room.getUnitName());
			applyUser.setBuildingId(room.getBuildingId());
			applyUser.setBuildingName(room.getBuildingName());
			applyUser.setResidentialId(room.getResidentialId());
			applyUser.setResidentialName(room.getResidentialName());
		}

		applyUser.setAuditStatus(jsonObject.getInt("auditStatus"));
		applyUser.setCheckOpinion(jsonObject.getStr("checkOpinion"));
		applyUser.setCheckDate(jsonObject.getDate("checkDate"));
		applyUser.setType(jsonObject.getInt("type"));
		applyUser.setNationality(jsonObject.getInt("nationality"));
		applyUser.setIdCard(jsonObject.getStr("idCard"));
		applyUser.setPassportImgUri(jsonObject.getStr("passportImgUri"));
		applyUser.setEntryImgUri(jsonObject.getStr("entryImgUri"));
		applyUser.setPhone(jsonObject.getStr("phone"));
		applyUser.setSex(jsonObject.getInt("sex"));
		applyUser.setCreateType(jsonObject.getInt("createType"));
		applyUser.setCreateTime(jsonObject.getDate("createDate"));
		applyUser.setFaceUrl(jsonObject.getStr("faceUrl"));
		applyUser.setOldFaceUrl(jsonObject.getStr("oldFaceUrl"));
		applyUser.setImageMd5(jsonObject.getStr("faceCode"));
		applyUser.setRecordType(jsonObject.getInt("recordType"));
		applyUser.setOperatorType(jsonObject.getInt("operatorType"));
		applyUser.setOperatorName(jsonObject.getStr("operatorName"));
		return applyUser;
	}

}
