package com.tld.service.impl; import com.alibaba.fastjson.JSON; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.tld.mapper.DictionaryMapper; import com.tld.model.Access; import com.tld.model.Goods; import com.tld.service.DictionaryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.tld.model.Dictionary; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class DictionaryServiceImpl implements DictionaryService { @Autowired private DictionaryMapper dictionaryMapper; @Override @Transactional(rollbackFor = Exception.class) public Map addDictionary(List dictionarys) { Map map = new HashMap<>(); try{ for(Dictionary dictionary : dictionarys) { String tableName = dictionaryMapper.getTableName(dictionary.getType()); if ("null".equals(tableName)) { map.put("msg", "500"); map.put("errMsg", "失败,请联系是否存在此字典"); return map; } dictionary.setTableName(tableName); dictionary.setTypeVal("0"); if (dictionary.getType().equals("物料字典")) { dictionaryMapper.addMaterial(dictionary); } else if (dictionary.getType().equals("库账对存")) { dictionaryMapper.addTreasuryAccount(dictionary); } else { dictionaryMapper.addDictionary(dictionary); } } //新增日志 Access access = new Access().setType("字典信息").setData(JSON.toJSONString(dictionarys)).setAccessType("0"); dictionaryMapper.addAccess(access); map.put("msg", "200"); } catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "失败"); } return map; } @Override public Map getDictionary(Dictionary dictionary) { Map map = new HashMap<>(); try{ String tableName = dictionaryMapper.getTableName(dictionary.getType()); dictionary.setTableName(tableName); List list = dictionaryMapper.getDictionary(dictionary); map.put("msg", "200"); map.put("data", list); } catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "失败"); } return map; } @Override public Map getDictionaryPage(Dictionary dictionary) { Map map = new HashMap<>(); try{ Map amountMap = new HashMap<>(); String tableName = dictionaryMapper.getTableName(dictionary.getType()); dictionary.setTableName(tableName); PageHelper.startPage(dictionary.getPage(), dictionary.getLimit()); PageInfo list = null; if(dictionary.getType().equals("物料字典")){ list = new PageInfo<>(dictionaryMapper.getDictionaryMaterialPage(dictionary)); for(Dictionary dictionary1 : list.getList()){ int amount = dictionaryMapper.getInventorySumAmount(dictionary1); amountMap.put(dictionary1.getTldId(), amount); } map.put("amount", amountMap); } else { list = new PageInfo<>(dictionaryMapper.getDictionaryPage(dictionary)); } map.put("msg", "200"); map.put("data", list); } catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "失败"); } return map; } @Override public Map updateDictionary(Dictionary dictionary, HttpServletRequest request) { Map map = new HashMap<>(); try{ String tableName = dictionaryMapper.getTableName(dictionary.getType()); if ("null".equals(tableName)) { map.put("msg", "500"); map.put("errMsg", "失败,请联系是否存在此字典"); return map; } dictionary.setTableName(tableName); dictionary.setModifyUser(request.getHeader("userId")); if (dictionary.getType().equals("物料字典")) { dictionaryMapper.updateMaterial(dictionary); } else if (dictionary.getType().equals("库账对存")) { dictionaryMapper.updateTreasuryAccount(dictionary); } else { dictionaryMapper.updateDictionary(dictionary); } map.put("msg", "200"); } catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "失败"); } return map; } @Override public Map deleteDictionary(Dictionary dictionary) { Map map = new HashMap<>(); try{ String tableName = dictionaryMapper.getTableName(dictionary.getType()); if ("null".equals(tableName)) { map.put("msg", "500"); map.put("errMsg", "失败,请联系是否存在此字典"); return map; } dictionary.setTableName(tableName); dictionaryMapper.deleteDictionary(dictionary); map.put("msg", "200"); } catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "失败"); } return map; } @Override public Map addDiction(Dictionary dictionary, HttpServletRequest request) { Map map = new HashMap<>(); try{ String tableName = dictionaryMapper.getTableName(dictionary.getType()); if ("null".equals(tableName)) { map.put("msg", "500"); map.put("errMsg", "失败,请联系是否存在此字典"); return map; } dictionary.setTableName(tableName); dictionary.setTypeVal("1"); if (dictionary.getType().equals("物料字典")) { dictionaryMapper.addMaterial(dictionary); } else if (dictionary.getType().equals("库账对存")) { dictionaryMapper.addTreasuryAccount(dictionary); } else { dictionaryMapper.addDictionary(dictionary); } map.put("msg", "200"); } catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "失败"); } return map; } }