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.TBbsTopic;
import com.nantian.cloud.jljc.entity.TTraining;
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.web.bind.annotation.*;

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

/**
 * 交流主题信息
 *
 * @date 2020-09-03 13:23:34
 */
@CrossOrigin
@AllArgsConstructor
@Api(value = "tbbstopic", tags = "交流主题信息")
@RestController
@RequestMapping("tbbstopic")
public class TBbsTopicController {
    @Autowired
    private TBbsTopicService tBbsTopicService;

    /**
     * 列表
     */
    @GetMapping("/page")
    @SysLog("分页获取交流主题信息")
    @ApiOperation(value = "分页获取交流主题信息", notes = "分页获取交流主题信息")
    @ApiImplicitParam(name = "TBbsTopic", value = "分页获取交流主题信息", required = true, paramType = "path")
    public R page(Page page, TBbsTopic tBbsTopic){
        //return R.ok(tBbsTopicService.page(page, Wrappers.query(tBbsTopic)));
        tBbsTopic.setDelFlag("1"); //查询未删除数据
        return R.ok(tBbsTopicService.getPage(page, tBbsTopic));
    }


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

    /**
     * 修改点击次数与点击时间
     */
    @GetMapping("/count")
    @SysLog("修改交流信息点击次数与点击时间")
    @ApiOperation(value = "修改点击次数与点击时间", notes = "修改点击次数与点击时间")
    @ApiImplicitParam(name = "id", value = "交流主题信息id", required = true, paramType = "path")
    public R count(Integer id){
        TBbsTopic tBbsTopic = tBbsTopicService.getById(id);
        if(tBbsTopic==null){
            return R.failed("交流主题信息不存在！");
        }
        tBbsTopic.setClickCount(tBbsTopic.getClickCount()+1);
        tBbsTopic.setLastclickTime(new Date());
        return R.ok(tBbsTopicService.updateById(tBbsTopic));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @SysLog("新增交流主题信息")
    @ApiImplicitParam(name = "TBbsTopic", value = "新增交流主题信息", required = true, dataType = "json", paramType = "path")
    @ApiOperation(value = "新增交流主题信息", notes = "新增交流主题信息")
    public R save(@RequestBody TBbsTopic tBbsTopic){
        ScUser u= SecurityUtils.getUser();
        tBbsTopic.setUserid(u.getId());
        tBbsTopic.setUsername(u.getUsername());
        tBbsTopic.setDeptId(u.getDeptId());
        tBbsTopic.setDelFlag("1");
        tBbsTopic.setUpdateTime(new Date());
        return R.ok(tBbsTopicService.save(tBbsTopic));
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @SysLog("修改交流主题信息")
    @ApiOperation(value = "修改交流主题信息", notes = "修改交流主题信息")
    @ApiImplicitParam(name = "TBbsTopic", value = "修改交流主题信息", required = true, dataType = "json", paramType = "path")
    public R update(@RequestBody TBbsTopic tBbsTopic){
        ScUser u= SecurityUtils.getUser();
        tBbsTopic.setUserid(u.getId());
        tBbsTopic.setUpdateTime(new Date());
        return R.ok(tBbsTopicService.updateById(tBbsTopic));
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @SysLog("删除交流主题信息")
    @ApiOperation(value = "删除交流主题信息", notes = "删除交流主题信息")
    public R removeById(Integer id){
        TBbsTopic tBbsTopic = tBbsTopicService.getById(id);
        ScUser u= SecurityUtils.getUser();
        tBbsTopic.setUserid(u.getId());
        tBbsTopic.setUpdateTime(new Date());
        tBbsTopic.setDelFlag("4");
        return R.ok(tBbsTopicService.updateById(tBbsTopic));
    }

}
