package cn.axzo.msg.center.dal;

import cn.axzo.msg.center.dal.mapper.TodoBusinessMapper;
import cn.axzo.msg.center.domain.entity.Todo;
import cn.axzo.msg.center.domain.entity.TodoBusiness;
import cn.axzo.msg.center.service.enums.BizFinalStateEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toSet;

/**
 * @author yanglin
 */
@Slf4j
@Component
public class TodoBusinessDao extends ServiceImpl<TodoBusinessMapper, TodoBusiness> {

    public Optional<TodoBusiness> findByBiz(String templateCode, String bizCode, boolean forUpdate) {
        TodoBusiness business = lambdaQuery()
                .eq(TodoBusiness::getTemplateCode, templateCode)
                .eq(TodoBusiness::getBizCode, bizCode)
                .last(forUpdate, "FOR UPDATE")
                .one();
        return Optional.ofNullable(business);
    }

    public void updateSampleTodCode(Long businessId, String todoCode) {
        lambdaUpdate()
                .eq(TodoBusiness::getId, businessId)
                .set(TodoBusiness::getSampleTodoCode, todoCode)
                .update();
    }

    public TodoBusinesses getBusinesses(Todo todo) {
        return getBusinesses(Collections.singletonList(todo));
    }

    public TodoBusinesses getBusinesses(List<Todo> todos) {
        if (CollectionUtils.isEmpty(todos))
            return new TodoBusinesses(emptyList(), emptyList());
        // 获取对应的待办业务
        Set<Long> businessIds = todos.stream()
                .map(Todo::getTodoBusinessId)
                .collect(toSet());
        return new TodoBusinesses(getByIds(businessIds), todos);
    }

    private List<TodoBusiness> getByIds(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids))
            return emptyList();
        return lambdaQuery()
                .in(TodoBusiness::getId, ids)
                .list();
    }

    public boolean updateBizExtParams(Long businessId, JSONObject bizExtParams) {
        TodoBusiness update = new TodoBusiness();
        update.setId(businessId);
        update.setBizExtParam(bizExtParams);
        return updateById(update);
    }

    public boolean updateBizFinalState(Long businessId, BizFinalStateEnum bizFinalState) {
        return lambdaUpdate()
                .eq(TodoBusiness::getId, businessId)
                .set(TodoBusiness::getBizFinalState, bizFinalState)
                .update();
    }

    public boolean updateBizFinalState(String templateCode, String bizCode, BizFinalStateEnum bizFinalState) {
        return lambdaUpdate()
                .eq(TodoBusiness::getTemplateCode, templateCode)
                .eq(TodoBusiness::getBizCode, bizCode)
                .set(TodoBusiness::getBizFinalState, bizFinalState)
                .update();
    }

}