package com.younike.merchant;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.SecurityUtils;
import com.younike.system.domain.ServerUser;
import com.younike.system.domain.Video;
import com.younike.system.param.VideoParam;
import com.younike.system.domain.vo.ServerUserSearchParam;
import com.younike.system.domain.vo.VideoVo;
import com.younike.system.service.IServerUserService;
import com.younike.system.service.IVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 【商家/运营管理：视频】Controller
 *
 * @author elev
 * @date 2022-04-13
 */
@Api("【请填写功能名称】接口")
@RestController
@RequestMapping("/system/video")
public class MerchantVideoController extends BaseController {
    @Autowired
    private IVideoService videoService;

    @Autowired
    private IServerUserService serverUserService;

    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation("查询【获取视频】列表")
    @GetMapping("/list")
    public Object list(VideoParam video) {
        startPage();
        List<VideoVo> list = videoService.getVideoList(video);
        return getDataTable(list);
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation("获取【请填写功能名称】详细信息")
    @GetMapping(value = "/{videoId}")
    public AjaxResult getInfo(@PathVariable("videoId") Long videoId) {
        return AjaxResult.success(videoService.getInfo(videoId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation("新增【店铺运营添加视频】")
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("/addVideo")
    public AjaxResult add(@RequestBody Video video) {
        //校验数据
        if (ObjectUtils.isEmpty(video) || ObjectUtils.isEmpty(video.getVideoUrl()) ||
                ObjectUtils.isEmpty(video.getVideoFaceUrl()) ||
                video.getVideoCategoryId() == null ||
                ObjectUtils.isEmpty(video.getVideoDesc()) ||
                video.getMerchantId() == null
        ) {
            return AjaxResult.error("参数错误");
        }

        video.setCreateBy(SecurityUtils.getNickName());
       
        video.setUpdateBy(SecurityUtils.getNickName());
      
        return toAjax(videoService.insertVideo(video));
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation("修改【视频】")
    @PutMapping
    public AjaxResult edit(@RequestBody Video video) {
        if(ObjectUtils.isEmpty(video)||video.getVideoId() == null){
            return AjaxResult.error("参数错误");
        }

     
        video.setUpdateBy(SecurityUtils.getNickName());
     
        return toAjax(videoService.updateVideo(video));
    }


    /**
     * 删除视频
     */
    @ApiOperation("删除视频")
    @Log(title = "删除视频", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @DeleteMapping("/{videoId}")
    public AjaxResult remove(@PathVariable Long videoId) {
        Video video = new Video();
        video.setVideoId(videoId);
        video.setDelFlag("2");
        return toAjax(videoService.updateVideo(video));
//        return toAjax(videoService.deleteVideoByVideoId(videoId));
    }

    @ApiOperation("查询服务人员列表")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @PostMapping("/serverUserList")
    public AjaxResult serverUserList(@RequestBody ServerUserSearchParam searchParam) {
        List<ServerUser> list = serverUserService.getServerUserList(searchParam);
        return AjaxResult.success(list);
    }

}

