| | |
| | | package com.example.erp.service.sd; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.parser.Feature; |
| | | import com.baomidou.dynamic.datasource.annotation.DS; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.example.erp.common.Constants; |
| | | import com.example.erp.entity.sd.Product; |
| | | import com.example.erp.entity.sd.ProductDetail; |
| | | import com.example.erp.exception.ServiceException; |
| | | import com.example.erp.mapper.sd.ProductDetailMapper; |
| | | import com.example.erp.mapper.sd.ProductMapper; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.List; |
| | | import java.text.DecimalFormat; |
| | | import java.util.*; |
| | | |
| | | @Service |
| | | @DS("erp_sd") |
| | | @DS("sd") |
| | | @Transactional |
| | | public class ProductService { |
| | | @Autowired |
| | | private final |
| | | ProductMapper productMapper; |
| | | public List<Product> defaultDateProduct() { |
| | | return productMapper.defaultProduct(); |
| | | private final |
| | | ProductDetailMapper productDetailMapper; |
| | | |
| | | public ProductService(ProductMapper productMapper, ProductDetailMapper productDetailMapper) { |
| | | this.productMapper = productMapper; |
| | | this.productDetailMapper = productDetailMapper; |
| | | } |
| | | //产品查询方法 |
| | | public Map<String,Object> defaultDateProduct(Integer pageNum, Integer pageSize, List<String> glassType, Product product) { |
| | | Integer offset = (pageNum-1)*pageSize; |
| | | String glassTypeId = null; |
| | | if(glassType.size()>1){ |
| | | glassTypeId = glassType.get(1); |
| | | } |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("data",productMapper.defaultProduct(offset,pageSize,glassTypeId,product)); |
| | | map.put("total",productMapper.getPageTotal(offset,pageSize,glassTypeId,product)); |
| | | return map; |
| | | } |
| | | |
| | | //产品保存方法 |
| | | public boolean saveProduct(Map<String,Object> productObject) { |
| | | //把传入主附表的object类型转换成实体类类型 |
| | | JSONObject productJson = new JSONObject(productObject); |
| | | Product product = JSONObject.parseObject(JSONObject.toJSONString(productJson.get("title")), Product.class); |
| | | List<ProductDetail> productDetails = JSONArray.parseArray(JSONObject.toJSONString(productJson.get("detail")), ProductDetail.class); |
| | | //完善主附表信息并返回 |
| | | Map<String,Object> getProductJson = updateProduct(productDetails,product); |
| | | Product getProduct = JSONObject.parseObject(JSONObject.toJSONString(getProductJson.get("title")), Product.class); |
| | | List<ProductDetail> getProductDetails = JSONArray.parseArray(JSONObject.toJSONString(getProductJson.get("detail")), ProductDetail.class); |
| | | |
| | | if(getProduct.getId()==null ){ |
| | | insertProduct(getProduct,getProductDetails); |
| | | }else{ |
| | | updateProductData(getProductDetails,getProduct); |
| | | } |
| | | return true; |
| | | |
| | | } |
| | | |
| | | private void updateProductData(List<ProductDetail> getProductDetails, Product getProduct) { |
| | | productMapper.update(getProduct,new UpdateWrapper<Product>().eq("id",getProduct.getId())); |
| | | productDetailMapper.delete(new QueryWrapper<ProductDetail>().eq("prod_id",getProduct.getId())); |
| | | for (ProductDetail getProductDetail : getProductDetails) { |
| | | getProductDetail.setProdId(getProduct.getId()); |
| | | } |
| | | productDetailMapper.insertList(getProductDetails); |
| | | } |
| | | |
| | | private void insertProduct(Product getProduct, List<ProductDetail> getProductDetails) { |
| | | //插入主表数据 |
| | | productMapper.insert(getProduct); |
| | | //给副表添加产品id |
| | | for (ProductDetail getProductDetail : getProductDetails) { |
| | | getProductDetail.setProdId(getProduct.getId()); |
| | | } |
| | | //插入副表 |
| | | productDetailMapper.insertList(getProductDetails); |
| | | } |
| | | |
| | | private Map<String,Object> updateProduct(List<ProductDetail> productDetailList,Product product) { |
| | | Map<String,Object> map = new HashMap<>(); |
| | | //定义产品玻璃厚度累加 |
| | | double thickness1 = 0; |
| | | //定义产品总厚度累加 |
| | | double totalThickness = 0; |
| | | //定义快速查询 |
| | | StringBuilder query = new StringBuilder(); |
| | | |
| | | //定义副表玻璃与间隔物排序 |
| | | int sortNum = 0; |
| | | //定义副表玻璃排序 |
| | | int glassSort = 0; |
| | | //定义副表中空分组 |
| | | int glassGroup = 1; |
| | | |
| | | for (ProductDetail productDetail : productDetailList) { |
| | | sortNum+=1; |
| | | productDetail.setSortNum(sortNum); |
| | | JSONObject separation = JSONObject.parseObject(productDetail.getSeparation(), Feature.OrderedField); |
| | | String getThickness = (String) separation.get("thickness"); |
| | | double thicknessDetail = Double.parseDouble(getThickness.replace("mm","")); |
| | | totalThickness += thicknessDetail; |
| | | |
| | | if(Objects.equals(productDetail.getDetailType(), "glass")){ |
| | | glassSort+=1; |
| | | productDetail.setGlassSort(glassSort); |
| | | productDetail.setGlassGroup(glassGroup); |
| | | thickness1 += thicknessDetail; |
| | | DecimalFormat decimalFormat = new DecimalFormat("###"); |
| | | query.append(decimalFormat.format(thicknessDetail)); |
| | | } |
| | | else if (Objects.equals(productDetail.getDetailType(), "Interlayer")) { |
| | | query.append("+"); |
| | | }else if(Objects.equals(productDetail.getDetailType(), "hollow")){ |
| | | glassGroup+=1; |
| | | query.append("*"); |
| | | } |
| | | } |
| | | product.setThickness(thickness1); |
| | | product.setTotalThickness(totalThickness); |
| | | product.setQuery(String.valueOf(query)); |
| | | map.put("title",product); |
| | | map.put("detail",productDetailList); |
| | | return map; |
| | | } |
| | | |
| | | public boolean deleteProductById(Integer id) { |
| | | Product product = productMapper.selectById(id); |
| | | if(product.getState()==1){ |
| | | throw new ServiceException(Constants.Code_600,"产品已审核,无法删除"); |
| | | } |
| | | productMapper.deleteById(id); |
| | | return true; |
| | | } |
| | | |
| | | public boolean updateProductStateById(Integer id,Integer state) { |
| | | return productMapper.updateProductStateById(id,state); |
| | | } |
| | | |
| | | public Map<String,Object> selectProductById(Integer id) { |
| | | Product product = productMapper.selectById(id); |
| | | List<ProductDetail> productDetailList = productDetailMapper.selectList( |
| | | new QueryWrapper<ProductDetail>().eq("prod_id", id)); |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("title",product); |
| | | map.put("detail",productDetailList); |
| | | return map; |
| | | } |
| | | } |