package org.springblade.modules.jjdb.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.jjdb.entity.JjdbEntity;
import org.springblade.modules.jjdb.mapper.JjdbMapper;
import org.springblade.modules.jjdb.service.IJjdbService;
import org.springblade.modules.jjdb.vo.JjdbVO;
import org.springblade.modules.mdjfdj.entity.MdjfDjEntity;
import org.springblade.modules.mdjfdj.service.impl.MdjfDjServiceImpl;
import org.springblade.modules.mdjfry.entity.MdjfRyEntity;
import org.springblade.modules.mdjfry.service.impl.MdjfRyServiceImpl;
import org.springblade.modules.mdjftj.entity.MdjfTjEntity;
import org.springblade.modules.mdjftj.service.impl.MdjfTjServiceImpl;
import org.springblade.modules.rwxffkb.entity.RwxffkbEntity;
import org.springblade.modules.rwxffkb.service.impl.RwxffkbServiceImpl;
import org.springblade.modules.system.vo.SqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 接警处置 服务实现类
 *
 * @author ldx
 * @since 2023-09-08
 */
@Service
public class JjdbServiceImpl extends BaseServiceImpl<JjdbMapper, JjdbEntity> implements IJjdbService {

	@Autowired
	JjdbMapper jjdbMapper;
	@Autowired
	MdjfDjServiceImpl mdjfDjService;
	@Autowired
	MdjfTjServiceImpl mdjfTjService;
	@Autowired
	MdjfRyServiceImpl mdjfRyService;
	@Autowired
	RwxffkbServiceImpl rwxffkbService;

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

	@Override
	@Transactional
	public void czJjdb(JjdbEntity jjdb) throws ParseException {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//处置修改 前端校验必填项
		jjdb.setSfcz("已处置");
		if ("是".equals(jjdb.getSfmdjf())) {
			//如实处置为矛盾纠纷，向矛盾纠纷登记表添加数据
			MdjfDjEntity mdjfDjEntity = new MdjfDjEntity();
			//暂时添加的报警内容
			mdjfDjEntity.setJfmc(jjdb.getJfmc());
			//暂时添加的报警内容
			mdjfDjEntity.setJfyy(jjdb.getCjqk());
			//暂时添加的报警时间
			if (StringUtils.isNotBlank(jjdb.getBjsj())) {
				mdjfDjEntity.setJffssj(sd.parse(jjdb.getBjsj().substring(0, 19)));
			}
			//暂时添加的案发地点
			mdjfDjEntity.setJffsdd(jjdb.getAfdd());
			//暂时添加的报警内容
			mdjfDjEntity.setJyaq(jjdb.getBjnr());
			//暂时添加为数据录入时间
			mdjfDjEntity.setJfdjsj(new Date());
			//暂时添加的转至社区
			mdjfDjEntity.setJfsssq(Long.parseLong(jjdb.getZzsq()));
			// 纠纷来源
			mdjfDjEntity.setJfly(jjdb.getJfly());
			// 纠纷类型
			mdjfDjEntity.setJflx(jjdb.getJflx());
			mdjfDjService.save(mdjfDjEntity);
			Long mdid = mdjfDjEntity.getId();
			//处置后添加到调节记录
			MdjfTjEntity mdjfTjEntity = new MdjfTjEntity();
			mdjfTjEntity.setJfId(mdjfDjEntity.getId());
			mdjfTjEntity.setTjsj(new Date());
			mdjfTjEntity.setTjdd(mdjfDjEntity.getJffsdd());
			mdjfTjEntity.setTjxq(jjdb.getPdly());
			mdjfTjEntity.setTjzt(jjdb.getSfytj());
			mdjfTjService.save(mdjfTjEntity);
			//处置后添加信息到任务下发反馈
			/*RwxffkbEntity rwxffkbEntity = new RwxffkbEntity();
			rwxffkbEntity.setRwid(mdid);
			rwxffkbEntity.setRwly("mdjf_dj");
			rwxffkbEntity.setRwlx("矛盾纠纷");
			rwxffkbEntity.setRwxfsj(new Date());
			rwxffkbEntity.setRwmc(jjdb.getJfmc());
			rwxffkbEntity.setZl(jjdb.getSfytj());
			rwxffkbEntity.setBz(jjdb.getBz());
			rwxffkbEntity.setSssq(Long.parseLong(jjdb.getZzsq()));
			rwxffkbService.save(rwxffkbEntity);*/
			//处置后添加信息到调解人员信息
			List<MdjfRyEntity> ryList = new ArrayList<>();
			MdjfRyEntity jfry = new MdjfRyEntity();
			jfry.setJfId(mdid);
			jfry.setSfzh(jjdb.getJfrysfzh());
			jfry.setLb("甲方");
			jfry.setXm(jjdb.getJfry());
			jfry.setLxdh(jjdb.getJfrylxdh());
			MdjfRyEntity yfry = new MdjfRyEntity();
			yfry.setJfId(mdid);
			yfry.setLb("乙方");
			yfry.setSfzh(jjdb.getYfrysfzh());
			yfry.setXm(jjdb.getYfry());
			yfry.setLxdh(jjdb.getYfrylxdh());
			ryList.add(jfry);
			ryList.add(yfry);
			mdjfRyService.saveBatch(ryList);
		}
		jjdbMapper.updateBySjdbh(jjdb);
	}


	@Override
	public JjdbEntity getDetail(String sjdbh) {
		return jjdbMapper.getDetail(sjdbh);
	}

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

}
