/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.workflow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codvision.authcore.bean.SysUserBean;
import com.codvision.authcore.util.AuthUtil;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.util.QueryPageUtil;
import com.codvision.workflow.entity.WorkFlowCcItem;
import com.codvision.workflow.mapper.WorkFlowCcItemMapper;
import com.codvision.workflow.service.WorkFlowCcItemService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hxl
 */
@Service
public class WorkFlowCcItemServiceImpl extends ServiceImpl<WorkFlowCcItemMapper, WorkFlowCcItem> implements WorkFlowCcItemService {

    @Override
    public IPage<WorkFlowCcItem> getPageResult(QueryPage<Map<String, Object>> queryPage) {
        SysUserBean currentUser = AuthUtil.getCurrentUser();
        Map<String, Object> paraMap = QueryPageUtil.queryMap(queryPage);
        paraMap.put("toUid", currentUser.getId());
        return getBaseMapper().getPageResult(queryPage.getPage(), paraMap);
    }

    @Override
    public void addCcItem(WorkFlowCcItem workFlowCcItem, List<Integer> toCcUserIds) {
        if (workFlowCcItem == null || CollectionUtils.isEmpty(toCcUserIds)) {
            return;
        }

        if (StringUtils.isBlank(workFlowCcItem.getCcTitle())) {
            String ccTitle = "【"+ workFlowCcItem.getTaskName() +"】 " + workFlowCcItem.getCcName() + "已处置完成";
            workFlowCcItem.setCcTitle(ccTitle);
        }

        List<WorkFlowCcItem> ccItems = new ArrayList<>(toCcUserIds.size());
        for (Integer toCcUserId : toCcUserIds) {
            WorkFlowCcItem ccItem = BeanUtil.copyProperties(workFlowCcItem, WorkFlowCcItem.class);
            ccItem.setToUid(toCcUserId);
            ccItems.add(ccItem);
        }

        getBaseMapper().batchAddCcItems(ccItems);
    }
}
