package org.springblade.modules.rhzf.fwInfo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.common.controller.vo.webService.WebServiceVO;
import org.springblade.common.controller.vo.webService.YwxxVO;
import org.springblade.common.utils.WebserviceUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.rhzf.fwInfo.entity.FwInfoEntity;
import org.springblade.modules.rhzf.fwInfo.entity.Fwccjgb;
import org.springblade.modules.rhzf.fwInfo.excel.FwInfoExcel;
import org.springblade.modules.rhzf.fwInfo.mapper.FwInfoMapper;
import org.springblade.modules.rhzf.fwInfo.service.IFwInfoService;
import org.springblade.modules.rhzf.fwInfo.vo.*;
import org.springblade.modules.rhzf.fwRy.vo.FwRyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 入户走访_房屋_基本信息 服务实现类
 *
 * @author BladeX
 * @since 2023-11-14
 */
@Service
public class FwInfoServiceImpl extends BaseServiceImpl<FwInfoMapper, FwInfoEntity> implements IFwInfoService {

//	@Autowired
//	RyCzrkMapper ryCzrkMapper;
//	@Autowired
//	RyJzrkMapper ryJzrkMapper;
//	@Autowired
//	RyJwrkMapper ryJwrkMapper;
//	@Autowired
//	RyWlrkMapper ryWlrkMapper;
//	@Autowired
//	RyZzrkMapper ryZzrkMapper;
//	@Autowired
//	FwRyMapper fwRyMapper;
	@Autowired
	private FwInfoJlServiceImpl fwInfoJlService;

	@Override
	public IPage<FwInfoVO>  selectFwInfoPage(IPage<FwInfoVO> page, FwInfoVO fwInfo) {
		List<FwInfoVO> fwInfoVOS = baseMapper.selectFwInfoPage(page, fwInfo);
		if(fwInfoVOS.size()>0){
			for (int i = 0; i < fwInfoVOS.size(); i++) {
				FwInfoVO fwInfoVO = fwInfoVOS.get(i);
				String dzbm = fwInfoVO.getDzbm();
				fwInfoVO.setDzXz(baseMapper.getFwxzByRkDzxx(dzbm));
			}
		}
		return page.setRecords(fwInfoVOS);
	}

	@Override
	public String getFwxzByRkDzxx(String dzbm) {
		return baseMapper.getFwxzByRkDzxx(dzbm);
	}


	@Override
	public List<FwInfoExcel> exportFwInfo(FwInfoVO fwInfo) {
		List<FwInfoExcel> fwInfoList = baseMapper.exportFwInfo(fwInfo);
//		if (fwInfoList.size() > 0) {
//			for (int i = 0; i < fwInfoList.size(); i++) {
//				FwInfoExcel fwInfoExcel = fwInfoList.get(i);
//				String dzbm = fwInfoExcel.getDzbm();
//				fwInfoExcel.setDzXz(baseMapper.getFwxzByRkDzxx(dzbm));
//			}
//		}
		//fwInfoList.forEach(fwInfo -> {
		//	fwInfo.setTypeName(DictCache.getValue(DictEnum.YES_NO, FwInfo.getType()));
		//});
		return fwInfoList;
	}

