package com.crm.manager.controller;

import com.crm.login.rely.backend.controller.BaseLoginController;
import com.crm.manager.service.CommonService;
import com.crm.manager.service.activity.WebEbookFeignService;
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.core.entity.web.book.WebEbookAddEntity;
import com.crm.rely.backend.core.entity.web.book.WebEbookDeleteEntity;
import com.crm.rely.backend.core.entity.web.book.WebEbookSearchListEntity;
import com.crm.rely.backend.core.entity.web.book.WebEbookUpdateEntity;
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/ebook")
public class WebEbookController extends BaseLoginController {

    @Autowired
    private WebEbookFeignService webEbookFeignService;

    @Autowired
    private CommonService commonService;

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

        return webEbookFeignService.add(entity);
    }

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


        return webEbookFeignService.delete(entity);
    }

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


        return webEbookFeignService.update(entity);
    }

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

        return webEbookFeignService.pageSearchList(entity);
    }

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

        return webEbookFeignService.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/ebook";
        commonService.validated(file);
        String path = commonService.uploadFile(middlePath, file);

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