package com.foreverwin.mesnac.equip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.mesnac.equip.model.EmailGroup;
import com.foreverwin.mesnac.equip.service.EmailGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author Max
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/Z-EMAIL-GROUP")
public class EmailGroupController {

    @Autowired
    public EmailGroupService emailGroupService;

    /**
    * 根据id查询
    *
    * @param emailGroup 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/search/{emailGroup}")
    public R getEmailGroupById(@PathVariable("emailGroup") String emailGroup) {
        String site = CommonMethods.getSite();
        EmailGroup _emailGroup = new EmailGroup();
        _emailGroup.setSite( site );
        _emailGroup.setEmailGroup( emailGroup );
        QueryWrapper<EmailGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity( _emailGroup );
        EmailGroup eg = emailGroupService.getOne( queryWrapper );
        if ( eg == null ) {
            return R.failed( "邮件群组["+emailGroup+"]不存在" );
        }
        return R.ok(eg);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getEmailGroupList(EmailGroup emailGroup){
        String site = CommonMethods.getSite();
        List<EmailGroup> result;
        emailGroup.setSite( site );
        QueryWrapper<EmailGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(emailGroup);
        result = emailGroupService.list(queryWrapper);
        return R.ok(result);
    }

}