package com.storlead.mail.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.mail.pojo.entity.EmailTemplatesEntity;
import com.storlead.mail.pojo.entity.SmtpPopSettingsEntity;
import com.storlead.mail.pojo.dto.EmailTemplatesDTO;
import com.storlead.mail.service.EmailTemplatesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-05-29 16:02
 */
@RestController
@RequestMapping("/email/templates")
@Api(tags = "邮件: 邮件模板")
@Log4j2
public class MailTemplatesApiController {

    @Resource
    private EmailTemplatesService templatesService;

    @PostMapping(value = "/page_list")
    @ApiOperation(value = "获取邮件模板" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> pageList(EmailTemplatesDTO dto) {

        Long currentUserId = LoginUserUtil.getCurrentUserId();
        Page<EmailTemplatesEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<EmailTemplatesEntity> queryWrapper = new LambdaQueryWrapper();
        // queryWrapper.select(EmailTemplatesEntity::getId,EmailTemplatesEntity::getIsDelete,EmailTemplatesEntity::getTemplateTitle,EmailTemplatesEntity::getTemplateType,EmailTemplatesEntity::getCreateTime,EmailTemplatesEntity::getUpdateTime);
        queryWrapper.eq(EmailTemplatesEntity::getOwnerBy,currentUserId);
        if (Objects.nonNull(dto.getTemplateType())) {
            queryWrapper.eq(EmailTemplatesEntity::getTemplateType,dto.getTemplateType());
        }
        queryWrapper.eq(EmailTemplatesEntity::getIsDelete,Integer.valueOf(0));
        IPage<EmailTemplatesEntity> pageList = templatesService.page(page,queryWrapper);
        return Result.result(pageList);
    }

    @PostMapping(value = "/getById")
    @ApiOperation(value = "获取邮件模板详情" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> getById(Long id) {
        EmailTemplatesEntity templatesEntity = templatesService.getById(id);
        return Result.result(templatesEntity);
    }


    @PostMapping(value = "/list")
    @ApiOperation(value = "获取邮件模板" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> list(EmailTemplatesDTO dto) {

        Long currentUserId = LoginUserUtil.getCurrentUserId();
        Page<EmailTemplatesEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<EmailTemplatesEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(EmailTemplatesEntity::getId,EmailTemplatesEntity::getIsDelete,EmailTemplatesEntity::getTemplateTitle,EmailTemplatesEntity::getTemplateType,EmailTemplatesEntity::getCreateTime,EmailTemplatesEntity::getUpdateTime);
        queryWrapper.eq(EmailTemplatesEntity::getOwnerBy,currentUserId);
        queryWrapper.eq(EmailTemplatesEntity::getIsDelete,Integer.valueOf(0));
        if (Objects.nonNull(dto.getTemplateType())) {
            queryWrapper.eq(EmailTemplatesEntity::getTemplateType,dto.getTemplateType());
        }
        queryWrapper.eq(EmailTemplatesEntity::getEnabled,dto.getEnabled());
        IPage<EmailTemplatesEntity> pageList = templatesService.page(page,queryWrapper);
        return Result.result(pageList);
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存模板" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> save(EmailTemplatesEntity entity) {
        templatesService.saveOrUpdate(entity);
        return Result.ok();
    }

    @ApiOperation(value = "删除")
    @PostMapping("deleteIds")
    public Result delete(Long [] ids) {
        if(Objects.isNull(ids)) {
            return Result.error("参数错误");
        }
        templatesService.lgDelete(Arrays.asList(ids));
        return Result.ok();
    }


    @ApiOperation("禁用或启用")
    @PostMapping("enabled")
    public Result enabled(Long id,Boolean enable) {
        LambdaUpdateWrapper<EmailTemplatesEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(EmailTemplatesEntity::getEnabled,enable);
        updateWrapper.eq(EmailTemplatesEntity::getId,id);
        templatesService.update(updateWrapper);
        return Result.ok();
    }
}
