package com.qmth.qrzk.repository.user.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qmth.qrzk.paper.vo.Stock;
import com.qmth.qrzk.repository.common.base.BaseDao;
import com.qmth.qrzk.repository.service.query.PaperParams;
import com.qmth.qrzk.repository.service.query.QueryParams;
import com.qmth.qrzk.repository.user.model.Paper;
import com.qmth.qrzk.repository.user.model.PaperPaperBatch;

@Service("paperServiceImpl")
public class PaperServiceImpl{

	@Autowired
	private BaseDao paperDao;
	
	public void insertPaper(Paper paper) {
		paperDao.insert("insertPaper", paper);
	}
	
	public List<Paper> getCheckOverPaper(){
		return paperDao.getList("getCheckOverPaper", null);
	}

	public List<String> getAllCode() {
		List<String> ls=new ArrayList<String>();
		List<Paper> list=paperDao.getList("getAllCodes");
		for(Paper p:list){
			ls.add(p.getCode());
		}
		return ls;
	}
	

	public int getCountByQY(PaperParams pp) {
		return paperDao.get("getCountByQY", pp);
	}

	public void updatePaper(PaperParams pp) {
		paperDao.update("updatePaper", pp);
	}
	
	public int updateInfoPaper(Paper paper) {
		return paperDao.update("updateInfoPaper", paper);
	}

	public List<Paper> getPaperByQY(PaperParams pp) {
		return paperDao.getList("getPaperByQY", pp);
	}
	
	public int queryRepertory(Paper paper) {
		return paperDao.get("queryRepertory", paper);
	}

	public int queryPaperCountByCodeAndBatchId(String code, int batchId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("code", code);
		paramMap.put("batchId", batchId);
		return paperDao.get("queryPaperCountByCodeAndBatchId", paramMap);
	}

	public void addPaper(Paper paper) {
		paperDao.insert("addPaper", paper);
	}
	
	public Paper findPaperById(int id) {
		List<Paper> ls=paperDao.getList("findPaperById", id);
		if(ls!=null){
			return ls.get(0);
		}
		return null;
	}

	public List<Paper> queryDraftPaperForReview(PaperParams pp) {
		return paperDao.getPagerList("queryDraftPaperForReview", pp);
	}
	
	public List<Paper> queryFormalPaperForUse(PaperParams pp) {
		return paperDao.getPagerList("queryFormalPaperForUse", pp);
	}
	
	public int getTotalCountByBatchId(PaperParams pp) {
		return paperDao.getCount("getTotalCountByBatchId", pp);
	}
	
	public int getTotalPaper(PaperParams pp) {
		return paperDao.getCount("getTotalPaper", pp);
	}
	
	public int getTotalPathPaper(PaperParams pp) {
		return paperDao.getCount("getTotalPathPaper", pp);
	}

	public int getQYListCount(PaperParams pp) {
		return paperDao.get("getQYListCount", pp);
	}

	public List<Paper> queryByBatchId(int batchId) {
		return paperDao.getList("queryByBatchId", batchId);
	}

	public List<Paper> paperSearch(PaperParams pp) {
		return paperDao.getList("paperSearch", pp);
	}
	
	public List<Paper> queryTotal(PaperParams pp) {
		return paperDao.getList("queryTotal", pp);
	}
	
	public int queryTotalCount(PaperParams pp) {
		return paperDao.get("queryTotalCount", pp);
	}
	
	public List<Paper> queryPapers(PaperParams pp){
		return paperDao.getList("queryPapers",pp);
	}

	public int paperSearchCount(PaperParams pp) {
		return paperDao.get("paperSearchCount", pp);
	}

	public void deletePaper(PaperParams pp) {
		paperDao.delete("deletePaper", pp);
	}

	public Paper getPaperByCodeBatchId(String code, int batchId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("code", code);
		paramMap.put("batchId", batchId);
		return paperDao.get("getPaperByCodeBatchId", paramMap);
	}
	
	public List<Paper> getPapersByBatchId(String code, int batchId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("code", code);
		paramMap.put("batchId", batchId);
		return paperDao.getList("getPaperByCodeBatchId", paramMap);
	}

	public int getCountByCode(int batchId, String code) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("code", code);
		paramMap.put("batchId", batchId);
		return paperDao.get("getCountByCode", paramMap);
	}

	public void updateStatusPath(Paper paper) {
		paperDao.update("updateStatusPath", paper);
	}

	public List<Paper> getMPList(PaperParams pp) {
		return paperDao.getList("getMPList", pp);
	}

	public int getMPListCount(PaperParams pp) {
		return paperDao.get("getMPListCount", pp);
	}

	public void removePapersFromBatch(int paperBatchId, Integer[] relatedPaperIds){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("paperBatchId", paperBatchId);
		paramMap.put("relatedPaperIds", relatedPaperIds);
		paperDao.delete("removePaperRelDbfPaperInfos", paramMap);
		paperDao.delete("removePaperPaperBatches", paramMap);
	}
	
	public void removePapersFromOldBatch(Integer[] relatedPaperIds){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("relatedPaperIds", relatedPaperIds);
		paperDao.delete("removePaperPaperBatches", paramMap);
	}
	
	public void insertPaperPaperBatches(int paperBatchId, Integer[] paperIds){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("paperBatchId", paperBatchId);
		paramMap.put("paperIds", paperIds);
		paperDao.insert("insertPaperPaperBatches", paramMap);
	}
	
	public void insertPaperPaperBatch(PaperPaperBatch ppb){
		paperDao.insert("insertPaperPaperBatch", ppb);
	}
	public void deletePaperPaperBatch(PaperPaperBatch ppb){
		paperDao.delete("deletePaperPaperBatch", ppb);
	}

	public List<Paper> queryBatchPapers(PaperParams pp) {
		return paperDao.getPagerList("queryBatchPapers", pp);
	}
	
	public List<Stock> queryStock(QueryParams pp) {
		return paperDao.getList("queryStock", pp);
	}
	
	public int queryStockCount(QueryParams pp) {
		return paperDao.get("queryStockCount", pp);
	}	
}
