package org.springblade.flow.engine.service.impl;

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

import org.apache.commons.lang3.StringUtils;
import org.springblade.flow.base.Pagination;
import org.springblade.flow.engine.dto.AvailableFlowDelegateDTO;
import org.springblade.flow.engine.entity.FlowDelegateEntity;
import org.springblade.flow.engine.mapper.FlowDelegateMapper;
import org.springblade.flow.engine.service.FlowDelegateService;
import org.springblade.flow.util.DateUtil;
import org.springblade.flow.util.RandomUtil;
import org.springblade.flow.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 流程委托
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Service
public class FlowDelegateServiceImpl extends ServiceImpl<FlowDelegateMapper, FlowDelegateEntity> implements FlowDelegateService {

    @Autowired
    private UserProvider userProvider;

    @Override
    public List<String> batchGetAvailableProcessData(List<AvailableFlowDelegateDTO> availableFlowDelegateDTOList) {
        for (AvailableFlowDelegateDTO availableFlowDelegateDTO : availableFlowDelegateDTOList) {
            String flowId = availableFlowDelegateDTO.getFlowId();
            String currentUserId = availableFlowDelegateDTO.getCurrentUserId();
            Date now = new Date();
            QueryWrapper<FlowDelegateEntity> flowDelegateEntityQueryWrapper = new QueryWrapper<>();
            LambdaQueryWrapper<FlowDelegateEntity> lambda = flowDelegateEntityQueryWrapper.lambda();
        }
        return null;
    }

    @Override
    public String getAvailableProcessData(AvailableFlowDelegateDTO availableFlowDelegateDTO) {
        String flowId = availableFlowDelegateDTO.getFlowId();
        String currentUserId = availableFlowDelegateDTO.getCurrentUserId();
        Date now = new Date();
        QueryWrapper<FlowDelegateEntity> flowDelegateEntityQueryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<FlowDelegateEntity> lambda = flowDelegateEntityQueryWrapper.lambda();
        lambda.eq(FlowDelegateEntity::getFlowId,flowId);
        lambda.eq(FlowDelegateEntity::getCreatorUserId,currentUserId);
        lambda.le(FlowDelegateEntity::getStartTime,now);
        lambda.ge(FlowDelegateEntity::getEndTime,now);
        lambda.orderByDesc(FlowDelegateEntity::getCreatorTime);
        List<FlowDelegateEntity> flowDelegateEntityList = this.list(flowDelegateEntityQueryWrapper);
        if(flowDelegateEntityList==null ||flowDelegateEntityList.isEmpty()){
            return "";
        }
        else{
            return flowDelegateEntityList.get(0).getFTouserid();
        }
    }
    @Override
    public List<FlowDelegateEntity> getList(Pagination pagination) {
        String userId = userProvider.getUserId();
        QueryWrapper<FlowDelegateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FlowDelegateEntity::getCreatorUserId, userId);
        if (!StringUtils.isEmpty(pagination.getKeyword())) {
            queryWrapper.lambda().and(
                    t -> t.like(FlowDelegateEntity::getFlowName, pagination.getKeyword())
                            .or().like(FlowDelegateEntity::getToUserName, pagination.getKeyword())
            );
        }
        //排序
        queryWrapper.lambda().orderByDesc(FlowDelegateEntity::getFSortCode);
        Page page = new Page(pagination.getCurrentPage(), pagination.getPageSize());
        IPage<FlowDelegateEntity> flowDelegateEntityPage = this.page(page, queryWrapper);
        return pagination.setData(flowDelegateEntityPage.getRecords(), page.getTotal());
    }


    @Override
    public List<FlowDelegateEntity> getList() {
        String userId = userProvider.getUserId();
        QueryWrapper<FlowDelegateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FlowDelegateEntity::getCreatorUserId, userId);
        return this.baseMapper.selectList(queryWrapper);
    }

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

    @Override
    public void delete(FlowDelegateEntity entity) {
        this.removeById(entity.getId());
    }

    @Override
    public void create(FlowDelegateEntity entity) {
        entity.setId(RandomUtil.uuId());
        entity.setFSortCode(RandomUtil.parses());
        entity.setCreatorUserId(userProvider.getUserId());
        this.save(entity);
    }

    @Override
    public List<FlowDelegateEntity> getUser(String userId) {
        Date thisTime = DateUtil.getNowDate();
        QueryWrapper<FlowDelegateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FlowDelegateEntity::getFTouserid, userId).le(FlowDelegateEntity::getStartTime, thisTime).ge(FlowDelegateEntity::getEndTime, thisTime);
        return this.list(queryWrapper);
    }

    @Override
    public boolean update(String id, FlowDelegateEntity entity) {
        entity.setId(id);
        entity.setLastModifyTime(new Date());
        entity.setLastModifyUserId(userProvider.getUserId());
        return this.updateById(entity);
    }
}
