package com.dgtis.dorm.manager.wx.web;

import com.dgtis.dorm.manager.db.domain.Storage;
import com.dgtis.dorm.manager.db.service.AlumniRecordStorageService;
import com.dgtis.dorm.manager.db.service.StorageService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/wx/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;
    @Autowired
    private AlumniRecordStorageService alumniRecordStorageService;

    /**
     *
     * 获取文件list
     *
     * @param key
     * @param oldName
     * @param startTime
     * @param endTime
     * @param page
     * @param limit
     *
     * @return
     */
    @GetMapping("/list")
    public Object list(String key,String oldName,String startTime,String endTime,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                       String sort, String order){

        Map<String,Object> storageList = alumniRecordStorageService.selectStorage(key,oldName,startTime,endTime,page,limit);

        return ResponseUtil.ok(storageList);
    }

    /**
     * 添加文件
     *
     * @param file
     * @return
     */
    @PostMapping("/create")
    public Object create(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
    	
    	String imgBelongs = request.getParameter("imgBelongs");
        Storage storageInfo = storageService.store(file , imgBelongs);

        return ResponseUtil.ok(storageInfo);
    }

    @PostMapping("/uploadPic")
    public Object uploadPic(@RequestParam("file") MultipartFile file) {
        Storage storageInfo = storageService.store(file);

        return ResponseUtil.ok(storageInfo);
    }


    /**
     * 删除文件
     *
     * @param id 文件id
     * @return
     */
    @PostMapping("/delete")
    public Object delete(String id) {
        storageService.deleteById(Integer.parseInt(id));
        return ResponseUtil.ok();
    }


}
