package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TBbsReplay;
import com.nantian.cloud.jljc.entity.TBbsTopic;
import com.nantian.cloud.jljc.entity.TTrainingPerson;
import com.nantian.cloud.jljc.service.TBbsReplayService;
import com.nantian.cloud.jljc.service.TBbsTopicService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;


/**
 * 交流回复信息
 *
 * @date 2020-09-03 13:23:34
 */
@CrossOrigin
@AllArgsConstructor
@Api(value = "tbbsreplay", tags = "交流回复信息")
@RestController
@RequestMapping("tbbsreplay")
public class TBbsReplayController {
    @Autowired
    private TBbsReplayService tBbsReplayService;
    @Autowired
    private TBbsTopicService tBbsTopicService;


    @GetMapping("/page")
    @SysLog("分页获取交流回复信息")
    @ApiOperation(value = "分页获取交流回复信息", notes = "分页获取交流回复信息")
    @ApiImplicitParam(name = "TBbsReplay", value = "分页获取交流主题信息，topicid必传", required = true, paramType = "path")
    public R page(Page page, TBbsReplay tBbsReplay) {
        tBbsReplay.setDelFlag("1");  //查询未删除数据
        return R.ok(tBbsReplayService.page(page, Wrappers.query(tBbsReplay)));
    }

    @SysLog("获取交流回复信息")
    @ApiOperation(value = "获取交流回复信息", notes = "获取交流回复信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        return R.ok(tBbsReplayService.getById(id));
    }

    @Transactional
    @SysLog("新增交流回复信息")
    @ApiOperation(value = "新增交流回复信息", notes = "新增交流回复信息")
    @PostMapping("/save")
    @ApiImplicitParam(name = "TBbsReplay", value = "新增回复", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody TBbsReplay tBbsReplay) {
        if (tBbsReplay.getTopicid() == null) {
            return R.failed("交流主题信息不存在，回复失败！");
        } else {
            TBbsTopic tBbsTopic = tBbsTopicService.getById(tBbsReplay.getTopicid());
            if (tBbsTopic != null) {  //修改交流主题信息的回复次数
                tBbsTopic.setReplayCount(tBbsTopic.getReplayCount() + 1);
                tBbsTopicService.updateById(tBbsTopic);
            }
        }

        ScUser u = SecurityUtils.getUser();
        tBbsReplay.setUserid(u.getId());
        tBbsReplay.setDeptId(u.getDeptId());
        tBbsReplay.setDelFlag("1");
        tBbsReplay.setUpdateTime(new Date());
        return R.ok(tBbsReplayService.save(tBbsReplay));
    }

    @SysLog("更新交流回复信息")
    @ApiOperation(value = "更新交流回复信息", notes = "更新交流回复信息")
    @PutMapping("/update")
    public R update(@RequestBody TBbsReplay tBbsReplay) {
        ScUser u = SecurityUtils.getUser();
        tBbsReplay.setUserid(u.getId());
        tBbsReplay.setUpdateTime(new Date());
        return R.ok(tBbsReplayService.updateById(tBbsReplay));
    }

    @SysLog("删除交流回复信息")
    @ApiOperation(value = "删除交流回复信息", notes = "删除交流回复信息")
    @DeleteMapping("/delete")
    public R delete(Integer id) {
        TBbsReplay tBbsReplay = tBbsReplayService.getById(id);
        ScUser u = SecurityUtils.getUser();
        tBbsReplay.setUserid(u.getId());
        tBbsReplay.setUpdateTime(new Date());
        tBbsReplay.setDelFlag("4");
        return R.ok(tBbsReplayService.updateById(tBbsReplay));
    }
}
