package cn.com.countrygarden.bi.main.service.review.impl;

import cn.com.countrygarden.bi.main.model.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.LinkedList;
import java.util.List;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.AccessToken;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.countrygarden.bi.main.model.review.ReviewInvestigated;
import cn.com.countrygarden.bi.main.model.review.ReviewQuestionnaire;
import cn.com.countrygarden.bi.main.model.review.ReviewQuestionnaireProblem;
import cn.com.countrygarden.bi.main.model.review.ReviewQuestionnaireProblemClassification;
import cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireMapper;
import cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireProblemClassificationMapper;
import cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireProblemMapper;
import cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireProblemOptionMapper;
import cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireProblemTypeMapper;
import cn.com.countrygarden.bi.main.dto.ReviewQuestionnairelistVo;
import cn.com.countrygarden.bi.main.dto.reviewInvestigatedIsRepeatVo;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireProblemEditVo;
import cn.com.countrygarden.bi.main.dao.review.ReviewInvestigatedMapper;
import cn.com.countrygarden.bi.main.dao.review.ReviewJudgeMapper;
import cn.com.countrygarden.bi.main.dto.ReviewAnswerBeforeVo;
import cn.com.countrygarden.bi.main.dto.ReviewQuestionnaireDetailDTO;
import cn.com.countrygarden.bi.main.service.review.ReviewQuestionnaireService;
import cn.com.countrygarden.bi.main.service.review.commonService.ClassificationVoService;
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-06-25
 */
@Service
@Transactional
public class ReviewQuestionnaireServiceImpl implements ReviewQuestionnaireService {
	@Autowired
	private ReviewQuestionnaireMapper reviewQuestionnaireMapper;
	@Autowired
	private ReviewInvestigatedMapper reviewinvestigatedMapper;
	
	

	@Autowired
	private ReviewQuestionnaireProblemMapper reviewQuestionnaireProblemMapper;

	@Autowired
	private ReviewQuestionnaireProblemOptionMapper reviewquestionnaireProblemOptionMapper;

	@Autowired
	private ReviewQuestionnaireProblemTypeMapper reviewQuestionnaireProblemTypeMapper;

	@Autowired
	private ReviewQuestionnaireProblemClassificationMapper reviewQuestionnaireProblemClassificationMapper;

	@Autowired
	private ReviewJudgeMapper reviewJudgeMapper;
	
	@Autowired
	private ClassificationVoService classificationVoService;

	@Override
	public Result queryPageList(ReviewQuestionnaire reviewQuestionnaire, int page, int size) {
		Page<Object> pageList = PageHelper.startPage(page, size);
		Condition condition = new Condition(ReviewQuestionnaire.class);
		Criteria criteria = condition.createCriteria();
		// TODO 添加条件
		reviewQuestionnaireMapper.selectByCondition(condition);
		PageData data = new PageData(pageList);
		data.setData(pageList.getResult());
		return Response.response(data);
	}

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

	@Override
	public Result add(ReviewQuestionnaire reviewQuestionnaire, AccessToken accessToken) {
		reviewQuestionnaire.setCreateBy(accessToken.getUserCode());
		reviewQuestionnaireMapper.insert(reviewQuestionnaire);
		return Response.response();
	}

	@Override
	public Result edit(ReviewQuestionnaire reviewQuestionnaire, AccessToken accessToken) {
		reviewQuestionnaireMapper.updateByPrimaryKey(reviewQuestionnaire);
		return Response.response();
	}

