package com.sqx.modules.shipinquan.content.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.service.ArtificerService;
import com.sqx.modules.shipinquan.entity.BlShipinquan;
import com.sqx.modules.shipinquan.entity.BlShipinquanDianzan;
import com.sqx.modules.shipinquan.entity.BlShipinquanPinglun;
import com.sqx.modules.shipinquan.service.BlShipinquanDianzanService;
import com.sqx.modules.shipinquan.service.BlShipinquanPinglunService;
import com.sqx.modules.shipinquan.service.BlShipinquanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/app/shipinquan")
@Api(value = "视频圈", tags = {"视频圈"})
public class AppShipinquanController {


    @Autowired
    private BlShipinquanService service;
    @Autowired
    private BlShipinquanPinglunService pinglunservice;
    @Autowired
    private BlShipinquanDianzanService dianzanService;
    @Autowired
    private ArtificerService artificerService;

    @GetMapping("/list")
    @ApiOperation("获取视频数据列表")
    public Result list(BlShipinquan entity, Integer page, Integer limit){
        if(page==null){
            page=1;
            limit=10;
        }
        return service.findPage(page,limit,entity);
    }


    @GetMapping("/getArticlelist")
    @ApiOperation("获取视频数据列表")
    public Result getArticlelist(BlShipinquan entity, Integer page, Integer limit){
        Artificer artificer=artificerService.getById(entity.getCreateBy());
        entity.setCreateBy(artificer.getUserId());
        if(page==null){
            page=1;
            limit=10;
        }
        return service.findPage(page,limit,entity);
    }


    @PostMapping("/add")
    @ApiOperation("添加视频内容")
    public Result add(@RequestBody BlShipinquan BlShipinquan){
        BlShipinquan.setCreateTime(new Date());
        service.save(BlShipinquan);
        return Result.success();
    }

    @GetMapping("/delete")
    @ApiOperation("删除")
    public Result delete(String ids){
        if(StringUtils.isNotBlank(ids)){
            String idarr[] = ids.split(",");
            for(String id:idarr){
                service.removeById(Integer.parseInt(id));
            }
        }

        return Result.success();
    }


    @PostMapping("/addContent")
    @ApiOperation("添加评论内容")
    public Result addContent(BlShipinquanPinglun blShipinquanPinglun){
        blShipinquanPinglun.setCreateTime(new Date());
        pinglunservice.save(blShipinquanPinglun);

        QueryWrapper<BlShipinquanPinglun> query = new QueryWrapper<BlShipinquanPinglun>();
        query.eq("shipinquan_id", blShipinquanPinglun.getShipinquanId());
        List<BlShipinquanPinglun> list = pinglunservice.list(query);

        BlShipinquan blShipinquan = service.getById(blShipinquanPinglun.getShipinquanId());
        blShipinquan.setPls(list.size());
        service.updateById(blShipinquan);

        return Result.success();
    }

    @GetMapping("/contentlist")
    @ApiOperation("获取评论数据列表")
    public Result contentlist(BlShipinquanPinglun entity, Integer page, Integer limit){
        IPage<BlShipinquanPinglun> pageList = pinglunservice.contentlist(new Page<>(page,limit),entity);
        return Result.success().put("data",pageList);
    }


    @PostMapping("/addDianzan")
    @ApiOperation("添加点赞信息")
    public Result addDianzan(BlShipinquanDianzan blShipinquanDianzan){
        if(StringUtils.isNotEmpty(blShipinquanDianzan.getShipinquanId()+"")){
            blShipinquanDianzan.setCreateTime(new Date());
            dianzanService.save(blShipinquanDianzan);

            BlShipinquan blShipinquan = service.getById(blShipinquanDianzan.getShipinquanId());
            Integer dzs = blShipinquan.getDzs();
            dzs++;
            blShipinquan.setDzs(dzs);
            service.updateById(blShipinquan);
            return Result.success();
        }else{
            return Result.error("点赞失败");
        }

    }

    @PostMapping("/delDianzan")
    @ApiOperation("添加点赞信息")
    public Result delDianzan(BlShipinquanDianzan blShipinquanDianzan){
        if(StringUtils.isNotEmpty(blShipinquanDianzan.getShipinquanId()+"") && StringUtils.isNotEmpty(blShipinquanDianzan.getCreateBy()+"")){
            QueryWrapper<BlShipinquanDianzan> query = new QueryWrapper<BlShipinquanDianzan>();
            query.eq("shipinquan_id",blShipinquanDianzan.getShipinquanId());
            query.eq("create_by",blShipinquanDianzan.getCreateBy());
            dianzanService.remove(query);

            BlShipinquan blShipinquan = service.getById(blShipinquanDianzan.getShipinquanId());
            Integer dzs = blShipinquan.getDzs();
            dzs--;
            blShipinquan.setDzs(dzs);
            service.updateById(blShipinquan);
            return Result.success();
        }else{
            return Result.error("点赞失败");
        }
    }



}
