package com.ruoyi.approvalFlow.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.approvalFlow.domain.AuditPaths;
import com.ruoyi.approvalFlow.domain.AuditPathsActs;
import com.ruoyi.approvalFlow.mapper.AuditPathsActsMapper;
import com.ruoyi.approvalFlow.mapper.AuditPathsMapper;
import com.ruoyi.approvalFlow.service.IAuditPathsActsService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 审批流配置明细Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-21
 */
@Service
public class AuditPathsActsServiceImpl implements IAuditPathsActsService {
    @Autowired
    private AuditPathsMapper auditPathsMapper;

    @Autowired
    private AuditPathsActsMapper auditPathsActsMapper;

    /**
     * 查询审批流配置明细
     *
     * @param id 审批流配置明细ID
     * @return 审批流配置明细
     */
    @Override
    public AuditPathsActs selectAuditPathsActsById(Long id) {
        return auditPathsActsMapper.selectAuditPathsActsById(id);
    }

    /**
     * 查询审批流配置明细列表
     *
     * @param auditPathsActs 审批流配置明细
     * @return 审批流配置明细
     */
    @Override
    public List<AuditPathsActs> selectAuditPathsActsList(AuditPathsActs auditPathsActs) {
        return auditPathsActsMapper.selectAuditPathsActsList(auditPathsActs);
    }

    @Override
    public Map<String, Object> selectAuditPathsList(AuditPathsActs auditPathsActs) {
        Map<String, Object> map = new HashMap<>();
        List<AuditPathsActs> actsList = auditPathsActsMapper.selectAuditPathsActsList(auditPathsActs);
        int count =  auditPathsActsMapper.selectAuditPathsActsCount(auditPathsActs);
        AuditPaths paths = new AuditPaths();
        paths.setStatus("A");
        List<AuditPaths> pathsList = auditPathsMapper.selectAuditPathsList(paths);
        map.put("count", count);
        map.put("auditPath", pathsList);
        map.put("auditPathAct", actsList);
        return map;
    }

    /**
     * 新增审批流配置明细
     *
     * @param auditPathsActs 审批流配置明细
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertAuditPathsActs(String auditPathsActs) {
        JSONArray auditPathsActsJSON = JSONArray.parseArray(auditPathsActs);
        List<AuditPathsActs> auditPathsActsList = JSONObject.parseArray(auditPathsActsJSON.toJSONString(), AuditPathsActs.class);
        int lin = 0;
        for (AuditPathsActs act : auditPathsActsList) {
            lin ++;
            int i = auditPathsActsMapper.updateAuditPathsActs(act);
            if (i <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("操作失败：第" + i + "行数据保存失败，请确认");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改审批流配置明细
     *
     * @param auditPathsActs 审批流配置明细
     * @return 结果
     */
    @Override
    public int updateAuditPathsActs(AuditPathsActs auditPathsActs) {
        return auditPathsActsMapper.updateAuditPathsActs(auditPathsActs);
    }

    /**
     * 批量删除审批流配置明细
     *
     * @param ids 需要删除的审批流配置明细ID
     * @return 结果
     */
    @Override
    public int deleteAuditPathsActsByIds(Long[] ids) {
        return auditPathsActsMapper.deleteAuditPathsActsByIds(ids);
    }

    /**
     * 删除审批流配置明细信息
     *
     * @param id 审批流配置明细ID
     * @return 结果
     */
    @Override
    public int deleteAuditPathsActsById(Long id) {
        return auditPathsActsMapper.deleteAuditPathsActsById(id);
    }

}
