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

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.SoraCodeMapper;
import com.cyksj.model.entity.SoraCode;
import com.cyksj.model.excel.ExcelSoraCodeData;
import com.cyksj.model.request.SoraCodeAddReq;
import com.cyksj.model.request.SoraCodeUpdateReq;
import com.cyksj.service.sora.SoraCodeService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Sora邀请码管理Controller
 *
 * @author system
 * @date 2025-10-13
 */
@Slf4j
@RestController
@RequestMapping("/manage/sora-code")
@RequiredArgsConstructor
public class SoraCodeController {

    private final SoraCodeService soraCodeService;

    private final SoraCodeMapper soraCodeMapper;

    private final BeanSearcher beanSearcher;

    private final HttpServletRequest request;

    /**
     * 添加Sora邀请码
     */
    @PostMapping("/add")
    public Result<String> addSoraCode(@RequestBody @Validated SoraCodeAddReq req) {
        soraCodeService.addSoraCode(req);
        return GatewayResponse.SUCCESS.newBuilder().toResult("添加成功");
    }

    /**
     * 更新Sora邀请码
     */
    @PutMapping("/update")
    public Result<String> updateSoraCode(@RequestBody @Validated SoraCodeUpdateReq req) {
        soraCodeService.updateSoraCode(req);
        return GatewayResponse.SUCCESS.newBuilder().toResult("更新成功");
    }

    /**
     * 删除Sora邀请码
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteSoraCode(@PathVariable Long id) {
        soraCodeService.deleteSoraCode(id);
        return GatewayResponse.SUCCESS.newBuilder().toResult("删除成功");
    }

    /**
     * 分页查询Sora邀请码
     */
    @GetMapping("/get")
    public Result<SearchResult<SoraCode> > getSoraCodePage() {
        SearchResult<SoraCode> search = beanSearcher.search(SoraCode.class, MapUtils.flatBuilder(request.getParameterMap()).build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(search);
    }

    /**
     * 批量导入Sora邀请码
     */
    @PostMapping("/batch-import")
    public Result<String> batchImportSoraCode(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), ExcelSoraCodeData.class, new AnalysisEventListener<ExcelSoraCodeData>() {
            @Override
            public void invoke(ExcelSoraCodeData data, AnalysisContext analysisContext) {
                String code = data.getCode();
                if (StrUtil.isNotBlank(code)) {
                    SoraCode soraCode = new SoraCode();
                    soraCode.setCode(code);
                    try {
                        soraCodeMapper.insert(soraCode);
                    } catch (DataAccessException e) {

                    }
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).sheet().doRead();
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }
}