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.SenderRegister;
import com.persagy.dmp.message.service.ISenderRegisterService;
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/register")
public class SenderRegisterController {

    @Autowired
    private ISenderRegisterService service;
    
    /**
     * 查询发送渠道注册信息
     * @param criteria
     * @return
     */
    @PostMapping("/query")
    public CommonResult<List<SenderRegister>> query(@RequestBody QueryCriteria criteria) {
        QueryWrapper<SenderRegister> wrapper = new QueryWrapper<>();
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, SenderRegister.class);
        Page page = service.queryByCondition(QueryCriteriaHelper.toPage(criteria), wrapper);
        return ResultHelper.multi(page.getRecords(), page.getTotal());
    }

    /**
     * 新增发送渠道注册信息
     * @param vo
     */
    @PostMapping("/create")
    public CommonResult<SenderRegister> create(@Valid @RequestBody SenderRegister vo){
       if(vo == null){
           throw new BusinessException(ResponseCode.A0400.getCode(), "发送渠道注册信息不能为空!");
       }
       List<SenderRegister> voList = new ArrayList<>();
       voList.add(vo);
       service.insertMessageRegister(voList);
       return ResultHelper.single(vo);
    }

    /**
     * 批量新增发送渠道注册信息
     * @param voList
     */
    @PostMapping("/createBatch")
    public CommonResult<List<SenderRegister>> createBatch(@Valid @RequestBody List<SenderRegister> voList){
        if(CollUtil.isEmpty(voList)){
            throw new BusinessException(ResponseCode.A0400.getCode(), "发送渠道注册信息不能为空!");
        }
        voList = service.insertMessageRegister(voList);
        return ResultHelper.multi(voList);
    }

    /**
     * 修改发送渠道注册信息
     * @param vo
     */
    @PostMapping("/update")
    public CommonResult<SenderRegister> update(@Valid @RequestBody SenderRegister vo){
        if(vo == null){
            throw new BusinessException(ResponseCode.A0400.getCode(), "发送渠道注册信息不能为空!");
        }
        List<SenderRegister> voList = new ArrayList<>();
        voList.add(vo);
        service.updateMessageRegister(voList);
        return ResultHelper.single(vo);
    }

    /**
     * 批量修改发送渠道注册信息
     * @param voList
     */
    @PostMapping("/updateBatch")
    public CommonResult<List<SenderRegister>> update(@Valid @RequestBody List<SenderRegister> voList){
        if(voList == null){
            throw new BusinessException(ResponseCode.A0400.getCode(), "发送渠道注册信息不能为空!");
        }
        service.updateMessageRegister(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.deleteMessageRegister(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.deleteMessageRegister(ids);
        return ResultHelper.success();
    }
}
