package com.bonus.material.countersign.service.impl;

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.countersign.domain.SignConfigVo;
import com.bonus.material.countersign.domain.SignProcessVo;
import com.bonus.material.countersign.mapper.SignConfigMapper;
import com.bonus.material.countersign.mapper.SignProcessMapper;
import com.bonus.material.countersign.service.ISignConfigService;
import com.bonus.material.countersign.service.ISignProcessService;
import org.hibernate.validator.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 会签流程配置管理Service业务层处理
 * 
 * @author hongchao
 * @date 2025-01-16
 */
@Service
public class SignProcessServiceImpl implements ISignProcessService
{
    @Autowired
    private SignProcessMapper signProcessMapper;

    /**
     * 查询会签流程配置管理列表
     * 
     * @param signProcessVo 会签流程配置管理
     * @return 会签流程配置管理
     */
    @Override
    public List<SignProcessVo> getList(SignProcessVo signProcessVo) {
        List<SignProcessVo> list = signProcessMapper.getListNew(signProcessVo);
        if (list.size()>0){
            for (SignProcessVo signProcessVos : list) {
                //将idStr用逗号分割，放入ids数组中
                String[] split = signProcessVos.getIdStr().split(",");
                Long[] ids = new Long[split.length];
                for (int i = 0; i < split.length; i++) {
                    ids[i] = Long.parseLong(split[i].trim());
                }
                signProcessVos.setIds(ids);
                //将orgIdStr用逗号分割，放入orgIds数组中
                String[] splitTwo = signProcessVos.getOrgIdStr().split(",");
                Long[] orgIds = new Long[splitTwo.length];
                for (int i = 0; i < splitTwo.length; i++) {
                    orgIds[i] = Long.parseLong(splitTwo[i].trim());
                }
                signProcessVos.setOrgIds(orgIds);
            }
        }
        return list;
    }

    /**
     * 新增会签流程配置管理
     *
     * @param signProcessVo 会签流程配置管理
     * @return 结果
     */
    @Override
    public AjaxResult insertProcessInfo(SignProcessVo signProcessVo) {
        try {
            if (signProcessVo != null && signProcessVo.getOrgIds() != null) {
                for (Long orgId : signProcessVo.getOrgIds()) {
                    signProcessVo.setOrgId(orgId);
                    int count = signProcessMapper.selectProcessRepeat(signProcessVo);
                    if (count > 0) {
                        return AjaxResult.error("该会签流程和类型下已有该会签组织，请重新选择新增");
                    }
                    int countTwo = signProcessMapper.insertProcessInfo(signProcessVo);
                    if (countTwo <= 0) {
                        return AjaxResult.error("会签流程配置新增失败");
                    }
                }
                return AjaxResult.success();
            } else {
                return AjaxResult.error("请选择会签组织");
            }
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 查询会签流程配置管理详情
     *
     * @param ids 主键
     * @return 会签流程配置管理
     */
    @Override
    public SignProcessVo selectProcessInfoById(Long[] ids)
    {
        SignProcessVo signProcessVo = signProcessMapper.selectProcessInfoById(ids);
        if (signProcessVo != null && !StringHelper.isNullOrEmptyString(signProcessVo.getOrgIdStr())){
            //将orgIdStr用逗号分割，放入orgIds数组中
            String[] split = signProcessVo.getOrgIdStr().split(",");
            Long[] orgIds = new Long[split.length];
            for (int i = 0; i < split.length; i++) {
                orgIds[i] = Long.parseLong(split[i].trim());
            }
            signProcessVo.setOrgIds(orgIds);
            //将idStr用逗号分割，放入ids数组中
            String[] splitTwo = signProcessVo.getIdStr().split(",");
            Long[] id = new Long[splitTwo.length];
            for (int i = 0; i < splitTwo.length; i++) {
                id[i] = Long.parseLong(splitTwo[i].trim());
            }
            signProcessVo.setIds(id);
        }
        return signProcessVo;
    }


    /**
     * 修改会签流程配置管理
     *
     * @param signProcessVo 会签流程配置管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateProcessInfo(SignProcessVo signProcessVo) {
        try {
            //先将数据删除
            for (Long id : signProcessVo.getIds()) {
                int count = signProcessMapper.delProcess(id);
                if (count <= 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("会签流程配置修改失败");
                }
            }
            for (Long orgId : signProcessVo.getOrgIds()) {
                signProcessVo.setOrgId(orgId);
                int count = signProcessMapper.selectProcessRepeat(signProcessVo);
                if (count > 0) {
                    return AjaxResult.error("该会签流程和类型下已有该会签组织，请重新选择修改");
                }
                int countTwo = signProcessMapper.insertProcessInfo(signProcessVo);
                if (countTwo <= 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("会签流程配置修改失败");
                }
            }
            return AjaxResult.success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }

    /**
     * 删除会签流程配置管理信息
     *
     * @param ids 主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delProcess(Long[] ids) {
        try {
            if (ids != null || ids.length > 0) {
                for (Long id : ids) {
                    int count = signProcessMapper.delProcess(id);
                    if (count <= 0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("会签流程配置删除失败");
                    }
                }
            }
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }
}
