package com.gic.haoban.manage.service.service.impl;

import com.gic.api.base.commons.Page;
import com.gic.commons.util.ToolUtil;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.SyncTaskDTO;
import com.gic.haoban.manage.api.enums.SyncTaskStatusEnum;
import com.gic.haoban.manage.service.dao.mapper.SyncTaskMapper;
import com.gic.haoban.manage.service.entity.TabHaobanSyncTask;
import com.gic.haoban.manage.service.service.SyncTaskService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Created 2020/4/10.
 *
 * @author hua
 */
@Service
public class SyncTaskServiceImpl implements SyncTaskService {

    @Autowired
    private SyncTaskMapper syncTaskMapper;


    @Override
    public void createTask(TabHaobanSyncTask tabHaobanSyncTask) {
        tabHaobanSyncTask.setTaskId(ToolUtil.randomUUID());
        tabHaobanSyncTask.setStatusFlag(SyncTaskStatusEnum.init.getVal());
        syncTaskMapper.insert(tabHaobanSyncTask);
    }

    @Override
    public boolean updateTaskStatus(String taskId, int status) {
        TabHaobanSyncTask tabHaobanSyncTask = new TabHaobanSyncTask();
        tabHaobanSyncTask.setTaskId(taskId);
        tabHaobanSyncTask.setStatusFlag(status);
        tabHaobanSyncTask.setUpdateTime(new Date());
        int i = syncTaskMapper.updateByPrimaryKeySelective(tabHaobanSyncTask);
        return i >= 0;
    }

    @Override
    public TabHaobanSyncTask getSyncTask(String taskId) {
        return syncTaskMapper.selectByPrimaryKey(taskId);
    }
    
    @Override
    public TabHaobanSyncTask getSyncNewestTask(String wxEnterpriseId, int taskType) {
        return syncTaskMapper.getSyncNewestTask(wxEnterpriseId,taskType);
    }

    @Override
    public Page<SyncTaskDTO> listTaskPage(String wxEnterpriseId, BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo);
        List<TabHaobanSyncTask> list = syncTaskMapper.listPageByWxEid(wxEnterpriseId);
        Page<SyncTaskDTO> page = PageUtil.changePageHelperToCurrentPage(new PageInfo<>(list), SyncTaskDTO.class);
        return page;
    }

	@Override
	public List<TabHaobanSyncTask> listTaskByType(int taskType) {
		List<TabHaobanSyncTask> list = syncTaskMapper.listTaskByType(taskType);
		return list;
	}
}
