package org.springblade.system.service.impl;

import jakarta.annotation.Resource;
import org.springblade.common.core.domain.AjaxResult;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.system.domain.RectificationDto;
import org.springblade.system.mapper.RectFeedbackMapper;
import org.springblade.system.service.RectFeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author：liang.chao
 * @Date：2025/12/3 - 14:18
 */
@Service
public class RectFeedbackServiceImpl implements RectFeedbackService {
	@Resource
	private RectFeedbackMapper rectFeedbackMapper;

	@Override
	public List<RectificationDto> list(RectificationDto dto) {
		return rectFeedbackMapper.list(dto);
	}

	@Override
	public RectificationDto detail(RectificationDto dto) {
		RectificationDto detail = rectFeedbackMapper.detail(dto);
		List<RectificationDto> recordDetails = rectFeedbackMapper.getRecordDetails(detail);
		detail.setRectificationDtos(recordDetails);
		return detail;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult rectify(RectificationDto dto) {
		try {
			// 整改完后变成待审核
			dto.setRectifyStatus("3");
			dto.setCreateUserId(AuthUtil.getUserId());
			Integer result = rectFeedbackMapper.rectify(dto);
			rectFeedbackMapper.saveRecord(dto);
			return result > 0 ? AjaxResult.success("整改成功") : AjaxResult.error("整改失败");
		} catch (Exception e) {
			return AjaxResult.error("请求出错了");
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult rectifyPass(RectificationDto dto) {
		try {
			// 整改完后变成通过
			dto.setRectifyStatus("1");
			dto.setCreateUserId(AuthUtil.getUserId());
			dto.setDescription("通过");
			Integer result = rectFeedbackMapper.rectify(dto);
			rectFeedbackMapper.saveRecord(dto);
			return result > 0 ? AjaxResult.success("整改通过") : AjaxResult.error("通过失败");
		} catch (Exception e) {
			return AjaxResult.error("请求出错了");
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult rectifyRefuse(RectificationDto dto) {
		try {
			// 整改完后变成驳回
			dto.setRectifyStatus("2");
			dto.setCreateUserId(AuthUtil.getUserId());
			Integer result = rectFeedbackMapper.rectify(dto);
			rectFeedbackMapper.saveRecord(dto);
			return result > 0 ? AjaxResult.success("整改通过") : AjaxResult.error("通过失败");
		} catch (Exception e) {
			return AjaxResult.error("请求出错了");
		}
	}
}
