package cn.ezhizao.project.business.email.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.email.domain.BizEmail;
import cn.ezhizao.project.business.email.service.IBizEmailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 提醒邮件配置文件Controller
 *
 * @author ezhizao
 * @date 2025-03-12
 */
@RestController
@RequestMapping("/business/email")
public class BizEmailController extends BaseController
{
    @Resource
    private IBizEmailService bizEmailService;

    /**
     * 查询提醒邮件配置文件列表
     */
    @PreAuthorize("@ss.hasPermi('business:email:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizEmail bizEmail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizEmail);
        startPage();
        List<BizEmail> list = bizEmailService.getList(bizEmail);
        return getDataTable(list);
    }

    /**
     * 导出提醒邮件配置文件列表
     */
    @PreAuthorize("@ss.hasPermi('business:email:export')")
    @Log(title = "提醒邮件配置文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizEmail bizEmail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizEmail);
        List<BizEmail> list = bizEmailService.getList(bizEmail);
        ExcelUtil<BizEmail> util = new ExcelUtil<BizEmail>(BizEmail.class);
        util.exportExcel(response, list, "提醒邮件配置文件数据");
    }

    /**
     * 获取提醒邮件配置文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:email:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizEmailService.getById(id));
    }

    /**
     * 新增提醒邮件配置文件
     */
    @PreAuthorize("@ss.hasPermi('business:email:add')")
    @Log(title = "提醒邮件配置文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizEmail bizEmail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizEmail);
        if (bizEmail.getType().equals(0)) {
            // 查询发送邮箱
            List<BizEmail> bizEmails = bizEmailService.query().eq("is_stop", 0).eq("type", 0).list();
            bizEmails.forEach(l -> l.setIsStop(1));
            bizEmailService.updateBatchById(bizEmails);
        }
        return toAjax(bizEmailService.save(bizEmail));
    }

    /**
     * 修改提醒邮件配置文件
     */
    @PreAuthorize("@ss.hasPermi('business:email:edit')")
    @Log(title = "提醒邮件配置文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizEmail bizEmail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizEmail);
        if (bizEmail.getType().equals(0)) {
            // 查询发送邮箱
            List<BizEmail> bizEmails = bizEmailService.query().ne("id", bizEmail.getId()).eq("is_stop", 0).eq("type", 0).list();
            bizEmails.forEach(l -> l.setIsStop(1));
            bizEmailService.updateBatchById(bizEmails);
        }
        return toAjax(bizEmailService.updateById(bizEmail));
    }

    /**
     * 删除提醒邮件配置文件
     */
    @PreAuthorize("@ss.hasPermi('business:email:remove')")
    @Log(title = "提醒邮件配置文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizEmailService.removeBatchByIds(ids));
    }
}
