package cn.com.countrygarden.bi.main.service.canteen.impl;
import cn.com.countrygarden.bi.main.model.PageData;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.sys.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.countrygarden.bi.main.model.canteen.CanteenInvestigate;
import cn.com.countrygarden.bi.main.model.canteen.CanteenScore;
import cn.com.countrygarden.bi.main.model.dup.ContractStatistical;
import cn.com.countrygarden.bi.main.model.review.ReviewQuestionnaire;
import cn.com.countrygarden.bi.main.model.review.ReviewQuestionnaireProblemClassification;
import cn.com.countrygarden.bi.main.dao.canteen.CanteenInvestigateMapper;
import cn.com.countrygarden.bi.main.dao.dup.TbHsprContractMapper;
import cn.com.countrygarden.bi.main.dao.performance.bdpsndocMapper;
import cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireProblemClassificationMapper;
import cn.com.countrygarden.bi.main.dto.CanteenInvestigateqCodeUrlDTO;
import cn.com.countrygarden.bi.main.dto.ReviewQuestioClassificationDTO;
import cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemOptionVo;
import cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemVo;
import cn.com.countrygarden.bi.main.dto.ReviewQuestionnairelistCanteenDTO;
import cn.com.countrygarden.bi.main.dto.bdpsndocDTO;
import cn.com.countrygarden.bi.main.dto.canteenCaBeforeDTO;
import cn.com.countrygarden.bi.main.dto.canteenInvestigationliebiaoDTO;
import cn.com.countrygarden.bi.main.service.canteen.CanteenInvestigateService;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;
import cn.com.countrygarden.bi.main.core.Response;

/**
 * 调查表-业务逻辑层实现
 *
 * @author 吕伟峰
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2018-08-13
 */
@Service
public class CanteenInvestigateServiceImpl implements CanteenInvestigateService{
    @Autowired
    private CanteenInvestigateMapper canteenInvestigateMapper;
    
    @Autowired
    private bdpsndocMapper bdpsndocmapper;

    @Autowired
	private TbHsprContractMapper tbHsprContractMapper;
    
    @Autowired
    private ReviewQuestionnaireProblemClassificationMapper reviewQuestionnaireProblemClassificationMapper;
    
    @Override
    public Result queryPageList(CanteenInvestigate canteenInvestigate, int page, int size) {
        Page<Object> pageList = PageHelper.startPage(page, size);
        Condition condition=new Condition(CanteenInvestigate.class);
        Criteria criteria = condition.createCriteria();
//		TODO 添加条件
		canteenInvestigateMapper.selectByCondition(condition);
        PageData data=new PageData(pageList);
        data.setData(pageList.getResult());
        return Response.response(data);
    }

    @Override
    public Result queryById(Integer id) {
        return Response.response(canteenInvestigateMapper.selectByPrimaryKey(id));
    }

    @Override
    public Result add(CanteenInvestigate canteenInvestigate, AccessToken accessToken) {
        canteenInvestigateMapper.insert(canteenInvestigate);
        return Response.response();
    }

    @Override
    public Result edit(CanteenInvestigate canteenInvestigate, AccessToken accessToken) {
        canteenInvestigateMapper.updateByPrimaryKey(canteenInvestigate);
        return Response.response();
    }

    @Override
    public Result del(Integer id) {
        int count = canteenInvestigateMapper.deleteByPrimaryKey(id);
        return Response.response(count);
    }

	@Override
	public Result canteenrqdtolist(String name, int page, int size, boolean hasRole, String userCode,
			Integer investigationmanagementId) {
		  Page<Object> pageList = PageHelper.startPage(page, size);
		  List<ReviewQuestionnairelistCanteenDTO> canteenrqdtolist=canteenInvestigateMapper.canteenrqdtolist(name, hasRole, userCode, investigationmanagementId);
		  
		  PageData data = new PageData(pageList);
			data.setData(pageList.getResult());
			return Response.response(data);
	}

	@Override
	public Result addcantapp(CanteenInvestigate canteenInvestigate, String qCodeUrl,AccessToken accessToken) {
		
		canteenInvestigate.setCreateBy(accessToken.getUserCode());
		canteenInvestigate.setIspartake(0);
		canteenInvestigate.setFlag(1);
		canteenInvestigateMapper.insert(canteenInvestigate);
		
		List<ContractStatistical> findOrderCode=tbHsprContractMapper.findOrderCode();
		
		for(int i=0;i<findOrderCode.size();i++) {
			ContractStatistical contrac=findOrderCode.get(i);
			if(contrac.getOrganCode().equals(canteenInvestigate.getAreaId())) {
				canteenInvestigateMapper.updateorgancode(canteenInvestigate.getId(), contrac.getOrganName());
			}
		}
		
		  List<ContractStatistical> findCommName=tbHsprContractMapper.findCommName(canteenInvestigate.getAreaId());
		  
		  for(int y=0;y<findCommName.size();y++) {
			  ContractStatistical contrac=findCommName.get(y);
			     if(contrac.getCommId()==canteenInvestigate.getProjectId()) {
			    	 canteenInvestigateMapper.updatefindcomm(canteenInvestigate.getId(), contrac.getCommName());
			     }
		  }
		  
		
		  
		  CanteenInvestigateqCodeUrlDTO canteenCodeUrlDTO=new CanteenInvestigateqCodeUrlDTO();
		  canteenCodeUrlDTO.setCantinid(canteenInvestigate.getId());
		  canteenCodeUrlDTO.setProjectId(canteenInvestigate.getProjectId());
		  canteenCodeUrlDTO.setQuestionnaireId(canteenInvestigate.getQuestionnaireId());
		  canteenCodeUrlDTO.setqCodeUrl(qCodeUrl+"?pjd="+canteenCodeUrlDTO.getProjectId()+"&qd="+canteenCodeUrlDTO.getQuestionnaireId()+
				 "&cd="+canteenCodeUrlDTO.getCantinid());
		  
			
		return Response.response(canteenCodeUrlDTO);
	}

