From ea58f91760251bc30550b3516f8922ab8357a35f Mon Sep 17 00:00:00 2001 From: "15989082884@163.com" <15989082884@163.com> Date: Fri, 19 Jan 2024 17:28:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TbsScheduleItemBudgetServiceImpl.java | 1 + .../third/PortalSupplierController.java | 1303 +++++++++++++++++ 2 files changed, 1304 insertions(+) create mode 100644 src/main/java/com/qs/serve/modules/third/PortalSupplierController.java diff --git a/src/main/java/com/qs/serve/modules/tbs/service/impl/TbsScheduleItemBudgetServiceImpl.java b/src/main/java/com/qs/serve/modules/tbs/service/impl/TbsScheduleItemBudgetServiceImpl.java index 393a71fd..37856edd 100644 --- a/src/main/java/com/qs/serve/modules/tbs/service/impl/TbsScheduleItemBudgetServiceImpl.java +++ b/src/main/java/com/qs/serve/modules/tbs/service/impl/TbsScheduleItemBudgetServiceImpl.java @@ -233,6 +233,7 @@ public class TbsScheduleItemBudgetServiceImpl extends ServiceImpl conLqw = new LambdaQueryWrapper<>(); + conBudgetIds.add(0L); conLqw.in(TbsBudgetCondition::getBudgetId,conBudgetIds); conLqw.in(TbsBudgetCondition::getBrandId,param.getBrandIds()); List conditions = budgetConditionMapper.selectList(conLqw); diff --git a/src/main/java/com/qs/serve/modules/third/PortalSupplierController.java b/src/main/java/com/qs/serve/modules/third/PortalSupplierController.java new file mode 100644 index 00000000..11f9a423 --- /dev/null +++ b/src/main/java/com/qs/serve/modules/third/PortalSupplierController.java @@ -0,0 +1,1303 @@ +package com.qs.serve.modules.third.util; + +import com.alibaba.fastjson.JSONArray; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.github.pagehelper.PageHelper; +import com.qs.serve.common.framework.redis.RedisService; +import com.qs.serve.common.model.annotation.SysLog; +import com.qs.serve.common.model.consts.RedisCacheKeys; +import com.qs.serve.common.model.dto.PageVo; +import com.qs.serve.common.model.dto.R; +import com.qs.serve.common.model.enums.BizType; +import com.qs.serve.common.model.enums.SystemModule; +import com.qs.serve.common.util.*; +import com.qs.serve.modules.bms.controller.my.BmsSupplierMyController; +import com.qs.serve.modules.bms.entity.BmsRegion; +import com.qs.serve.modules.bms.entity.BmsRegion2; +import com.qs.serve.modules.bms.entity.BmsSupplier; +import com.qs.serve.modules.bms.entity.BmsSupplierComRegion; +import com.qs.serve.modules.bms.entity.bo.BmsSupplierBo; +import com.qs.serve.modules.bms.entity.bo.BmsSupplierExcelBo; +import com.qs.serve.modules.bms.entity.bo.BmsSupplierParentBo; +import com.qs.serve.modules.bms.entity.vo.BmsSupplierTreeVo; +import com.qs.serve.modules.bms.mapper.BmsSupplierComRegionMapper; +import com.qs.serve.modules.bms.mapper.BmsSupplierMapper; +import com.qs.serve.modules.bms.service.*; +import com.qs.serve.modules.his.entity.HisUserSupplier; +import com.qs.serve.modules.his.service.HisUserSupplierService; +import com.qs.serve.modules.sys.entity.SysUser; +import com.qs.serve.modules.sys.entity.bo.SysRelateSuppliersParam; +import com.qs.serve.modules.sys.entity.bo.SysRelateSuppliersParam2; +import com.qs.serve.modules.sys.entity.dto.SysUserSimpleVo; +import com.qs.serve.modules.sys.entity.dto.SysUserVo; +import com.qs.serve.modules.sys.service.SysAttachService; +import com.qs.serve.modules.sys.service.SysPostUserService; +import com.qs.serve.modules.sys.service.SysUserService; +import com.qs.serve.modules.tbs.common.TbsCenterType; +import com.qs.serve.modules.tbs.entity.TbsBudget; +import com.qs.serve.modules.tbs.service.TbsBudgetService; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.time.LocalDateTime; +import java.util.*; +import java.util.stream.Collectors; + +/** + * 基础档案 客户 + * @author YenHex + * @since 2022-10-10 + */ +@Slf4j +@AllArgsConstructor +@RestController +@RequestMapping("bms/supplier") +public class PortalSupplierController { + + private BmsSupplierService bmsSupplierService; + private BmsSupplierComRegionMapper supplierComRegionMapper; + private BmsSupplierAddressService bmsSupplierAddressService; + private BmsSupplierVisitAddressService bmsSupplierVisitAddressService; + private BmsSupplierContactsService bmsSupplierContactsService; + private BmsSupplierChannelService bmsSupplierChannelService; + private BmsRegionService bmsRegionService; + private BmsRegion2Service bmsRegion2Service; + private SysUserService sysUserService; + private SysPostUserService sysPostUserService; + private BmsSupplierMapper bmsSupplierMapper; + private HisUserSupplierService hisUserSupplierService; + private RedisService redisService; + private TbsBudgetService tbsBudgetService; + private BmsSupplierMyController supplierMyController; + private SysAttachService attachService; + + /** + * 翻页查询 + * @param param + * @return + */ + @PostMapping("/list") + @PreAuthorize("hasRole('bms:supplier:query')") + public R> getList(@RequestBody BmsSupplier param){ + + param = this.initQueryParam(param); + + String userId = param.getUserId(); + if(param.getLoadByCurrent()!=null&¶m.getLoadByCurrent().equals(1)){ + List userIds = sysPostUserService.listByChildIds(userId); + userIds.add(userId); + param.setSelectUserIds(userIds); + }else { + param.setCurrUserId(userId); + } + + List list = bmsSupplierService.selectSupplierList(param); + initSupplierList(list); + initParentList(list); + for (BmsSupplier supplier : list) { + supplier.setMasterFlag(0); + if(supplier.getUserId()!=null&&supplier.getUserId().equals(userId)){ + supplier.setMasterFlag(1); + break; + } + } + return R.ok(list); + } + + + /** + * 翻页查询,值=1,过滤当前用户负责数据 + * @param param + * @return + */ + @PostMapping("/page") + @PreAuthorize("hasRole('bms:supplier:query')") + public R> getPage(@RequestBody BmsSupplier param){ + param = this.initQueryParam(param); + + if(param.getLoadByCurrent()!=null&¶m.getLoadByCurrent().equals(1)){ + PageUtil.setPageSize(param.getPageSize()+""); + PageUtil.setPageNum(param.getPageNum()+""); + return supplierMyController.getUnderlingAndMinePage(param); + } + + PageHelper.startPage(param.getPageNum(),param.getPageSize()); + List list = bmsSupplierService.selectSupplierList(param); + initSupplierList(list); + initParentList(list); + return R.byPageHelperList(list); + } + + @PostMapping("/exportExcel") + @PreAuthorize("hasRole('bms:supplier:query')") + public R> exportExcel(@RequestBody BmsSupplier param){ + + param = this.initQueryParam(param); + + List list = bmsSupplierService.selectSupplierList(param); + initSupplierList(list); + initParentList(list); + + List boList = list.stream().map(a->BmsSupplierExcelBo.toExcelBo(a)).collect(Collectors.toList()); + + return R.ok(boList); + } + + private BmsSupplier initQueryParam(BmsSupplier param){ + //兼容 前端接错参数 + if(param.getSupplierName()!=null){ + param.setName(param.getSupplierName()); + } + if(param.getSupplierCode()!=null){ + param.setCode(param.getSupplierCode()); + } + + if(StringUtils.hasText(param.getRegion2Label())){ + LambdaQueryWrapper region2LambdaQueryWrapper = new LambdaQueryWrapper<>(); + region2LambdaQueryWrapper.like(BmsRegion2::getPathNames,param.getRegion2Label()); + List region2List = bmsRegion2Service.list(region2LambdaQueryWrapper); + if(region2List.size()>0) { + param.setRegion2Ids(region2List.stream().map(a -> a.getId()).collect(Collectors.toList())); + }else{ + param.setRegion2Ids(Arrays.asList("#")); + } + } + if(StringUtils.hasText(param.getSearchRegion2FirstName())){ + LambdaQueryWrapper region2LambdaQueryWrapper = new LambdaQueryWrapper<>(); + region2LambdaQueryWrapper.like(BmsRegion2::getName,param.getSearchRegion2FirstName()); + List region2List = bmsRegion2Service.list(region2LambdaQueryWrapper); + if(region2List.size()>0) { + param.setSearchRegion2First(region2List.stream().map(a -> a.getId()).collect(Collectors.toList())); + }else{ + param.setSearchRegion2First(Arrays.asList("#")); + } + } + if(StringUtils.hasText(param.getSearchRegion2SecondName())){ + LambdaQueryWrapper region2LambdaQueryWrapper = new LambdaQueryWrapper<>(); + region2LambdaQueryWrapper.like(BmsRegion2::getName,param.getSearchRegion2SecondName()); + List region2List = bmsRegion2Service.list(region2LambdaQueryWrapper); + if(region2List.size()>0) { + param.setSearchRegion2Second(region2List.stream().map(a -> a.getId()).collect(Collectors.toList())); + }else{ + param.setSearchRegion2Second(Arrays.asList("#")); + } + } + if(StringUtils.hasText(param.getSearchRegion2ThirdName())){ + LambdaQueryWrapper region2LambdaQueryWrapper = new LambdaQueryWrapper<>(); + region2LambdaQueryWrapper.like(BmsRegion2::getName,param.getSearchRegion2ThirdName()); + List region2List = bmsRegion2Service.list(region2LambdaQueryWrapper); + if(region2List.size()>0) { + param.setSearchRegion2Third(region2List.stream().map(a -> a.getId()).collect(Collectors.toList())); + }else{ + param.setSearchRegion2Third(Arrays.asList("#")); + } + } + + if(StringUtils.hasText(param.getRegionLabel())){ + LambdaQueryWrapper regionLambdaQueryWrapper = new LambdaQueryWrapper<>(); + regionLambdaQueryWrapper.like(BmsRegion::getPathNames,param.getRegionLabel()); + List regionList = bmsRegionService.list(regionLambdaQueryWrapper); + if(regionList.size()>0) { + param.setRegionIds(regionList.stream().map(a -> a.getId()).collect(Collectors.toList())); + }else{ + param.setRegion2Ids(Arrays.asList("#")); + } + } + if(StringUtils.hasText(param.getSearchRegionFirstName())){ + LambdaQueryWrapper regionLambdaQueryWrapper = new LambdaQueryWrapper<>(); + regionLambdaQueryWrapper.like(BmsRegion::getName,param.getSearchRegionFirstName()); + List regionList = bmsRegionService.list(regionLambdaQueryWrapper); + if(regionList.size()>0) { + param.setSearchRegionFirst(regionList.stream().map(a -> a.getId()).collect(Collectors.toList())); + }else{ + param.setSearchRegionFirst(Arrays.asList("#")); + } + } + if(StringUtils.hasText(param.getSearchRegionSecondName())){ + LambdaQueryWrapper regionLambdaQueryWrapper = new LambdaQueryWrapper<>(); + regionLambdaQueryWrapper.like(BmsRegion::getName,param.getSearchRegionSecondName()); + List regionList = bmsRegionService.list(regionLambdaQueryWrapper); + if(regionList.size()>0) { + param.setSearchRegionSecond(regionList.stream().map(a -> a.getId()).collect(Collectors.toList())); + }else{ + param.setSearchRegionSecond(Arrays.asList("#")); + } + } + return param; + } + + /** + * 树查询 + * @param param + * @return + */ + @GetMapping("/tree") + @PreAuthorize("hasRole('bms:supplier:query')") + public R getTree(BmsSupplier param){ + String json = redisService.getString(RedisCacheKeys.SUPPLIER_RELATE_TREE); + if(json==null){ + List list = bmsSupplierService.selectSupplierList(param); + initSupplierList(list); + List list2 = list.stream().map(supplier->{ + BmsSupplierTreeVo treeNode = CopierUtil.copy(supplier,new BmsSupplierTreeVo()); + treeNode.setId(supplier.getId()); + treeNode.setParentId(supplier.getPid()); + treeNode.setSort(0); + return treeNode; + }).collect(Collectors.toList()); + list2 = TreeUtil.buildByRecursive(list2,TreeUtil.DEFAULT_PID_STRING); + String jsonString = JsonUtil.objectToJson(list2); + redisService.set(RedisCacheKeys.SUPPLIER_RELATE_TREE,jsonString); + return R.ok(list2); + } + return R.ok(JSONArray.parseArray(json)); + } + + + + /** + * 更改客户授权(用户页面) + * @return + */ + @PostMapping("/relateSuppliers") + @PreAuthorize("hasRole('bms:supplier:update')") + public R relateSuppliers(@RequestBody @Valid SysRelateSuppliersParam param){ + List supplierIds = param.getSupplierIds(); + String userId = param.getUserId(); + List list = bmsSupplierService.listByIds(supplierIds); + for (BmsSupplier supplier : list) { + if(userId.equals(supplier.getUserId())){ + continue; + } + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + boolean exist = false; + for (String otherUserId : supplier.getOtherUserIds()) { + if(userId.equals(otherUserId)){ + exist = true; + break; + } + } + if(exist){ + continue; + } + } + List otherUserIds = new ArrayList<>(); + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + otherUserIds.addAll(Arrays.asList(supplier.getOtherUserIds())); + } + otherUserIds.add(userId); + //更新次要负责人 + BmsSupplier supplierParam = new BmsSupplier(); + supplierParam.setId(supplier.getId()); + supplierParam.setOtherUserIds(otherUserIds.toArray(new String[otherUserIds.size()])); + bmsSupplierService.updateById(supplierParam); + } + initSupplierOtherInfo(supplierIds); + //清空临时表 + hisUserSupplierService.cleanTable(); + return R.ok(); + } + + /** + * 更改客户授权(客户页面) + * @return + */ + @PostMapping("/relateSuppliers2") + @PreAuthorize("hasRole('bms:supplier:update')") + public R relateSuppliers2(@RequestBody SysRelateSuppliersParam2 param){ + BmsSupplier supplier = bmsSupplierService.getById(param.getSupplierId()); + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + for (String userId : supplier.getOtherUserIds()) { + param.getUserIds().add(userId); + } + } + List uids = param.getUserIds().stream() + .filter(uid->supplier.getUserId()==null || !supplier.getUserId().equals(uid)) + .collect(Collectors.toList()); + BmsSupplier bmsSupplier = new BmsSupplier(); + bmsSupplier.setId(param.getSupplierId().toString()); + bmsSupplier.setOtherUserIds(param.getUserIds()==null?new String[]{}:uids.toArray(new String[uids.size()])); + bmsSupplierService.updateById(bmsSupplier); + initSupplierOtherInfo(Arrays.asList(supplier.getId())); + //清空临时表 + hisUserSupplierService.cleanTable(); + this.saveTOHis(param.getUserIds(), supplier); + return R.ok(); + } + + + /** + * 删除客户分配 + * @return + */ + @PostMapping("/relateRemove") + @PreAuthorize("hasRole('bms:supplier:update')") + public R relateRemove(@RequestBody SysRelateSuppliersParam param){ + List supplierIds = param.getSupplierIds(); + String userId = param.getUserId(); + List list = bmsSupplierService.listByIds(supplierIds); + for (BmsSupplier supplier : list) { + if(userId.equals(supplier.getUserId())){ + bmsSupplierMapper.removeRelateUserInfo(Long.parseLong(supplier.getId())); + } + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + List otherUserIds = new ArrayList<>(); + for (String otherUserId : supplier.getOtherUserIds()) { + if(userId.equals(otherUserId)){ + continue; + } + otherUserIds.add(otherUserId); + } + //更新次要负责人 + BmsSupplier supplierParam = new BmsSupplier(); + supplierParam.setId(supplier.getId()); + supplierParam.setOtherUserIds(otherUserIds.toArray(new String[otherUserIds.size()])); + bmsSupplierService.updateById(supplierParam); + } + } + initSupplierOtherInfo(supplierIds); + //清空临时表 + hisUserSupplierService.cleanTable(); + redisService.remove(RedisCacheKeys.SUPPLIER_RELATE_TREE); + return R.ok(); + } + + /** + * 删除客户分配 + * @return + */ + @PostMapping("/relateRemove2") + @PreAuthorize("hasRole('bms:supplier:update')") + public R relateRemove2(@RequestBody SysRelateSuppliersParam2 param){ + List userIds = param.getUserIds(); + BmsSupplier supplier = bmsSupplierService.getById(param.getSupplierId()); + for (String userId : userIds) { + if(supplier.getUserId()!=null && userId.equals(supplier.getUserId())){ + bmsSupplierMapper.removeRelateUserInfo(Long.parseLong(supplier.getId())); + } + } + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + List otherUserIds = new ArrayList<>(); + for (String otherUserId : supplier.getOtherUserIds()) { + if(!userIds.contains(otherUserId)){ + otherUserIds.add(otherUserId); + } + } + BmsSupplier supplier1 = new BmsSupplier(); + supplier1.setId(supplier.getId()); + supplier1.setOtherUserIds(otherUserIds.toArray(new String[otherUserIds.size()])); + if(otherUserIds.size()<1){ + supplier1.setOtherUserNames(""); + supplier1.setOtherUserCodes(""); + }else { + List sysUsers = sysUserService.listByIds(otherUserIds); + String names = sysUsers.stream().map(SysUser::getName).collect(Collectors.joining(",")); + String codes = sysUsers.stream().map(SysUser::getCode).collect(Collectors.joining(",")); + supplier1.setOtherUserNames(names); + supplier1.setOtherUserCodes(codes); + } + bmsSupplierService.updateById(supplier1); + } + initSupplierOtherInfo(Arrays.asList(supplier.getId())); + //清空临时表 + hisUserSupplierService.cleanTable(); + redisService.remove(RedisCacheKeys.SUPPLIER_RELATE_TREE); + //移除客户直属负责人 + LambdaQueryWrapper rmLqw = new LambdaQueryWrapper<>(); + rmLqw.in(HisUserSupplier::getUserId,userIds); + rmLqw.eq(HisUserSupplier::getSupplierId,supplier.getId()); + rmLqw.eq(HisUserSupplier::getType,2); + hisUserSupplierService.remove(rmLqw); + return R.ok(); + } + + + + /** + * 设置主要负责人 + * @param supplierId + * @param userId + * @return + */ + @GetMapping("/setMainUser") + @PreAuthorize("hasRole('bms:supplier:update')") + public R setMainUser(String supplierId,String userId){ + BmsSupplier supplier = bmsSupplierService.getById(supplierId); + if(supplier.getUserId()!=null&&supplier.getUserId().equals(userId)){ + return R.ok(); + } + SysUser sysUser = sysUserService.getById(userId); + BmsSupplier supplierParam = new BmsSupplier(); + supplierParam.setId(supplierId); + supplierParam.setUserId(sysUser.getId()); + supplierParam.setUserName(sysUser.getName()); + supplierParam.setUserCode(sysUser.getCode()); + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + List otherUserIds = new ArrayList<>(Arrays.asList(supplier.getOtherUserIds())); + if(supplier.getUserId() != null){ + otherUserIds.add(supplier.getUserId()); + } + otherUserIds = otherUserIds.stream().filter(uid->!uid.equals(userId)).collect(Collectors.toList()); + Set otherUserIdSet = new HashSet<>(otherUserIds); + String[] otherArr = otherUserIdSet.toArray(new String[otherUserIdSet.size()]); + supplierParam.setOtherUserIds(otherArr); + } + bmsSupplierService.updateById(supplierParam); + initSupplierOtherInfo(Arrays.asList(supplier.getId())); + return R.ok(); + } + + + + /** + * 移除主要负责人 + * @param supplierId + * @return + */ + @GetMapping("/setOtherUser") + @PreAuthorize("hasRole('bms:supplier:update')") + public R setOtherUser(Long supplierId){ + BmsSupplier supplier = bmsSupplierService.getById(supplierId); + List userIds = new ArrayList<>(); + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + userIds = Arrays.asList(supplier.getOtherUserIds()); + } + if(supplier.getUserId()==null){ + return R.ok(); + } + Set otherUserIdSet = new HashSet<>(userIds); + otherUserIdSet.add(supplier.getUserId()); + String[] otherArr = otherUserIdSet.toArray(new String[otherUserIdSet.size()]); + bmsSupplierMapper.removeRelateUserInfo(supplierId); + BmsSupplier bmsSupplier = new BmsSupplier(); + bmsSupplier.setId(supplier.getId()); + bmsSupplier.setOtherUserIds(otherArr); + bmsSupplierService.updateById(bmsSupplier); + //清空临时表 + hisUserSupplierService.cleanTable(); + initSupplierOtherInfo(Arrays.asList(supplierId.toString())); + return R.ok(); + } + + /** + * 获取负责人接口(用于后续成本中心) + * @param supplierId + * @return + */ + @GetMapping("/getMgrUser") + public R> getMgrUser(Long supplierId){ + BmsSupplier supplier = bmsSupplierService.getById(supplierId); + List userIds = new ArrayList<>(); + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + userIds = Arrays.asList(supplier.getOtherUserIds()); + } + Set otherUserIdSet = new HashSet<>(userIds); + if(supplier.getUserId()==null){ + otherUserIdSet.add(supplier.getUserId()); + } + if(otherUserIdSet.size()<1){ + return R.ok(); + } + List userList = sysUserService.listByIds(otherUserIdSet); + List sysUsers = userList.stream().map(SysUser::toSysUserVo).collect(Collectors.toList()); + return R.ok(sysUsers); + } + + /** + * 根据ID查询 + * @param id + * @return + */ + @GetMapping("/getById/{id}") + @SysLog(module = SystemModule.BASE, title = "供应商", biz = BizType.QUERY) + @PreAuthorize("hasRole('bms:supplier:query')") + public R getById(@PathVariable("id") String id,Integer loadRegionInfo){ + BmsSupplier bmsSupplier = bmsSupplierService.getById(id); + if(StringUtils.hasText(bmsSupplier.getPid())&&!bmsSupplier.getPid().equals("0")){ + BmsSupplier parentInfo = bmsSupplierService.getById(bmsSupplier.getPid()); + bmsSupplier.setParentRegionInfo(parentInfo); + }else { + bmsSupplier.setPid(null); + } + BmsRegion region = bmsRegionService.getById(bmsSupplier.getRegionLast()); + if(region!=null){ + bmsSupplier.setRegionLabel(region.getName()); + } + BmsRegion2 region2 = bmsRegion2Service.getById(bmsSupplier.getRegion2Last()); + if(region2!=null){ + bmsSupplier.setRegion2Label(region2.getName()); + } + List userVos = new ArrayList<>(); + if(bmsSupplier.getUserId()!=null){ + SysUser sysUser = sysUserService.getById(bmsSupplier.getUserId()); + SysUserSimpleVo simpleVo = sysUser.toSimpleVo(true); + simpleVo.setSupplierMainFlag(1); + userVos.add(simpleVo); + } + if(CollectionUtil.isNotEmpty(bmsSupplier.getOtherUserIds())){ + List userList = sysUserService.listByIds(Arrays.asList(bmsSupplier.getOtherUserIds())); + List userVos2 = userList.stream().map(ur->{ + SysUserSimpleVo simpleVo = ur.toSimpleVo(true); + simpleVo.setSupplierMainFlag(0); + return simpleVo; + }).collect(Collectors.toList()); + userVos.addAll(userVos2); + } + if(loadRegionInfo!=null&&loadRegionInfo.equals(1)){ + BmsRegion obj1 = bmsRegionService.getById(bmsSupplier.getRegionFirst()); + BmsRegion obj2 = bmsRegionService.getById(bmsSupplier.getRegionSecond()); + BmsRegion obj3 = bmsRegionService.getById(bmsSupplier.getRegionThird()); + BmsRegion obj4 = bmsRegionService.getById(bmsSupplier.getRegionFourthly()); + bmsSupplier.setRegionFirstInfo(obj1); + bmsSupplier.setRegionSecondInfo(obj2); + bmsSupplier.setRegionThirdInfo(obj3); + bmsSupplier.setRegionFourthlyInfo(obj4); + BmsRegion2 obj11 = bmsRegion2Service.getById(bmsSupplier.getRegion2First()); + BmsRegion2 obj22 = bmsRegion2Service.getById(bmsSupplier.getRegion2Second()); + BmsRegion2 obj33 = bmsRegion2Service.getById(bmsSupplier.getRegion2Third()); + BmsRegion2 obj44 = bmsRegion2Service.getById(bmsSupplier.getRegion2Fourthly()); + bmsSupplier.setRegion2FirstInfo(obj11); + bmsSupplier.setRegion2SecondInfo(obj22); + bmsSupplier.setRegion2ThirdInfo(obj33); + bmsSupplier.setRegion2FourthlyInfo(obj44); + } + if(bmsSupplier.getSupplierAttachIds()!=null&&bmsSupplier.getSupplierAttachIds().length>0){ + bmsSupplier.setSupplierAttachInfoList(attachService.listByIds(Arrays.asList(bmsSupplier.getSupplierAttachIds()))); + } + bmsSupplier.setUserVos(userVos); + return R.ok(bmsSupplier); + } + + /** + * 根据ID更新 + * @param param + * @return + */ + @PostMapping("/updateById") + @SysLog(module = SystemModule.BASE, title = "供应商", biz = BizType.UPDATE) + @PreAuthorize("hasRole('bms:supplier:update')") + public R updateById(@RequestBody BmsSupplierBo param){ + if(param.getId()==null){ + return R.error("id不能为空"); + } + bmsSupplierService.edit(param); + redisService.remove(RedisCacheKeys.SUPPLIER_RELATE_TREE); + hisUserSupplierService.cleanTable(); + return R.ok(); + } + + /** + * 根据ID更新供应商标识 + * @apiNote (SupplierFlag,id)不能为空 + * @param param + * @return + */ + @PostMapping("/updateSupplierFlagById") + @SysLog(module = SystemModule.BASE, title = "供应商标识", biz = BizType.UPDATE) + public R updateSupplierFlagById(@RequestBody BmsSupplierBo param){ + if(param.getId()==null){ + return R.error("id不能为空"); + } + BmsSupplier entity = new BmsSupplier(); + entity.setId(param.getId()); + entity.setSupplierFlag(param.getSupplierFlag()); + bmsSupplierService.updateById(entity); + return R.ok(); + } + + + /** + * 更新父级ID + * @param param + * @return + */ + @PostMapping("/updateParent") + @SysLog(module = SystemModule.BASE, title = "供应商", biz = BizType.UPDATE) + @PreAuthorize("hasRole('bms:supplier:update')") + public R updateParent(@RequestBody BmsSupplierParentBo param){ + BmsSupplier supplier = new BmsSupplier(); + BmsSupplier supplierdb = null; + if(param.getId()!=null){ + supplier.setId(param.getId()); + supplierdb = bmsSupplierService.getById(param.getId()); + } + if(supplierdb!=null && !supplierdb.getPid().equals(param.getPid())){ + List childList = bmsSupplierService.listChildByPathIds(supplierdb.getPathIds()); + if(param.getPid()==null||param.getPid().equals("0")){ + supplier.setPid("0"); + supplier.setParentCode("0"); + supplier.setPathIds("0_"+supplier.getId()); + }else { + BmsSupplier parentNode = bmsSupplierService.getById(param.getPid()); + supplier.setPid(param.getPid()); + String[] pids = parentNode.getPathIds().split("_"); + for (String pid : pids) { + if(pid.equals(supplier.getId())){ + return R.error("数据出现循环递归,保存失败"); + } + } + supplier.setPathIds(parentNode.getPathIds()+"_"+supplier.getId()); + supplier.setParentCode(parentNode.getCode()); + } + for (BmsSupplier bmsSupplier : childList) { + BmsSupplier bmsSupplier2 = new BmsSupplier(); + bmsSupplier2.setPathIds(bmsSupplier.getPathIds().replace(supplierdb.getPathIds(),supplier.getPathIds())); + bmsSupplier2.setId(bmsSupplier.getId()); + bmsSupplierService.updateById(bmsSupplier2); + } + } + bmsSupplierService.updateById(supplier); + redisService.remove(RedisCacheKeys.SUPPLIER_RELATE_TREE); + return R.ok(); + } + + /** + * 新增 + * @param param + * @return + */ + @PostMapping("/save") + @SysLog(module = SystemModule.BASE, title = "供应商", biz = BizType.INSERT) + @PreAuthorize("hasRole('bms:supplier:insert')") + public R save(@RequestBody @Valid BmsSupplierBo param){ + bmsSupplierService.edit(param); + //清空临时表 + hisUserSupplierService.cleanTable(); + redisService.remove(RedisCacheKeys.SUPPLIER_RELATE_TREE); + return R.ok(); + } + + /** + * 删除 + * @param id + * @return + */ + @DeleteMapping("/deleteById/{id}") + @SysLog(module = SystemModule.BASE, title = "供应商", biz = BizType.DELETE) + @PreAuthorize("hasRole('bms:supplier:delete')") + public R deleteById(@PathVariable("id") String id){ + if(bmsSupplierService.listChild(id).size()>0){ + return R.error("含有子客户无法删除"); + } + LambdaQueryWrapper budgetLambdaQueryWrapper = new LambdaQueryWrapper<>(); + budgetLambdaQueryWrapper.eq(TbsBudget::getCenterType, TbsCenterType.customer); + budgetLambdaQueryWrapper.eq(TbsBudget::getCenterId,id); + if(0 list) { + //设置销售区域 + List regionIds = list.stream() + .filter(sup -> sup.getRegionLast() != null && !sup.getRegionLast().equals("0")) + .map(BmsSupplier::getRegionLast).collect(Collectors.toList()); + regionIds.addAll(list.stream() + .filter(sup -> sup.getRegionFirst() != null && !sup.getRegionFirst().equals("0")) + .map(BmsSupplier::getRegionFirst).collect(Collectors.toList())); + regionIds.addAll(list.stream() + .filter(sup -> sup.getRegionSecond() != null && !sup.getRegionSecond().equals("0")) + .map(BmsSupplier::getRegionSecond).collect(Collectors.toList())); + regionIds.addAll(list.stream() + .filter(sup -> sup.getRegionThird() != null && !sup.getRegionThird().equals("0")) + .map(BmsSupplier::getRegionThird).collect(Collectors.toList())); + regionIds.addAll(list.stream() + .filter(sup -> sup.getRegionFourthly() != null && !sup.getRegionFourthly().equals("0")) + .map(BmsSupplier::getRegionFourthly).collect(Collectors.toList())); + + List regionList = null; + if (CollectionUtil.isNotEmpty(regionIds)) { + regionList = bmsRegionService.listByIds(regionIds); + } + if (CollectionUtil.isNotEmpty(regionList)){ + for (BmsSupplier supplier : list) { + String regionId = supplier.getRegionLast(); + String regionFirst = supplier.getRegionFirst(); + String regionSecond = supplier.getRegionSecond(); + String regionThird = supplier.getRegionThird(); + String regionFourthly = supplier.getRegionFourthly(); + + for (BmsRegion region : regionList) { + if (region.getId().equals(regionId)) { + supplier.setRegionLabel(region.getName()); + } + if (region.getId().equals(regionFirst)) { + supplier.setRegionFirstInfo(region); + } + if (region.getId().equals(regionSecond)) { + supplier.setRegionSecondInfo(region); + } + if (region.getId().equals(regionThird)) { + supplier.setRegionThirdInfo(region); + } + if (region.getId().equals(regionFourthly)) { + supplier.setRegionFourthlyInfo(region); + } + } + } + } + //设置行政区域 + List region2Ids = list.stream() + .filter(sup->sup.getRegion2Last()!=null&&!sup.getRegion2Last().equals("0")) + .map(BmsSupplier::getRegion2Last).collect(Collectors.toList()); + region2Ids.addAll(list.stream() + .filter(sup -> sup.getRegion2First() != null && !sup.getRegion2First().equals("0")) + .map(BmsSupplier::getRegion2First).collect(Collectors.toList())); + region2Ids.addAll(list.stream() + .filter(sup -> sup.getRegion2Second() != null && !sup.getRegion2Second().equals("0")) + .map(BmsSupplier::getRegion2Second).collect(Collectors.toList())); + region2Ids.addAll(list.stream() + .filter(sup -> sup.getRegion2Third() != null && !sup.getRegion2Third().equals("0")) + .map(BmsSupplier::getRegion2Third).collect(Collectors.toList())); + region2Ids.addAll(list.stream() + .filter(sup -> sup.getRegion2Fourthly() != null && !sup.getRegion2Fourthly().equals("0")) + .map(BmsSupplier::getRegion2Fourthly).collect(Collectors.toList())); + + List region2List = null; + if(CollectionUtil.isNotEmpty(region2Ids)){ + region2List = bmsRegion2Service.listByIds(region2Ids); + } + if(CollectionUtil.isNotEmpty(region2List)){ + for (BmsSupplier supplier : list) { + String region2Id = supplier.getRegion2Last(); + String regionFirst = supplier.getRegion2First(); + String regionSecond = supplier.getRegion2Second(); + String regionThird = supplier.getRegion2Third(); + String regionFourthly = supplier.getRegion2Fourthly(); + + for (BmsRegion2 region : region2List) { + String regionId = region.getId(); + + if (regionId.equals(region2Id)) { + supplier.setRegion2Label(region.getName()); + } + if (region.getId().equals(regionFirst)) { + supplier.setRegion2FirstInfo(region); + } + if (region.getId().equals(regionSecond)) { + supplier.setRegion2SecondInfo(region); + } + if (region.getId().equals(regionThird)) { + supplier.setRegion2ThirdInfo(region); + } + if (region.getId().equals(regionFourthly)) { + supplier.setRegion2FourthlyInfo(region); + } + } + } + } + } + + + private void initParentList(List list){ + for (BmsSupplier supplier : list) { + if(StringUtils.hasText(supplier.getPid())&&!supplier.getPid().equals("0")){ + BmsSupplier parentInfo = bmsSupplierService.getById(supplier.getPid()); + supplier.setParentRegionInfo(parentInfo); + }else { + supplier.setPid(null); + } + } + } + + /** + * 初始化其它信息 + * @param supplierIds + */ + private void initSupplierOtherInfo(List supplierIds){ + List suppliers = bmsSupplierService.listByIds(supplierIds); + List updParams = new ArrayList<>(); + for (BmsSupplier supplier : suppliers) { + if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){ + List otherUserIds = Arrays.asList(supplier.getOtherUserIds()); + BmsSupplier param = new BmsSupplier(); + param.setId(supplier.getId()); + if(otherUserIds.size()>0){ + List userList = sysUserService.listByIds(otherUserIds); + String codes = userList.stream().map(SysUser::getCode).collect(Collectors.joining(",")); + String names = userList.stream().map(SysUser::getName).collect(Collectors.joining(",")); + param.setOtherUserCodes(codes); + param.setOtherUserNames(names); + }else { + param.setOtherUserCodes(""); + param.setOtherUserNames(""); + } + updParams.add(param); + } + } + if(CollectionUtil.isNotEmpty(updParams)){ + bmsSupplierService.updateBatchById(updParams); + } + } + + /** + * 获取大区下来列表 + * @return + */ + @GetMapping("/getComRegion") + public R> getComRegion(){ + List supplierComRegions = supplierComRegionMapper.selectList(new QueryWrapper<>()); + return R.ok(supplierComRegions); + } + + /** + * 新增 + * @param param + * @return + */ + @PostMapping("/importExcel") +// @SysLog(module = SystemModule.BASE, title = "供应商", biz = BizType.INSERT) + @PreAuthorize("hasRole('bms:supplier:insert')") + public R importExcel(@RequestBody List param){ + param.forEach(a->{ + a.setErrorInfo(""); + if(a.getType().equals("4")){ + return; + } + if(!StringUtils.hasText(a.getCustomerCode())){ + a.setErrorInfo(a.getErrorInfo()+"客户编号不能为空;"); + } + if(a.getType().equals("3")){ + return; + } + if(!StringUtils.hasText(a.getCustomerName())){ + a.setErrorInfo(a.getErrorInfo()+"客户名称不能为空;"); + } + if(!StringUtils.hasText(a.getSalesRegionName())){ + a.setErrorInfo(a.getErrorInfo()+"销售区域不能为空;"); + } + if(!StringUtils.hasText(a.getFirstLevelAdministrativeRegionName())){ + a.setErrorInfo(a.getErrorInfo()+"省不能为空;"); + } + if(!StringUtils.hasText(a.getSecondaryLevelAdministrativeRegionName())){ + a.setErrorInfo(a.getErrorInfo()+"市不能为空;"); + } + if(!StringUtils.hasText(a.getAdministrativeRegionName())){ + a.setErrorInfo(a.getErrorInfo()+"行政区域不能为空;"); + } + if(a.getIsSupplier()==null){ + a.setErrorInfo(a.getErrorInfo()+"是否供应商字段不能为空;"); + } +// if(!StringUtils.hasText(a.getMinorHeadCodes()) && !StringUtils.hasText(a.getMainHeadCode())){ +// a.setErrorInfo(a.getErrorInfo()+"负责人不能为空;"); +// } + if(a.getIsExpendableExpense()==null){ + a.setErrorInfo(a.getErrorInfo()+"是否投放费用字段不能为空;"); + } +// if(a.getCloseAccountStatus()==null){ +// a.setErrorInfo(a.getErrorInfo()+"闭户状态不能为空;"); +// } + }); + List saveList = param.stream().filter(a->a.getType().equals("1")).collect(Collectors.toList()); + List updateList = param.stream().filter(a->a.getType().equals("2")).collect(Collectors.toList()); + List deleteList = param.stream().filter(a->a.getType().equals("3")).collect(Collectors.toList()); + + /** --------------------------------------------------------------------------- **/ + List bmsRegionNames = new ArrayList<>(); + bmsRegionNames.addAll( + saveList.stream().filter(a->a.getSalesRegionName()!=null) + .map(a->a.getSalesRegionName()).collect(Collectors.toList()) + ); + bmsRegionNames.addAll( + updateList.stream().filter(a->a.getSalesRegionName()!=null) + .map(a->a.getSalesRegionName()).collect(Collectors.toList()) + ); + bmsRegionNames = bmsRegionNames.stream().distinct().collect(Collectors.toList()); + List regionList = new ArrayList<>(); + if(bmsRegionNames.size()>0){ + LambdaQueryWrapper bmsRegionLambdaQueryWrapper = new LambdaQueryWrapper<>(); + bmsRegionLambdaQueryWrapper.in(BmsRegion::getName,bmsRegionNames); + regionList = bmsRegionService.list(bmsRegionLambdaQueryWrapper); + List existBmsRegionName = regionList.stream().map(a->a.getName()).collect(Collectors.toList()); + if(regionList.size(){ + if(a.getType().equals("4")){ + return; + } + if(!existBmsRegionName.contains(a.getSalesRegionName())){ + a.setErrorInfo(a.getErrorInfo()+"不存在销售区域"+a.getSalesRegionName()+";"); + } + }); + + } + } + + List bmsBizRegionNames = new ArrayList<>(); + List bizRegionList = new ArrayList<>(); + + bmsBizRegionNames.addAll( + saveList.stream().filter(a->a.getSecondaryLevelAdministrativeRegionName()!=null + && a.getFirstLevelAdministrativeRegionName()!=null && a.getAdministrativeRegionName()!=null) + .map(a->a.getFirstLevelAdministrativeRegionName()+"_"+a.getSecondaryLevelAdministrativeRegionName()+"_"+a.getAdministrativeRegionName()).collect(Collectors.toList()) + ); + bmsBizRegionNames.addAll( + updateList.stream().filter(a->a.getSecondaryLevelAdministrativeRegionName()!=null + && a.getFirstLevelAdministrativeRegionName()!=null && a.getAdministrativeRegionName()!=null) + .map(a->a.getFirstLevelAdministrativeRegionName()+"_"+a.getSecondaryLevelAdministrativeRegionName()+"_"+a.getAdministrativeRegionName()).collect(Collectors.toList()) + ); + bmsBizRegionNames = bmsBizRegionNames.stream().distinct().collect(Collectors.toList()); + if(bmsBizRegionNames.size()>0){ + LambdaQueryWrapper bmsRegion2LambdaQueryWrapper = new LambdaQueryWrapper<>(); + bmsRegion2LambdaQueryWrapper.in(BmsRegion2::getPathNames,bmsBizRegionNames); + bizRegionList = bmsRegion2Service.list(bmsRegion2LambdaQueryWrapper); + List exsitBizRegeionName = bizRegionList.stream().map(BmsRegion2::getPathNames).collect(Collectors.toList()); + if(bizRegionList.size(){ + if(a.getType().equals("4")){ + return; + } + if(!exsitBizRegeionName.contains( + a.getFirstLevelAdministrativeRegionName()+"_"+a.getSecondaryLevelAdministrativeRegionName()+"_"+a.getAdministrativeRegionName()) + ){ + a.setErrorInfo(a.getErrorInfo()+"不存在行政区域"+a.getFirstLevelAdministrativeRegionName()+"_"+a.getSecondaryLevelAdministrativeRegionName()+"_"+a.getAdministrativeRegionName()+";"); + } + }); + } + } + /** ------------------------------- 校验负责人 ------------------------------------------------ */ + List userCodes = new ArrayList<>(); + userCodes.addAll( + saveList.stream().filter(a->StringUtils.hasText(a.getMainHeadCode())) + .map(a->Arrays.asList(a.getMainHeadCode().split(";"))) + .flatMap(List::stream).collect(Collectors.toList()) + ); + userCodes.addAll( + saveList.stream().filter(a->StringUtils.hasText(a.getMinorHeadCodes())) + .map(a->Arrays.asList(a.getMinorHeadCodes().split(";"))) + .flatMap(List::stream).collect(Collectors.toList()) + ); + userCodes.addAll( + updateList.stream().filter(a->StringUtils.hasText(a.getMainHeadCode())) + .map(a->Arrays.asList(a.getMainHeadCode().split(";"))) + .flatMap(List::stream).collect(Collectors.toList()) + ); + userCodes.addAll( + updateList.stream().filter(a->StringUtils.hasText(a.getMinorHeadCodes())) + .map(a->Arrays.asList(a.getMinorHeadCodes().split(";"))) + .flatMap(List::stream).collect(Collectors.toList()) + ); + userCodes = userCodes.stream().distinct().collect(Collectors.toList()); + List userList = new ArrayList<>(); + if(userCodes.size()>0){ + LambdaQueryWrapper sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>(); + sysUserLambdaQueryWrapper.in(SysUser::getCode,userCodes); + userList = sysUserService.list(sysUserLambdaQueryWrapper); + List exsitUserCodes = userList.stream().map(a->a.getCode()).collect(Collectors.toList()); + if(userList.size(){ + if(a.getType().equals("4")){ + return; + } + if(a.getType().equals("3")){ + return; + } + List userCodeTemp = new ArrayList<>(); + if(StringUtils.hasText(a.getMainHeadCode())){ + userCodeTemp.addAll(Arrays.asList(a.getMainHeadCode().split(";"))); + } + if(StringUtils.hasText(a.getMinorHeadCodes())){ + userCodeTemp.addAll(Arrays.asList(a.getMinorHeadCodes().split(";"))); + } + List noExistUserCode = userCodeTemp.stream().filter(b->!exsitUserCodes.contains(b)).collect(Collectors.toList()); + if(noExistUserCode.size()>0){ + a.setErrorInfo(a.getErrorInfo()+"不存在人员"+noExistUserCode.stream().collect(Collectors.joining(","))); + } + }); + } + } + /** --------------------------------------------------------------------------- **/ + + /** --------------------------------------------------------------------------- **/ + /** ------------------------------- 校验新增 ------------------------------------------------ */ + if(saveList.size()>0){ + List saveCodes = saveList.stream().filter(a->StringUtils.hasText(a.getCustomerCode())).map(a->a.getCustomerCode()).distinct().collect(Collectors.toList()); + if(saveCodes.size()>0){ + LambdaQueryWrapper checkSave = new LambdaQueryWrapper<>(); + checkSave.in(BmsSupplier::getCode,saveCodes); + List checkSaveSupplier = bmsSupplierService.list(checkSave); + if(checkSaveSupplier.size()>0){ + List existSaveCodes = checkSaveSupplier.stream().map(a->a.getCode()).collect(Collectors.toList()); + param.forEach(a->{ + if(a.getType().equals("1")){ + if(existSaveCodes.contains(a.getCustomerCode())){ + a.setErrorInfo(a.getErrorInfo()+"已存在客户编码"); + } + } + }); + } + } + } + + + /** --------------------------------------------------------------------------- **/ + + /** ------------------------------- 校验更新 ------------------------------------------------ */ + if(updateList.size()>0){ + List updateCheckCodes = updateList.stream().filter(a->StringUtils.hasText(a.getCustomerCode())).map(a->a.getCustomerCode()).distinct().collect(Collectors.toList()); + if(updateCheckCodes.size()==0){ + param.forEach(a->{ + if(a.getType().equals("2")){ + a.setErrorInfo(a.getErrorInfo()+"更新不存在客户编码;"); + } + }); + + }else{ + LambdaQueryWrapper checkUpdateCustomerCode = new LambdaQueryWrapper<>(); + checkUpdateCustomerCode.in(BmsSupplier::getCode,updateCheckCodes); + List checkUpdateSupplierList = bmsSupplierService.list(checkUpdateCustomerCode); + List existUpdateCodes = checkUpdateSupplierList.stream().map(a->a.getCode()).collect(Collectors.toList()); + param.forEach(a->{ + if(a.getType().equals("2")){ + if(!existUpdateCodes.contains(a.getCustomerCode())){ + a.setErrorInfo(a.getErrorInfo()+"更新不存在客户编码;"); + } + } + }); + } + } + /** ----------------------------------------------------------------------------------- */ + + /** ------------------------------- 删除更新 ------------------------------------------------ */ + if(deleteList.size()>0){ + List deleteCheckCodes = deleteList.stream().map(a->a.getCustomerCode()).collect(Collectors.toList()); + if(deleteCheckCodes.size()==0){ + param.forEach(a->{ + if(a.getType().equals("3")){ + a.setErrorInfo(a.getErrorInfo()+"删除不存在客户编码"); + } + }); + }else{ + LambdaQueryWrapper checkDeleteCustomerCode = new LambdaQueryWrapper<>(); + checkDeleteCustomerCode.in(BmsSupplier::getCode,deleteCheckCodes); + List checkDeleteSupplierList = bmsSupplierService.list(checkDeleteCustomerCode); + List existDeleteCodes = checkDeleteSupplierList.stream().map(a->a.getCode()).collect(Collectors.toList()); + param.forEach(a->{ + if(a.getType().equals("3")){ + if(!existDeleteCodes.contains(a.getCustomerCode())){ + a.setErrorInfo(a.getErrorInfo()+"删除不存在客户编码;"); + } + } + }); + } + } + /** ----------------------------------------------------------------------------------- */ + + /** ------------------------------- 返回错误 ------------------------------------------- */ + if(param.stream().filter(a->a.getErrorInfo().length()>0).count()>0) { + return R.ok(param, "导入失败,请打开EXCEL查询错误详情!"); + } + /** ----------------------------------------------------------------------------------- */ + + Map bizRegionMap = bizRegionList.stream().collect(Collectors.toMap(BmsRegion2::getPathNames, a->a)); + Map saleRegionMap = regionList.stream().collect(Collectors.toMap(BmsRegion::getName, a->a)); + Map sysUserMap = userList.stream().collect(Collectors.toMap(SysUser::getCode, a->a)); + + /** ------------------------------- 保存新增 ------------------------------------------------ */ + if(saveList.size()>0) { + List saveBmsSuppliers = saveList.stream().map(a->{ + BmsSupplier supplier = new BmsSupplier(); + supplier.setName(a.getCustomerName()); + supplier.setCode(a.getCustomerCode()); + supplier.setBelong(a.getAffiliatedCompany()); + supplier.setCostFlag(a.getIsExpendableExpense()); + supplier.setStopFlag(a.getCloseAccountStatus()); + supplier.setStopFlagDate(a.getCloseAccountDate()); + supplier.setCooperatePauseFlag(a.getSuspendCooperationStatus()); + supplier.setCooperatePauseFlagDate(a.getSuspendCooperationDate()); + if(StringUtils.hasText(a.getMainHeadCode())){ + SysUser sysUser = sysUserMap.get(a.getMainHeadCode()); + supplier.setUserId(sysUser.getId()); + supplier.setUserCode(sysUser.getCode()); + supplier.setUserName(sysUser.getName()); + } + + if(StringUtils.hasText(a.getMinorHeadCodes())){ + List otherUserCodes = Arrays.asList(a.getMinorHeadCodes().split(";")); + List otherUserNames = new ArrayList<>(); + List otherUserIds = new ArrayList<>(); + otherUserCodes.forEach(b->{ + SysUser sysUser = sysUserMap.get(b); + otherUserNames.add(sysUser.getName()); + otherUserIds.add(sysUser.getId()); + }); + supplier.setOtherUserCodes(otherUserCodes.stream().collect(Collectors.joining(","))); + supplier.setOtherUserNames(otherUserNames.stream().collect(Collectors.joining(","))); + supplier.setOtherUserIds(otherUserIds.toArray(new String[otherUserIds.size()])); + } + + if(StringUtils.hasText(a.getSalesRegionName())){ + BmsRegion saleRegion = saleRegionMap.get(a.getSalesRegionName()); + supplier.setRegionLast(saleRegion.getId()); + supplier.setRegionSecond(saleRegion.getId()); + supplier.setRegionFirst(saleRegion.getPid()); + } + + if(StringUtils.hasText(a.getSecondaryLevelAdministrativeRegionName()) + && StringUtils.hasText(a.getFirstLevelAdministrativeRegionName()) + && StringUtils.hasText(a.getAdministrativeRegionName())){ + BmsRegion2 bizRegion = bizRegionMap.get(a.getFirstLevelAdministrativeRegionName()+"_"+a.getSecondaryLevelAdministrativeRegionName()+"_"+a.getAdministrativeRegionName()); + supplier.setRegion2Third(bizRegion.getId()); + supplier.setRegion2Second(bizRegion.getPid()); + String firstRegion2Id = bizRegion.getPathIds().split("_")[0]; + supplier.setRegion2First(firstRegion2Id); + supplier.setRegion2Last(bizRegion.getId()); + } + + return supplier; + }).collect(Collectors.toList()); + bmsSupplierService.saveBatch(saveBmsSuppliers); + } + /** ----------------------------------------------------------------------------------- */ + /** ------------------------------- 保存更新 ------------------------------------------------ */ + if(updateList.size()>0) { + LambdaQueryWrapper oriBmsLqw = new LambdaQueryWrapper<>(); + oriBmsLqw.in(BmsSupplier::getCode,updateList.stream().map(b->b.getCustomerCode()).collect(Collectors.toList())); + List oriBmsSuppliers = bmsSupplierService.list(oriBmsLqw); + Map codeToSupplierId = oriBmsSuppliers.stream().collect(Collectors.toMap(BmsSupplier::getCode, BmsSupplier::getId)); + + List updateBmsSuppliers = updateList.stream().map(a->{ + BmsSupplier supplier = new BmsSupplier(); + + supplier.setId(codeToSupplierId.get(a.getCustomerCode())); + supplier.setName(a.getCustomerName()); + supplier.setCode(a.getCustomerCode()); + supplier.setCostFlag(a.getIsExpendableExpense()); + supplier.setBelong(a.getAffiliatedCompany()); + supplier.setStopFlag(a.getCloseAccountStatus()); + supplier.setStopFlagDate(a.getCloseAccountDate()); + supplier.setCooperatePauseFlag(a.getSuspendCooperationStatus()); + supplier.setCooperatePauseFlagDate(a.getSuspendCooperationDate()); + if(StringUtils.hasText(a.getMainHeadCode())){ + SysUser sysUser = sysUserMap.get(a.getMainHeadCode()); + supplier.setUserId(sysUser.getId()); + supplier.setUserCode(sysUser.getCode()); + supplier.setUserName(sysUser.getName()); + } + + if(StringUtils.hasText(a.getMinorHeadCodes())){ + List otherUserCodes = Arrays.asList(a.getMinorHeadCodes().split(";")); + List otherUserNames = new ArrayList<>(); + List otherUserIds = new ArrayList<>(); + otherUserCodes.forEach(b->{ + SysUser sysUser = sysUserMap.get(b); + otherUserNames.add(sysUser.getName()); + otherUserIds.add(sysUser.getId()); + }); + supplier.setOtherUserCodes(otherUserCodes.stream().collect(Collectors.joining(","))); + supplier.setOtherUserNames(otherUserNames.stream().collect(Collectors.joining(","))); + supplier.setOtherUserIds(otherUserIds.toArray(new String[otherUserIds.size()])); + } + + if(StringUtils.hasText(a.getSalesRegionName())){ + BmsRegion saleRegion = saleRegionMap.get(a.getSalesRegionName()); + supplier.setRegionLast(saleRegion.getId()); + supplier.setRegionSecond(saleRegion.getId()); + supplier.setRegionFirst(saleRegion.getPid()); + } + + if(StringUtils.hasText(a.getSecondaryLevelAdministrativeRegionName()) + && StringUtils.hasText(a.getFirstLevelAdministrativeRegionName()) + && StringUtils.hasText(a.getAdministrativeRegionName())){ + BmsRegion2 bizRegion = bizRegionMap.get(a.getFirstLevelAdministrativeRegionName()+"_"+a.getSecondaryLevelAdministrativeRegionName()+"_"+a.getAdministrativeRegionName()); + supplier.setRegion2Third(bizRegion.getId()); + supplier.setRegion2Second(bizRegion.getPid()); + String firstRegion2Id = bizRegion.getPathIds().split("_")[0]; + supplier.setRegion2First(firstRegion2Id); + supplier.setRegion2Last(bizRegion.getId()); + } + + return supplier; + }).collect(Collectors.toList()); + bmsSupplierService.updateBatchById(updateBmsSuppliers); + + List updateBmsSuppliersUserNullById = updateList.stream() + .filter(a->!StringUtils.hasText(a.getMainHeadCode())) + .map(a-> codeToSupplierId.get(a.getCustomerCode())).collect(Collectors.toList()); + + if(updateBmsSuppliersUserNullById.size()>0){ + bmsSupplierService.updateBmsSuppliersUserNullById(updateBmsSuppliersUserNullById); + } + + List updateBmsSuppliersOtherUserNullById = updateList.stream() + .filter(a->!StringUtils.hasText(a.getMinorHeadCodes())) + .map(a-> codeToSupplierId.get(a.getCustomerCode())).collect(Collectors.toList()); + + if(updateBmsSuppliersOtherUserNullById.size()>0){ + bmsSupplierService.updateBmsSuppliersOtherUserNullById(updateBmsSuppliersOtherUserNullById); + } + } + /** ----------------------------------------------------------------------------------- */ + /** ------------------------------- 删除数据 ------------------------------------------------ */ + if(deleteList.size()>0) { + LambdaQueryWrapper oriBmsLqw = new LambdaQueryWrapper<>(); + oriBmsLqw.in(BmsSupplier::getCode,deleteList.stream().map(b->b.getCustomerCode()).collect(Collectors.toList())); + List oriBmsSuppliers = bmsSupplierService.list(oriBmsLqw); +// Map codeToSupplierId = oriBmsSuppliers.stream().collect(Collectors.toMap(BmsSupplier::getCode, BmsSupplier::getId)); + List deleteIds = oriBmsSuppliers.stream().map(a->a.getId()).collect(Collectors.toList()); + bmsSupplierService.removeBatchByIds(deleteIds); + } + /** ----------------------------------------------------------------------------------- */ + + return R.ok(); + } + + + /** + * 直属负责人,直接添加到his表 + * @param userIds + * @param supplier + */ + private void saveTOHis(List userIds, BmsSupplier supplier) { + //直属负责人,直接添加到his表 + for (String userId : userIds) { + HisUserSupplier hisUserSupplier = new HisUserSupplier(); + hisUserSupplier.setUserId(userId); + hisUserSupplier.setSupplierId(Long.parseLong(supplier.getId())); + hisUserSupplier.setSupplierName(supplier.getName()); + hisUserSupplier.setSupplierCode(supplier.getCode()); + hisUserSupplier.setType(2); + hisUserSupplier.setSaleRegionFirst(supplier.getRegionFirst()); + hisUserSupplier.setSaleRegionSecond(supplier.getRegionSecond()); + hisUserSupplier.setSaleRegionThird(supplier.getRegionThird()); + hisUserSupplier.setSaleRegionFourthly(supplier.getRegionFourthly()); + hisUserSupplier.setMasterFlag(1); + hisUserSupplier.setCreateTime(LocalDateTime.now()); + hisUserSupplier.setBizRegionFirst(supplier.getRegion2First()); + hisUserSupplier.setBizRegionSecond(supplier.getRegion2Second()); + hisUserSupplier.setBizRegionThird(supplier.getRegion2Third()); + hisUserSupplier.setBizRegionFourthly(supplier.getRegion2Fourthly()); + hisUserSupplier.setCostFlag(supplier.getCostFlag()); + hisUserSupplier.setSupplierFlag(supplier.getSupplierFlag()); + hisUserSupplier.setStopFlag(supplier.getStopFlag()); + hisUserSupplier.setStopFlagDate(supplier.getStopFlagDate()); + hisUserSupplier.setCooperatePauseFlag(supplier.getCooperatePauseFlag()); + hisUserSupplier.setCooperatePauseFlagDate(supplier.getCooperatePauseFlagDate()); + hisUserSupplierService.save(hisUserSupplier); + } + } + + + + +} +