package com.cyksj.service.sys.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.GoodsDonMapper;
import com.cyksj.mapper.manage.help.HelpQuestionClassificationMapper;
import com.cyksj.mapper.manage.help.HelpQuestionMapper;
import com.cyksj.model.entity.GoodsDon;
import com.cyksj.model.entity.HelpQuestion;
import com.cyksj.model.entity.HelpQuestionClassification;
import com.cyksj.service.sys.SysHelpService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/*
 *项目名: netflix
 *文件名: SysHelpServiceImpl
 *创建者: JavaZou
 *创建时间:2022/12/7 10:20
 */
@Service
@RequiredArgsConstructor
public class SysHelpServiceImpl implements SysHelpService {
	private final GoodsDonMapper goodsDonMapper;

	private final HelpQuestionClassificationMapper classificationMapper;

	private final HelpQuestionMapper helpQuestionMapper;

	@Override
	public void postQuestionClassification(HelpQuestionClassification helpQuestionClassification) {
		if (StrUtil.isEmpty(helpQuestionClassification.getName())) {
			throw BusinessRuntimeException.getInstance("分类名称不能为空");
		}
		if (helpQuestionClassification.getSort() == null) {
			throw BusinessRuntimeException.getInstance("排序不能为空");
		}
		if (helpQuestionClassification.getGoodsId() != null) {
			GoodsDon goodsDon = goodsDonMapper.selectById(helpQuestionClassification.getGoodsId());
			if (goodsDon == null || !goodsDon.getDeleted()) {
				throw BusinessRuntimeException.getInstance("所选平台不存在");
			}
			if (!goodsDon.getStatus()) {
				throw BusinessRuntimeException.getInstance("所选平台未上架");
			}
			HelpQuestionClassification exits = classificationMapper.selectOne(Wrappers.lambdaQuery(HelpQuestionClassification.class)
					.eq(HelpQuestionClassification::getGoodsId, helpQuestionClassification.getGoodsId())
					.eq(HelpQuestionClassification::getDeleted, true)
					.select(HelpQuestionClassification::getId)
					.last("limit 1"));
			if (exits != null) {
				throw BusinessRuntimeException.getInstance("已关联的该平台问题分类");
			}
		}
		if (helpQuestionClassification.getGoodsId() == null && StrUtil.isEmpty(helpQuestionClassification.getLogo())) {
			throw BusinessRuntimeException.getInstance("未关联平台,请上传对应问题分类logo");
		}
		HelpQuestionClassification classification = classificationMapper.selectOne(Wrappers.lambdaQuery(HelpQuestionClassification.class)
				.eq(HelpQuestionClassification::getDeleted, true)
				.eq(HelpQuestionClassification::getSort, helpQuestionClassification.getSort())
				.last("limit 1"));
		if (classification != null) {
			throw BusinessRuntimeException.getInstance("排序相同");
		}
		classificationMapper.insert(helpQuestionClassification);
	}

	@Override
	public void editQuestionClassification(HelpQuestionClassification helpQuestionClassification) {
		if (helpQuestionClassification.getId() == null) {
			throw BusinessRuntimeException.getInstance("id error");
		}
		HelpQuestionClassification exits = classificationMapper.selectById(helpQuestionClassification.getId());
		if (exits == null || !exits.getDeleted()) {
			throw BusinessRuntimeException.getInstance("分类已被删除");
		}
		if (helpQuestionClassification.getSort() != null) {
			HelpQuestionClassification classification = classificationMapper.selectOne(Wrappers.lambdaQuery(HelpQuestionClassification.class)
					.eq(HelpQuestionClassification::getDeleted, true)
					.eq(HelpQuestionClassification::getSort, helpQuestionClassification.getSort())
					.ne(HelpQuestionClassification::getId, helpQuestionClassification.getId())
					.last("limit 1"));
			if (classification != null) {
				throw BusinessRuntimeException.getInstance("排序相同");
			}
		}
		classificationMapper.updateById(helpQuestionClassification);
	}

	@Override
	public void saveHelpQuestion(HelpQuestion helpQuestion) {
		if (ObjectUtil.hasEmpty(helpQuestion.getClassificationId(), helpQuestion.getName(), helpQuestion.getContent(), helpQuestion.getSort())) {
			throw BusinessRuntimeException.getInstance("请补充必填项");
		}
		HelpQuestionClassification classification = classificationMapper.selectById(helpQuestion.getClassificationId());
		if (classification == null || !classification.getDeleted()) {
			throw BusinessRuntimeException.getInstance("分类不存在");
		}
		HelpQuestion helpQuestion1 = helpQuestionMapper.selectOne(Wrappers.lambdaQuery(HelpQuestion.class)
				.eq(HelpQuestion::getDeleted, true)
				.eq(HelpQuestion::getClassificationId, helpQuestion.getClassificationId())
				.eq(HelpQuestion::getSort, helpQuestion.getSort())
				.last("limit 1"));
		if (helpQuestion1 != null) {
			throw BusinessRuntimeException.getInstance("排序相同");
		}
		helpQuestionMapper.insert(helpQuestion);
	}

	@Override
	public void editHelpQuestion(HelpQuestion helpQuestion) {
		if (helpQuestion.getId() == null) {
			throw BusinessRuntimeException.getInstance("id error");
		}
		HelpQuestion exits = helpQuestionMapper.selectById(helpQuestion.getId());
		if (exits == null || !exits.getDeleted()) {
			throw BusinessRuntimeException.getInstance("问题已被删除");
		}
		if (helpQuestion.getClassificationId() != null && !exits.getClassificationId().equals(helpQuestion.getClassificationId())) {
			throw BusinessRuntimeException.getInstance("问题所属分类无法修改");
		}
		if (helpQuestion.getSort() != null) {
			HelpQuestion helpQuestion1 = helpQuestionMapper.selectOne(Wrappers.lambdaQuery(HelpQuestion.class)
					.eq(HelpQuestion::getDeleted, true)
					.eq(HelpQuestion::getClassificationId, helpQuestion.getClassificationId())
					.eq(HelpQuestion::getSort, helpQuestion.getSort())
					.ne(HelpQuestion::getId, helpQuestion.getId())
					.last("limit 1"));
			if (helpQuestion1 != null) {
				throw BusinessRuntimeException.getInstance("排序相同");
			}
		}
		helpQuestionMapper.updateById(helpQuestion);
	}
}
