package com.inspect.metadata.bdconfig.service.impl;

import com.inspect.base.core.utils.StringUtils;
import com.inspect.metadata.alg.domain.BasedataAlgSubtype;
import com.inspect.metadata.alg.mapper.BasedataAlgSubtypeMapper;
import com.inspect.metadata.bdconfig.domain.BasedataBdConfigAlgConvert;
import com.inspect.metadata.bdconfig.mapper.BasedataBdConfigAlgConvertMapper;
import com.inspect.metadata.bdconfig.service.IBasedataBdConfigAlgConvertService;
import com.inspect.base.core.utils.DateUtils;

import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class BasedataBdConfigAlgConvertServiceImpl implements IBasedataBdConfigAlgConvertService {
    private final BasedataBdConfigAlgConvertMapper basedataBdconfigAlgConvertMapper;
    private final BasedataAlgSubtypeMapper basedataAlgSubtypeMapper;

    public BasedataBdConfigAlgConvertServiceImpl(BasedataBdConfigAlgConvertMapper basedataBdconfigAlgConvertMapper, BasedataAlgSubtypeMapper basedataAlgSubtypeMapper) {
        this.basedataBdconfigAlgConvertMapper = basedataBdconfigAlgConvertMapper;
        this.basedataAlgSubtypeMapper = basedataAlgSubtypeMapper;
    }

    public BasedataBdConfigAlgConvert selectBasedataBdConfigAlgConvertByAlgConvertId(Long algConvertId) {
        return this.basedataBdconfigAlgConvertMapper.selectBasedataBdConfigAlgConvertByAlgConvertId(algConvertId);
    }

    public List<BasedataBdConfigAlgConvert> selectBasedataBdConfigAlgConvertList(BasedataBdConfigAlgConvert basedataBdconfigAlgConvert) {
        return this.basedataBdconfigAlgConvertMapper.selectBasedataBdConfigAlgConvertList(basedataBdconfigAlgConvert);
    }

    public int insertBasedataBdConfigAlgConvert(BasedataBdConfigAlgConvert basedataBdconfigAlgConvert) {
        basedataBdconfigAlgConvert.setCreateTime(DateUtils.getNowDate());
        String algIdS = basedataBdconfigAlgConvert.getAlgId();
        StringBuilder argCodeBuilder = new StringBuilder();
        StringBuilder argNameBuilder = new StringBuilder();
        if (null != algIdS && !algIdS.isEmpty()) {
            String[] algArray = algIdS.split(StringUtils.COMMA);

            for (int i = 0; i < algArray.length; ++i) {
                String algId = algArray[i];
                BasedataAlgSubtype algSubtype = this.basedataAlgSubtypeMapper.selectBasedataAlgSubtypeByAlgSubtypeId(Long.valueOf(algId));
                if (i == algArray.length - 1) {
                    argCodeBuilder.append(algSubtype.getAlgSubtypeCode());
                    argNameBuilder.append(algSubtype.getAlgSubtypeName());
                } else {
                    argCodeBuilder.append(algSubtype.getAlgSubtypeCode() + StringUtils.COMMA);
                    argNameBuilder.append(algSubtype.getAlgSubtypeName() + StringUtils.COMMA);
                }
            }
        }

        basedataBdconfigAlgConvert.setAlgCode(argCodeBuilder.toString());
        basedataBdconfigAlgConvert.setAlgName(argNameBuilder.toString());
        return this.basedataBdconfigAlgConvertMapper.insertBasedataBdConfigAlgConvert(basedataBdconfigAlgConvert);
    }

    public int updateBasedataBdConfigAlgConvert(BasedataBdConfigAlgConvert basedataBdconfigAlgConvert) {
        basedataBdconfigAlgConvert.setUpdateTime(DateUtils.getNowDate());
        String algIdS = basedataBdconfigAlgConvert.getAlgId();
        StringBuilder argCodeBuilder = new StringBuilder();
        StringBuilder argNameBuilder = new StringBuilder();
        if (null != algIdS && !algIdS.isEmpty()) {
            String[] algArray = algIdS.split(StringUtils.COMMA);
            for (int i = 0; i < algArray.length; ++i) {
                String algId = algArray[i];
                BasedataAlgSubtype algSubtype = this.basedataAlgSubtypeMapper.selectBasedataAlgSubtypeByAlgSubtypeId(Long.valueOf(algId));
                if (i == algArray.length - 1) {
                    argCodeBuilder.append(algSubtype.getAlgSubtypeCode());
                    argNameBuilder.append(algSubtype.getAlgSubtypeName());
                } else {
                    argCodeBuilder.append(algSubtype.getAlgSubtypeCode() + StringUtils.COMMA);
                    argNameBuilder.append(algSubtype.getAlgSubtypeName() + StringUtils.COMMA);
                }
            }
        }

        basedataBdconfigAlgConvert.setAlgCode(argCodeBuilder.toString());
        basedataBdconfigAlgConvert.setAlgName(argNameBuilder.toString());
        return this.basedataBdconfigAlgConvertMapper.updateBasedataBdConfigAlgConvert(basedataBdconfigAlgConvert);
    }

    public int deleteBasedataBdConfigAlgConvertByAlgConvertIds(Long[] algConvertIds) {
        return this.basedataBdconfigAlgConvertMapper.deleteBasedataBdConfigAlgConvertByAlgConvertIds(algConvertIds);
    }

    public int deleteBasedataBdConfigAlgConvertByAlgConvertId(Long algConvertId) {
        return this.basedataBdconfigAlgConvertMapper.deleteBasedataBdConfigAlgConvertByAlgConvertId(algConvertId);
    }
}
