package cn.iocoder.yudao.module.custom.service.impl;

import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.custom.entity.CustomerItem;
import cn.iocoder.yudao.module.custom.mapper.CustomerItemMapper;
import cn.iocoder.yudao.module.custom.service.CustomerItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 客户管理 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class CustomerItemServiceImpl extends ServiceImpl<CustomerItemMapper, CustomerItem> implements CustomerItemService {


    /**
     * 保存客户扩展信息
     *
     * @param cusId    客户id
     * @param itemList 扩展表集合
     * @return void
     * @author PQZ
     * @date 10:01 2024/8/2
     **/
    @Override
    public void saveCutomItem(String cusId, List<CustomerItem> itemList) {
        /*1、根据客户id删除已有扩展信息*/
        LambdaQueryWrapper<CustomerItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerItem::getCusId, cusId).eq(BaseDO::getDeleted, 0);
        this.remove(lambdaQueryWrapper);

        /*2、保存扩展信息集合*/
        itemList.forEach(item -> {
            item.setId(null);
            item.setCusId(cusId);
        });
        this.saveBatch(itemList);
    }
}