package
cn.com.countrygarden.bi.main.controller.test;

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.test.Test;
import cn.com.countrygarden.bi.main.service.test.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * package: cn.com.countrygarden.bi.main.controller
 * describe:
 * creat_user: lsl
 * creat_date: 2018/6/9
 * creat_time: 22:00
 **/
@RestController("TestController")
@RequestMapping("/api/test")

public class TestController extends BaseController {

    @Autowired
    private TestService testService;


    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")

    public Result list( Test test,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request){
        return testService.queryPageList(test,page,size,getAccessToken(request).getUserCode());
    }

    @GetMapping(value = "/one", produces = "application/json;charset=utf-8")
    public Result queryById(@RequestParam(value = "id", required = true)Integer id,
                            HttpServletRequest request){
        return testService.queryById(id);
    }

    @PostMapping(value = "/add")
    public Result add(Test test,
                      HttpServletRequest request){
        return testService.add(test,getAccessToken(request));
    }

    @PutMapping(value = "/edit")
    public Result edit(Test test, HttpServletRequest request){
        return testService.edit(test,getAccessToken(request));
    }

    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)Integer id){
        return testService.del(id);
    }

    @GetMapping(value = "/test", produces = "application/json;charset=utf-8")
    public Result test(@RequestParam(value = "id", required = false)Integer id,
                            HttpServletRequest request){
        return testService.conditionTest(id);
    }
    
    
    @ResponseBody
	@RequestMapping(value = "/fileUpload", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	public Result upload(@RequestParam(value = "name", defaultValue = "1", required = true) String name, HttpServletRequest request,
			@RequestParam(value = "uploadfiles", defaultValue = "", required = true) MultipartFile multipartFile)
			throws ServiceException {

		if (multipartFile == null) {
			throw new ServiceException("请选择需要上传的文件");
		} else if (name == null) {
			throw new ServiceException("请输入文件名");
		}
		CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
		if (commonsMultipartFile.isEmpty()) {
			return Response.responseFail("上传文件为空文件");
		}
		String fileName = commonsMultipartFile.getOriginalFilename();
		

		return Response.response(testService.TestFileUpload( multipartFile,request));

	}

}

