package com.huaheng.api.general.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.huaheng.common.exception.service.ServiceException; import com.huaheng.common.utils.StringUtils; import com.huaheng.framework.web.domain.AjaxResult; import com.huaheng.pc.config.company.domain.Company; import com.huaheng.pc.config.company.service.CompanyService; import com.huaheng.pc.config.customer.domain.Customer; import com.huaheng.pc.config.customer.service.CustomerServiceImpl; import com.huaheng.pc.config.material.domain.Material; import com.huaheng.pc.config.material.service.MaterialService; import com.huaheng.pc.config.supplier.domain.Supplier; import com.huaheng.pc.config.supplier.service.SupplierService; import com.huaheng.pc.config.warehouse.domain.Warehouse; import com.huaheng.pc.config.warehouse.service.WarehouseService; import com.huaheng.pc.system.dept.domain.Dept; import com.huaheng.pc.system.dept.service.IDeptService; import com.huaheng.pc.system.dict.domain.DictData; import com.huaheng.pc.system.dict.domain.DictType; import com.huaheng.pc.system.dict.mapper.DictDataMapper; import com.huaheng.pc.system.dict.mapper.DictTypeMapper; import com.huaheng.pc.system.dict.service.IDictDataService; import com.huaheng.pc.system.dict.service.IDictTypeService; import com.huaheng.pc.system.user.domain.User; import com.huaheng.pc.system.user.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; @Component @Transactional public class BasicDataApiService { @Autowired IDictTypeService dictTypeService; @Autowired IDictDataService dictDataService; @Autowired MaterialService materialService; @Resource private DictTypeMapper dictTypeMapper; @Resource private DictDataMapper dictDataMapper; @Autowired IUserService iUserService; @Autowired IDeptService iDeptService; @Autowired CustomerServiceImpl iCustomerService; @Autowired WarehouseService iWarehouseService; @Autowired SupplierService iSupplierService; @Autowired CompanyService companyService; //检查仓库 public Warehouse checkWarehouse(String code) { LambdaQueryWrapper<Warehouse> warehouseLam = Wrappers.lambdaQuery(); warehouseLam.eq(Warehouse::getCode, code); Warehouse warehouse = iWarehouseService.getOne(warehouseLam); if (warehouse == null) { throw new ServiceException("数据出现问题,系统没有该仓库"); } return warehouse; } /** * 字典通用接口 * 1、判断必填字段是否为空 * 2、仓库是否正确 * 3、检查字典头表,如果没有就添加 * 4、增新一条字典明细表的记录 * @param dictData * @return */ public AjaxResult dict(DictData dictData) { //1、判断必填字段是否为空 if(StringUtils.isEmpty(dictData.getWarehouseCode())){ return AjaxResult.error("没有仓库编码"); } if(StringUtils.isEmpty(dictData.getDictLabel())){ return AjaxResult.error("没有字典标签"); } if(StringUtils.isEmpty(dictData.getDictType())){ return AjaxResult.error("没有字典类型"); } if(StringUtils.isEmpty(dictData.getDictValue())){ return AjaxResult.error("没有字典键值"); } //2、仓库是否正确 int result = 0; this.checkWarehouse(dictData.getWarehouseCode()); //3、检查字典头表,如果没有就添加 DictData condition = new DictData(); try { condition.setDictType(dictData.getDictType()); condition.setWarehouseCode(dictData.getWarehouseCode()); condition.setDictValue(dictData.getDictValue()); condition.setEnable(true); List<DictData> dictDatas = dictDataService.selectDictDataList(dictData); if (dictDatas.size() < 1) { //找出字典头表的id DictType dictType = new DictType(); dictType.setWarehouseCode(dictData.getWarehouseCode()); dictType.setDictType(dictData.getDictType()); List<DictType> dictList = dictTypeService.selectDictTypeList(dictType); if (dictList.size() < 1) { dictType.setWarehouseId(dictData.getWarehouseId()); dictType.setCreateBy(dictData.getCreateBy()); dictType.setCreateTime(dictData.getCreateTime()); if(StringUtils.isEmpty(dictType.getCreateBy())) { result = dictTypeService.insertDictType(dictType); }else { result=dictTypeMapper.insertDictType(dictType); } dictList.add(dictType); if (result < 1) { throw new ServiceException("新增字典类型失败!"); } } //4、增新一条字典明细表的记录 dictData.setHeaderId(dictList.get(0).getId()); if(StringUtils.isEmpty(dictData.getCreateBy())) { result = dictDataService.insertDictData(dictData); }else { result=dictDataMapper.insertDictData(dictData); } if (result < 1) { throw new ServiceException("新增字典数据失败!"); } } }catch (Exception e){ throw new ServiceException("字典数据有误!!"); } return AjaxResult.success("新增字典成功"); } /** * 检查是否存在物料,如果存在就修改,不存在就新增 * 1、判断必填字段是否为空 * 2、检查仓库和货主 * 3、查看此物料在系统是否存在 * @param material * @return */ @Transactional public AjaxResult material(Material material) { //1、判断必填字段是否为空 if(StringUtils.isEmpty(material.getCode())){ return AjaxResult.error("物料编码不能为空!!"); } if(StringUtils.isEmpty(material.getWarehouseCode())){ return AjaxResult.error("仓库编码不能为空!!"); } if(StringUtils.isEmpty(material.getCompanyCode())){ return AjaxResult.error("货主编码不能为空!!"); } if(StringUtils.isEmpty(material.getType())){ return AjaxResult.error("物料类型不能为空!!"); } if(StringUtils.isEmpty(material.getName())){ return AjaxResult.error("物料名称不能为空!!"); } if(StringUtils.isEmpty(material.getSpec())){ return AjaxResult.error("物料规格不能为空!!"); } if(StringUtils.isEmpty(material.getUnit())){ return AjaxResult.error("物料单位不能为空!!"); } //2、检查仓库和货主 this.checkWarehouse(material.getWarehouseCode()); LambdaQueryWrapper<Company> companyLam = Wrappers.lambdaQuery(); companyLam.eq(Company::getCode,material.getCompanyCode()); Company company=companyService.getOne(companyLam); if(company==null){ return AjaxResult.error("没有该货主!!"); } Boolean flag = false; try { //3、查看此物料在系统是否存在 LambdaQueryWrapper<Material> materialLam = Wrappers.lambdaQuery(); materialLam.eq(Material::getCode,material.getCode()) .eq(Material::getWarehouseCode,material.getWarehouseCode()); Material entity = materialService.getOne(materialLam); if (entity == null) { flag = materialService.save(material); if (flag == false) { throw new ServiceException("新增物料失败!"); } } else { // return AjaxResult.error("已有该物料,无法进行修改!!"); material.setId(entity.getId()); flag = materialService.updateById(material); if (flag == false) { throw new ServiceException("更新物料失败!"); }else { return AjaxResult.success("更新物流成功!"); } } }catch (Exception e){ throw new ServiceException("物料数据问题"); } return AjaxResult.success("新增物料成功"); } /** * 人员档案通用接口 * 1、判断必填字段是否为空 * 2、判断系统中是否有该用户,如果有则更新,如果没有则新增 *新增: (1)、默认密码为123456 * (2)默认为普通用户 * (3)默认为长沙仓库 * @param user * @return */ public AjaxResult user(User user){ //1、判断必填字段是否为空 int result = 0; User user1 = new User(); if(user.getLoginName()==null || user.getLoginName()=="") { return AjaxResult.error("没有人员编码!!"); } if (user.getUserName()==null || user.getUserName()==""){ return AjaxResult.error("没有人员名称!!"); } if(user.getDeptId()==null){ return AjaxResult.error("没有部门ID!!"); } if(iDeptService.selectDeptById(user.getDeptId())==null){ return AjaxResult.error("系统没有此部门!!"); } try { user1.setLoginName(user.getLoginName()); //2、判断系统中是否有该用户,如果有则更新,如果没有则新增 if (iUserService.selectmen(user.getLoginName()) == null) { //(1)默认密码为123456 if(user.getPassword()==null) { user.setPassword("123456"); } //(2)默认为普通用户 if(StringUtils.isEmpty(user.getRoleIds())) { List<Integer> roleIds=new ArrayList<>(); roleIds.add(2); user.setRoleIds(roleIds); } //(3)默认为长沙仓库 if(StringUtils.isEmpty(user.getCompanyIdList())) { List<Integer> companyIdList = new ArrayList<>(); companyIdList.add(2); user.setCompanyIdList(companyIdList); } result = iUserService.insertUser(user); if (result < 1) { throw new ServiceException("新增人员档案失败!"); } else { return AjaxResult.success("新增人员档案成功!"); } } else { return AjaxResult.error("已有该人员档案,无法进行修改!"); // result = iUserService.updateUser(user); // if (result < 1) { // throw new ServiceException("更新人员档案失败!"); // } else { // return AjaxResult.success("更新人员档案成功!"); // } } }catch (Exception e){ throw new ServiceException("数据问题。。。"); } } /** * 部门档案通用接口 * 1、判断必填字段是否为空 * 2、部门编码长度应是双数 * * @param dept * @return */ @Transactional public AjaxResult dept(Dept dept) { //1、判断必填字段是否为空 int result = 0; String code = dept.getCode(); if (code == null || code == "") { return AjaxResult.error("部门编码不能为空!!"); } try { Dept rs = iDeptService.selectDepts(code); //2、部门编码长度应是双数 if (rs == null) { int x = code.length() % 2; if (x != 0) { return AjaxResult.error("部门编码长度应是双数"); } else { int y = code.length() / 2; if (y >= 1) { String scode = code.substring(0, 2); if (iDeptService.selectDepts(scode) == null) { dept.setCode(scode); dept.setParentId(100); dept.setAncestors("0,100"); dept.setOrderNum("1"); result = iDeptService.insertDept(dept); if (result < 1) { throw new ServiceException("新增部门档案失败!"); } } } for (int z = 1; z <=y; z++) { //找到上级部门 String sqcode = code.substring(0, 2 * (z - 1)); Dept sdept = iDeptService.selectDepts(sqcode); String sscode = code.substring(0, 2 * z); if (iDeptService.selectDepts(sscode) == null) { dept.setCode(sscode); dept.setParentId(sdept.getId()); dept.setAncestors(sdept.getAncestors() + "," + sdept.getId()); dept.setOrderNum(String.valueOf(z)); result = iDeptService.insertDept(dept); if (result < 1) { throw new ServiceException("新增部门档案失败!"); } } } } return AjaxResult.success("新增部门成功"); } else { dept.setId(rs.getId()); int num = iDeptService.updatesDept(dept); if (num < 1) { throw new ServiceException("部门修改失败"); } else { return AjaxResult.success("部门修改成功"); } } } catch (Exception e) { throw new ServiceException("数据问题。。。"); } } /** * 客户档案通用接口 * 1、判断必填字段是否为空 * 2、检查仓库 * 3、查看此客户在系统是否存在 * @param customer * @return */ public AjaxResult customer(Customer customer){ Boolean flag = true; //1、判断必填字段是否为空 if(customer.getCode()==null||customer.getCode()=="") { return AjaxResult.error("客户代码不能为空!!"); } if(StringUtils.isEmpty(customer.getWarehouseCode())){ return AjaxResult.error("没有仓库编码"); } if(StringUtils.isEmpty(customer.getName())){ return AjaxResult.error("没有客户名称"); } if(StringUtils.isEmpty(customer.getCompanyCode())){ return AjaxResult.error("没有货主编码"); } try { //2、检查仓库 this.checkWarehouse(customer.getWarehouseCode()); //3、查看此客户在系统是否存在 LambdaQueryWrapper<Customer> customerLam = Wrappers.lambdaQuery(); customerLam.eq(Customer::getCode,customer.getCode()) .eq(Customer::getWarehouseCode,customer.getWarehouseCode()); Customer ctr = iCustomerService.getOne(customerLam); //不存在添加 if ( ctr == null) { flag = iCustomerService.save(customer); if (flag == false) { throw new ServiceException("新增客户档案失败!"); } else { return AjaxResult.success("新增客户档案成功!"); } } else { return AjaxResult.error("已有该客户,无法进行修改!!"); // customer.setId(rs.getId()); // result = iCustomerService.updateByModel(customer); // if (result < 1) { // throw new ServiceException("更新客户档案失败!"); // } else { // return AjaxResult.success("更新客户档案成功!"); // } } }catch (Exception e){ throw new ServiceException("数据问题。。。"); } } /** * 仓库档案通用接口 * 1、判断必填字段是否为空 * 2、判断系统中是否有该仓库,若有则更新,若无则新增 * @param warehouse * @return */ public AjaxResult warehouse(Warehouse warehouse){ if(warehouse.getCode()==null||warehouse.getCode()=="") { return AjaxResult.error("仓库编码不能为空!!"); } if(warehouse.getName()==null||warehouse.getName()=="") { return AjaxResult.error("仓库名称不能为空!!"); } try { LambdaQueryWrapper<Warehouse> warehouseLam = Wrappers.lambdaQuery(); warehouseLam.eq(Warehouse::getCode,warehouse.getCode()); Warehouse whs = iWarehouseService.getOne(warehouseLam); //2、判断系统中是否有该仓库,若有则更新,若无则新增 if (whs == null) { Boolean flag = iWarehouseService.save(warehouse); if (flag == false) { throw new ServiceException("新增仓库档案失败!"); } else { return AjaxResult.success("新增仓库档案成功!"); } } else { return AjaxResult.error("已有该仓库,无法进行修改!"); // warehouse.setId(rs.getId()); // result = iWarehouseService.updateByModel(warehouse); // if (result < 1) { // throw new ServiceException("更新仓库档案失败!"); // } else { // return AjaxResult.success("更新仓库档案成功!"); // } } }catch (Exception e){ throw new ServiceException("仓库数据问题。。。"); } } /** * 供应商档案通用接口 * 1、判断必填字段是否为空 * 2、检查仓库 * 3、查看此供应商在系统是否存在 * @param supplier * @return */ public AjaxResult supplier(Supplier supplier){ //1、判断必填字段是否为空 if(StringUtils.isEmpty(supplier.getCode())){ return AjaxResult.error("没有供应商代码"); } if(StringUtils.isEmpty(supplier.getName())){ return AjaxResult.error("没有供应商名称"); } if(StringUtils.isEmpty(supplier.getWarehouseCode())){ return AjaxResult.error("没有仓库编码"); } //2、检查仓库 this.checkWarehouse(supplier.getCode()); //3、查看此供应商在系统是否存在 try { LambdaQueryWrapper<Supplier> supplierLam = Wrappers.lambdaQuery(); supplierLam.eq(Supplier::getCode,supplier.getCode()) .eq(Supplier::getWarehouseCode,supplier.getWarehouseCode()); Supplier spl=iSupplierService.getOne(supplierLam); if (spl== null) { Boolean flag = iSupplierService.save(supplier); if (flag == false) { throw new ServiceException("新增供应商失败!"); } else { return AjaxResult.success("新增供应商成功!"); } } else { return AjaxResult.error("已有该供应商,无法修改!!"); } }catch (Exception e){ throw new ServiceException("供应商数据问题。。。"); } } }