/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.business.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import org.springblade.business.vo.UserOpinionCommentsVO;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.business.entity.UserOpinionComments;
import org.springblade.business.service.IUserOpinionCommentsService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户意见评论表 控制器
 *
 * @author BladeX
 * @since 2022-05-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/userOpinionComments")
@Api(value = "用户意见评论表", tags = "用户意见评论表接口")
public class UserOpinionCommentsController extends BladeController {

	private final IUserOpinionCommentsService userOpinionCommentsService;

	/**
	 * 获取当前工单下的所有评论
	 * @param userOpinionId 工单ID
	 * @return 结果集（一级评论）
	 */
	@GetMapping("/queryUserOpinionCommentsByUserOpinionId")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取当前工单下的所有评论")
	public R<List<UserOpinionCommentsVO>> queryUserOpinionCommentsByUserOpinionId(@RequestParam String userOpinionId){
		return R.data(this.userOpinionCommentsService.queryUserOpinionCommentsByUserOpinionId(userOpinionId));
	}

	/**
	 * 新增用户工单评论
	 * @param comments 数据
	 * @return 新增结果
	 */
	@PostMapping("/saveUserOpinionComments")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "新增评论")
	public R<Boolean> saveUserOpinionComments(@RequestBody UserOpinionComments comments){
		return R.data(this.userOpinionCommentsService.save(this.setUserData(comments)));
	}

	//设置登录用户信息
	private UserOpinionComments setUserData(UserOpinionComments userOpinionComments){
		//获取当前登录人
		BladeUser user = AuthUtil.getUser();
		//记录操作人信息
		userOpinionComments.setCreateUser(user.getUserId());
		userOpinionComments.setCreateDept(user.getDeptId().contains(",") ? Long.parseLong(user.getDeptId().split(",")[0]) : Long.parseLong(user.getDeptId()));
		userOpinionComments.setCreateTime(new Date());
		return userOpinionComments;
	}

}
