package com.slodon.b2b2c.investment.controller;

import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.investment.bean.analysis.dto.CompanyListDTO;
import com.slodon.b2b2c.investment.bean.analysis.vo.CompanyEntity;
import com.slodon.b2b2c.investment.constant.InvestmentConst;
import com.slodon.b2b2c.investment.model.AnalysisModel;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author sunshihao
 * @version 1.0
 * @description: TODO
 * @date 2025/9/2 13:28
 */
@Api(tags = "ai商情分析历史")
@RestController
@RequestMapping("/v3/ai/history")
@Slf4j
public class AiController {
    @Resource
    private AnalysisModel analysisModel;
    /***
     * @description: ai商情分析任务列表
     * @param: phone
     * @return: com.slodon.b2b2c.core.response.JsonResult<java.util.List<com.slodon.b2b2c.investment.bean.analysis.vo.CompanyEntity>>
     * @author sunshihao
     * @date: 2025/9/1 10:26
     */
    @GetMapping("/aiList")
    public JsonResult<PageVO<CompanyEntity>> aiList(HttpServletRequest request,
                                                    @RequestParam(value = "phone", required = false) String phone,
                                                    @RequestParam(value = "startTime", required = false) String startTime,
                                                    @RequestParam(value = "endTime", required = false) String endTime,
                                                    @RequestParam(value = "companyName", required = false) String companyName) {
        List<CompanyEntity> list;
        PagerInfo pager;
        // 定义格式转换器
        DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(InvestmentConst.YYYY_MM_DD_HH_MM_SS);
        LocalDateTime stime = null;
        LocalDateTime etime = null;
        if(StringUtils.isNotEmpty(startTime)){
            stime = LocalDateTime.parse(startTime, FORMATTER);
        }
        if(StringUtils.isNotEmpty(endTime)){
            etime = LocalDateTime.parse(endTime, FORMATTER);
        }
        try {
            pager = WebUtil.handlerPagerInfo(request);
            CompanyListDTO dto = new CompanyListDTO();
            dto.setPhone(phone);
            dto.setStartTime(stime);
            dto.setEndTime(etime);
            dto.setCompanyName(companyName);
            list = analysisModel.aiList(dto, pager);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return SldResponse.fail( e.getMessage());
        }
        return SldResponse.success(new PageVO<>(list, pager));
    }
}
