package com.simuwang.manage.api.company;


import com.simuwang.base.common.page.TableDataInfo;
import com.simuwang.base.common.result.AjaxResult;
import com.simuwang.base.pojo.vo.CompanyEmailConfigVO;
import com.simuwang.base.pojo.vo.CompanyEmailSendHistoryVO;
import com.simuwang.manage.api.base.BaseController;
import com.simuwang.manage.service.CompanyEmailConfigService;
import com.simuwang.manage.service.CompanyEmailSendHistoryService;
import com.smppw.common.pojo.ResultVo;
import com.smppw.common.pojo.enums.status.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * FileName: CompanyInformationController
 * Author:   chenjianhua
 * Date:     2024/9/6 15:25
 * Description: ${DESCRIPTION}
 */
@RestController
@RequestMapping("/company")
public class CompanyEmailSendHistoryController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(CompanyEmailSendHistoryController.class);
    @Autowired
    private CompanyEmailSendHistoryService companyEmailSendHistoryService;

    @Autowired
    private CompanyEmailConfigService companyEmailConfigService;
    /**
     * 表格查询
     * @param email
     * @return
     */
    @RequestMapping("search-company-email")
    public TableDataInfo searchCompanyEmail(@RequestParam(value = "companyName",required = false)String companyName,
                                            @RequestParam(value = "email",required = false)String email,
                                            @RequestParam(value = "sendStatus",required = false)Integer sendStatus){
        List<CompanyEmailSendHistoryVO> result = companyEmailSendHistoryService.searchCompanyEmail(companyName,email,sendStatus);
        return getDataTable(result);
    }

    /**
     * 查询邮箱发送历史
     * @param email
     * @return
     */
    @RequestMapping("search-email-history")
    public TableDataInfo searchEmailHistory(@RequestParam(value = "email",required = false)String email){
        List<CompanyEmailSendHistoryVO> result = companyEmailSendHistoryService.searchEmailHistory(email);
        return getDataTable(result);
    }

    /**
     * 删除邮箱配置
     * @param email
     * @return
     */
    @PostMapping("delete-email")
    public AjaxResult deleteEmail(@RequestParam(value = "email",required = false)String email){
        companyEmailSendHistoryService.deleteEmail(email);
        return AjaxResult.success();
    }
    /**
     * 删除邮箱发送历史数据
     * @param ids
     * @return
     */
    @PostMapping("delete-email-history")
    public AjaxResult deleteEmailHistory(String ids){
        companyEmailSendHistoryService.deleteEmailHistory(ids);
        return AjaxResult.success();
    }

    /**
     * 保存邮箱配置
     * @param companyEmailConfigVOS
     * @return
     */
    @PostMapping("save-email-config")
    public ResultVo saveCompanyEmailConfig(@RequestBody List<CompanyEmailConfigVO> companyEmailConfigVOS){
        ResultVo vo = new ResultVo(ResultCode.SAVE_SUCCESS);
        try{
            companyEmailConfigService.saveCompanyEmailConfig(companyEmailConfigVOS);
        }catch (Exception e){
            vo = new ResultVo(ResultCode.SAVE_FAILED);
            logger.error(e.getMessage(),e);
        }
        return vo;
    }

    /**
     * 修改邮箱配置
     * @param companyEmailConfigVO
     * @return
     */
    @PostMapping("update-email-config")
    public ResultVo updateCompanyEmailConfig(@RequestBody CompanyEmailConfigVO companyEmailConfigVO){
        ResultVo vo = new ResultVo(ResultCode.SAVE_SUCCESS);
        try{
            companyEmailConfigService.updateCompanyEmailConfig(companyEmailConfigVO);
        }catch (Exception e){
            vo = new ResultVo(ResultCode.SAVE_FAILED);
            logger.error(e.getMessage(),e);
        }
        return vo;
    }

    /**
     * 删除邮箱配置
     * @param id
     * @return
     */
    @PostMapping("delete-email-config")
    public ResultVo deleteCompanyEmailConfig(@RequestParam("id")Integer id){
        ResultVo vo = new ResultVo(ResultCode.DELETE_SUCCESS);
        try{
            companyEmailConfigService.deleteCompanyEmailConfig(id);
        }catch (Exception e){
            vo = new ResultVo(ResultCode.DELETE_FAILED);
            logger.error(e.getMessage(),e);
        }
        return vo;
    }
}
