package com.crm.manager.controller;

import com.crm.manager.service.custom.CustomFileFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.entity.custom.file.CustomFileAllEntity;
import com.crm.rely.backend.core.entity.custom.file.CustomFileApproveEntity;
import com.crm.rely.backend.core.entity.custom.file.CustomFileDeleteEntity;
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;

/**
 * @description: 客户文件控制器
 * @author: houn
 * @create: 2020-06-13 17:58
 **/
@RestController
@RequestMapping("/custom/file")
public class CustomFileController {

    @Autowired
    private CustomFileFeignService customFileFeignService;

    @PostMapping("/search/custom/list")
    public BaseResultDto searcherList(@RequestBody CustomFileAllEntity entity) throws Exception {

        return customFileFeignService.searchCustomList(entity);
    }


    @PostMapping("/approve")
    public BaseResultDto approve(@RequestBody @Validated CustomFileApproveEntity entity) throws Exception {
        if (!Constants.PASSED_STATUS.equals(entity.getStatus()) && !Constants.REJECTED_STATUS.equals(entity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        return customFileFeignService.approve(entity);
    }

    /**
     * 反审核
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/de/approve")
    public BaseResultDto deApprove(@RequestBody @Validated CustomFileApproveEntity entity) throws Exception {

        //判断标记是否为反审核 不是则提示没有权限
        if (!Constants.UPLOAD_STATUS.equals(entity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        return customFileFeignService.approve(entity);
    }

    @PostMapping("/upload/{customId}/{type}")
    public BaseResultDto mangerUpload(@RequestParam(value = "file", required = false) MultipartFile file, @PathVariable("customId") Long customId, @PathVariable("type") Integer type) throws Exception {


        return customFileFeignService.mangerUpload(file, customId, type);
    }

    @PostMapping("/delete")
    public BaseResultDto managerDelete(@RequestBody @Validated CustomFileDeleteEntity entity) throws Exception {

        customFileFeignService.managerDelete(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/upload/{customId}/{type}/{id}")
    public BaseResultDto mangerUpload(@RequestParam(value = "file", required = false) MultipartFile file, @PathVariable("customId") Long customId, @PathVariable("type") Integer type, @PathVariable("id") Long id) throws Exception {

        return customFileFeignService.mangerUpload(file, customId, type, id);
    }
}
