package com.cyksj.web.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.manage.sys.FrontProblemCollectRecordMapper;
import com.cyksj.model.entity.FrontProblemCollectRecord;
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;

/*
 *项目名: netflix
 *文件名: FrontProblemCollectRecordController
 *创建者: JavaZou
 *创建时间:2023/4/14 12:18
 */
@RestController
@RequestMapping("/problem")
@RequiredArgsConstructor
public class FrontProblemCollectRecordController {
	private final FrontProblemCollectRecordMapper recordMapper;

	private final HttpServletRequest request;

	private final BeanSearcher beanSearcher;

	@PostMapping
	public Result saveProblem(@RequestBody FrontProblemCollectRecord record) {
		String errorMsg = record.getErrorMsg();
		String track = record.getTrack();
		String handle = record.getHandle();
		Boolean isFlag = false;
		if (StrUtil.isNotBlank(errorMsg)) {
			record.setErrorMsg(StrUtil.subSufByLength(errorMsg, 2000));
			isFlag = true;
		}
		if (StrUtil.isNotBlank(track)) {
			record.setTrack(StrUtil.subSufByLength(track, 2000));
			isFlag = true;
		}
		if (StrUtil.isNotBlank(handle)) {
			record.setHandle(StrUtil.subSufByLength(handle, 2000));
			isFlag = true;
		}
		if (isFlag) {
			recordMapper.insert(record);
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 列表
	 */
	@GetMapping
	public Result<SearchResult<FrontProblemCollectRecord>> getRecords() {
		SearchResult<FrontProblemCollectRecord> search = beanSearcher.search(FrontProblemCollectRecord.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 清空
	 */
	@DeleteMapping("/empty")
	public Result<String> delete() {
		recordMapper.delete(null);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
