package com.xhpc.tradebill.controller;


import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.tradebill.domain.XhpcTradebillUploadRecordDomain;
import com.xhpc.tradebill.service.CommonService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/common")
public class CommonController extends BaseController {

    @Resource
    CommonService commonService;

    @Log(title = "上传对账单(OSS方式)", businessType = BusinessType.INSERT)
    @PostMapping("/oss/upload")
    public AjaxResult OssUpload(@RequestBody XhpcTradebillUploadRecordDomain domain) throws Exception{
        return AjaxResult.success(commonService.ossUpload(domain));
    }

    @Log(title = "上传对账单(文件方式)", businessType = BusinessType.INSERT)
    @PostMapping("/file/upload")
    public AjaxResult fileUpload(MultipartFile file, int type) throws Exception {
        if (file ==null || type < 1){
            return AjaxResult.error("文件或者对账类型为空");
        }
        return AjaxResult.success(commonService.fileUpload(file, type));
    }

    @GetMapping("/file/getPage")
    public AjaxResult getFilePage(int type,
                                String name) throws Exception {
        startPage();
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("type", type);
        parmas.put("createBy", SecurityUtils.getUserId());
        parmas.put("name", name);

        return AjaxResult.success(commonService.getFilePage(parmas));
    }
}
