package com.zd.chemical.service.impl;

import com.zd.chemical.domain.ActAuditconfig;
import com.zd.chemical.domain.ActAudituser;
import com.zd.chemical.domain.vo.ActAuditconfigVo;
import com.zd.chemical.domain.vo.ActAudituserVo;
import com.zd.chemical.mapper.ActAuditconfigMapper;
import com.zd.chemical.mapper.ActAudituserMapper;
import com.zd.chemical.service.IActAuditconfigService;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;

/**
 * 工作流审批Service业务层处理
 *
 * @author zd
 * @date 2022-08-16
 */
@Service
public class ActAuditconfigServiceImpl implements IActAuditconfigService
{
    @Autowired
    private ActAuditconfigMapper actAuditconfigMapper;

    @Autowired
    private ActAudituserMapper actAudituserMapper;

    /**
     * 查询工作流审批
     *
     * @param id 工作流审批主键
     * @return 工作流审批
     */
    @Override
    public ActAuditconfig selectActAuditconfigById(Long id)
    {
        return actAuditconfigMapper.selectActAuditconfigById(id);
    }

    /**
     * 查询工作流审批列表
     *
     * @param actAuditconfig 工作流审批
     * @return 工作流审批
     */
    @Override
    public List<ActAuditconfig> selectActAuditconfigList(ActAuditconfig actAuditconfig)
    {
        return actAuditconfigMapper.selectActAuditconfigList(actAuditconfig);
    }

    /**
     * 查询工作流审批列表
     *
     * @param actAuditconfig 工作流审批
     * @return 工作流审批
     */
    @Override
    public List<ActAuditconfigVo> getConfigList(ActAuditconfig actAuditconfig)
    {
        actAuditconfig.setAuditStatus(1);
        List <ActAuditconfig> actAuditconfigList = actAuditconfigMapper.selectActAuditconfigList(actAuditconfig);
        List <ActAuditconfigVo> collect = Optional.ofNullable(actAuditconfigList).orElseGet(Collections::emptyList)
                .stream()
                .map(a -> {
                    ActAuditconfigVo actAuditconfigVo = new ActAuditconfigVo();
                    BeanUtils.copyProperties(a, actAuditconfigVo);
                    ActAudituser actAudituser = new ActAudituser();
                    actAudituser.setConfigId(a.getId());
                    List <ActAudituserVo> actAudituserList = actAudituserMapper.getActUserInfoList(actAudituser);
                    actAuditconfigVo.setActAudituserInfo(actAudituserList);
                    return actAuditconfigVo;
                }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<ActAuditconfig> getListByIds(List<Long> ids){
        if (ids.isEmpty()){
            new ArrayList <>();
        }
        return actAuditconfigMapper.getListByIds(ids);
    }

    /**
     * 新增工作流审批
     *
     * @param actAuditconfigList 工作流审批
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertActAuditconfig(List<ActAuditconfigVo> actAuditconfigList)
    {
        //先删除审批相关配置
//        actAudituserMapper.deleteActAudituserByAll();
        actAuditconfigMapper.deleteActAuditconfigByAll();
        //插入审批相关数据
        Optional.ofNullable(actAuditconfigList).orElseGet(Collections::emptyList)
                .stream()
                .forEach(a->{
                    ActAuditconfig actAuditconfig = new ActAuditconfig();
                    BeanUtils.copyProperties(a,actAuditconfig);
                    //设置其他公共字段
                    SaveUtil.setCommonAttr(actAuditconfig);
                    actAuditconfigMapper.insertActAuditconfig(actAuditconfig);
                    LongAdder longAdder = new LongAdder();

                    Optional.ofNullable(a.getActAudituser()).orElseGet(Collections::emptyList)
                            .stream()
                            .forEach(b->{
                                longAdder.add(1);
                                b.setConfigId(actAuditconfig.getId());
                                b.setAuditOrder(Integer.parseInt(longAdder.sum()+""));
                                //设置其他公共字段
                                SaveUtil.setCommonAttr(b);
                                actAudituserMapper.insertActAudituser(b);
                            });
                });
        return 1;
    }

    /**
     * 修改工作流审批
     *
     * @param actAuditconfig 工作流审批
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateActAuditconfig(ActAuditconfig actAuditconfig)
    {
        actAuditconfig.setUpdateTime(DateUtils.getNowDate());
        actAuditconfig.setUpdateBy(SecurityUtils.getUsername());
        return actAuditconfigMapper.updateActAuditconfig(actAuditconfig);
    }

    /**
     * 批量删除工作流审批
     *
     * @param ids 需要删除的工作流审批主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteActAuditconfigByIds(Long[] ids)
    {
        return actAuditconfigMapper.deleteActAuditconfigByIds(ids);
    }

    /**
     * 删除工作流审批信息
     *
     * @param id 工作流审批主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteActAuditconfigById(Long id)
    {
        return actAuditconfigMapper.deleteActAuditconfigById(id);
    }
}
