package cn.cslg.pas.service; import cn.cslg.pas.common.model.PersonnelVO; import cn.cslg.pas.common.model.vo.ClientVO; import cn.cslg.pas.common.model.vo.ProjectVO; import cn.cslg.pas.common.utils.*; import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils; import cn.cslg.pas.domain.Client; import cn.cslg.pas.mapper.ClientMapper; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** *

* 委托方表 服务类 *

* * @author 王岩 * @since 2022-02-17 */ @Service @RequiredArgsConstructor(onConstructor_ = {@Lazy}) public class ClientService extends ServiceImpl { private final LoginUtils loginUtils; private final CacheUtils cacheUtils; private final ApiUtils apiUtils; public IPage getPageList(ClientVO params) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if (StringUtils.isNotEmpty(params.getName())) { queryWrapper.like(Client::getName, params.getName()); } queryWrapper.orderByDesc(Client::getCreateTime); IPage lst = this.page(new Page<>(params.getCurrent(), params.getSize()), queryWrapper); List list = this.page(new Page<>(params.getCurrent(), params.getSize()), queryWrapper).getRecords(); //获取专题库负责人对应信息 Map map1 = new HashMap<>(); try { map1.put("personnelId", list.stream().map(Client::getPersonnelId).collect(Collectors.toList())); String jsonObject1 = apiUtils.invokeApi(new JSONObject(map1), "/permission/api/system/getPersonnelById", "post", "data"); JSONArray jsonArray = JSON.parseArray(jsonObject1); List personnelList = jsonArray.toJavaList(ProjectVO.Personnel.class); for (Client client : list) { for (ProjectVO.Personnel personnel : personnelList) { if (client.getPersonnelId() != null) { if (client.getPersonnelId().equals(personnel.getId())) { client.setPersonnelName(personnel.getPersonnelName()); } } } } } catch (Exception e) { e.printStackTrace(); } lst.setRecords(list); return lst; } public Client getClientByName(String name) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Client::getName, name); return this.getOne(queryWrapper); } public List> getClientName(List ids) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(Client::getId, ids); List clients =this.list(queryWrapper); List> mapList =new ArrayList<>(); clients.forEach(item->{ Map map =new HashMap<>(); map.put("clientId",item.getId()); map.put("clientName",item.getName()); mapList.add(map); }); return mapList; } public List getClientByTenant(Integer tenantId) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Client::getTenantId, tenantId); return this.list(queryWrapper); } public String add(Client client) { client.setCreateBy(loginUtils.getId()); client.setCreateTime(DateUtils.getDateTime()); client.setStatus(1); client.insert(); return Response.success(client.getId()); } public String edit(Client client) { client.updateById(); return Response.success(); } public String delete(Integer id) { this.removeById(id); return Response.success(true); } public List getClientByObjectIds(List ids) { if (ids == null || ids.size() == 0) { return new ArrayList<>(); } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(Client::getId, ids); return this.list(queryWrapper); } public String add2(Client client) { Client temp = this.getClientByName(client.getName()); if (temp != null) { return Response.error("客户名称已存在"); } PersonnelVO personnelVO = cacheUtils.getLoginUserPersonnel(loginUtils.getId()); client.setCreateBy(personnelVO.getId()); client.setCreateTime(DateUtils.getDateTime()); client.setStatus(1); client.insert(); return Response.success(client.getId()); } public String edit2(Client client) { Client temp = this.getClientByName(client.getName()); if (temp != null && !temp.getId().equals(client.getId())) { return Response.error("客户名称已存在"); } client.updateById(); return Response.success(); } public String delete2(Integer id) { this.removeById(id); return Response.success(); } /** * @author 沈永艺 * @description 获取所有的客户列表 */ public String getAllClient() { return Response.success(this.list()); } }