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.common.module.utils.QueryGenerator;
import cn.com.ctop.manage.modules.actor.entity.Actor;
import cn.com.ctop.manage.modules.actor.entity.ActorComment;
import cn.com.ctop.manage.modules.actor.entity.ActorPhoto;
import cn.com.ctop.manage.modules.actor.entity.ActorVideo;
import cn.com.ctop.manage.modules.actor.service.IActorCommentService;
import cn.com.ctop.manage.modules.actor.service.IActorPhotoService;
import cn.com.ctop.manage.modules.actor.service.IActorService;
import cn.com.ctop.manage.modules.actor.service.IActorVideoService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jeecg-boot
 */
@RestController
@Slf4j
@RequestMapping("/ctop/actor")
public class ActorController {
    @Autowired
    private IActorService actorService;
    @Autowired
    private IActorCommentService actorCommentService;
    @Autowired
    private IActorPhotoService actorPhotoService;
    @Autowired
    private IActorVideoService actorVideoService;

    @ResponseBody
    @GetMapping("/detail")
    public Result<Actor> getDetail(@RequestParam(name = "actorId") Long actorId) {
        Result<Actor> result = new Result<>();
        Actor actor = actorService.getById(actorId);
        result.setResult(actor);
        result.setSuccess(true);
        return result;
    }

    /**
     * 分页列表查询
     *
     * @param actor
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<IPage<Actor>> queryPageList(Actor actor,
                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              HttpServletRequest req) {
        Result<IPage<Actor>> result = new Result<>();
        QueryWrapper<Actor> queryWrapper = QueryGenerator.initQueryWrapper(actor, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<Actor> page = new Page<>(pageNo, pageSize);
        IPage<Actor> pageList = actorService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @GetMapping(value = "/photo/list")
    public Result<IPage<ActorPhoto>> queryPhotoPageList(ActorPhoto actorPhoto,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        Result<IPage<ActorPhoto>> result = new Result<>();
        QueryWrapper<ActorPhoto> queryWrapper = QueryGenerator.initQueryWrapper(actorPhoto, req.getParameterMap());
        Page<ActorPhoto> page = new Page<>(pageNo, pageSize);
        IPage<ActorPhoto> pageList = actorPhotoService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @GetMapping(value = "/comment/list")
    public Result<IPage<ActorComment>> queryCommentPageList(ActorComment actorComment,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            HttpServletRequest req) {
        Result<IPage<ActorComment>> result = new Result<>();
        QueryWrapper<ActorComment> queryWrapper = QueryGenerator.initQueryWrapper(actorComment, req.getParameterMap());
        Page<ActorComment> page = new Page<>(pageNo, pageSize);
        IPage<ActorComment> pageList = actorCommentService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

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

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

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

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

    @PostMapping(value = "/video/add")
    public Result<ActorVideo> addVideo(ActorVideo actorVideo) {
        Result<ActorVideo> result = new Result<>();
        try {
            String videoUrl = actorVideo.getVideoUrl();
            if (!Check.isNull(videoUrl) && !videoUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                actorVideo.setVideoUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + videoUrl);
            }
            actorVideoService.save(actorVideo);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @PostMapping(value = "/photo/add")
    public Result<ActorPhoto> addPhoto(ActorPhoto actorPhoto) {
        Result<ActorPhoto> result = new Result<>();
        try {
            String photoUrl = actorPhoto.getPhotoUrl();
            if (!Check.isNull(photoUrl) && !photoUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                actorPhoto.setPhotoUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + photoUrl);
            }
            actorPhotoService.save(actorPhoto);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @GetMapping(value = "/video/list")
    public Result<IPage<ActorVideo>> queryVideoPageList(ActorVideo actorVideo,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        Result<IPage<ActorVideo>> result = new Result<>();
        QueryWrapper<ActorVideo> queryWrapper = QueryGenerator.initQueryWrapper(actorVideo, req.getParameterMap());
        Page<ActorVideo> page = new Page<>(pageNo, pageSize);
        IPage<ActorVideo> pageList = actorVideoService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }


    /**
     * 添加
     *
     * @param actor
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Actor> add(@RequestBody Actor actor) {
        Result<Actor> result = new Result<>();
        try {
            String coverUrl = actor.getCoverUrl();
            if (!Check.isNull(coverUrl) && !coverUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                actor.setCoverUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + coverUrl);
            }
            actorService.save(actor);
            if (null != actor.getImageUrl() && !actor.getImageUrl().isEmpty()) {
                for (String url : actor.getImageUrl()) {
                    ActorPhoto photo = new ActorPhoto();
                    photo.setActorId(actor.getId());
                    if (!Check.isNull(url) && !url.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                        photo.setPhotoUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + url);
                    }
                    actorPhotoService.save(photo);
                }
            }
            if (actor.getVideoUrl() != null && !actor.getVideoUrl().isEmpty()) {
                for (String url : actor.getVideoUrl()) {
                    ActorVideo video = new ActorVideo();
                    video.setActorId(actor.getId());
                    if (!Check.isNull(url) && !url.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                        video.setVideoUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + url);
                    }
                    actorVideoService.save(video);
                }
            }
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param actor
     * @return
     */
    @PutMapping(value = "/edit")
    public Result<Actor> edit(@RequestBody Actor actor) {
        Result<Actor> result = new Result<>();

        Actor actorEntity = actorService.getById(actor.getId());
        if (actorEntity == null) {
            result.error500("未找到对应实体");
        } else {
            String coverUrl = actor.getCoverUrl();
            if (!Check.isNull(coverUrl) && !coverUrl.contains(KuaishouInterfaceConstant.HTTPS_PREFIX)) {
                actor.setCoverUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + coverUrl);
            }

            boolean ok = actorService.updateById(actor);
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

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