package com.ruoyi.system.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.dto.TCrmSupplierDTO;
import com.ruoyi.system.mapper.TCrmSupplierMapper;
import com.ruoyi.system.model.TCrmSupplier;
import com.ruoyi.system.model.TCrmSupplierToWarehouse;
import com.ruoyi.system.query.TCrmSupplierQuery;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.TCrmSupplierService;
import com.ruoyi.system.service.TCrmSupplierToWarehouseService;
import com.ruoyi.system.vo.TCrmSupplierVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
*
* crm供应商 服务实现类
*
*
* @author xiaochen
* @since 2025-08-20
*/
@Service
public class TCrmSupplierServiceImpl extends ServiceImpl implements TCrmSupplierService {
@Autowired
private ISysUserService sysUserService;
@Autowired
private TCrmSupplierToWarehouseService crmSupplierToWarehouseService;
@Override
public PageInfo pageList(TCrmSupplierQuery query) {
PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
List list = this.baseMapper.pageList(query,pageInfo);
if(CollectionUtils.isEmpty(list)){
return pageInfo;
}
// List supplierIds = list.stream().map(TCrmSupplierVO::getId).collect(Collectors.toList());
// List crmSupplierToWarehouses = crmSupplierToWarehouseService.list(Wrappers.lambdaQuery(TCrmSupplierToWarehouse.class)
// .eq(TCrmSupplierToWarehouse::getSupplierId, supplierIds));
// for (TCrmSupplierVO crmSupplierVO : list) {
// List supplierToWarehouses = crmSupplierToWarehouses.stream().filter(crmSupplierToWarehouse -> crmSupplierToWarehouse.getSupplierId().equals(crmSupplierVO.getId())).collect(Collectors.toList());
// if(!CollectionUtils.isEmpty(supplierToWarehouses)){
// crmSupplierVO.setWarehouseId(warehouseIds);
// }
// }
pageInfo.setRecords(list);
return pageInfo;
}
@Override
public R addSupplier(TCrmSupplierDTO dto) {
// 判断账号是否已存在
SysUser sysUser = sysUserService.selectUserByUserName(dto.getAccount());
if(Objects.nonNull(sysUser)){
return R.fail(dto.getAccount()+"-账号已存在");
}
this.save(dto);
// 添加供应商和仓库关联关系
// TCrmSupplierToWarehouse crmSupplierToWarehouse = new TCrmSupplierToWarehouse();
// List warehouseIds = dto.getWarehouseIds();
// if(!CollectionUtils.isEmpty(warehouseIds)){
// List crmSupplierToWarehouses = new ArrayList<>();
// for (String warehouseId : warehouseIds) {
// crmSupplierToWarehouse.setSupplierId(dto.getId());
// crmSupplierToWarehouse.setWarehouseId(warehouseId);
// crmSupplierToWarehouses.add(crmSupplierToWarehouse);
// }
// crmSupplierToWarehouseService.saveBatch(crmSupplierToWarehouses);
// }
// 添加账号
SysUser user = new SysUser();
user.setUserName(dto.getAccount());
user.setPhonenumber(dto.getPhone());
user.setNickName(dto.getSupplierName());
user.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
user.setStatus("0");
user.setDelFlag("0");
user.setRoleType(4);
user.setRoleId(4L);
sysUserService.insertUser(user);
dto.setUserId(user.getUserId());
this.updateById(dto);
return R.ok();
}
@Override
public R updateSupplier(TCrmSupplierDTO dto) {
// 判断账号是否已存在
SysUser sysUser1 = sysUserService.selectUserByUserName(dto.getAccount());
if(Objects.nonNull(sysUser1) && !sysUser1.getUserId().equals(dto.getUserId())){
return R.fail(dto.getAccount()+"-账号已存在");
}
SysUser user = sysUserService.selectUserById(dto.getUserId());
this.updateById(dto);
// // 删除供应商和仓库关联关系
// crmSupplierToWarehouseService.remove(Wrappers.lambdaQuery(TCrmSupplierToWarehouse.class).eq(TCrmSupplierToWarehouse::getSupplierId, dto.getId()));
// // 添加供应商和仓库关联关系
// List warehouseIds = dto.getWarehouseIds();
// if(!CollectionUtils.isEmpty(warehouseIds)){
// List crmSupplierToWarehouses = new ArrayList<>();
// for (String warehouseId : warehouseIds) {
// TCrmSupplierToWarehouse crmSupplierToWarehouse = new TCrmSupplierToWarehouse();
// crmSupplierToWarehouse.setSupplierId(dto.getId());
// crmSupplierToWarehouse.setWarehouseId(warehouseId);
// crmSupplierToWarehouses.add(crmSupplierToWarehouse);
// }
// crmSupplierToWarehouseService.saveBatch(crmSupplierToWarehouses);
// }
if(Objects.nonNull(user)){
// 修改账号
user.setPhonenumber(dto.getPhone());
user.setUserName(dto.getAccount());
user.setNickName(dto.getSupplierName());
if(StringUtils.isNotEmpty(dto.getPassword())){
user.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
}
sysUserService.updateUser(user);
}
return R.ok();
}
}