package com.crm.manager.controller;

import com.crm.login.rely.backend.controller.BaseLoginController;
import com.crm.manager.service.CommonService;
import com.crm.manager.service.WebVideoFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoAddEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoDeleteEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoSearchListEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoUpdateEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/web/video")
public class WebVideoController extends BaseLoginController {

    @Autowired
    private WebVideoFeignService webVideoFeignService;

    @Autowired
    private CommonService commonService;

    /**
     * 添加
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated WebVideoAddEntity entity) throws Exception {

        return webVideoFeignService.add(entity);
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated WebVideoDeleteEntity entity) throws Exception {


        return webVideoFeignService.delete(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated WebVideoUpdateEntity entity) throws Exception {


        return webVideoFeignService.update(entity);
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto list(@RequestBody @Validated WebVideoSearchListEntity entity) throws Exception {

        return webVideoFeignService.pageSearchList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto single(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return webVideoFeignService.single(entity);
    }

    @PostMapping("/{method}/upload")
    public BaseResultDto upload(@PathVariable("method") String method, @RequestParam(value = "file", required = false) MultipartFile file) throws Exception {

        if (!"add".equals(method) && !"update".equals(method)) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }

        String middlePath = "/web/video";
        commonService.validated(file);
        String path = commonService.uploadFile(middlePath, file);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }
}
