package com.zcloud.controller.questionnaire;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.service.questionnaire.SurveyanswerService;
import com.zcloud.service.questionnaire.SurveycorpinfoService;
import com.zcloud.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 说明：习题管理
 * 作者：luoxiaobao
 * 时间：2021-07-03
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/surveyanswer")
public class AppSurveyanswerController extends BaseController {



	@Autowired
	private SurveyanswerService surveyanswerService;

	@Autowired
	private SurveycorpinfoService surveycorpinfoService;


	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/submit")
	@ResponseBody
	public Object submit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		List<JSONObject> list = (List<JSONObject>) JSON.parse(pd.get("questions").toString());
		List<PageData> answerList = new ArrayList<PageData>();
		for(JSONObject u : list) {
			PageData answer = new PageData();
			answer.putAll(pd);
			answer.put("ANSWER_ID", this.get32UUID());	//主键

			answer.put("QUESTION_ID", u.get("QUESTION_ID"));
			answer.put("QUESTIONTYPE", u.get("QUESTIONTYPE"));
			answer.put("ANSWER", u.get("ANSWER"));

			answer.put("CREATOR",pd.get("USER"));	//添加人
			answer.put("CREATE_TIME", DateUtil.date2Str(new Date()));	//创建时间
			answer.put("OPERATOR", pd.get("USER"));	//修改人
			answer.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
			answer.put("ISDELETE", "0");	//是否删除
			answerList.add(answer);
		}
		surveyanswerService.saveBatch(answerList);
		map.put("result", errInfo);
		return map;
	}


	/**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
//	// @RequiresPermissions("survey:edit")
	@ResponseBody
	public Object goEdit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();

//		List<PageData> itemList = surveyanswerService.findById(pd);

		pd = surveyanswerService.findById(pd);	//根据ID读取/
		map.put("pd", pd);
//		map.put("itemList", itemList);
		map.put("result", errInfo);
		return map;
	}

	/**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getUserId")
	@ResponseBody
	public Object getUserId() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		PageData count = surveyanswerService.getUserId(pd);	//根据ID读取/
//		List<PageData> itemList = surveyanswerService.getUserId(pd);
		map.put("count", count);
//		map.put("itemList", itemList);
		map.put("result", errInfo);
		return map;
	}

}
