package com.huimv.admin.service.secondary.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.huimv.admin.domain.secondary.DaJqda;
import com.huimv.admin.domain.secondary.DaZsda;
import com.huimv.admin.repo.secondary.DaJqdaRepo;
import com.huimv.admin.repo.secondary.DaZsdaRepository;
import com.huimv.admin.service.secondary.DaZsdaService;
import com.huimv.admin.utils.Result;
import com.huimv.admin.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@DS("thired")
public class DaZsdaServiceImpl implements DaZsdaService {

	@Autowired
	private DaZsdaRepository rep;

	@Autowired
	private DaJqdaRepo jqdaRepo;

	@Override
	public Result add(DaZsda entity){
		if (entity == null){

			return new Result(ResultCode.FAIL);
		}
		try {

			entity.setMcid(11);
			Integer  id = rep.findMax().get(0);
			entity.setXgsj(new Date());
			rep.save(entity);


			String  JQID = jqdaRepo.findMax().get(0);

			String jqid = "000"+(Integer.parseInt(JQID)+1);
			DaJqda daJqda =new DaJqda() ;
			daJqda.setZSID(id);
			daJqda.setJQID(jqid);

			jqdaRepo.save(daJqda);


			return new Result(ResultCode.SUCCESS);
		}catch (Exception e){
			return new Result(ResultCode.FAIL);
		}
	}



	@Override
	public Result remove(Integer[] ids) {
		if (ids == null || ids.length==0){
			return new Result(ResultCode.FAIL);
		}
		try {
			for (Integer id : ids) {
				rep.deleteById(id);
			}
			return new Result(ResultCode.SUCCESS);
		}catch (Exception e){
			return new Result(ResultCode.FAIL);
		}
	}

	@Override
	public Result update(DaZsda entity) {
		if (entity == null){
			return new Result(ResultCode.FAIL);
		}
		try {
			entity.setMcid(11);
			entity.setXgsj(new Date());
			rep.save(entity);
			return new Result(ResultCode.SUCCESS);
		}catch (Exception e){
			return new Result(ResultCode.FAIL);
		}
	}

	@Override
	public Result findAll(String name , Integer pageNum , Integer pageSize) {
		try {
			Map map = new HashMap();
			Integer startPage = (pageNum-1) * pageSize;
			List<DaZsda> all = rep.findAll(name,startPage,pageSize);
			int size = all.size() ;
			map.put("total",size);
			map.put("totalPageNum",(size  +  pageSize  - 1) / pageSize);
			Map map1 = new HashMap();
			List list = new ArrayList();
			for (DaZsda daZsda : all) {
				Integer id = daZsda.getId();
				map1.put("id",id);
				map1.put("csq1",daZsda.getCsq1());
				map1.put("csq2",daZsda.getCsq2());
				map1.put("dqlq",daZsda.getDqlq());
				map1.put("flq",daZsda.getFlq());
				map1.put("gly",daZsda.getGly());
				map1.put("glyxm",daZsda.getGlyxm());
				map1.put("hkbz",daZsda.getHkbz());
				map1.put("lcbz",daZsda.getLcbz());
				map1.put("mcid",daZsda.getMcid());
				map1.put("spjk",daZsda.getSpjk());
				map1.put("xgr",daZsda.getXgr());
				map1.put("xgsj",(daZsda.getXgsj()+"").substring(0,10));
				map1.put("yzjd",daZsda.getYzjd());
				map1.put("zdbz",daZsda.getZdbz());
				map1.put("zfbz",daZsda.getZfbz());
				map1.put("zsbz",daZsda.getZsbz());
				map1.put("zsmc",daZsda.getZsmc());
				map1.put("zsqy",daZsda.getZsqy());
				map1.put("ssqy",daZsda.getSsqy());
				map1.put("jqid",jqdaRepo.findAllByZSID(id).get(0).getJQID());
				list.add(map1);
			}
			map.put("data",list);
			
			return new Result(ResultCode.SUCCESS,map);
		}catch (Exception e){
			return new Result(ResultCode.FAIL);
		}
	}

	@Override
	public Result findAllById(Integer id) {
		try {
			DaZsda entity = rep.findById(id).get();
			return new Result(ResultCode.SUCCESS);
		}catch (Exception e){
			return new Result(ResultCode.FAIL);
		}
	}
}
