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

import com.bonus.sgzb.base.domain.WarehouseKeeper;
import com.bonus.sgzb.base.mapper.WarehouseKeeperMapper;
import com.bonus.sgzb.base.service.WarehouseKeeperService;
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 WarehouseKeeperServiceImpl implements WarehouseKeeperService {

    @Autowired
    private WarehouseKeeperMapper warehouseKeeperMapper;

    @Override
    public List<WarehouseKeeper> getWarehouseKeeperInfoAll(WarehouseKeeper bean) {
        return warehouseKeeperMapper.getWarehouseKeeperInfoAll(bean);
    }

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

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

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

        return i;
    }


}
