package com.hys.app.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.mybatisplus.base.BaseServiceImpl;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.mapper.erp.MsgCompanyMapper;
import com.hys.app.mapper.system.AdminUserMapper;
import com.hys.app.model.erp.dos.MsgCompanyConfig;
import com.hys.app.model.erp.dto.IdsDto;
import com.hys.app.model.system.dos.AdminUser;
import com.hys.app.service.erp.MsgCompanyManager;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单业务层实现
 *
 * @author 张崧
 * 2024-01-24 15:58:31
 */
@Service
@AllArgsConstructor
public class MsgCompanyManagerImpl extends BaseServiceImpl<MsgCompanyMapper, MsgCompanyConfig> implements MsgCompanyManager {

    private AdminUserMapper adminUserMapper;

    @Override
    public void bandMsg(IdsDto idsDto) {
        AdminUser user = adminUserMapper.selectById(AdminUserContext.getAdminUserId());
        if (!user.getUsername().equals("admin")){
            throw new ServiceException("只有管理员账号才能进行此操作");
        }
        //删除之前绑定的
        remove(new LambdaQueryWrapper<MsgCompanyConfig>().eq(MsgCompanyConfig::getCompanyId,user.getCompanyId()));
        List<MsgCompanyConfig> collect = idsDto.getMsgConfigs().stream().map(item -> new MsgCompanyConfig(user.getCompanyId(), item.getId(),item.getCode())).collect(Collectors.toList());
        saveBatch(collect);
    }
}

