| | |
| | | package com.ruoyi.production.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.pojo.Customer; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.production.dto.ProductProcessDto; |
| | | import com.ruoyi.production.dto.SysDeptAndUserDto; |
| | | import com.ruoyi.production.mapper.ProcessRouteItemMapper; |
| | | import com.ruoyi.production.mapper.ProductProcessMapper; |
| | | import com.ruoyi.production.mapper.ProductProcessRouteItemMapper; |
| | | import com.ruoyi.production.pojo.ProcessRouteItem; |
| | | import com.ruoyi.production.pojo.ProductProcess; |
| | | import com.ruoyi.production.pojo.ProductProcessRouteItem; |
| | | import com.ruoyi.production.service.ProductProcessService; |
| | | import com.ruoyi.project.system.domain.SysDept; |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.project.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.project.system.mapper.SysUserDeptMapper; |
| | | import com.ruoyi.project.system.mapper.SysUserMapper; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | public class ProductProcessServiceImpl extends ServiceImpl<ProductProcessMapper, ProductProcess> implements ProductProcessService { |
| | | @Autowired |
| | | private ProductProcessMapper productProcessMapper; |
| | | @Autowired |
| | | private ProcessRouteItemMapper processRouteItemMapper; |
| | | @Autowired |
| | | private ProductProcessRouteItemMapper productProcessRouteItemMapper; |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | | @Autowired |
| | | private SysUserDeptMapper sysUserDeptMapper; |
| | | @Autowired |
| | | private SysUserMapper sysUserMapper; |
| | | |
| | | @Override |
| | | public IPage<ProductProcessDto> listPage(Page page, ProductProcessDto productProcessDto) { |
| | |
| | | ProductProcess productProcess = new ProductProcess(); |
| | | BeanUtils.copyProperties(productProcessDto,productProcess); |
| | | boolean save = productProcessMapper.insert(productProcess) > 0; |
| | | if (save) { |
| | | if (save && ObjectUtils.isNull(productProcessDto.getNo())) { |
| | | // 根据id生成no字段:GX + 8位数字(不足8位前面补0) |
| | | String no = "GX" + String.format("%08d", productProcess.getId()); |
| | | productProcess.setNo(no); |
| | |
| | | productProcessMapper.updateById(productProcess); |
| | | return AjaxResult.success(); |
| | | } |
| | | return AjaxResult.error(); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | @Override |
| | | public AjaxResult importData(MultipartFile file) { |
| | | try { |
| | | ExcelUtil<ProductProcess> util = new ExcelUtil<ProductProcess>(ProductProcess.class); |
| | | List<ProductProcess> productProcessList = util.importExcel(file.getInputStream()); |
| | | if(CollectionUtils.isEmpty(productProcessList)){ |
| | | return AjaxResult.warn("模板错误或导入数据为空"); |
| | | } |
| | | productProcessList.forEach(productProcess -> { |
| | | if (ObjectUtils.isEmpty(productProcess)) { |
| | | throw new RuntimeException("使用模板进行导入"); |
| | | } |
| | | if (ObjectUtils.isEmpty(productProcess.getName())) { |
| | | throw new RuntimeException("工序名称不能为空"); |
| | | } |
| | | }); |
| | | this.saveOrUpdateBatch(productProcessList); |
| | | return AjaxResult.success(true); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return AjaxResult.error(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public String batchDelete(List<Integer> ids) { |
| | | //查询是否生产中已经引用了这些工序 |
| | | List<ProcessRouteItem> processRouteItems = processRouteItemMapper.selectList(Wrappers.<ProcessRouteItem>lambdaQuery().in(ProcessRouteItem::getProcessId, ids)); |
| | | List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList(Wrappers.<ProductProcessRouteItem>lambdaQuery().in(ProductProcessRouteItem::getProcessId, ids)); |
| | | if (!CollectionUtils.isEmpty(processRouteItems) || !CollectionUtils.isEmpty(productProcessRouteItems)){ |
| | | throw new RuntimeException("该工序已经被使用,无法删除"); |
| | | } |
| | | productProcessMapper.deleteBatchIds(ids); |
| | | return null; |
| | | } |
| | | |
| | | public List<SysDeptAndUserDto> listDeptUserTree() { |
| | | return buildDeptUserTree(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 构建完整的部门-用户树形结构 |
| | | */ |
| | | public List<SysDeptAndUserDto> buildDeptUserTree() { |
| | | // 1. 查询所有根部门(parent_id = 0) |
| | | List<SysDeptAndUserDto> rootDeptList = sysDeptMapper.selectChildrenDeptByParentId(0L); |
| | | |
| | | // 2. 递归构建每个根部门的子节点,并挂载用户 |
| | | for (SysDeptAndUserDto rootDept : rootDeptList) { |
| | | buildChildrenDept(rootDept); |
| | | loadDeptUser(rootDept); |
| | | } |
| | | |
| | | return rootDeptList; |
| | | } |
| | | |
| | | /** |
| | | * 递归构建子部门 |
| | | */ |
| | | private void buildChildrenDept(SysDeptAndUserDto parentDept) { |
| | | // 查询当前部门的子部门 |
| | | List<SysDeptAndUserDto> childrenDept = sysDeptMapper.selectChildrenDeptByParentId(parentDept.getDeptId()); |
| | | if (childrenDept == null || childrenDept.isEmpty()) { |
| | | parentDept.setChildrenList(new ArrayList<>()); |
| | | return; |
| | | } |
| | | |
| | | // 为每个子部门递归构建下级,并挂载用户 |
| | | for (SysDeptAndUserDto childDept : childrenDept) { |
| | | buildChildrenDept(childDept); |
| | | loadDeptUser(childDept); |
| | | } |
| | | |
| | | parentDept.setChildrenList(childrenDept); |
| | | } |
| | | |
| | | /** |
| | | * 为部门加载关联的用户 |
| | | */ |
| | | private void loadDeptUser(SysDeptAndUserDto dept) { |
| | | // 查询部门关联的用户ID |
| | | List<Long> userIdList = sysUserDeptMapper.selectUserIdByDeptId(dept.getDeptId()); |
| | | if (userIdList == null || userIdList.isEmpty()) { |
| | | dept.setUserList(new ArrayList<>()); |
| | | return; |
| | | } |
| | | |
| | | // 根据用户ID查询用户信息 |
| | | List<SysUser> userList = sysUserMapper.selectUserListByIds(userIdList); |
| | | dept.setUserList(userList); |
| | | } |
| | | } |