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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.manage.help.HelpQuestionClassificationMapper;
import com.cyksj.mapper.manage.help.HelpQuestionMapper;
import com.cyksj.model.entity.HelpQuestion;
import com.cyksj.model.entity.HelpQuestionClassification;
import com.cyksj.service.sys.SysHelpService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/*
 *项目名: netflix
 *文件名: HelpController
 *创建者: JavaZou
 *创建时间:2022/12/7 14:16
 */
@RestController
@RequestMapping("/manage/help")
@RequiredArgsConstructor
public class HelpController {
	private final SysHelpService sysHelpService;

	private final HelpQuestionClassificationMapper classificationMapper;

	private final HelpQuestionMapper helpQuestionMapper;

	/**
	 * 新增问题分类
	 */
	@PostMapping("/question/classification")
	public Result<String> saveHelpQuestionClassification(@RequestBody HelpQuestionClassification helpQuestionClassification) {
		sysHelpService.postQuestionClassification(helpQuestionClassification);
		return GatewayResponse.SUCCESS.newBuilder().toResult("新增问题分类成功");
	}

	/**
	 * 删除问题分类
	 */
	@DeleteMapping("/question/classification/{id}")
	public Result<String> deleteHelpQuestionClassificationById(@PathVariable Long id) {
		HelpQuestionClassification helpQuestionClassification = classificationMapper.selectById(id);
		if (helpQuestionClassification != null) {
			helpQuestionClassification.setDeleted(false);
			classificationMapper.updateById(helpQuestionClassification);
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult("删除问题分类成功");
	}

	/**
	 * 编辑问题分类
	 */
	@PutMapping("/question/classification")
	public Result<String> editHelpQuestionClassification(@RequestBody HelpQuestionClassification helpQuestionClassification) {
		sysHelpService.editQuestionClassification(helpQuestionClassification);
		return GatewayResponse.SUCCESS.newBuilder().toResult("编辑问题分类成功");
	}

	/**
	 * 问题分类列表
	 */
	@GetMapping("/question/classification")
	public Result<Page<HelpQuestionClassification>> saveHelpQuestionClassification(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "5") Integer limit) {
		Page<HelpQuestionClassification> page = classificationMapper.selectPage(new Page<>(start, limit), Wrappers.lambdaQuery(HelpQuestionClassification.class)
				.eq(HelpQuestionClassification::getDeleted, true)
				.orderByDesc(HelpQuestionClassification::getSort));
		return GatewayResponse.SUCCESS.newBuilder().toResult(page);
	}

	/**
	 * 上下架分类
	 */
	@PutMapping("/question/classification/status/{id}")
	public Result<String> updateHelpQuestionClassificationStatus(@PathVariable Long id) {
		HelpQuestionClassification classification = classificationMapper.selectById(id);
		if (classification == null || !classification.getDeleted()) {
			throw BusinessRuntimeException.getInstance("分类不存在");
		}
		classification.setStatus(!classification.getStatus());
		classificationMapper.updateById(classification);
		return GatewayResponse.SUCCESS.newBuilder().toResult("操作成功");
	}

	/**
	 * 新增问题
	 */
	@PostMapping("/question")
	public Result<String> saveHelpQuestion(@RequestBody HelpQuestion helpQuestion) {
		sysHelpService.saveHelpQuestion(helpQuestion);
		return GatewayResponse.SUCCESS.newBuilder().toResult("新增问题成功");
	}

	/**
	 * 删除问题
	 */
	@DeleteMapping("/question/{id}")
	public Result<String> deleteHelpQuestionById(@PathVariable Long id) {
		HelpQuestion helpQuestion = helpQuestionMapper.selectById(id);
		if (helpQuestion == null || !helpQuestion.getDeleted()) {
			throw BusinessRuntimeException.getInstance("问题不存在");
		}
		helpQuestion.setDeleted(false);
		helpQuestionMapper.updateById(helpQuestion);
		return GatewayResponse.SUCCESS.newBuilder().toResult("删除问题成功");
	}

	/**
	 * 修改问题
	 */
	@PutMapping("/question")
	public Result<String> editHelpQuestion(@RequestBody HelpQuestion helpQuestion) {
		sysHelpService.editHelpQuestion(helpQuestion);
		return GatewayResponse.SUCCESS.newBuilder().toResult("编辑问题成功");
	}

	/**
	 * 问题列表
	 */
	@GetMapping("/question")
	public Result<Page<HelpQuestion>> saveHelpQuestion(Long classificationId, @RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "5") Integer limit) {
		if (classificationId == null) {
			throw BusinessRuntimeException.getInstance("问题分类id为空");
		}
		HelpQuestionClassification helpQuestionClassification = classificationMapper.selectById(classificationId);
		if (helpQuestionClassification == null || !helpQuestionClassification.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该分类已被删除");
		}
		Page<HelpQuestion> page = helpQuestionMapper.selectPage(new Page<>(start, limit),
				Wrappers.lambdaQuery(HelpQuestion.class)
						.eq(HelpQuestion::getClassificationId, classificationId)
						.eq(HelpQuestion::getDeleted, true)
						.orderByDesc(HelpQuestion::getSort));
		return GatewayResponse.SUCCESS.newBuilder().toResult(page);
	}

	/**
	 * 问题详情
	 */
	@GetMapping("/get/question/detail")
	public Result<HelpQuestion> getQuestionById(@NotNull Long id) {
		if (id == null || id < 1) {
			throw BusinessRuntimeException.getInstance("id error");
		}
		HelpQuestion helpQuestion = helpQuestionMapper.selectById(id);
		return GatewayResponse.SUCCESS.newBuilder().toResult(helpQuestion);
	}

	/**
	 * 上下架问题
	 */
	@PutMapping("/question/status/{id}")
	public Result<String> saveHelpQuestion(@PathVariable Long id) {
		HelpQuestion helpQuestion = helpQuestionMapper.selectById(id);
		if (helpQuestion == null || !helpQuestion.getDeleted()) {
			throw BusinessRuntimeException.getInstance("问题不存在");
		}
		helpQuestion.setStatus(!helpQuestion.getStatus());
		helpQuestionMapper.updateById(helpQuestion);
		return GatewayResponse.SUCCESS.newBuilder().toResult("操作成功");
	}
}
