package cn.com.qmth.examcloud.core.questions.api.controller;


import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.com.qmth.examcloud.api.commons.enums.DataRuleType;
import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.api.commons.security.bean.UserDataRule;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperSearchInfo;
import cn.com.qmth.examcloud.core.questions.service.PaperService;
import cn.com.qmth.examcloud.web.security.DataRule;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("${api_cqb}/paper_storage")
public class PaperStorageController extends ControllerSupport {

    @Autowired
    PaperService paperService;

    @DataRule(type = DataRuleType.COURSE)
    @ApiOperation(value = "试卷仓库分页查询", notes = "试卷仓库分页查询")
    @ResponseBody
    @GetMapping(value = "/findPage/{curPage}/{pageSize}")
    public ResponseEntity<Object> getGenPapers(@ModelAttribute PaperSearchInfo paperSearchInfo,
                                               @PathVariable int curPage, @PathVariable int pageSize) {
        User user = getAccessUser();
        UserDataRule ud = getUserDataRule(DataRuleType.COURSE);
        paperSearchInfo.setOrgId(user.getRootOrgId().toString());
        return new ResponseEntity<>(paperService.getStoragePaperPage(paperSearchInfo, curPage, pageSize,ud), HttpStatus.OK);
    }
    
    @ResponseBody
    @ApiOperation(value = "回收试卷", notes = "回收试卷")
    @PutMapping(value = "/recovery/{paperIds}")
    public ResponseEntity<Object> recoveryPaper(@PathVariable String paperIds) {
    	User user = getAccessUser();
        List<String> paperList = Stream.of(paperIds.split(",")).collect(Collectors.toList());
        paperService.updatePapersStorage(paperList,1,user.getRootOrgId().toString());
        return new ResponseEntity<>(HttpStatus.OK);

    }
    
    @ResponseBody
    @ApiOperation(value = "释放试卷", notes = "释放试卷")
    @PutMapping(value = "/release/{paperIds}")
    public ResponseEntity<Object> releasePaper(@PathVariable String paperIds) {
    	User user = getAccessUser();
        List<String> paperList = Stream.of(paperIds.split(",")).collect(Collectors.toList());
        paperService.updatePapersStorage(paperList,0,user.getRootOrgId().toString());
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
