package com.persagy.dmp.message.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.basic.utils.QueryCriteriaHelper;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.message.model.vo.MessageType;
import com.persagy.dmp.message.service.IMessageTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息发送 Controller
 * @author linhuili
 * @date 2021-09-26
 */
@RestController
@RequestMapping("/message/type")
public class MessageTypeController {

    @Autowired
    private IMessageTypeService service;
    
    /**
     * 查询消息类型
     * @param criteria
     * @return
     */
    @PostMapping("/query")
    public CommonResult<List<MessageType>> query(@RequestBody QueryCriteria criteria) {
        QueryWrapper<MessageType> wrapper = new QueryWrapper<>();
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, MessageType.class);
        Page page = service.queryByCondition(QueryCriteriaHelper.toPage(criteria), wrapper);
        return ResultHelper.multi(page.getRecords(), page.getTotal());
    }

    /**
     * 新增消息类型
     * @param vo
     */
    @PostMapping("/create")
    public CommonResult<MessageType> create(@Valid @RequestBody MessageType vo){
       if(vo == null){
           throw new BusinessException(ResponseCode.A0400.getCode(), "消息类型不能为空!");
       }
       List<MessageType> voList = new ArrayList<>();
       voList.add(vo);
       service.insertMessageType(voList);
       return ResultHelper.single(vo);
    }

    /**
     * 批量新增消息类型
     * @param voList
     */
    @PostMapping("/createBatch")
    public CommonResult<List<MessageType>> createBatch(@Valid @RequestBody List<MessageType> voList){
        if(CollUtil.isEmpty(voList)){
            throw new BusinessException(ResponseCode.A0400.getCode(), "消息类型不能为空!");
        }
        voList = service.insertMessageType(voList);
        return ResultHelper.multi(voList);
    }

    /**
     * 修改消息类型
     * @param vo
     */
    @PostMapping("/update")
    public CommonResult<MessageType> update(@Valid @RequestBody MessageType vo){
        if(vo == null){
            throw new BusinessException(ResponseCode.A0400.getCode(), "消息类型不能为空!");
        }
        List<MessageType> voList = new ArrayList<>();
        voList.add(vo);
        service.updateMessageType(voList);
        return ResultHelper.single(vo);
    }

    /**
     * 批量修改消息类型
     * @param voList
     */
    @PostMapping("/updateBatch")
    public CommonResult<List<MessageType>> update(@Valid @RequestBody List<MessageType> voList){
        if(voList == null){
            throw new BusinessException(ResponseCode.A0400.getCode(), "消息类型不能为空!");
        }
        service.updateMessageType(voList);
        return ResultHelper.multi(voList);
    }

    /**
     * 删除消息类型
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String id){
        if(StringUtils.isEmpty(id)){
            throw new BusinessException(ResponseCode.A0400.getCode(), "id不能为空");
        }
        service.deleteMessageType(CollUtil.newArrayList(id));
        return ResultHelper.success();
    }

    /**
     * 批量删除消息类型
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    public CommonResult deleteBatch(@RequestBody List<String> ids){
        if(ids == null || ids.size()<=0){
            throw new BusinessException(ResponseCode.A0400.getCode(), "id不能为空");
        }
        service.deleteMessageType(ids);
        return ResultHelper.success();
    }

}
