package cn.pluss.platform.controller.questionsAnswers;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.QuestionsAnswers;
import cn.pluss.platform.questionsAnswers.QuestionsAnswersService;
import cn.pluss.platform.util.PageUtils;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("questionsAnswers")
public class QuestionsAnswersController extends BaseNoModelController {

	@Autowired
	private QuestionsAnswersService questionsAnswersService;


	/**
	 * 引导页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {

		return "questionsAnswers/questionsAnswersList";
	}

	@GetMapping("/page")
	@ResponseBody
	public Result<PageInfo<QuestionsAnswers>> page(Page<QuestionsAnswers> page,QuestionsAnswers questionsAnswers){
		QueryWrapper<QuestionsAnswers> queryWrapper = new QueryWrapper<QuestionsAnswers>().orderByAsc("seq");
		if (StringUtil.isNotEmpty(questionsAnswers.getStatus())) {
			queryWrapper.eq("status",questionsAnswers.getStatus());
		}
		if (StringUtil.isNotEmpty(questionsAnswers.getQuestion())) {
			queryWrapper.eq("question", questionsAnswers.getQuestion());
		}
		page =questionsAnswersService.page(page,queryWrapper);
		return ResultGenerator.genSuccessResult(PageUtils.page(page));
	}

	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
	public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
		if (id != null ) {
			QuestionsAnswers questionsAnswersServiceById = questionsAnswersService.getById(id);
			request.setAttribute("entity",questionsAnswersServiceById);
		}
		return "questionsAnswers/questionsAnswersBaseInfo";
	}

	@PostMapping("/saveOrUpdate")
	@ResponseBody
	public Result<QuestionsAnswers> saveOrUpdate(@RequestBody QuestionsAnswers questionsAnswers){
		if (questionsAnswers.getId() == null ){
			questionsAnswers.setCreateDt(new Date());
			questionsAnswersService.save(questionsAnswers);
		}else {
			UpdateWrapper<QuestionsAnswers> questionsAnswersUpdateWrapper=new UpdateWrapper<QuestionsAnswers>().eq("id",questionsAnswers.getId());
			questionsAnswersService.update(questionsAnswers,questionsAnswersUpdateWrapper);
		}
		return ResultGenerator.genSuccessResult("保存成功",questionsAnswers);
	}

	@PostMapping("/delete")
	@ResponseBody
	public Result<QuestionsAnswers> delete(@RequestBody QuestionsAnswers questionsAnswers){
		questionsAnswersService.removeById(questionsAnswers.getId());
		return ResultGenerator.genSuccessResult("删除成功！",questionsAnswers);
	}
}
