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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.cyksj.common.constant.Constant;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.ChatgptUserTokenPrepareMapper;
import com.cyksj.mapper.GoodsDonSkuMapper;
import com.cyksj.model.entity.ChatgptUserTokenPrepare;
import com.cyksj.model.entity.GoodsDonSku;
import com.cyksj.model.request.ChatGptUserTokenGenerateReq;
import com.cyksj.model.views.ChatgptUserTokenPrepareView;
import com.cyksj.web.util.EasyExcelUtils;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 项目名: yhlxj2
 * 文件名: CmsMirrorController
 * 创建者: JavaZou
 * 创建时间:2024/4/18 17:45
 */
@RestController
@RequestMapping("/manage/mirror/gpt")
@RequiredArgsConstructor
public class CmsMirrorController {

	private final GoodsDonSkuMapper goodsDonSkuMapper;

	private final ChatgptUserTokenPrepareMapper chatgptUserTokenPrepareMapper;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	/**
	 * 生成并导出gpt会话
	 */
	@GetMapping("/generateAndExport/gpt/session")
	@Transactional(rollbackFor = Exception.class)
	public void generateAndExportGptSession(ChatGptUserTokenGenerateReq gptUserTokenGenerateReq, HttpServletResponse response) {
		Integer num = gptUserTokenGenerateReq.getNum();
		if (num == null || num <= 0) {
			throw new IllegalArgumentException("生成数量必须大于0");
		}
		Long skuId = gptUserTokenGenerateReq.getSkuId();
		GoodsDonSku sku = goodsDonSkuMapper.selectById(skuId);
		if (sku == null) {
			throw new IllegalArgumentException("镜像规格不存在");
		}
		if (!sku.getIsCar()) {
			throw new IllegalArgumentException("不是gpt车队规格");
		}
		String name = Optional.ofNullable(gptUserTokenGenerateReq.getName()).orElse(Constant.YH_GPT_USER_NAME);
		String img = Optional.ofNullable(gptUserTokenGenerateReq.getName()).orElse(Constant.DEFAULT_HEAD_IMG);
		List<ChatgptUserTokenPrepare> prepares = new ArrayList<>(num);
		ChatgptUserTokenPrepare chatgptUserTokenPrepare = null;

		for (int i = 0; i < num; i++) {
			chatgptUserTokenPrepare = new ChatgptUserTokenPrepare();
			chatgptUserTokenPrepare.setUserToken(UUID.randomUUID().toString());
			chatgptUserTokenPrepare.setName(name);
			chatgptUserTokenPrepare.setImg(img);
			chatgptUserTokenPrepare.setSkuId(skuId);
			chatgptUserTokenPrepare.setSkuName(sku.getSpecVal());
			prepares.add(chatgptUserTokenPrepare);
		}
		if (CollUtil.isNotEmpty(prepares)) {
			Lists.partition(prepares, 500).forEach(e -> chatgptUserTokenPrepareMapper.batchInsert(e));
		}
		EasyExcelUtils.createExcelStreamMutilByEasyExcel(response, ChatgptUserTokenPrepare.class, prepares, "用户token","用户token", ExcelTypeEnum.XLSX, null);
	}

	/**
	 * 镜像车队token 列表
	 */
	@GetMapping("/get/list")
	public Result<SearchResult<ChatgptUserTokenPrepareView>> getList() {
		SearchResult<ChatgptUserTokenPrepareView> search = beanSearcher.search(ChatgptUserTokenPrepareView.class, MapUtils.flatBuilder(request.getParameterMap())
				.orderBy(ChatgptUserTokenPrepareView::getId).desc()
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}
}
