package com.simuwang.manage.api.email;

import com.simuwang.base.common.page.TableDataInfo;
import com.simuwang.base.common.result.AjaxResult;
import com.simuwang.base.pojo.vo.EmailParseInfoVO;
import com.simuwang.base.pojo.vo.EmailTypeRuleVO;
import com.simuwang.manage.api.base.BaseController;
import com.simuwang.manage.service.ParseEmailService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * FileName: ParseEmailController
 * Author:   chenjianhua
 * Date:     2024/9/10 11:21
 * Description: ${DESCRIPTION}
 */
@RestController
@RequestMapping("/parse")
public class ParseEmailController extends BaseController {

    @Autowired
    private ParseEmailService parseEmailService;

    /**
     * 保存邮件类型识别配置
     * @param emailTypeRuleVO
     * @return
     */
    @PostMapping("/save-email-type")
    public AjaxResult saveEmailType(@RequestBody EmailTypeRuleVO emailTypeRuleVO){
        try{
            parseEmailService.saveEmailType(emailTypeRuleVO);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return AjaxResult.error("保存失败");
        }
        return AjaxResult.success();
    }

    /**
     * 查询邮件类型识别配置
     * @param
     * @return
     */
    @GetMapping("/search-email-type")
    public AjaxResult searchEmailType(){
        EmailTypeRuleVO emailTypeRuleVO = new EmailTypeRuleVO();
        try{
            emailTypeRuleVO = parseEmailService.searchEmailType();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return AjaxResult.error("查询失败");
        }
        return AjaxResult.success(emailTypeRuleVO);
    }

    /**
     * 表格查询
     * @param emailTitle
     * @param emailType
     * @param parseStatus
     * @return
     */
    @GetMapping("/search-email-list")
    public TableDataInfo searchEmailList(@RequestParam(value = "emailTitle",required = false)String emailTitle,
                                         @RequestParam(value = "emailType",required = false)Integer emailType,
                                         @RequestParam(value = "parseStatus",required = false)Integer parseStatus){
        List<EmailParseInfoVO> result = parseEmailService.searchEmailList(emailTitle,emailType,parseStatus);
        return getDataTable(result);
    }

    /**
     * 详情查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult searchEmail(@PathVariable("id") Integer id){
        EmailParseInfoVO result = parseEmailService.searchEmailById(id);
        return AjaxResult.success(result);
    }

    /**
     * 解析邮件数据统计
     * @param parseDate
     * @return
     */
    @GetMapping("/search-email-count")
    public AjaxResult searchEmailCount(@RequestParam("parseDate") String parseDate){
        Map<String,Integer> result = parseEmailService.searchEmailCount(parseDate);
        return AjaxResult.success(result);
    }
}