	@Override
	public IPage<LdBzdzVO> getLdList(IPage<LdBzdzVO> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.getLdList(page, params));
	}

	@Override
	public List<DyVO> getDyList(Map<String, Object> params) {
		return baseMapper.getDyList(params);
	}

	@Override
	public List<LcVO> getLcList(Map<String, Object> params) {
		return baseMapper.getLcList(params);
	}

	@Override
	public List<FjVO> getFjList(Map<String, Object> params) {
		return baseMapper.getFjList(params);
	}

	@Override
	public RhzfSaveVO getFjDetail(Map<String, Object> params) {
		return baseMapper.getFjDetail(params);
	}
	@Override
	public RhzfSaveVO getFjDetailByFwIdAndDzbm(Map<String, Object> params) {
		return baseMapper.getFjDetailByFwIdAndDzbm(params);
	}

	@Override
	public List<FwRyVO> getRyList(Map<String, Object> params) {
		return baseMapper.getRyList(params);
	}

	@Override
	public SyjzryVO getCzrk(Map<String, Object> params) {
		return baseMapper.getCzrk(params);
	}

	@Override
	public SyjzryVO getJwrk(Map<String, Object> params) {
		return baseMapper.getJwrk(params);
	}

	@Override
	public SyjzryVO getJzrk(Map<String, Object> params) {
		return baseMapper.getJzrk(params);
	}

	@Override
	public SyjzryVO getWlrk(Map<String, Object> params) {
		return baseMapper.getWlrk(params);
	}

	@Override
	public SyjzryVO getZzrk(Map<String, Object> params) {
		return baseMapper.getZzrk(params);
	}

	@Override
	public FwInfoEntity getFwIsExist(FwInfoEntity fwInfo) {
		return baseMapper.getFwIsExist(fwInfo);
	}
	@Override
	public FwInfoEntity getFwByFwidAndDzbm(Map<String, Object> params) {
		return baseMapper.getFwByFwidAndDzbm(params);
	}
	@Override
	public List<FwInfoVO> getFwByRysfzh(String sfzh) {
		return baseMapper.getFwByRysfzh(sfzh);
	}

	@Override
	public int getFjNum(FwInfoEntity fwInfo) {
		return baseMapper.getFjNum(fwInfo);
	}

	@Override
	public String getCzrkInfo(WebServiceVO webServiceVO) {
		String jsonString = JSONObject.toJSONString(webServiceVO, false);
		// 接口返回结果
		String responseString = WebserviceUtil.commonInvokeQuality(
			"http://10.106.52.152:5000/sqjwService/services/querybzdz",
			"http://www.cchongda.com.cn/sqjwService/services/querybzdz",
			"execute", jsonString);
		return responseString;
	}

	@Override
	public YwxxVO getYwxxParams(Long userId) {
		return baseMapper.getYwxxParams(userId);
	}

	@Override
	public Integer getTimes(String dzbm) {
		return baseMapper.getTimes(dzbm);
	}

	@Override
	public List<LdBzdzVO> getJzwList(Map<String, Object> params) {
		return baseMapper.getJzwList(params);
	}

	@Override
	public List<LdBzdzVO> getDzList(Map<String, Object> params) {
		return baseMapper.getDzList(params);
	}

	@Override
	public List<LdBzdzVO> getFjJzwList(Map<String, Object> params) {
		return baseMapper.getFjJzwList(params);
	}

	@Override
	public int getRhzfCs(Long userId) {
		return baseMapper.getRhzfCs(userId);
	}

	@Override
	public List<FwInfoExcel> getExportRyList(List<FwInfoExcel> list) {
		if(list.size()>0){
			for (FwInfoExcel fwInfoExcel:list) {
				Long fwid = fwInfoExcel.getFwid();
				String ryxx = "";
				List<FwInfoExcel> fwRyAll = baseMapper.getFwRyAll(fwid);
				if(fwRyAll.size()>0){
					for (FwInfoExcel fwRy:fwRyAll) {
						ryxx += fwRy.getRyxm() + "," + fwRy.getRydh() + "," + fwRy.getRyzjhm() + "," + fwRy.getRylb() + "\n";
					}
				}
				fwInfoExcel.setFwrycount(ryxx);
//				QueryWrapper<FwRyEntity> fwryqe = new QueryWrapper<>();
//				fwryqe.eq("fw_id",fwid);
//				fwryqe.eq("is_deleted",0);
//				List<FwRyEntity> ryEntities = fwRyMapper.selectList(fwryqe);
//				if(ryEntities.size()>0){
//					for (int j = 0; j < ryEntities.size(); j++) {
//						if(ryEntities.get(j).getRyType() == "czrk"){
//							ExportFwRyExcel czFwRyExcel = new ExportFwRyExcel();
//							RyCzrkEntity ryCzrkEntity = ryCzrkMapper.selectById(ryEntities.get(j).getRyId());
//							czFwRyExcel.setDeptName(fwInfoExcel.getDeptName());
//							czFwRyExcel.setCreateTime(fwInfoExcel.getCreateTime());
//							czFwRyExcel.setDzXz(fwInfoExcel.getDzXz());
//							czFwRyExcel.setFwJs(fwInfoExcel.getFwJs());
//							czFwRyExcel.setFwxz(fwInfoExcel.getFwxz());
//							czFwRyExcel.setFwyt(fwInfoExcel.getFwyt());
//							czFwRyExcel.setUserName(fwInfoExcel.getUserName());
//							czFwRyExcel.setFwMj(fwInfoExcel.getFwMj());
//							czFwRyExcel.setRyxm(ryCzrkEntity.getRyXm());
//							czFwRyExcel.setRylb("常住");
//							czFwRyExcel.setRyHzgx(ryCzrkEntity.getRyHzgx());
//							czFwRyExcel.setRydh(ryCzrkEntity.getRyDh());
//							czFwRyExcel.setRyzjhm(ryCzrkEntity.getRySfzh());
//							exportFwRyExcels.add(czFwRyExcel);
//						}
//						if(ryEntities.get(j).getRyType() == "jwrk"){
//							ExportFwRyExcel jwFwRyExcel = new ExportFwRyExcel();
//							RyJwrkEntity ryJwrkEntity = ryJwrkMapper.selectById(ryEntities.get(j).getRyId());
//							jwFwRyExcel.setDeptName(fwInfoExcel.getDeptName());
//							jwFwRyExcel.setCreateTime(fwInfoExcel.getCreateTime());
//							jwFwRyExcel.setDzXz(fwInfoExcel.getDzXz());
//							jwFwRyExcel.setFwJs(fwInfoExcel.getFwJs());
//							jwFwRyExcel.setFwxz(fwInfoExcel.getFwxz());
//							jwFwRyExcel.setFwyt(fwInfoExcel.getFwyt());
//							jwFwRyExcel.setUserName(fwInfoExcel.getUserName());
//							jwFwRyExcel.setFwMj(fwInfoExcel.getFwMj());
//							jwFwRyExcel.setRyxm(ryJwrkEntity.getRyXs()+ryJwrkEntity.getRyMz());
//							jwFwRyExcel.setRylb("境外");
//							jwFwRyExcel.setRyHzgx("");
//							jwFwRyExcel.setRydh(ryJwrkEntity.getRyDh());
//							jwFwRyExcel.setRyzjhm(ryJwrkEntity.getRyZjhm());
//							exportFwRyExcels.add(jwFwRyExcel);
//						}
//						if(ryEntities.get(j).getRyType() == "jzrk"){
//							ExportFwRyExcel jzFwRyExcel = new ExportFwRyExcel();
//							RyJzrkEntity ryJzrkEntity = ryJzrkMapper.selectById(ryEntities.get(j).getRyId());
//							jzFwRyExcel.setDeptName(fwInfoExcel.getDeptName());
//							jzFwRyExcel.setCreateTime(fwInfoExcel.getCreateTime());
//							jzFwRyExcel.setDzXz(fwInfoExcel.getDzXz());
//							jzFwRyExcel.setFwJs(fwInfoExcel.getFwJs());
//							jzFwRyExcel.setFwxz(fwInfoExcel.getFwxz());
//							jzFwRyExcel.setFwyt(fwInfoExcel.getFwyt());
//							jzFwRyExcel.setUserName(fwInfoExcel.getUserName());
//							jzFwRyExcel.setFwMj(fwInfoExcel.getFwMj());
//							jzFwRyExcel.setRyxm(ryJzrkEntity.getRyXm());
//							jzFwRyExcel.setRylb("寄住");
//							jzFwRyExcel.setRyHzgx(ryJzrkEntity.getRyHzgx());
//							jzFwRyExcel.setRydh(ryJzrkEntity.getRyDh());
//							jzFwRyExcel.setRyzjhm(ryJzrkEntity.getRySfzh());
//							exportFwRyExcels.add(jzFwRyExcel);
//						}
//						if(ryEntities.get(j).getRyType() == "wlrk"){
//							ExportFwRyExcel wlFwRyExcel = new ExportFwRyExcel();
//							RyWlrkEntity ryWlrkEntity = ryWlrkMapper.selectById(ryEntities.get(j).getRyId());
//							wlFwRyExcel.setDeptName(fwInfoExcel.getDeptName());
//							wlFwRyExcel.setCreateTime(fwInfoExcel.getCreateTime());
//							wlFwRyExcel.setDzXz(fwInfoExcel.getDzXz());
//							wlFwRyExcel.setFwJs(fwInfoExcel.getFwJs());
//							wlFwRyExcel.setFwxz(fwInfoExcel.getFwxz());
//							wlFwRyExcel.setFwyt(fwInfoExcel.getFwyt());
//							wlFwRyExcel.setUserName(fwInfoExcel.getUserName());
//							wlFwRyExcel.setFwMj(fwInfoExcel.getFwMj());
//							wlFwRyExcel.setRyxm(ryWlrkEntity.getRyXm());
//							wlFwRyExcel.setRylb("外来");
//							wlFwRyExcel.setRyHzgx(ryWlrkEntity.getRyHzgx());
//							wlFwRyExcel.setRydh(ryWlrkEntity.getRyDh());
//							wlFwRyExcel.setRyzjhm(ryWlrkEntity.getRySfzh());
//							exportFwRyExcels.add(wlFwRyExcel);
//						}
//						if(ryEntities.get(j).getRyType() == "zzrk"){
//							ExportFwRyExcel zzFwRyExcel = new ExportFwRyExcel();
//							RyZzrkEntity ryZzrkEntity = ryZzrkMapper.selectById(ryEntities.get(j).getRyId());
//							zzFwRyExcel.setCreateTime(fwInfoExcel.getCreateTime());
//							zzFwRyExcel.setDzXz(fwInfoExcel.getDzXz());
//							zzFwRyExcel.setFwJs(fwInfoExcel.getFwJs());
//							zzFwRyExcel.setFwxz(fwInfoExcel.getFwxz());
//							zzFwRyExcel.setFwyt(fwInfoExcel.getFwyt());
//							zzFwRyExcel.setUserName(fwInfoExcel.getUserName());
//							zzFwRyExcel.setFwMj(fwInfoExcel.getFwMj());
//							zzFwRyExcel.setRyxm(ryZzrkEntity.getRyXm());
//							zzFwRyExcel.setRylb("暂住");
//							zzFwRyExcel.setRyHzgx(ryZzrkEntity.getRyHzgx());
//							zzFwRyExcel.setRydh(ryZzrkEntity.getRyDh());
//							zzFwRyExcel.setRyzjhm(ryZzrkEntity.getRySfzh());
//							exportFwRyExcels.add(zzFwRyExcel);
//						}
//					}
//				}else{
//					ExportFwRyExcel kfFwRyExcel = new ExportFwRyExcel();
//					kfFwRyExcel.setDeptName(fwInfoExcel.getDeptName());
//					kfFwRyExcel.setCreateTime(fwInfoExcel.getCreateTime());
//					kfFwRyExcel.setDzXz(fwInfoExcel.getDzXz());
//					kfFwRyExcel.setFwJs(fwInfoExcel.getFwJs());
//					kfFwRyExcel.setFwxz(fwInfoExcel.getFwxz());
//					kfFwRyExcel.setFwyt(fwInfoExcel.getFwyt());
//					kfFwRyExcel.setUserName(fwInfoExcel.getUserName());
//					kfFwRyExcel.setFwMj(fwInfoExcel.getFwMj());
//					exportFwRyExcels.add(kfFwRyExcel);
//				}

			}
		}
		return list;
	}

	@Override
	public List<LdBzdzVO> getCsLdList(List<Long> ldphxlhList) {
		return baseMapper.getCsLdList(ldphxlhList);
	}

	@Override
	public int getFwSl(Long userId) {
		return baseMapper.getFwSl(userId);
	}

	@Override
	public RkxxVO getRyxxBySfzh(Map<String, String> params) {
		return baseMapper.getRyxxBySfzh(params);
	}

	@Override
	public int getKfsl(Long userId) {
		return baseMapper.getKfsl(userId);
	}

	@Override
	public String getTarget(String userAccount) {
		return baseMapper.getTarget(userAccount);
	}

	@Override
	public List<FwInfoVO> getMphxlhByLdphxlh(String ldphxlh) {
		return baseMapper.getMphxlhByLdphxlh(ldphxlh);
	}

	@Override
	public FwInfoVO selectFwInfo(FwInfoVO fwInfo, String dzbm) {
		FwInfoVO fw = baseMapper.selectFwInfo(fwInfo, dzbm);
		if(fw != null){
			fw.setDzXz(baseMapper.getFwxzByRkDzxx(fw.getDzbm()));
		}
		return fw;
	}

	@Override
	public String getTargetDm(String gajgdm) {
		return baseMapper.getTargetDm(gajgdm);
	}

	@Override
	public List<LdBzdzVO> getLdByJg(List<String> jgIdList) {
		return baseMapper.getLdByJg(jgIdList);
	}

	@Override
	public List<Fwccjgb> getJcdList(String pc) {
		return baseMapper.getJcdList(pc);
	}

	@DS("jl")
	@Override
	public void saveToJl(FwInfoEntity fwInfo) {
		fwInfoJlService.save(fwInfo);
	}

	@DS("jl")
	@Override
	public void updateByIdToJl(FwInfoEntity fwInfo) {
		fwInfoJlService.updateById(fwInfo);
	}

	@Override
	public void gxUpdateTime(Long fwId) {
		baseMapper.gxUpdateTime(fwId);
	}

	@Override
	public int countByType(int type, Map<String, Object> params) {
		return baseMapper.countByType(type, params);
	}

	@Override
	public void updateMdId(Long fwId, Long mdId) {
		baseMapper.updateMdId(fwId, mdId);
	}

	@Override
	public IPage<RhzfHistoryVO> history(IPage<RhzfHistoryVO> page, Map<String, Object> params) {
		List<RhzfHistoryVO> historyList = baseMapper.history(page, params);
		return page.setRecords(historyList);
	}
}
