package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcTradebillUploadRecordDomain;
import com.xhpc.activity.service.CommonService;
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.common.util.LogUserUtils;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


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

    @Resource
    CommonService commonService;

    @Resource
    LogUserUtils logUserUtils;

    @Log(title = "上传对账单(OSS方式)", businessType = BusinessType.INSERT)
    @PostMapping("/oss/upload")
    public AjaxResult OssUpload(HttpServletRequest request, HttpServletResponse response, @RequestBody XhpcTradebillUploadRecordDomain domain) throws Exception{
        LoginUser logUser = logUserUtils.getLogUser(request);
        domain.setTenantId(logUser.getTenantId());
        return AjaxResult.success(commonService.ossUpload(request,response,domain));
    }


    @Log(title = "上传对账单(文件方式)", businessType = BusinessType.INSERT)
    @PostMapping("/file/upload")
    public AjaxResult fileUpload(HttpServletRequest request, HttpServletResponse response,  MultipartFile file, int type, String startTime, String endTime) throws Exception {
        if (file ==null || type < 1){
            return AjaxResult.error("文件或者对账类型为空");
        }
        LoginUser logUser = logUserUtils.getLogUser(request);

        return AjaxResult.success(commonService.fileUpload(request,response,"000000", file, type, startTime, endTime));
    }

    @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));
    }
}
