/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.rhzf.fwRy.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.rhzf.fwInfo.mapper.FwInfoMapper;
import org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO;
import org.springblade.modules.rhzf.fwRy.entity.FwRyEntity;
import org.springblade.modules.rhzf.fwRy.excel.FwRyExcel;
import org.springblade.modules.rhzf.fwRy.excel.RyxqExcel;
import org.springblade.modules.rhzf.fwRy.mapper.FwRyMapper;
import org.springblade.modules.rhzf.fwRy.service.IFwRyService;
import org.springblade.modules.rhzf.fwRy.vo.FwRyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 入户走访_房屋_人员关联表 服务实现类
 *
 * @author BladeX
 * @since 2023-11-14
 */
@Service
public class FwRyServiceImpl extends BaseServiceImpl<FwRyMapper, FwRyEntity> implements IFwRyService {
	@Autowired
	FwInfoMapper fwInfoMapper;

	@Autowired
	private FwRyJlServiceImpl fwRyJlService;

	@Override
	public IPage<FwRyVO> selectFwRyPage(IPage<FwRyVO> page, FwRyVO fwRy) {
		return page.setRecords(baseMapper.selectFwRyPage(page, fwRy));
	}


	@Override
	public List<FwRyExcel> exportFwRy(Wrapper<FwRyEntity> queryWrapper) {
		List<FwRyExcel> fwRyList = baseMapper.exportFwRy(queryWrapper);
		//fwRyList.forEach(fwRy -> {
		//	fwRy.setTypeName(DictCache.getValue(DictEnum.YES_NO, FwRy.getType()));
		//});
		return fwRyList;
	}

	@Override
	public int getCountByRyXlh(String ryjbxxxlh) {
		return baseMapper.getCountByRyXlh(ryjbxxxlh);
	}

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

	@Override
	public List<SyjzryVO> getRyCountByDzbm(Long fwId) {
		List<SyjzryVO> syjzryList2 = new ArrayList<>();
		QueryWrapper<FwRyEntity> qe = new QueryWrapper<>();
		qe.eq("fw_id",fwId);
		List<FwRyEntity> ryEntities = list(qe);
		if(ryEntities.size() > 0){
			Map<String, Object> params = new HashMap<>();
			for (FwRyEntity fwRyVO : ryEntities) {
				SyjzryVO syjzryVO = new SyjzryVO();
				params.put("ryId", fwRyVO.getRyId());
				if (fwRyVO.getRyType().equals("czrk")) {
					syjzryVO = fwInfoMapper.getCzrk(params);
				} else if (fwRyVO.getRyType().equals("jwrk")) {
					syjzryVO = fwInfoMapper.getJwrk(params);
					syjzryVO.setRyYwm(syjzryVO.getRyMz());
				} else if (fwRyVO.getRyType().equals("jzrk")) {
					syjzryVO = fwInfoMapper.getJzrk(params);
				} else if (fwRyVO.getRyType().equals("wlrk")) {
					syjzryVO = fwInfoMapper.getWlrk(params);
				} else if (fwRyVO.getRyType().equals("zzrk")) {
					syjzryVO = fwInfoMapper.getZzrk(params);
				}
				syjzryList2.add(syjzryVO);
			}
		}
		return syjzryList2;
	}

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

	@Override
	public void updateFwId(Long fwId, String dzbm, String ryType, Long ryId) {
		baseMapper.updateFwId(fwId, dzbm, ryType, ryId);
	}

	@Override
	public void deleteByRyidAndType(Map<String, Object> params) {
		baseMapper.deleteByRyidAndType(params);
	}

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

	@Override
	public IPage<SyjzryVO> getFwry(IPage<SyjzryVO> page, SyjzryVO syjzryVO) {
		List<SyjzryVO> fwry = baseMapper.getFwry(page, syjzryVO);
		if(fwry.size()>0){
			for (int i = 0; i < fwry.size(); i++) {
				SyjzryVO syjzryVOs = fwry.get(i);
				String dzbm = syjzryVOs.getDzbm();
				syjzryVOs.setDzXz(fwInfoMapper.getFwxzByRkDzxx(dzbm));
			}
		}
		return page.setRecords(fwry);
	}

	@Override
	public List<SyjzryVO> fwryList(SyjzryVO syjzryVO) {
		return baseMapper.fwryList(syjzryVO);
	}

	@Override
	public List<RyxqExcel> exportRyxq(Map<String, Object> params) {
		List<RyxqExcel> fwry = baseMapper.exportRyxq(params);
		if(fwry.size()>0){
			for (int i = 0; i < fwry.size(); i++) {
				RyxqExcel ryxqExcel = fwry.get(i);
				ryxqExcel.setDzXz(fwInfoMapper.getFwxzByRkDzxx(ryxqExcel.getDzbm()));
			}
		}
		return fwry;
	}

	@DS("jl")
	@Override
	public void saveToJl(FwRyEntity fwRyEntity) {
		fwRyJlService.save(fwRyEntity);
	}

	@DS("jl")
	@Override
	public void updateFwIdToJl(Long fwId, String dzbm, String ryType, Long ryId) {
		baseMapper.updateFwId(fwId, dzbm, ryType, ryId);
	}

	@DS("jl")
	@Override
	public void deleteByRyidAndTypeToJl(Map<String, Object> params) {
		baseMapper.deleteByRyidAndType(params);
	}
}
