package com.ruoyi.zzb.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.common.utils.StringUtils;
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.SaveViewLogVO;
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 {

	@Autowired
	private StudyService studyService;

	@PostMapping("/findAllInfo")
	public JSONObject findAllInfo(HttpServletResponse response, HttpServletRequest request,@Validated @RequestBody UserSearchParam userSearchParam) {
		try {
			Page<StudyDocInfo> result = studyService.findAllInfo(userSearchParam);
			return RspResult.success(result);
		}catch (Exception e){
			e.printStackTrace();
			return RspResult.error(e.getMessage());
		}

	}

	@PostMapping("/findDocInfoById")
	public JSONObject findDocInfoById(HttpServletResponse response, HttpServletRequest request,@RequestBody JSONObject params) {

		try {
			StudyDocInfo result = studyService.findDocInfoById(params.getString("docId"));
			return RspResult.success(result);
		}catch (Exception e){
			e.printStackTrace();
			return RspResult.error(e.getMessage());
		}
	}

	@PostMapping("/saveDoc")
	public JSONObject 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 RspResult.success(result);
		}catch (Exception e){
			e.printStackTrace();
			return RspResult.error(e.getMessage());
		}

	}


	@PostMapping("/deleteById")
	public JSONObject deleteById(HttpServletResponse response, HttpServletRequest request,@RequestBody JSONObject params) {
		try {
			String docId = params.getString("docId");
			boolean result = studyService.deleteById(docId);
			return RspResult.success(result);
		}catch (Exception e){
			e.printStackTrace();
			return RspResult.error(e.getMessage());
		}
	}

	@PostMapping("/updatePageViewNumById")
	public JSONObject updatePageViewNumById(HttpServletResponse response, HttpServletRequest request,@Validated @RequestBody UpdatePageViewNumVO updatePageViewNumVO) {
		try {
			boolean result = studyService.updatePageViewNumById(updatePageViewNumVO);
			return RspResult.success(result);
		}catch (Exception e){
			e.printStackTrace();
			return RspResult.error(e.getMessage());
		}
	}

	@PostMapping("/saveViewLog")
	public JSONObject saveViewLog(HttpServletResponse response, HttpServletRequest request, @Validated @RequestBody SaveViewLogVO saveViewLogVO) {
		try {

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

			saveViewLogVO.setUserId(sysUser.getUserId()+"");
			boolean result = studyService.saveViewLogInfo(saveViewLogVO,sysUser);
			return RspResult.success(result);
		}catch (Exception e){
			e.printStackTrace();
			return RspResult.error(e.getMessage());
		}

	}



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

		try {
			Page<StudyDocInfo> result = studyService.findInfoByTitle(userSearchParam);
			return RspResult.success(result);
		}catch (Exception e){
			e.printStackTrace();
			return RspResult.error(e.getMessage());
		}

	}



	@PostMapping("/updateDocByDocId")
	public JSONObject updateDocByDocId(HttpServletResponse response, HttpServletRequest request,@Validated @RequestBody StudyDocInfo studyDocInfo) {
		try {
			boolean result = studyService.updateById(studyDocInfo);
			return RspResult.success(result);
		}catch (Exception e){
			e.printStackTrace();
			return RspResult.error(e.getMessage());
		}

	}

}
