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

import cn.hutool.core.util.StrUtil;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.UserBenefits;
import com.cyksj.model.views.UserBenefitsView;
import com.cyksj.service.mange.RefreshCacheService;
import com.cyksj.service.user.UserBenefitsBackService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目名: yhlxj2
 * 文件名: UserBenefitsController
 * 创建者: JavaZou
 * 创建时间:2024/9/23 14:27
 */
@RestController
@RequestMapping("/manage/user/benefits")
@RequiredArgsConstructor
public class UserBenefitsController {
	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final UserBenefitsBackService userBenefitsBackService;

	private final RefreshCacheService refreshCacheService;

	/**
	 * 用户权益列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<UserBenefitsView>> get() {
		SearchResult<UserBenefitsView> search = beanSearcher.search(UserBenefitsView.class, MapUtils.flatBuilder(request.getParameterMap())
				.orderBy(UserBenefitsView::getSorted).asc()
				.build());
		search.getDataList().forEach(data->{
			if (StrUtil.isNotBlank(data.getRenewConfig())) {
				try {
					data.setRenewConfigList(Jsons.parseList(data.getRenewConfig(), UserBenefits.RenewConfig.class));
				} catch (Exception e) {
				}
			}
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 增加用户权益
	 */
	@PostMapping("/post")
	public Result<String> saveUserBenefits(@RequestBody UserBenefits userBenefits) throws Exception {
		userBenefitsBackService.saveOrUpdatePojo(userBenefits);
		refreshCacheService.refreshUserBenefitsConfigCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑用户权益
	 */
	@PutMapping("/update")
	public Result<String> updateUserBenefits(@RequestBody UserBenefits userBenefits) throws Exception {
		userBenefitsBackService.saveOrUpdatePojo(userBenefits);
		refreshCacheService.refreshUserBenefitsConfigCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除用户权益
	 */
	@DeleteMapping("/delete/{id}")
	public Result<String> deleteById(@PathVariable Long id) {
		userBenefitsBackService.removeById(id);
		refreshCacheService.refreshUserBenefitsConfigCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 排序
	 */
	@PutMapping("/sorted")
	public Result<String> sorted(@RequestBody UserBenefits userBenefits) {
		Long id = userBenefits.getId();
		UserBenefits byId = userBenefitsBackService.getById(id);
		byId.setSorted(userBenefits.getSorted());
		userBenefitsBackService.updateById(byId);
		refreshCacheService.refreshUserBenefitsConfigCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 上下架
	 */
	@PutMapping("/update/status/{id}")
	public Result<String> updateStatusById(@PathVariable Long id) {
		UserBenefits byId = userBenefitsBackService.getById(id);
		byId.setStatus(!byId.getStatus());
		userBenefitsBackService.updateById(byId);
		refreshCacheService.refreshUserBenefitsConfigCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
