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.DaLqda;
import com.huimv.admin.domain.secondary.DaZsda;
import com.huimv.admin.repo.secondary.DaJqdaRepo;
import com.huimv.admin.repo.secondary.DaLqdaRepository;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

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

	@Autowired
	private DaZsdaRepository rep;

	@Autowired
	private DaJqdaRepo jqdaRepo;

	@Autowired
	private DaLqdaRepository lqdaRep;

	@Value("${MCID}")
	private  Integer MCID ;


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

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

		entity.setMcid(MCID);

		entity.setZfbz(0);
		entity.setXgsj(new Date());
		rep.save(entity);


		DaJqda daJqda1 = jqdaRepo.findMax().get(0);

		String jqid = "000"+(Integer.parseInt(daJqda1.getJQID())+1);
		DaJqda daJqda =new DaJqda() ;
		Integer  id = rep.findMax().get(0);
		daJqda.setZSID(id);
		daJqda.setJQID(jqid);
		daJqda.setXGSJ(new Date());
		daJqda.setZFBZ(0);
		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(MCID);
			entity.setXgsj(new Date());
			entity.setZfbz(0);
			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();
			int size = rep.findAlla().size() ;
			Integer startPage = (pageNum-1) * pageSize;
			List<DaZsda> all ;
			if (name != null && !name.equals("")){
				size = rep.findAllc(name);
				all= rep.findAll(name,startPage,pageSize);
			}else {
				 all = rep.findAllb(startPage,pageSize);
			}

			map.put("total",size);
			map.put("totalPageNum",(size  +  pageSize  - 1) / pageSize);

			List list = new ArrayList();
			for (DaZsda daZsda : all) {
				Map map1 = new HashMap();
				Integer id = daZsda.getId();
				map1.put("id",id);
				map1.put("csq1",daZsda.getCsq1());
				map1.put("csq2",daZsda.getCsq2());
//				lqdaRep.findById(daZsda.getDqlq());
				String dqlq = daZsda.getDqlq();
				map1.put("dqlq",dqlq);
				if (dqlq != null && !dqlq.equals("")){
					map1.put("dqlq",lqdaRep.findBy(Integer.parseInt(dqlq)));
				}


				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("dqlqId",dqlq);
				List<DaJqda> allByZSID = jqdaRepo.findAllByZSID(id);
				if (allByZSID != null && allByZSID.size()>0){
					map1.put("jqid",allByZSID.get(0).getJQID());
				}else {
					map1.put("jqid",null);
				}

				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);
		}
	}
}
