package com.dgtly.order.service.impl;

import java.util.List;

import com.dgtly.common.utils.StringUtils;
import com.dgtly.order.domain.DiySalesorgtree;
import org.springframework.stereotype.Service;
import com.dgtly.order.mapper.DiysalesorgStartMapper;
import com.dgtly.order.domain.DiysalesorgStart;
import com.dgtly.order.service.IDiysalesorgStartService;
import com.dgtly.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 销售组织架构启用管理Service业务层处理
 * 
 * @author nsy
 * @date 2021-01-19
 */
@Service
public class DiysalesorgStartServiceImpl implements IDiysalesorgStartService 
{
    @Resource
    private DiysalesorgStartMapper diysalesorgStartMapper;

    /**
     * 查询销售组织架构启用管理
     * 
     * @param salesorgCode 销售组织架构启用管理ID
     * @return 销售组织架构启用管理
     */
    @Override
    public DiysalesorgStart selectDiysalesorgStartById(String salesorgCode)
    {
        return diysalesorgStartMapper.selectDiysalesorgStartById(salesorgCode);
    }

    /**
     * 查询销售组织架构启用管理列表
     * 
     * @param diysalesorgStart 销售组织架构启用管理
     * @return 销售组织架构启用管理
     */
    @Override
    public List<DiysalesorgStart> selectDiysalesorgStartList(DiysalesorgStart diysalesorgStart)
    {
        return diysalesorgStartMapper.selectDiysalesorgStartList(diysalesorgStart);
    }

    /**
     * 新增销售组织架构启用管理
     * 
     * @param diysalesorgStart 销售组织架构启用管理
     * @return 结果
     */
    @Override
    public int insertDiysalesorgStart(DiysalesorgStart diysalesorgStart)
    {
        return diysalesorgStartMapper.insertDiysalesorgStart(diysalesorgStart);
    }

    /**
     * 修改销售组织架构启用管理
     * 
     * @param diysalesorgStart 销售组织架构启用管理
     * @return 结果
     */
    @Override
    public int updateDiysalesorgStart(DiysalesorgStart diysalesorgStart)
    {
        return diysalesorgStartMapper.updateDiysalesorgStart(diysalesorgStart);
    }

    /**
     * 删除销售组织架构启用管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDiysalesorgStartByIds(String ids)
    {
        return diysalesorgStartMapper.deleteDiysalesorgStartByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除销售组织架构启用管理信息
     * 
     * @param salesorgCode 销售组织架构启用管理ID
     * @return 结果
     */
    @Override
    public int deleteDiysalesorgStartById(String salesorgCode)
    {
        return diysalesorgStartMapper.deleteDiysalesorgStartById(salesorgCode);
    }

    /**
     * 返回销售组织架构列表
     * @param salesorgtree
     * @return
     */
    @Override
    public List<DiySalesorgtree> findSalesOrgList(DiySalesorgtree salesorgtree) {
        try {
            StringUtils.beanAttributeValueTrim(salesorgtree);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return diysalesorgStartMapper.selectSalesOrgList(salesorgtree);
    }

    /**
     * 新增保存销售组织架构启用
     * @param org6codes
     * @param org6names
     * @return
     */
    @Override
    public int saveSalesorgStart(String org6codes,String org6names) {
        int state = 0;
        String[] indexorg6codes = Convert.toStrArray(org6codes);
        String[] indexorg6names = Convert.toStrArray(org6names);
        for (int i = 0; i < indexorg6codes.length; i++){
            String code = indexorg6codes[i];
            String name = indexorg6names[i];
            DiysalesorgStart diysalesorgStart = diysalesorgStartMapper.selectDiysalesorgStartById(code);
            if(diysalesorgStart == null){
                diysalesorgStart = new DiysalesorgStart();
                diysalesorgStart.setSalesorgCode(code);
                diysalesorgStart.setSalseorgName(name);
                state = diysalesorgStartMapper.insertDiysalesorgStart(diysalesorgStart);
            }
            state = 1;
            continue;
        }
        return state;
    }
}
