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

import cn.com.qmth.stmms.ms.admin.service.InspectRangeService;
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.*;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by yuanpan on 2017/10/19.
 */
@RestController
@RequestMapping("api/admin/inspect-range")
public class InspectRangeApi {

    @Autowired
    private InspectRangeRepo inspectRangeRepo;

    @Autowired
    private InspectRangeService inspectRangeService;

    @Autowired
    private MarkUserRepo markUserRepo;


    @Autowired
    private PaperRepo paperRepo;

    @Autowired
    private MarkTaskRepo markTaskRepo;

    @Autowired
    private InspectTaskRepo inspectTaskRepo;

    @GetMapping
    public List<InspectRange> list(@RequestParam Long workId) {
        return inspectRangeRepo.findByWorkId(workId);
    }

    @PostMapping
    public void create(@RequestBody InspectRange inspectRange) {

        InspectRange.InspectRangeType type = inspectRange.getType();
        Integer condition = inspectRange.getCondition();
        Long workId = inspectRange.getWorkId();

        if (type == null || condition == null || workId == null) {
            throw new RuntimeException("接口参数缺失");
        }

        //判断该评卷工作是否已经有抽查工作了
//        int inspectRangeCount = inspectRangeRepo.countByWorkId(workId);
//        if (inspectRangeCount > 0) {
//            throw new RuntimeException("请勿重复创建抽查范围");
//        }

        //判断是否已经全部打分完成了
        long unfinishedPaperCount = paperRepo.countByWorkIdAndScoreIsNull(workId);

        if (unfinishedPaperCount > 0) {
            throw new RuntimeException("打分还没有完成，不能进行抽查工作。");
        }

        inspectRangeService.save(inspectRange);

    }

    @DeleteMapping(value = "{domain}")
    public void delete(@PathVariable(value = "domain") InspectRange domain) {

        int inspectTaskCount = inspectTaskRepo.countByWorkIdAndInspectRangeId(domain.getWorkId(), domain.getId());
        if (inspectTaskCount > 0) {
            throw new RuntimeException("抽查已经启动，不可以删除！");
        }

        inspectRangeRepo.delete(domain);
    }

    @PutMapping(value = "{domain}")
    public void update(@PathVariable InspectRange domain, @RequestBody InspectRange inspectRange) {

        //如果抽查已经启动，则不能再修改了
        if (domain.isActive()) {
            throw new RuntimeException("抽查已经启动了,无法修改抽查范围。");
        }

        domain.setCondition(inspectRange.getCondition());
        domain.setActive(false);
        domain.setType(inspectRange.getType());
        inspectRangeService.save(domain);
    }


    @PatchMapping(value = "{domain}/start")
    public void start(@PathVariable InspectRange domain) {

        //已经启动了就不用再启动了
        if (domain.isActive()) {
            throw new RuntimeException("抽查已经启动了");
        }

        //判断是否可以启动
        for (Subject subject : Subject.values()) {
            //判断是否每一科目分配了复查专家账号
            List<MarkUser> inspectors4Subject = markUserRepo.findByWorkIdAndSubjectAndRole(domain.getWorkId(), subject, Role.INSPECTOR);
            if (inspectors4Subject == null || inspectors4Subject.size() < 1) {
                throw new RuntimeException("至少添加一个" + subject.getName() + "复评专家");
            }
        }

        inspectRangeService.start(domain.getId());
    }
}
