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

import cn.com.qmth.stmms.ms.admin.dto.MarkLogCollectDTO;
import cn.com.qmth.stmms.ms.admin.dto.MarkLogOperDTO;
import cn.com.qmth.stmms.ms.commons.utils.excel.ExportUtils;
import cn.com.qmth.stmms.ms.core.domain.MarkLog;
import cn.com.qmth.stmms.ms.core.domain.MarkLogOperType;
import cn.com.qmth.stmms.ms.core.repository.MarkLogRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("api/export/log")
public class MarkLogExporter {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    MarkLogRepo markLogRepo;

    /**
     * 导出操作日志
     *
     * @param workId
     * @param subject
     * @param response
     */
    @GetMapping("/oper")
    public void oper(@RequestParam(required = false) Long workId,
                     @RequestParam(required = false) String subject,
                     @RequestParam(required = false) String examNumber,
                     @RequestParam(required = false) String studentName,
                     @RequestParam(required = false) Integer operType,
                     @RequestParam(required = false) String startTime,
                     @RequestParam(required = false) String endTime,
                     HttpServletResponse response) {
        Specification<MarkLog> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (Objects.nonNull(subject)) {
                predicates.add(cb.equal(root.get("subject").as(String.class), subject));
            }
            if (Objects.nonNull(examNumber)) {
                predicates.add(cb.equal(root.get("examNumber").as(String.class), examNumber));
            }
            if (Objects.nonNull(studentName)) {
                predicates.add(cb.like(root.get("studentName").as(String.class), "%" + studentName + "%"));
            }
            if (Objects.nonNull(operType)) {
                predicates.add(cb.equal(root.get("operType").as(Integer.class), operType));
            }
            if (Objects.nonNull(startTime)) {
                //大于或等于传入时间
                predicates.add(cb.greaterThanOrEqualTo(root.get("createTime").as(String.class), startTime));
            }
            if (Objects.nonNull(endTime)) {
                //小于或等于传入时间
                predicates.add(cb.lessThanOrEqualTo(root.get("createTime").as(String.class), endTime));
            }
            if (Objects.nonNull(workId)) {
                predicates.add(cb.equal(root.get("workId").as(Long.class), workId));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        List<MarkLog> result = markLogRepo.findAll(specification);
        List<MarkLogOperDTO> markLogOperDTOS = new ArrayList<>();
        //科目名称
        for (MarkLog markLog : result) {
            MarkLogOperDTO markLogOperDTO = new MarkLogOperDTO();
            markLogOperDTO.setWorkName(markLog.getWorkName());
            markLogOperDTO.setCreateUserName(markLog.getCreateUserName());
            markLogOperDTO.setCreateRole(markLog.getCreateRole());
            markLogOperDTO.setSubject(markLog.getSubject());
            markLogOperDTO.setExamNumber(markLog.getExamNumber());
            markLogOperDTO.setStudentName(markLog.getStudentName());
            markLogOperDTO.setOperType(markLog.getOperType());
            markLogOperDTO.setOperDataBefore(Objects.isNull(markLog.getOperDataBefore()) ? "" : markLog.getOperDataBefore());
            markLogOperDTO.setOperDataAfter(Objects.isNull(markLog.getOperDataAfter()) ? "" : markLog.getOperDataAfter());
            markLogOperDTO.setCreateTime(sdf.format(markLog.getCreateTime()));
            markLogOperDTOS.add(markLogOperDTO);
        }
        ExportUtils.exportEXCEL("操作日志导出", MarkLogOperDTO.class, markLogOperDTOS, response);
    }


    /**
     * 导出采集日志
     *
     * @param workId
     * @param subject
     * @param response
     */
    @GetMapping("collect")
    public void collect(@RequestParam(required = false) Long workId,
                        @RequestParam(required = false) String subject,
                        @RequestParam(required = false) String examNumber,
                        @RequestParam(required = false) String studentName,
                        @RequestParam(required = false) String createUserId,
                        @RequestParam(required = false) String startTime,
                        @RequestParam(required = false) String endTime,
                        HttpServletResponse response) {
        Specification<MarkLog> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (Objects.nonNull(subject)) {
                predicates.add(cb.equal(root.get("subject").as(String.class), subject));
            }
            if (Objects.nonNull(examNumber)) {
                predicates.add(cb.equal(root.get("examNumber").as(String.class), examNumber));
            }
            if (Objects.nonNull(studentName)) {
                predicates.add(cb.like(root.get("studentName").as(String.class), "%" + studentName + "%"));
            }
            if (Objects.nonNull(createUserId)) {
                predicates.add(cb.equal(root.get("createUserId").as(String.class), createUserId));
            }
            if (Objects.nonNull(startTime)) {
                //大于或等于传入时间
                predicates.add(cb.greaterThanOrEqualTo(root.get("createTime").as(String.class), startTime));
            }
            if (Objects.nonNull(endTime)) {
                //小于或等于传入时间
                predicates.add(cb.lessThanOrEqualTo(root.get("createTime").as(String.class), endTime));
            }
            if (Objects.nonNull(workId)) {
                predicates.add(cb.equal(root.get("workId").as(Long.class), workId));
            }
            //采集端
            predicates.add(cb.equal(root.get("operType").as(Integer.class), MarkLogOperType.COLLECT.getId()));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        List<MarkLog> result = markLogRepo.findAll(specification);
        List<MarkLogCollectDTO> markLogCollectDTOS = new ArrayList<>();
        //科目名称
        for (MarkLog markLog : result) {
            MarkLogCollectDTO markLogCollectDTO = new MarkLogCollectDTO();
            markLogCollectDTO.setWorkName(markLog.getWorkName());
            markLogCollectDTO.setSubject(markLog.getSubject());
            markLogCollectDTO.setStudentName(markLog.getStudentName());
            markLogCollectDTO.setExamNumber(markLog.getExamNumber());
            markLogCollectDTO.setCreateUserName(markLog.getCreateUserName());
            markLogCollectDTO.setCreateTime(sdf.format(markLog.getCreateTime()));
            markLogCollectDTO.setRemark(Objects.isNull(markLog.getRemark())?"":markLog.getRemark());
            markLogCollectDTO.setLoginTime(sdf.format(markLog.getLoginTime()));
            markLogCollectDTOS.add(markLogCollectDTO);
        }
        ExportUtils.exportEXCEL("采集日志导出", MarkLogCollectDTO.class, markLogCollectDTOS, response);
    }
}
