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.constant.ValidEnum;
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.MessageVO;
import com.persagy.dmp.message.service.IMessageService;
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")
public class MessageController {

    @Autowired
    private IMessageService service;

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

    /**
     * 新增消息
     * @param vo
     */
    @PostMapping("/create")
    public CommonResult<MessageVO> create(@Valid @RequestBody MessageVO vo){
        if(vo == null){
            throw new BusinessException(ResponseCode.A0400.getCode(), "消息不能为空!");
        }
        List<MessageVO> voList = new ArrayList<>();
        voList.add(vo);
        //添加创建默认值
        addCreateDefaultValue(voList);
        //新增消息
        service.insertMessage(voList);
        return ResultHelper.single(vo);
    }

    /**
     * 批量新增消息
     * @param voList
     */
    @PostMapping("/createBatch")
    public CommonResult<List<MessageVO>> createBatch(@Valid @RequestBody List<MessageVO> voList){
        if(CollUtil.isEmpty(voList)){
            throw new BusinessException(ResponseCode.A0400.getCode(), "消息不能为空!");
        }
        //添加创建默认值
        addCreateDefaultValue(voList);
        //新增消息
        voList = service.insertMessage(voList);
        return ResultHelper.multi(voList);
    }

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

    /**
     * 批量修改消息
     * @param voList
     */
    @PostMapping("/updateBatch")
    public CommonResult<List<MessageVO>> update(@Valid @RequestBody List<MessageVO> voList){
        if(voList == null){
            throw new BusinessException(ResponseCode.A0400.getCode(), "消息不能为空!");
        }
        service.updateMessage(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.deleteMessage(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.deleteMessage(ids);
        return ResultHelper.success();
    }

    /**
     * 添加创建默认值
     * @param voList
     */
    private void addCreateDefaultValue(List<MessageVO> voList){
        if(CollUtil.isEmpty(voList)){
            return;
        }
        for (MessageVO messageVO : voList) {
            if(messageVO.getValid() == null){
              messageVO.setValid(ValidEnum.TRUE.getType());
              messageVO.setTopFlag(false);
              messageVO.setReadFlag(false);
            }
        }
    }
}
