package com.yunzhupaas.integrate.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.yunzhupaas.integrate.entity.IntegrateTaskEntity;
import com.yunzhupaas.integrate.mapper.IntegrateTaskMapper;
import com.yunzhupaas.integrate.model.integrate.IntegratePageModel;
import com.yunzhupaas.integrate.service.IntegrateNodeService;
import com.yunzhupaas.integrate.service.IntegrateTaskService;
import com.yunzhupaas.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IntegrateTaskServiceImpl extends SuperServiceImpl<IntegrateTaskMapper, IntegrateTaskEntity> implements IntegrateTaskService {

    
    @Autowired
    private IntegrateNodeService integrateNodeService;

    @Override
    public List<IntegrateTaskEntity> getList(IntegratePageModel pagination) {
        QueryWrapper<IntegrateTaskEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(IntegrateTaskEntity::getIntegrateId,pagination.getIntegrateId());
        //日期范围（近7天、近1月、近3月、自定义）
        String startTime = pagination.getStartTime() != null ? pagination.getStartTime() : null;
        String endTime = pagination.getEndTime() != null ? pagination.getEndTime() : null;
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            Date startTimes = new Date(Long.parseLong(startTime));
            Date endTimes = new Date(Long.parseLong(endTime));
            queryWrapper.lambda().ge(IntegrateTaskEntity::getExecutionTime, startTimes).le(IntegrateTaskEntity::getExecutionTime, endTimes);
        }
        if (ObjectUtil.isNotEmpty(pagination.getResultType())) {
            queryWrapper.lambda().eq(IntegrateTaskEntity::getResultType, pagination.getResultType());
        }
        queryWrapper.lambda().orderByDesc(IntegrateTaskEntity::getExecutionTime);
        Page<IntegrateTaskEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        IPage<IntegrateTaskEntity> userPage = page(page, queryWrapper);
        return pagination.setData(userPage.getRecords(), page.getTotal());
    }

    @Override
    public List<IntegrateTaskEntity> getList(List<String> id) {
        List<IntegrateTaskEntity> list = new ArrayList<>();
        QueryWrapper<IntegrateTaskEntity> queryWrapper = new QueryWrapper<>();
        if (id.size() > 0) {
            queryWrapper.lambda().in(IntegrateTaskEntity::getIntegrateId, id);
            list.addAll(this.list(queryWrapper));
        }
        return list;
    }

    @Override
    public IntegrateTaskEntity getInfo(String id) {
        QueryWrapper<IntegrateTaskEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(IntegrateTaskEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(IntegrateTaskEntity entity) {
        if (StringUtil.isEmpty(entity.getId())) {
            entity.setId(RandomUtil.uuId());
        }
        entity.setCreatorTime(new Date());
        entity.setCreatorUserId(UserProvider.getUser().getUserId());
        entity.setEnabledMark(0);
        this.save(entity);
    }

    @Override
    public Boolean update(String id, IntegrateTaskEntity entity) {
        entity.setId(id);
        entity.setLastModifyTime(new Date());
        entity.setLastModifyUserId(UserProvider.getUser().getUserId());
        return this.updateById(entity);
    }

    @Override
    public void delete(String id) {
        if(StringUtils.isNotEmpty(id)) {
           QueryWrapper<IntegrateTaskEntity> queryWrapper = new QueryWrapper<>();
           queryWrapper.lambda().eq(IntegrateTaskEntity::getId, id);
           this.remove(queryWrapper);
           integrateNodeService.delete(id);
        }
    }

}

