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

import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.dto.QuestionnaireFieldsView;
import com.cyksj.model.entity.QuestionnairePromotion;
import com.cyksj.service.mange.form.QuestionnairePromotionService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/*
 *项目名: netflix
 *文件名: QuestionnairePromotionController
 *创建者: JavaZou
 *创建时间:2023/6/14 11:36
 */
@RestController
@RequestMapping("/manage/questionnaire")
@RequiredArgsConstructor
public class QuestionnairePromotionController {
	private final QuestionnairePromotionService promotionService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	@PostMapping("/post")
	public Result<Long> saveQuestionnaire(@RequestBody @Validated QuestionnairePromotion promotion) {
		Long promotionId = promotionService.saveOrUpdatePromotion(promotion);
		return GatewayResponse.SUCCESS.newBuilder().toResult(promotionId);
	}

	@PutMapping("/put")
	public Result<String> updateQuestionnaire(@RequestBody @Validated QuestionnairePromotion promotion) {
		promotionService.saveOrUpdatePromotion(promotion);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	@DeleteMapping("/delete/{id}")
	public Result<String> delById(@PathVariable Long id) {
		QuestionnairePromotion byId = promotionService.getById(id);
		if (byId == null || !byId.getDeleted()) throw BusinessRuntimeException.getInstance("该记录已被删除");
		byId.setDeleted(false);
		promotionService.updateById(byId);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	@GetMapping("/get")
	public Result<SearchResult<QuestionnairePromotion>> getPromotions() {
		SearchResult<QuestionnairePromotion> search = beanSearcher.search(QuestionnairePromotion.class, MapUtils.flatBuilder(request.getParameterMap())
				.field(QuestionnairePromotion::getDeleted, true).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 问卷详情 问卷字段以及答案列表
	 */
	@GetMapping("/template/get/detail/{promotionId}")
	public Result<SearchResult<QuestionnaireFieldsView>> getFieldsView(@PathVariable Long promotionId) {
		QuestionnairePromotion byId = promotionService.getById(promotionId);
		if (byId==null) throw BusinessRuntimeException.getInstance("问卷不存在");
		SearchResult<QuestionnaireFieldsView> search = beanSearcher.search(QuestionnaireFieldsView.class, MapUtils.flatBuilder(request.getParameterMap())
				.field(QuestionnaireFieldsView::getTemplateId, byId.getTemplateId())
				.put("promotion_id", String.format(" and fd.promotion_id = %s", promotionId))
				.orderBy(QuestionnaireFieldsView::getSorted).asc()
				.orderBy(QuestionnaireFieldsView::getId).desc()
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 上下架问卷
	 */
	@PutMapping("/update/status/{id}")
	public Result<String> updateStatusById(@PathVariable Long id) {
		QuestionnairePromotion promotion = promotionService.getById(id);
		Boolean status = promotion.getStatus();
		Boolean isUp = promotion.getIsUp();
		//上架
		if (!status && !isUp) {
			promotion.setIsUp(true);
		}
		promotion.setStatus(!status);
		promotionService.updateById(promotion);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