	@Override
	public Result del(Integer id) {
		int count = reviewQuestionnaireMapper.deleteByPrimaryKey(id);
		return Response.response(count);
	}
	// 答题前返回的数据
		public Result selectAnswerBeforeVo(int investigatedId, int reviewJudgeId, int questionnaireId) {
			ReviewAnswerBeforeVo anserVo = new ReviewAnswerBeforeVo();
			reviewInvestigatedIsRepeatVo investigatedIsRepeatVo = reviewinvestigatedMapper
					.selectInvestigatedIsRepeatVo(investigatedId, reviewJudgeId);

			if (investigatedIsRepeatVo.getFlag() == 0) {
				if (investigatedIsRepeatVo.getIsSubmission() == 0) {
					anserVo = reviewinvestigatedMapper.selectAnswerBeforeVo(investigatedId, questionnaireId);
					anserVo.setProblemVo(reviewQuestionnaireProblemMapper.queryQuestionnaireProblemVo(anserVo.getQuestionnaireId()));
				} else if (investigatedIsRepeatVo.getIsSubmission() == 1 && investigatedIsRepeatVo.getIsRepeat() == 1) {
					anserVo = reviewinvestigatedMapper.selectAnswerBeforeVo(investigatedId, questionnaireId);
					anserVo.setProblemVo(reviewQuestionnaireProblemMapper.queryQuestionnaireProblemVo(anserVo.getQuestionnaireId()));
				}
			}


			anserVo.setInvestigatedIsRepeatVo(investigatedIsRepeatVo);

			return Response.response(anserVo);
		}
	


	// 删除问卷,逻辑删除
	public Result questionnaireParamVoOne(Integer id) {	
	    //删除问卷的同时更改问题分类flag的状态
		classificationVoService.ClassificationFlagUpdateService(id);
		
		// 根据问卷ID更改问卷状态为0
		ReviewQuestionnaire reviewQuestionnaire = new ReviewQuestionnaire();
		reviewQuestionnaire.setId(id);
		reviewQuestionnaire.setFlag(0);
		// 如果更改成功再根据问卷ID更改问题状态为0
		if (reviewQuestionnaireMapper.updateByPrimaryKeySelective(reviewQuestionnaire) > 0) {
			reviewQuestionnaireProblemMapper.updateProblemFlag(id);
		}
		return Response.response();
	}
	// 编辑问题前返回的数据
	public Result queryQuestionnaireAndproblem(int id) {
 
		
		ReviewQuestionnaireProblemEditVo questionnaireProblemEditVo = new ReviewQuestionnaireProblemEditVo();
		ReviewQuestionnaire reviewQuestionnaire = reviewQuestionnaireMapper.selectByPrimaryKey(id);
		questionnaireProblemEditVo.setQuestionnaireId(id);
		questionnaireProblemEditVo.setInvestigationmanagementId(reviewQuestionnaire.getInvestigationmanagementId());
		questionnaireProblemEditVo.setQuestionnaireName(reviewQuestionnaire.getName());
		questionnaireProblemEditVo.setClassificationEditIds(classificationVoService.ClassificationFlagYesEdit(id));
		questionnaireProblemEditVo.setQuestionnaireDescribed(reviewQuestionnaire.getDescribed());
		questionnaireProblemEditVo.setCreateBy(reviewQuestionnaire.getCreateBy());
		questionnaireProblemEditVo.setCreateTime(reviewQuestionnaire.getCreateTime());
		//questionAry
		questionnaireProblemEditVo.setQtypeAry(reviewQuestionnaireProblemTypeMapper.selectQuestionnaireProblemTypeVo(reviewQuestionnaire.getInvestigationmanagementId()));
		questionnaireProblemEditVo.setQuestionAry(reviewQuestionnaireProblemMapper.queryQuestionnaireProblemdanVo(id));
		questionnaireProblemEditVo.setQclassAry(
				reviewQuestionnaireProblemClassificationMapper.queryQuestionnaireProblemClassificationVo(reviewQuestionnaire.getInvestigationmanagementId()));
		return Response.response(questionnaireProblemEditVo);
	}


	@Override
	public Result ReviewQuestionnairelistVo(String name, int page, int size, boolean hasRole, String userCode,Integer investigationmanagementId ) {
		// TODO Auto-generated method stub
		Page<Object> pageList = PageHelper.startPage(page, size);
		List<ReviewQuestionnairelistVo> reviewQuestionnairelistVo = reviewQuestionnaireMapper
				.ReviewQuestionnairelistVo(name, hasRole, userCode,investigationmanagementId);

		PageData data = new PageData(pageList);
		data.setData(pageList.getResult());
		return Response.response(data);

	}


}
