package cn.com.qmth.stmms.ms.admin.api;

import cn.com.qmth.stmms.ms.admin.assembler.MarkUserAssembler;
import cn.com.qmth.stmms.ms.admin.dto.MarkUserDTO;
import cn.com.qmth.stmms.ms.commons.utils.excel.ExportUtils;
import cn.com.qmth.stmms.ms.core.domain.InspectRange;
import cn.com.qmth.stmms.ms.core.domain.user.MarkUser;
import cn.com.qmth.stmms.ms.core.domain.user.Role;
import cn.com.qmth.stmms.ms.core.repository.InspectRangeRepo;
import cn.com.qmth.stmms.ms.core.repository.InspectTaskRepo;
import cn.com.qmth.stmms.ms.core.repository.MarkUserRepo;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * Created by yuanpan on 2017/10/16.
 */
@RestController
@RequestMapping("api/admin/inspectors")
public class InspectorApi {

    @Autowired
    private MarkUserRepo markUserRepo;

    @Autowired
    private MarkUserAssembler markUserAssembler;

    @Autowired
    private InspectTaskRepo inspectTaskRepo;

    @Autowired
    private InspectRangeRepo inspectRangeRepo;


    @GetMapping
    public List<MarkUser> list(@RequestParam Long workId) {

        return markUserRepo.findByWorkIdAndRole(workId, Role.INSPECTOR);
    }

    @PostMapping
    public void create(@RequestBody MarkUser inspectUser) {

        Long workId = inspectUser.getWorkId();

        InspectRange inspectRange = inspectRangeRepo.findByWorkIdAndActive(workId, true);
        if (inspectRange != null) {
            throw new RuntimeException("评卷工作已启动，不能再新建复评专家了！");
        }

        MarkUser inspector = markUserRepo.findByLoginName(inspectUser.getLoginName());
        if (inspector != null) {
            throw new RuntimeException("该登录名已经存在");
        }
        inspectUser.setLoginName(inspectUser.getLoginName());
        markUserRepo.save(inspectUser);

    }

    @PutMapping(value = "{domain}")
    public void update(@PathVariable MarkUser domain, @RequestBody MarkUser inspectUser) {
        domain.setName(inspectUser.getName());
        markUserRepo.save(domain);
    }

    @DeleteMapping(value = "{domain}")
    public void delete(@PathVariable MarkUser domain) {
        //判断是否有复查任务
        int taskCount = inspectTaskRepo.countByMarkerId(domain.getId());
        if (taskCount > 0) {
            throw new RuntimeException("该复评专家已经有复评任务了，不能删除！");
        }
        markUserRepo.delete(domain.getId());
    }

    @GetMapping("export")
    public void export(@RequestParam Long workId, HttpServletResponse response) {
        List<MarkUserDTO> markUserDTOs = new ArrayList<>();
        Stream<MarkUser> markUserStream = markUserRepo.findByWorkIdAndRole(workId, Role.INSPECTOR).stream();

        markUserStream.forEach(u -> {
            markUserDTOs.add(markUserAssembler.toDTO(u));
        });
        String fileName = "复评专家列表";
        ExportUtils.exportEXCEL(fileName, MarkUserDTO.class, markUserDTOs, response);
    }
}
