package com.yida.data.user.controller.web;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.common.ResultMsgType;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.message.EduMessageStrategy;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.user.service.EduMessageStrategyService;
import com.yida.data.user.vo.EduMessageStrategyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息策略 Controller
 *
 * @author wjm
 * @date 2021-08-23 19:39:20
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/web/eduMessageStrategy")
@Api(tags = "学校消息策略后台接口")
@RequiredArgsConstructor
public class EduMessageStrategyController {

    private final EduMessageStrategyService eduMessageStrategyService;

    /***
     * @Author jianMingWang
     * @Description //获取当前学校关注回复信息
     * @Date 19:46 2021/8/23
     * @Param [schoolId]
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.common.core.entity.message.EduMessageStrategy>
     **/
    @ApiOperation("获取当前学校关注回复信息")
    @GetMapping("/findMessageBySchoolId")
    public ResultBean<EduMessageStrategy> findMessageBySchoolId(@ApiParam(value = "学校id", required = true) @RequestParam Long schoolId,
                                                                @ApiParam(value = "类型：1：关注自动回复，2：关键词回复，3：收到消息回复", required = true) @RequestParam Integer type,
                                                                @ApiParam(value = "子类型，1=智慧迎新关注回复", required = true) @RequestParam Integer subType) {
        try {
            return ResultBean.buildSuccess(eduMessageStrategyService.getOne(Wrappers.<EduMessageStrategy>query().lambda()
                    .eq(EduMessageStrategy::getSchoolId, schoolId)
                    .eq(EduMessageStrategy::getType, type)
                    .eq(EduMessageStrategy::getSubType, subType)
                    .eq(EduMessageStrategy::getDelFlag, 0)));
        } catch (Exception e) {
            log.error("获取当前学校关注回复信息: {}", e.getMessage(), e);
            return ResultBean.buildError(ResultMsgType.QUERY_FAIL.getValue());
        }
    }


    /***
     * @Author jianMingWang
     * @Description 保存或修改关注回复信息
     * @Date 19:46 2021/8/23
     * @Param [schoolId]
     * @return com.yida.data.common.core.common.ResultBean
     **/
    @ApiOperation("保存或修改关注回复信息")
    @PostMapping("/saveOrUpdateMessage")
    public ResultBean saveOrUpdateMessage(@ApiParam(value = "主键ID", required = true) @RequestBody EduMessageStrategyVo eduMessageStrategyVo) {
        try {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            return ResultBean.buildSuccess(eduMessageStrategyService.saveOrUpdateMessage(eduMessageStrategyVo, currentUser));
        } catch (Exception e) {
            log.error("保存或修改关注回复信息: {}", e.getMessage(), e);
            return ResultBean.buildError(ResultMsgType.QUERY_FAIL.getValue());
        }
    }

    /***
     * @Author jianMingWang
     * @Description 保存或修改关注回复信息
     * @Date 19:46 2021/8/23
     * @Param [schoolId]
     * @return com.yida.data.common.core.common.ResultBean
     **/
    @ApiOperation("删除回复信息")
    @GetMapping("/delMessage")
    public ResultBean saveOrUpdateMessage(@ApiParam(value = "主键ID", required = true) @RequestBody List<Integer> ids) {
        try {
            return ResultBean.buildSuccess(eduMessageStrategyService.removeByIds(ids));
        } catch (Exception e) {
            log.error("删除回复信息: {}", e.getMessage(), e);
            return ResultBean.buildError(ResultMsgType.QUERY_FAIL.getValue());
        }
    }
}
