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


import com.huimv.admin.domain.secondary.DaLqda;
import com.huimv.admin.repo.secondary.DaLqdaRepository;
import com.huimv.admin.service.secondary.DaLqdaService;
import com.huimv.admin.utils.Result;
import com.huimv.admin.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.dynamic.datasource.annotation.DS;


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

@Service
@DS("thired")
public class DaLqdaServiceImpl implements DaLqdaService {

	@Autowired
	private DaLqdaRepository rep;

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

			return new Result(ResultCode.FAIL);
		}
		try {
			rep.save(entity);
			/*entity.setMcid(11);
			Integer  id = rep.findMax().get(0);
			entity.setXgsj(new Date());
			DaJqda daJqda =new DaJqda() ;
			daJqda.setZSID(id);
			jqdaRepo.save(daJqda);
			rep.save(entity);*/

			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(DaLqda 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();
			int size = rep.findAll().size() ;
			Integer startPage = (pageNum-1) * pageSize;
			List<DaLqda> all = rep.findAll(name,startPage,pageSize);
			map.put("total",size);
			map.put("totalPageNum",(size  +  pageSize  - 1) / pageSize);
			map.put("data",all);

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




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