	@Override
	public Result canteenInvestigationlist(canteenInvestigationliebiaoDTO canteentionliebiaoDTO, int page, int size,
			boolean hasRole, String userCode) {	
		canteentionliebiaoDTO.setHaseole(hasRole);
		canteentionliebiaoDTO.setGetusercode(userCode);
		 Page<Object> pageList = PageHelper.startPage(page, size);
		 List<canteenInvestigationliebiaoDTO> canteentionliebli=canteenInvestigateMapper.canteentionliebli(canteentionliebiaoDTO);
		
		 
		 PageData data = new PageData(pageList);
			data.setData(pageList.getResult());
			return Response.response(data);
	}

	@Override
	public Result canteenInvestigationedit(CanteenInvestigate canteenInvestigate, AccessToken accessToken) {
		
		canteenInvestigate.setIspartake(0);
		canteenInvestigate.setUpdateBy(accessToken.getUserCode());
		canteenInvestigate.setUpdateTime(new Date());
		canteenInvestigateMapper.updatecandiaoch(canteenInvestigate);
		
		return Response.response();
	}

	@Override
	public Result canteenInvestigationgokai(CanteenInvestigate canteenInvestigate, AccessToken accessToken) {
		canteenInvestigate.setIspartake(1);
		canteenInvestigate.setUpdateBy(accessToken.getUserCode());
		canteenInvestigate.setUpdateTime(new Date());
		canteenInvestigateMapper.updatecandiaoch(canteenInvestigate);
		return Response.response();
	}

	@Override
	public Result bdpsndoc(String bdid) {
		
		List<bdpsndocDTO> bdpsndoc=bdpsndocmapper.bdpsndoc(bdid);
		
		List<bdpsndocDTO> bdpsndocdto=new ArrayList<bdpsndocDTO>();
		
		    if (bdpsndoc.size()>0) {
		    	bdpsndocdto.add(0, bdpsndoc.get(0));
		    	return Response.response(bdpsndocdto);
			}else {
				return Response.response();
			}
			
		
	}

	@Override
	public Result cantequestionnaireaca(int questionnaireId, int projectid, int canteeninsdid,
			int investigatagementid) {
		   
		canteenCaBeforeDTO canteencabefore=new canteenCaBeforeDTO();
		canteenInvestigationliebiaoDTO canteeninvegate=canteenInvestigateMapper.canteenInvesdati(questionnaireId, projectid, canteeninsdid,investigatagementid);		
		
		//List<ReviewQuestioClassificationDTO> pqpclist=reviewQuestionnaireProblemClassificationMapper.pqpclist(questionnaireId);
		
		List<ReviewQuestionnaireProblemVo> problemVo=canteenInvestigateMapper.problemVo(questionnaireId, investigatagementid);
		
		
		
		for (ReviewQuestionnaireProblemVo reviewQuestionnaireProblemVo : problemVo) {
			 		
		    if(reviewQuestionnaireProblemVo.getType()==3) {
		    	List<ReviewQuestionnaireProblemOptionVo> option=canteenInvestigateMapper.option(reviewQuestionnaireProblemVo.getId());		    	
		    		reviewQuestionnaireProblemVo.setOption(option);			    		
		    }		  
	    }
		
		List<ReviewQuestionnaireProblemVo> problemjianweVo=canteenInvestigateMapper.problemjianweVo(questionnaireId, investigatagementid);
		
		List<ReviewQuestionnaireProblemVo> problemdawen=new ArrayList<ReviewQuestionnaireProblemVo>();
		
		for (int i = 0; i < problemVo.size(); i++) {
			problemdawen.add(problemVo.get(i));
		}
		
		for (int i = 0; i < problemjianweVo.size(); i++) {
			problemdawen.add(problemjianweVo.get(i));
		}
		
	
		
		canteencabefore.setCanteenInvestigationliebiaodto(canteeninvegate);
		 //canteencabefore.setPqpclist(pqpclist);
		canteencabefore.setProblemVo(problemdawen);
		//canteencabefore.setProblemjianweVo(problemjianweVo);
		
		
		return Response.response(canteencabefore);
	}

	@Override
	public Result getdeid(canteenInvestigationliebiaoDTO canteentionliebiaoDTO,String qCodeUrl) {
		
		
		String qcode=qCodeUrl;
		
		qcode+="?pjd="+canteentionliebiaoDTO.getProjectId()+"&qd="+canteentionliebiaoDTO.getQuestionnaireId()+
				"&cd="+canteentionliebiaoDTO.getId();
		
	
		return Response.response(qcode);
	}

	

	

	

}