package com.foreverwin.mesnac.common.controller;

import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.CommonMethods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mesnac.common.service.MessageService;
import com.foreverwin.mesnac.common.model.Message;
import java.util.List;

/**
 *
 * @author zjw
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/Z-MESSAGE")
public class MessageController {

    @Autowired
    public MessageService messageService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getMessageById(@PathVariable String id) {
        return R.ok( messageService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getMessageList(Message message){
        List<Message> result;
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(message);
        result = messageService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<Message> frontPage, Message message){
        IPage result;
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(message);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(Message::getHandle, frontPage.getGlobalQuery())
                .or().like(Message::getSite, frontPage.getGlobalQuery())
                .or().like(Message::getObjectBo, frontPage.getGlobalQuery())
                .or().like(Message::getType, frontPage.getGlobalQuery())
                .or().like(Message::getMessageType, frontPage.getGlobalQuery())
                .or().like(Message::getSendUserGroup, frontPage.getGlobalQuery())
                .or().like(Message::getSendUser, frontPage.getGlobalQuery())
                .or().like(Message::getContent, frontPage.getGlobalQuery())
                .or().like(Message::getUpUserGroup, frontPage.getGlobalQuery())
                .or().like(Message::getUpUser, frontPage.getGlobalQuery())
                .or().like(Message::getUpMessageType, frontPage.getGlobalQuery())
                .or().like(Message::getNode, frontPage.getGlobalQuery())
                .or().like(Message::getStatus, frontPage.getGlobalQuery())
                .or().like(Message::getCreatedUser, frontPage.getGlobalQuery())
                .or().like(Message::getModifiedUser, frontPage.getGlobalQuery())
    );
        }
        result = messageService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param message  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody Message message) {
        return R.ok(messageService.save(message));
    }

    /**
     * 修改
     * @param message  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody Message message) {
        return R.ok(messageService.updateById(message));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(messageService.removeById(id));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(messageService.removeByIds(ids));
    }
}