package com.dfyy.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dfyy.bussiness.DjzPesticide;
import com.dfyy.dao.DjzAddDao;
import com.dfyy.dao.DjzPesticideCompositionDao;
import com.dfyy.dao.DjzPesticideDao;
import com.dfyy.dao.DjzPesticideDosageDao;
import com.dfyy.dao.SUserDao;

@Service
@Transactional
public class DjzPesticideService {

	@Autowired
	private DjzPesticideDao dao;

	@Autowired
	private DjzPesticideCompositionDao compositionDao;

	@Autowired
	private DjzPesticideDosageDao dosageDao;

	@Autowired
	private DjzAddDao adddao;

	@Autowired
	private SUserDao suserDao;

	@Autowired
	private PointLogService logService;

	@Autowired
	private ActiveLogService activeLogService;

	public List<DjzPesticide> getByPage(int page, Integer size) {
		List<DjzPesticide> all = dao.getRecords(new PageRequest(page, size));
		return all;

	}

	public int allCount() {
		return dao.getCount();
	}

	public List<DjzPesticide> search(String context, int page, Integer size) {
		List<DjzPesticide> all = dao.search(context, new PageRequest(page, size));
		return all;
	}

	public int searchCount(String context) {
		return dao.searchCount(context);
	}

	public DjzPesticide getById(int id) {
		DjzPesticide all = dao.findOne(id);
		all.setComposition(compositionDao.getRecords(id));
		all.setDosage(dosageDao.getRecords(id));
		return all;
	}

	public List<DjzPesticide> findByCode(String code) {
		List<DjzPesticide> all = dao.findByCode(code);
		return all;
	}

	public void create(DjzPesticide dto) {
		DjzPesticide disease = new DjzPesticide();
		disease.setCode(dto.getCode());
		disease.setName(dto.getName());
		disease.setType(dto.getType());
		disease.setSpname(dto.getSpname());
		disease.setManufacturer(dto.getManufacturer());
		disease.setToxicity(dto.getToxicity());
		disease.setFormulation(dto.getFormulation());
		disease.setTotalcontent(dto.getTotalcontent());
		disease.setCountry(dto.getCountry());
		disease.setRemark(dto.getRemark());
		disease.setStart(dto.getStart());
		disease.setClose(dto.getClose());
		disease.setServicetel(dto.getServicetel());
		disease.setWebsite(dto.getWebsite());
		disease.setStatus(0);
		dao.save(disease);

		adddao.update(dto.getCode());

	}

	public void edit(DjzPesticide dto) {
		DjzPesticide disease = dao.findOne(dto.getId());
		disease.setCode(dto.getCode());
		disease.setName(dto.getName());
		disease.setCode(dto.getCode());
		disease.setName(dto.getName());
		disease.setType(dto.getType());
		disease.setSpname(dto.getSpname());
		disease.setManufacturer(dto.getManufacturer());
		disease.setToxicity(dto.getToxicity());
		disease.setFormulation(dto.getFormulation());
		disease.setTotalcontent(dto.getTotalcontent());
		disease.setCountry(dto.getCountry());
		disease.setRemark(dto.getRemark());
		disease.setStart(dto.getStart());
		disease.setClose(dto.getClose());
		disease.setServicetel(dto.getServicetel());
		disease.setWebsite(dto.getWebsite());

		dao.save(disease);

		adddao.update(dto.getCode());

	}

	public void delete(Integer id) {
		DjzPesticide disease = dao.findOne(id);
		disease.setStatus(-1);
		dao.save(disease);
	}
}
