package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.manage.modules.actor.entity.*;
import cn.com.ctop.manage.modules.actor.service.IYardCommentService;
import cn.com.ctop.manage.modules.actor.service.IYardPhotoService;
import cn.com.ctop.manage.modules.actor.service.IYardService;
import cn.com.ctop.manage.modules.actor.service.IYardVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yumeng
 * 2019年11月12日10:04:00
 */
@Slf4j
@RestController
@RequestMapping("/ctop/yard")
public class YardController {
    @Autowired
    private IYardPhotoService yardPhotoService;
    @Autowired
    private IYardService yardService;
    @Autowired
    private IYardVideoService yardVideoService;
    @Autowired
    private IYardCommentService yardCommentService;

    @GetMapping("/detail")
    public Result<Yard> getDetail(@RequestParam(name = "id") Long id) {
        Result<Yard> result = new Result<>();
        Yard yard = yardService.getById(id);
        result.setResult(yard);
        result.setSuccess(true);
        return result;
    }

    @PostMapping(value = "/add")
    public Result<Yard> add(@RequestBody Yard yard) {
        Result<Yard> result = new Result<>();
        try {
            String coverUrl = yard.getCoverUrl();
            if (!Check.isNull(coverUrl) && !coverUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                yard.setCoverUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + coverUrl);
            }
            yardService.save(yard);
            if (null != yard.getImageUrl() && !yard.getImageUrl().isEmpty()) {
                for (String url : yard.getImageUrl()) {
                    YardPhoto photo = new YardPhoto();
                    photo.setYardId(yard.getId());
                    if (!Check.isNull(url) && !url.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                        photo.setPhotoUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + url);
                    }
                    yardPhotoService.save(photo);
                }
            }
            if (yard.getVideoUrl() != null && !yard.getVideoUrl().isEmpty()) {
                for (String url : yard.getVideoUrl()) {
                    YardVideo video = new YardVideo();
                    video.setYardId(yard.getId());
                    if (!Check.isNull(url) && !url.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                        video.setVideoUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + url);
                    }
                    yardVideoService.save(video);
                }
            }
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @PutMapping(value = "/edit")
    public Result<Yard> edit(@RequestBody Yard yard) {
        Result<Yard> result = new Result<>();

        Yard yardEntity = yardService.getById(yard.getId());
        if (yardEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = yardService.updateById(yard);
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    @GetMapping(value = "/list")
    public Result<IPage<Yard>> queryPageList(Yard yard,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpServletRequest req) {
        Result<IPage<Yard>> result = new Result<>();
        QueryWrapper<Yard> queryWrapper = QueryGenerator.initQueryWrapper(yard, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<Yard> page = new Page<Yard>(pageNo, pageSize);
        IPage<Yard> pageList = yardService.page(page, queryWrapper);

        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @GetMapping(value = "/photo/list")
    public Result<IPage<YardPhoto>> queryPhotoPageList(YardPhoto yardPhoto,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        Result<IPage<YardPhoto>> result = new Result<>();

        QueryWrapper<YardPhoto> queryWrapper = QueryGenerator.initQueryWrapper(yardPhoto, req.getParameterMap());
        Page<YardPhoto> page = new Page<>(pageNo, pageSize);
        IPage<YardPhoto> pageList = yardPhotoService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @GetMapping(value = "/comment/list")
    public Result<IPage<YardComment>> queryCommentPageList(YardComment yardComment,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        Result<IPage<YardComment>> result = new Result<>();

        QueryWrapper<YardComment> queryWrapper = QueryGenerator.initQueryWrapper(yardComment, req.getParameterMap());
        Page<YardComment> page = new Page<>(pageNo, pageSize);
        IPage<YardComment> pageList = yardCommentService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @DeleteMapping(value = "/comment/delete")
    public Result<YardComment> deleteComment(@RequestParam(name = "id", required = true) String id) {
        Result<YardComment> result = new Result<>();
        YardComment yardComment = yardCommentService.getById(id);
        if (yardComment == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = yardCommentService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    @DeleteMapping(value = "/video/delete")
    public Result<YardVideo> deleteVideo(@RequestParam(name = "id", required = true) String id) {
        Result<YardVideo> result = new Result<>();
        YardVideo yardVideo = yardVideoService.getById(id);
        if (yardVideo == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = yardVideoService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    @DeleteMapping(value = "/photo/delete")
    public Result<YardPhoto> deletePhoto(@RequestParam(name = "id", required = true) String id) {
        Result<YardPhoto> result = new Result<>();
        YardPhoto yardPhoto = yardPhotoService.getById(id);
        if (yardPhoto == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = yardPhotoService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    @PostMapping(value = "/comment/add")
    public Result<YardComment> addComment(YardComment yardComment) {
        Result<YardComment> result = new Result<>();
        try {
            yardCommentService.save(yardComment);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @PostMapping(value = "/video/add")
    public Result<YardVideo> addVideo(YardVideo yardVideo) {
        Result<YardVideo> result = new Result<>();
        try {
            yardVideoService.save(yardVideo);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @PostMapping(value = "/photo/add")
    public Result<YardPhoto> addPhoto(YardPhoto yardPhoto) {
        Result<YardPhoto> result = new Result<>();
        try {
            yardPhotoService.save(yardPhoto);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @GetMapping(value = "/video/list")
    public Result<IPage<YardVideo>> queryVideoPageList(YardVideo yardVideo,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        Result<IPage<YardVideo>> result = new Result<>();

        QueryWrapper<YardVideo> queryWrapper = QueryGenerator.initQueryWrapper(yardVideo, req.getParameterMap());
        Page<YardVideo> page = new Page<>(pageNo, pageSize);
        IPage<YardVideo> pageList = yardVideoService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            yardService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败",e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }


}
