/*
 *      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.mdjfdj.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.cache.DictBizCache;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.appmenu.vo.DbsxVO;
import org.springblade.modules.appmenu.vo.RwtxListVO;
import org.springblade.modules.mdjfdj.entity.MdjfDjEntity;
import org.springblade.modules.mdjfdj.excel.MdjfDjExcel;
import org.springblade.modules.mdjfdj.mapper.MdjfDjMapper;
import org.springblade.modules.mdjfdj.service.IMdjfDjService;
import org.springblade.modules.mdjfdj.vo.JqVO;
import org.springblade.modules.mdjfdj.vo.MdjfDjVO;
import org.springblade.modules.mdjfhf.service.impl.HfServiceImpl;
import org.springblade.modules.mdjftj.entity.MdjfTjEntity;
import org.springblade.modules.mdjftj.service.impl.MdjfTjServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 矛盾纠纷登记表 服务实现类
 *
 * @author BladeX
 * @since 2023-08-02
 */
@Service
public class MdjfDjServiceImpl extends BaseServiceImpl<MdjfDjMapper, MdjfDjEntity> implements IMdjfDjService {

	private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	MdjfTjServiceImpl mdjfTjService;
	@Autowired
	HfServiceImpl hfService;
	@Override
	public IPage<MdjfDjVO> selectmdjfDjPage(IPage<MdjfDjVO> page, Map<String, Object> params) {
		List<MdjfDjVO> mdjfDjVOS = baseMapper.selectmdjfDjPage(page, params);
		if(mdjfDjVOS.size()>0){
			for (int i = 0; i < mdjfDjVOS.size(); i++) {
				MdjfDjVO mdjfDjVO = mdjfDjVOS.get(i);
				String mdjbV = mdjfDjVO.getMdjbV();
				if(mdjfDjVO != null){
					if(StringUtils.isNotBlank(mdjfDjVO.getHfztNew())){
						mdjfDjVO.setHfztNewValve(DictBizCache.getValue("hfzt",mdjfDjVO.getHfztNew()));
					}
					if(StringUtils.isNotBlank(mdjbV)){
						mdjfDjVO.setMdjbVVlaue(DictBizCache.getValue("mddj", mdjbV));
						if(mdjfDjVO.getHfjzsj() != null){
							mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj())+"前进行回访");
						}

						if("A".equals(mdjbV)){
							if(mdjfDjVO.getHfjzsj()!=null && mdjfDjVO.getHfjzsj2()!=null){
								if((new Date()).compareTo(mdjfDjVO.getHfjzsj()) > 0 && (new Date()).compareTo(mdjfDjVO.getHfjzsj2()) < 0){
									mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj3())+"前进行回访");
									Integer num1 = hfService.getYhfcsByMdIdAndHfjzsj(mdjfDjVO.getId(), mdjfDjVO.getHfjzsj());
									if(num1 == 0){
										mdjfDjVO.setCscs(1);
										mdjfDjVO.setCszt("1");
									}
								}
							}
							if(mdjfDjVO.getHfjzsj2()!=null && mdjfDjVO.getHfjzsj3()!=null){
								if((new Date()).compareTo(mdjfDjVO.getHfjzsj2()) > 0 && (new Date()).compareTo(mdjfDjVO.getHfjzsj3()) < 0){
									mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj3())+"前进行回访");
									Integer num1 = hfService.getYhfcsByMdIdAndHfjzsj(mdjfDjVO.getId(), mdjfDjVO.getHfjzsj2());
									if(num1 == 0){
										mdjfDjVO.setCscs(2);
										mdjfDjVO.setCszt("1");
									}
									if(num1 == 1 ){
										mdjfDjVO.setCszt("1");
										if(mdjfDjVO.getCscs() == 1){
											mdjfDjVO.setCscs(2);
										}
									}
								}
							}
							if(mdjfDjVO.getHfjzsj3()!=null){
								if((new Date()).compareTo(mdjfDjVO.getHfjzsj3()) > 0){
									mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj3())+"前进行回访");
									Integer num1 =  hfService.getYhfcsByMdIdAndHfjzsj(mdjfDjVO.getId(), mdjfDjVO.getHfjzsj3());
									if(num1 == 0 ){
										mdjfDjVO.setCscs(3);
										mdjfDjVO.setCszt("1");
									}
									if(num1 == 1 ){
										mdjfDjVO.setCszt("1");
										if(mdjfDjVO.getCscs() == 0){
											mdjfDjVO.setCscs(2);
										}
										if(mdjfDjVO.getCscs() == 1){
											mdjfDjVO.setCscs(3);
										}

									}
									if(num1 == 2 ){
										mdjfDjVO.setCszt("1");
										if(mdjfDjVO.getCscs() == 0){
											mdjfDjVO.setCscs(1);
										}
										if(mdjfDjVO.getCscs() == 1){
											mdjfDjVO.setCscs(2);
										}
										if(mdjfDjVO.getCscs() == 2){
											mdjfDjVO.setCscs(3);
										}
									}
								}
							}

						}
						if("B".equals(mdjbV)){
							if(mdjfDjVO.getHfjzsj()!=null && mdjfDjVO.getHfjzsj2()!=null) {
								if ((new Date()).compareTo(mdjfDjVO.getHfjzsj()) > 0 && (new Date()).compareTo(mdjfDjVO.getHfjzsj2()) < 0) {
									mdjfDjVO.setTsnr("请于" + simpleDateFormat.format(mdjfDjVO.getHfjzsj2()) + "前进行回访");
									Integer num1 = hfService.getYhfcsByMdIdAndHfjzsj(mdjfDjVO.getId(), mdjfDjVO.getHfjzsj());
									if (num1 == 0) {
										mdjfDjVO.setCscs(1);
										mdjfDjVO.setCszt("1");
									}
								}
							}
							if(mdjfDjVO.getHfjzsj2()!=null){
								if((new Date()).compareTo(mdjfDjVO.getHfjzsj2()) > 0){
									mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj2())+"前进行回访");
									Integer num1 =  hfService.getYhfcsByMdIdAndHfjzsj(mdjfDjVO.getId(), mdjfDjVO.getHfjzsj2());
									if(num1 == 0 ){
										mdjfDjVO.setCscs(2);
										mdjfDjVO.setCszt("1");
									}
									if(num1 == 1 ){
										mdjfDjVO.setCszt("1");
										if(mdjfDjVO.getCscs() ==1){
											mdjfDjVO.setCscs(2);
										}
										if(mdjfDjVO.getCscs() == 0){
											mdjfDjVO.setCscs(1);
										}


									}
								}
							}

						}
						if("D".equals(mdjbV) || "C".equals(mdjbV)){
							if(mdjfDjVO.getHfjzsj()!=null){
								if((new Date()).compareTo(mdjfDjVO.getHfjzsj()) > 0){
									Integer num1 =  hfService.getYhfcsByMdIdAndHfjzsj(mdjfDjVO.getId(), mdjfDjVO.getHfjzsj());
									if(num1 == 0 ){
										mdjfDjVO.setCscs(1);
										mdjfDjVO.setCszt("1");
									}
								}
							}

						}
					}
					if(StringUtils.isNotBlank(mdjfDjVO.getCszt())){
						mdjfDjVO.setCsztValue(DictBizCache.getValue("cszt",mdjfDjVO.getCszt()));
					}
				}
			}
		}
		return page.setRecords(mdjfDjVOS);
	}


	@Override
	public List<MdjfDjExcel> exportmdjfDj(Map<String, Object> params) {
		List<MdjfDjExcel> mdjfdjList = baseMapper.exportmdjfDj(params);
		if(mdjfdjList.size()>0){
			for (int i = 0; i < mdjfdjList.size(); i++) {
				String hfztNew = mdjfdjList.get(i).getHfztNew();
				if(StringUtils.isNotBlank(hfztNew)){
					mdjfdjList.get(i).setHfztNew(DictBizCache.getValue("hfzt",hfztNew));
				}else{
					mdjfdjList.get(i).setHfztNew("未回访");
				}
			}
		}
		return mdjfdjList;
	}

	@Override
	public MdjfDjEntity getDetail(MdjfDjEntity mdjfdj) {
		return baseMapper.getDetail(mdjfdj);
	}

	@Override
	public IPage<MdjfDjVO> pageToApp(IPage<MdjfDjVO> page, Map<String, Object> params,String[] mdIds) {
		List<MdjfDjVO> mdjfDjVOS = baseMapper.pageToApp(page, params,mdIds);
		if(mdjfDjVOS.size()>0){
			for (int i = 0; i < mdjfDjVOS.size(); i++) {
				MdjfDjVO mdjfDjVO = mdjfDjVOS.get(i);
				String mdjbV = mdjfDjVO.getMdjbV();
				if(mdjfDjVO != null){
					int cscs = 0;
					if(mdjfDjVO.getDychf() == 2){
						cscs++;
					}
					if(mdjfDjVO.getDechf() == 2){
						cscs++;
					}
					if(mdjfDjVO.getDschf() == 2){
						cscs++;
					}
					mdjfDjVO.setCscs(cscs);
					if(StringUtils.isNotBlank(mdjfDjVO.getHfztNew())){
						mdjfDjVO.setHfztNewValve(DictBizCache.getValue("hfzt",mdjfDjVO.getHfztNew()));
					}
					if(StringUtils.isNotBlank(mdjfDjVO.getCszt())){
						mdjfDjVO.setCsztValue(DictBizCache.getValue("cszt",mdjfDjVO.getCszt()));
					}
					if(StringUtils.isNotBlank(mdjbV)){
						mdjfDjVO.setMdjbVVlaue(DictBizCache.getValue("mddj", mdjbV));
						if("0".equals(mdjfDjVO.getHfztNew()) || "1".equals(mdjfDjVO.getHfztNew())){
							if(mdjfDjVO.getHfjzsj() != null){
								mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj())+"前进行回访");
							}
							if("A".equals(mdjbV)){
								if(mdjfDjVO.getHfjzsj()!=null && mdjfDjVO.getHfjzsj2()!=null){
									if((new Date()).compareTo(mdjfDjVO.getHfjzsj()) > 0 && (new Date()).compareTo(mdjfDjVO.getHfjzsj2()) < 0){
										mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj2())+"前进行回访");
									}
								}
								if(mdjfDjVO.getHfjzsj2()!=null && mdjfDjVO.getHfjzsj3()!=null){
									if((new Date()).compareTo(mdjfDjVO.getHfjzsj2()) > 0 && (new Date()).compareTo(mdjfDjVO.getHfjzsj3()) < 0){
										mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj3())+"前进行回访");
									}
								}
								if(mdjfDjVO.getHfjzsj3()!=null){
									if((new Date()).compareTo(mdjfDjVO.getHfjzsj3()) > 0){
										mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj3())+"前进行回访");
									}
								}

							}
							if("B".equals(mdjbV)){
								if(mdjfDjVO.getHfjzsj()!=null && mdjfDjVO.getHfjzsj2()!=null) {
									if ((new Date()).compareTo(mdjfDjVO.getHfjzsj()) > 0 && (new Date()).compareTo(mdjfDjVO.getHfjzsj2()) < 0) {
										mdjfDjVO.setTsnr("请于" + simpleDateFormat.format(mdjfDjVO.getHfjzsj2()) + "前进行回访");

									}
								}
								if(mdjfDjVO.getHfjzsj2()!=null){
									if((new Date()).compareTo(mdjfDjVO.getHfjzsj2()) > 0){
										mdjfDjVO.setTsnr("请于"+simpleDateFormat.format(mdjfDjVO.getHfjzsj2())+"前进行回访");
									}
								}
							}
						}
					}

				}
			}
		}
		return page.setRecords(mdjfDjVOS);
	}



	@Override
	@Transactional
	public void addHuiFang(MdjfDjEntity mdjfdj) {
		mdjfdj.setHfr(AuthUtil.getUserName());
		mdjfdj.setHfsj(new Date());
		saveOrUpdate(mdjfdj);
		MdjfTjEntity mdjfTjEntity = new MdjfTjEntity();
		mdjfTjEntity.setJfId(mdjfdj.getId());
		mdjfTjEntity.setTjzt("调解成功已回访");
		mdjfTjService.updateByHf(mdjfTjEntity);
	}

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

	@Override
	public DbsxVO getDbsxVOtj(String deptId) {
		return baseMapper.getDbsxVOtj(deptId);
	}

	@Override
	public DbsxVO getDbsxVOhf(String deptId) {
		return baseMapper.getDbsxVOhf(deptId);
	}

	@Override
	public List<MdjfDjEntity> getMdHfjzsjIsNotNull() {
		return baseMapper.getMdHfjzsjIsNotNull();
	}

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

	@Override
	public IPage<MdjfDjVO> getMdList(IPage<MdjfDjVO> page, Map<String, Object> params) {
		List<MdjfDjVO> mdjfDjVOS = baseMapper.selectmdjfDjPage(page, params);
		return page.setRecords(mdjfDjVOS);
	}

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

	@Override
	public List<JqVO> getJqTx(String deptId) {
		return baseMapper.getJqTx(deptId);
	}

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

	@DS("jl")
	@Override
	public void saveToJl(MdjfDjEntity mdjfDjEntity) {
		baseMapper.insert(mdjfDjEntity);
	}

	@DS("jl")
	@Override
	public void updateByIdToJl(MdjfDjEntity mdjfdj) {
		baseMapper.updateById(mdjfdj);
	}

	@DS("jl")
	@Override
	public void saveOrUpdateJl(MdjfDjEntity mdjfDjEntity) {
		baseMapper.updateById(mdjfDjEntity);
	}

	@Override
	public RwtxListVO getRwtxVOtj(String deptId) {
		return baseMapper.getRwtxVOtj(deptId);
	}

	@Override
	public RwtxListVO getRwtxVOhf(String deptId) {
		return baseMapper.getRwtxVOhf(deptId);
	}

}
