package com.zhongshu.card.server.core.dao.attendance.impl;

import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.zhongshu.card.client.openApi.params.PageLeaveOpenApiParam;
import com.zhongshu.card.client.type.LeaveQueryType;
import com.zhongshu.card.client.type.LeaveStatus;
import com.zhongshu.card.server.core.dao.attendance.LeaveApprovalDao;
import com.zhongshu.card.server.core.dao.attendance.extend.LeaveApprovalDaoExtend;
import com.zhongshu.card.server.core.domain.attendance.LeaveApproval;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.regex.Pattern;

public class LeaveApprovalDaoImpl implements LeaveApprovalDaoExtend {

    @Autowired
    DBHelper dbHelper;

    @Override
    public Page<LeaveApproval> page(Pageable pageable, LeaveQueryType queryType, String projectId, String sceneId, String userId, Long startTime, Long endTime, List<String> leaveType, List<LeaveStatus> leaveStatus, String approvalUserName, String leaveUserName, String leaveUserPhone, Long approvalStartTime, Long approvalEndTime) {
        Criteria criteria = new Criteria();

        if (StringUtils.isNotBlank(projectId)){
            criteria.and("projectId").is(projectId);
        }

        if (StringUtils.isNotBlank(sceneId)){
            criteria.and("sceneId").is(sceneId);
        }

        if (leaveType!=null && !leaveType.isEmpty()){
            criteria.and("leaveType").in(leaveType);
        }

        if (startTime != null && endTime == null){
            criteria.and("createTime").gte(startTime);
        }

        if (startTime == null && endTime != null){
            criteria.and("createTime").lte(endTime);
        }

        if (startTime != null && endTime != null){
            criteria.and("createTime").gte(startTime).lte(endTime);
        }

        if (StringUtils.isNotBlank(approvalUserName)){
            Pattern approvalUserNamePattern = Pattern.compile("^.*" + approvalUserName + ".*$");
            criteria.and("approvalUserName").regex(approvalUserNamePattern);
        }

        if (StringUtils.isNotBlank(leaveUserName)){
            Pattern leaveUserNamePattern = Pattern.compile("^.*" + leaveUserName + ".*$");
            criteria.and("leaveUserName").regex(leaveUserNamePattern);
        }

        if (StringUtils.isNotBlank(leaveUserPhone)){
            criteria.and("leaveUserPhone").is(leaveUserPhone);
        }

        if (approvalStartTime != null && approvalEndTime ==null){
            criteria.and("approvalTime").gte(approvalStartTime);
        }

        if (approvalStartTime == null && approvalEndTime != null){
            criteria.and("approvalTime").lte(approvalEndTime);
        }

        if (approvalStartTime != null && approvalEndTime != null){
            criteria.and("approvalTime").gte(approvalStartTime).lte(approvalEndTime);
        }

        if (LeaveQueryType.Wait.equals(queryType)){
            criteria.and("leaveStatus").is(LeaveStatus.Wait);
            criteria.and("approvalUser").is(userId);
        }else if (LeaveQueryType.Approval.equals(queryType)){
            if (leaveStatus!=null && !leaveStatus.isEmpty()){
                criteria.and("leaveStatus").in(leaveStatus);
            }else {
                criteria.and("leaveStatus").ne(LeaveStatus.Wait);
            }
            criteria.and("approvalUser").is(userId);
        }else if (LeaveQueryType.Apply.equals(queryType)){
            if (leaveStatus!=null && !leaveStatus.isEmpty()){
                criteria.and("leaveStatus").in(leaveStatus);
            }
            criteria.and("leaveUser").is(userId);
        }

        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));

        return dbHelper.pages(query, pageable, LeaveApproval.class);
    }

    @Override
    public Page<LeaveApproval> openApiPage(Pageable pageable, PageLeaveOpenApiParam param) {

        Criteria criteria = new Criteria();

        if (StringUtils.isNotBlank(param.getProjectId())){
            criteria.and("projectId").is(param.getProjectId());
        }

        if (StringUtils.isNotBlank(param.getSceneId())){
            criteria.and("sceneId").is(param.getSceneId());
        }

        if (param.getLeaveType()!=null && !param.getLeaveType().isEmpty()){
            criteria.and("leaveType").in(param.getLeaveType());
        }

        if (param.getLeaveStatus() != null && !param.getLeaveStatus().isEmpty()){
            criteria.and("leaveStatus").in(param.getLeaveStatus());
        }

        if (param.getStartTime() != null && param.getEndTime() != null){
            criteria.and("createTime").gte(param.getStartTime()).lte(param.getEndTime());
        }

        if (StringUtils.isNotBlank(param.getApprovalUser())){
            criteria.and("approvalUser").is(param.getApprovalUser());
        }

        if (StringUtils.isNotBlank(param.getLeaveUser())){
            criteria.and("leaveUser").is(param.getLeaveUser());
        }

        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));

        return dbHelper.pages(query, pageable, LeaveApproval.class);
    }
}
