package com.bonus.sgzb.base.service.impl;

import com.bonus.sgzb.base.domain.MaintenanceGang;
import com.bonus.sgzb.base.mapper.MaintenanceGangMapper;
import com.bonus.sgzb.base.service.MaintenanceGangService;
import com.bonus.sgzb.common.core.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lsun
 */
@Service
public class MaintenanceGangServiceImpl implements MaintenanceGangService {

    @Autowired
    private MaintenanceGangMapper MaintenanceGangMapper;

    @Override
    public List<MaintenanceGang> getMaintenanceGangInfoAll(MaintenanceGang bean) {
        return MaintenanceGangMapper.getMaintenanceGangInfoAll(bean);
    }

    @Override
    public List<MaintenanceGang> getMaUserList(String userName) {
        return MaintenanceGangMapper.getMaUserList(userName);
    }

    @Override
    public int add(MaintenanceGang bean) {
        List<MaintenanceGang> list = new ArrayList<>();
        String typeIds = bean.getTypeIds();
        if(StringHelper.isNotEmpty(typeIds)){
            String[] splitTypeIds = typeIds.split("@");
            //先删除，在去添加
            MaintenanceGangMapper.deleteByIdsAll(splitTypeIds);
            for (String typeId : splitTypeIds) {
                MaintenanceGang beans = new MaintenanceGang();
                beans.setTypeIds(typeId);
                beans.setUserId(bean.getUserId());
                beans.setCompanyId(bean.getCompanyId());
                list.add(beans);
            }
        }
        int i =0;
        if(list.size()>0){
            i= MaintenanceGangMapper.add(list);
        }else{

        }
        return i;
    }

    @Override
    public int deleteByIds(MaintenanceGang bean) {
        int i = 1;
        if(StringHelper.isNotEmpty(bean.getType())){
            if("1".equals(bean.getType())){
                MaintenanceGangMapper.deleteByIds(bean.getTypeIds());
            }else{
                if(StringHelper.isNotEmpty(bean.getTypeIds())){
                    List<MaintenanceGang> list = new ArrayList<>();
                    String[] splitTypeIds = bean.getTypeIds().split("@");
                    MaintenanceGangMapper.deleteByIdsAll(splitTypeIds);
                }
            }
        }

        return i;
    }


}
