package org.springblade.openinterface.dianxin.xiaoyigj.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springblade.community.agency.entity.Agency;
import org.springblade.community.agency.service.IAgencyService;
import org.springblade.community.houseuser.entity.Houseuser;
import org.springblade.community.houseuser.service.IHouseuserService;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.tool.api.R;
import org.springblade.openinterface.dianxin.constants.JiYueConf;
import org.springblade.openinterface.dianxin.util.MyHttpUtil;
import org.springblade.openinterface.dianxin.util.StringUtil;
import org.springblade.openinterface.dianxin.xiaoyigj.entity.JiYueUser;
import org.springblade.openinterface.dianxin.util.AuthUtil;
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.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @author lidexi
 * @date 2021/6/22 18:53
 * 宁夏社区分平台数据推送到集约平台控制层
 */

@RequestMapping("/jiyue/push")
@RestController
@AllArgsConstructor
public class DataPushController {

	private final IHouseuserService houseuserService;
	private final IAgencyService agencyService;
	private final BladeLogger bladeLogger;

	/**
	 * 用户数据推送
	 * @param residentialIds 小区id集合
	 * @return
	 */
	@PostMapping("/userDataPush")
	public R userDataPush(@RequestParam String residentialIds){



		List<Houseuser> houseusers = houseuserService.getBaseMapper().selectList(new QueryWrapper<>(new Houseuser()).lambda().in(Houseuser::getResidentialId, residentialIds.split(",")));
		System.out.println("总住户："+houseusers.size());
		houseusers.forEach(houseuser -> {
			JiYueUser jiYueUser = new JiYueUser();
			jiYueUser.setUserId(houseuser.getId().toString());
			jiYueUser.setName(houseuser.getName());
			jiYueUser.setPhone(houseuser.getTel());
			jiYueUser.setRole(String.valueOf(1));
			jiYueUser.setCommunityId(houseuser.getResidentialId().toString());
			jiYueUser.setCommunityName(houseuser.getResidentialName());
			Long localCommunityId = houseuser.getAgencyId();
			if(localCommunityId != null){
				Agency agency = agencyService.getById(localCommunityId);
				if(agency != null){
					jiYueUser.setStreet(StringUtil.numberAfterFillZero(agency.getRegionStreet(),12));
					jiYueUser.setDistrict(StringUtil.numberAfterFillZero(agency.getRegionArea(),12));
					jiYueUser.setCity(StringUtil.numberAfterFillZero(agency.getRegionCity(),12));
					jiYueUser.setProvince(StringUtil.numberAfterFillZero(agency.getRegionProvince(),12));
					//待定
					jiYueUser.setPlatformId("64");
					jiYueUser.setAction("add");

					ResponseEntity responseEntity = MyHttpUtil.post(jiYueUser,JiYueConf.UrlConstant.USERPUSH);
					System.out.println(responseEntity.toString());

				}
			}
		});
		return R.data(true);
	}



}
