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


import com.bonus.sgzb.base.domain.BmUnitInfo;
import com.bonus.sgzb.base.domain.BmUnitPerson;
import com.bonus.sgzb.base.mapper.BmUnitInfoMapper;
import com.bonus.sgzb.base.mapper.BmUnitPersonMapper;
import com.bonus.sgzb.base.service.BmUnitInfoService;
import com.bonus.sgzb.common.core.exception.ServiceException;
import com.bonus.sgzb.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BmUnitInfoServiceImpl implements BmUnitInfoService {

    @Autowired
    private BmUnitInfoMapper bmUnitInfoMapper;

    @Autowired
    private BmUnitPersonMapper bmUnitPersonMapper;

    @Override
    public List<BmUnitInfo> getUnitInfoAll() {

        return bmUnitInfoMapper.getUnitInfoAll();
    }

    @Override
    public List<BmUnitInfo> getUnitInfo(BmUnitInfo bmUnitInfo) {
        return bmUnitInfoMapper.getUnitInfo(bmUnitInfo);
    }

    @Override
    public int unitInfoAdd(BmUnitInfo bmUnitInfo) {
        return bmUnitInfoMapper.unitInfoAdd(bmUnitInfo);
    }

    @Override
    public void remove(Long[] unitIds) {
        bmUnitInfoMapper.remove(unitIds);
    }

    @Override
    public int updateBmUnitInfo(BmUnitInfo bmUnitInfo) {
        return bmUnitInfoMapper.updateBmUnitInfo(bmUnitInfo);
    }

    @Override
    public int deleteUnitInfoById(Long unitId) {
        return bmUnitInfoMapper.deleteUnitInfoById();
    }

    @Override
    public String importUser(List<BmUnitInfo> bmUnitInfoList, boolean updateSupport, Long userId) {

        if (StringUtils.isNull(bmUnitInfoList) || bmUnitInfoList.size() == 0)
        {
            throw new ServiceException("导入往来公司数据不能为空！");
        }
        BmUnitPerson bmUnitPerson = new BmUnitPerson();
        bmUnitPerson.setUserId(userId);
        //保存往来公司信息
        for (BmUnitInfo bmUnitInfo: bmUnitInfoList) {
            bmUnitInfoMapper.unitInfoAdd(bmUnitInfo);
            //保存往来公司-人员配置
            bmUnitPerson.setUnitId(bmUnitInfo.getUnitId());
            bmUnitPersonMapper.unitPersonAdd(bmUnitPerson);
        }
        return "导入成功";
    }
}
