package com.cyksj.web.controller.manage.active;

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.UserActivity;
import com.cyksj.service.user.UserActivityService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目名: yhlxj
 * 文件名: UserActivityController
 * 创建者: JavaZou
 * 创建时间:2024/4/17 12:22
 */
@RestController
@RequestMapping("/manage/activity")
@Slf4j
@RequiredArgsConstructor
public class UserActivityController {
	private final UserActivityService userActivityService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	/**
	 * 新建活动
	 */
	@PostMapping("/post")
	public Result<String> insertActivity(@RequestBody @Validated UserActivity userActivity) throws Exception {
		userActivityService.saveOrUpdateActivity(userActivity);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑活动
	 */
	@PutMapping("/update")
	public Result<String> updateActivity(@RequestBody @Validated UserActivity userActivity) throws Exception {
		userActivityService.saveOrUpdateActivity(userActivity);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除活动
	 */
	@DeleteMapping("/delete/{id}")
	public Result<String> deleteActivity(@PathVariable Long id) {
		UserActivity activity = userActivityService.getById(id);
		if (activity != null && activity.getDeleted()) {
			activity.setDeleted(false);
			userActivityService.updateById(activity);
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 活动列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<UserActivity> > get() {
		SearchResult<UserActivity> search = beanSearcher.search(UserActivity.class, MapUtils.flatBuilder(request.getParameterMap()).field(UserActivity::getDeleted, 1).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

}
