/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.base.entity.Departmentsource;
import com.tofly.base.mapper.DepartmentsourceMapper;
import com.tofly.base.service.DepartmentsourceService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 单位资源配置关联信息表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@Service("departmentsourceService")
public class DepartmentsourceServiceImpl extends ServiceImpl<DepartmentsourceMapper, Departmentsource> implements DepartmentsourceService {

    @Override
    @Transactional
    public boolean updateDepartmentsource(Long departmentId, String sourceIds) {
        //首先删除部门的资源信息。
        remove(Wrappers.<Departmentsource>lambdaQuery()
                .eq(Departmentsource::getDepartmentId,departmentId));
        //然后再新增资源信息
        if(!StringUtil.isEmpty(sourceIds)){
            List<Departmentsource> list=new ArrayList<>();
            String[] sourceId=sourceIds.split(",");
            Departmentsource departmentsource;
            for(String s:sourceId){
                departmentsource=new Departmentsource();
                departmentsource.setDepartmentId(departmentId);
                departmentsource.setSourceId(Long.valueOf(s));
                //list.add(departmentsource);
                save(departmentsource);
            }
            //saveBatch(list);
        }
        return true;
    }

    @Override
    public ResultRespone selectByDepartMent(Long departmentId) {
        QueryWrapper<Departmentsource> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("DEPARTMENT_ID",departmentId);
        return ResultRespone.success(baseMapper.selectList(wrapperOne));
    }
}
