package com.ruoyi.zzb.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.zzb.common.bean.RspResult;
import com.ruoyi.zzb.study.domain.StudyDocInfo;
import com.ruoyi.zzb.study.domain.UserSearchParam;
import com.ruoyi.zzb.study.domain.req.QueryInfoVO;
import com.ruoyi.zzb.study.domain.req.UpdatePageViewNumVO;
import com.ruoyi.zzb.study.service.StudyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/api/study")
public class StudyController extends BaseController {

	@Autowired
	private StudyService studyService;

	@PostMapping("/findAllInfo")
	public AjaxResult findAllInfo(HttpServletResponse response, HttpServletRequest request, @Validated @RequestBody UserSearchParam userSearchParam) {
		try {
			Page<StudyDocInfo> result = studyService.findAllInfo(userSearchParam);
			return success(result);
//			return RspResult.success(result);
		}catch (Exception e){
			log.error("============【学习助手】异常：findAllInfo======",e);
			return error(e.getMessage());
		}

	}

	@PostMapping("/findDocInfoById")
	public AjaxResult findDocInfoById(HttpServletResponse response, HttpServletRequest request, @Validated @RequestBody QueryInfoVO queryInfoVO) {

		try {
			LoginUser loginUser = SecurityUtils.getLoginUser();
			SysUser sysUser = loginUser.getUser();

			queryInfoVO.setUserId(sysUser.getUserId()+"");
			StudyDocInfo result = studyService.findDocInfoById(queryInfoVO.getDocId());

			if(result != null){		//有记录详情，保存浏览记录
				studyService.saveViewLogInfo(queryInfoVO,sysUser);
			}

			return success(result);
		}catch (Exception e){
			log.error("============【学习助手】异常：findDocInfoById======",e);
			return error(e.getMessage());
		}
	}

	@PostMapping("/saveDoc")
	public AjaxResult saveDoc(HttpServletResponse response, HttpServletRequest request,@Validated @RequestBody StudyDocInfo studyDocInfo) {

		try {
			LoginUser loginUser = SecurityUtils.getLoginUser();
			SysUser sysUser = loginUser.getUser();

			studyDocInfo.setCreateUserId(sysUser.getUserId()+"");
			studyDocInfo.setCreatePoliceNo(sysUser.getPoliceNo());
			studyDocInfo.setCreateUserName(sysUser.getNickName());

			boolean result = studyService.saveDocInfo(studyDocInfo);
			return success(result);
		}catch (Exception e){
			log.error("============【学习助手】异常：saveDocInfo======",e);
			return error(e.getMessage());
		}

	}


	@PostMapping("/deleteById")
	public AjaxResult deleteById(HttpServletResponse response, HttpServletRequest request,@RequestBody JSONObject params) {
		try {
			String docId = params.getString("docId");
			boolean result = studyService.deleteById(docId);
			return success(result);
		}catch (Exception e){
			log.error("============【学习助手】异常：deleteById======",e);
			return error(e.getMessage());
		}
	}

	@PostMapping("/updatePageViewNumById")
	public AjaxResult updatePageViewNumById(HttpServletResponse response, HttpServletRequest request,@Validated @RequestBody UpdatePageViewNumVO updatePageViewNumVO) {
		try {
			boolean result = studyService.updatePageViewNumById(updatePageViewNumVO);
			return success(result);
		}catch (Exception e){
			log.error("============【学习助手】异常：updatePageViewNumById======",e);
			return error(e.getMessage());
		}
	}

//	@PostMapping("/saveViewLog")		关闭保存访问日志对外接口
	public AjaxResult saveViewLog(HttpServletResponse response, HttpServletRequest request, @Validated @RequestBody QueryInfoVO queryInfoVO) {
		try {

			LoginUser loginUser = SecurityUtils.getLoginUser();
			SysUser sysUser = loginUser.getUser();

			queryInfoVO.setUserId(sysUser.getUserId()+"");
			boolean result = studyService.saveViewLogInfo(queryInfoVO,sysUser);
			return success(result);
		}catch (Exception e){
			log.error("============【学习助手】异常：saveViewLog======",e);
			return error(e.getMessage());
		}

	}



	@PostMapping("/findInfoByTitle")
	public AjaxResult findInfoByTitle(HttpServletResponse response, HttpServletRequest request, @Validated @RequestBody UserSearchParam userSearchParam) {

		try {
			Page<StudyDocInfo> result = studyService.findInfoByTitle(userSearchParam);
			return success(result);
		}catch (Exception e){
			log.error("============【学习助手】异常：findInfoByTitle======",e);
			return error(e.getMessage());
		}

	}



	@PostMapping("/updateDocByDocId")
	public AjaxResult updateDocByDocId(HttpServletResponse response, HttpServletRequest request,@Validated @RequestBody StudyDocInfo studyDocInfo) {
		try {
			boolean result = studyService.updateById(studyDocInfo);
			return success(result);
		}catch (Exception e){
			log.error("============【学习助手】异常：updateDocByDocId======",e);
			return error(e.getMessage());
		}

	}

}
