|
@@ -1,325 +1,78 @@
|
|
|
package com.jsh.erp.service;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.jsh.erp.constants.BusinessConstants;
|
|
|
-import com.jsh.erp.constants.ExceptionConstants;
|
|
|
-import com.jsh.erp.datasource.entities.Material;
|
|
|
+import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
import com.jsh.erp.datasource.entities.Unit;
|
|
|
-import com.jsh.erp.datasource.entities.UnitExample;
|
|
|
-import com.jsh.erp.datasource.entities.User;
|
|
|
-import com.jsh.erp.datasource.mappers.MaterialMapperEx;
|
|
|
-import com.jsh.erp.datasource.mappers.UnitMapper;
|
|
|
-import com.jsh.erp.datasource.mappers.UnitMapperEx;
|
|
|
-import com.jsh.erp.datasource.vo.UnitListVo;
|
|
|
-import com.jsh.erp.exception.BusinessRunTimeException;
|
|
|
-import com.jsh.erp.exception.JshException;
|
|
|
-import com.jsh.erp.utils.PageUtils;
|
|
|
-import com.jsh.erp.utils.StringUtil;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.web.context.request.RequestContextHolder;
|
|
|
-import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
-@Service
|
|
|
-public class UnitService {
|
|
|
- private Logger logger = LoggerFactory.getLogger(UnitService.class);
|
|
|
+public interface UnitService extends IService<Unit> {
|
|
|
|
|
|
- @Resource
|
|
|
- private UnitMapper unitMapper;
|
|
|
+ Unit getUnit(long id);
|
|
|
|
|
|
- @Resource
|
|
|
- private UnitMapperEx unitMapperEx;
|
|
|
- @Resource
|
|
|
- private UserService userService;
|
|
|
- @Resource
|
|
|
- private LogService logService;
|
|
|
- @Resource
|
|
|
- private MaterialMapperEx materialMapperEx;
|
|
|
-
|
|
|
- public Unit getUnit(long id)throws Exception {
|
|
|
- Unit result=null;
|
|
|
- try{
|
|
|
- result=unitMapper.selectByPrimaryKey(id);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.readFail(logger, e);
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public List<Unit> getUnitListByIds(String ids)throws Exception {
|
|
|
- List<Long> idList = StringUtil.strToLongList(ids);
|
|
|
- List<Unit> list = new ArrayList<>();
|
|
|
- try{
|
|
|
- UnitExample example = new UnitExample();
|
|
|
- example.createCriteria().andIdIn(idList);
|
|
|
- list = unitMapper.selectByExample(example);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.readFail(logger, e);
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- public List<Unit> getUnit()throws Exception {
|
|
|
- UnitExample example = new UnitExample();
|
|
|
- example.createCriteria().andEnabledEqualTo(true).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
|
|
|
- List<Unit> list=null;
|
|
|
- try{
|
|
|
- list=unitMapper.selectByExample(example);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.readFail(logger, e);
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- public List<Unit> select(String name)throws Exception {
|
|
|
- List<Unit> list=null;
|
|
|
- try{
|
|
|
- PageUtils.startPage();
|
|
|
- list=unitMapperEx.selectByConditionUnit(name);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.readFail(logger, e);
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
- public int insertUnit(JSONObject obj, HttpServletRequest request)throws Exception {
|
|
|
- Unit unit = JSONObject.parseObject(obj.toJSONString(), Unit.class);
|
|
|
- int result=0;
|
|
|
- try{
|
|
|
- parseNameByUnit(unit);
|
|
|
- unit.setEnabled(true);
|
|
|
- result=unitMapper.insertSelective(unit);
|
|
|
- logService.insertLog("多单位",
|
|
|
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(unit.getName()).toString(), request);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.writeFail(logger, e);
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
- public int updateUnit(JSONObject obj, HttpServletRequest request)throws Exception {
|
|
|
- Unit unit = JSONObject.parseObject(obj.toJSONString(), Unit.class);
|
|
|
- int result=0;
|
|
|
- try{
|
|
|
- parseNameByUnit(unit);
|
|
|
- result=unitMapper.updateByPrimaryKeySelective(unit);
|
|
|
- if(unit.getRatioTwo()==null) {
|
|
|
- unitMapperEx.updateRatioTwoById(unit.getId());
|
|
|
- }
|
|
|
- if(unit.getRatioThree()==null) {
|
|
|
- unitMapperEx.updateRatioThreeById(unit.getId());
|
|
|
- }
|
|
|
- logService.insertLog("多单位",
|
|
|
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(unit.getName()).toString(), request);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.writeFail(logger, e);
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
+ List<Unit> getUnitListByIds(String ids)throws Exception;
|
|
|
|
|
|
/**
|
|
|
- * 根据单位信息生成名称的格式
|
|
|
- * @param unit
|
|
|
+ * 所有单位集合
|
|
|
*/
|
|
|
- private void parseNameByUnit(Unit unit) {
|
|
|
- String unitName = unit.getBasicUnit() + "/" + "(" + unit.getOtherUnit() + "=" + unit.getRatio().toString() + unit.getBasicUnit() + ")";
|
|
|
- if(StringUtil.isNotEmpty(unit.getOtherUnitTwo()) && unit.getRatioTwo()!=null) {
|
|
|
- unitName += "/" + "(" + unit.getOtherUnitTwo() + "=" + unit.getRatioTwo().toString() + unit.getBasicUnit() + ")";
|
|
|
- if(StringUtil.isNotEmpty(unit.getOtherUnitThree()) && unit.getRatioThree()!=null) {
|
|
|
- unitName += "/" + "(" + unit.getOtherUnitThree() + "=" + unit.getRatioThree().toString() + unit.getBasicUnit() + ")";
|
|
|
- }
|
|
|
- }
|
|
|
- unit.setName(unitName);
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
- public int deleteUnit(Long id, HttpServletRequest request)throws Exception {
|
|
|
- return batchDeleteUnitByIds(id.toString());
|
|
|
- }
|
|
|
+ List<Unit> unitList()throws Exception;
|
|
|
|
|
|
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
- public int batchDeleteUnit(String ids, HttpServletRequest request) throws Exception{
|
|
|
- return batchDeleteUnitByIds(ids);
|
|
|
- }
|
|
|
+ List<Unit> select(String name)throws Exception;
|
|
|
|
|
|
+ /**
|
|
|
+ * 新增单位
|
|
|
+ * @param unit 单位
|
|
|
+ */
|
|
|
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
- public int batchDeleteUnitByIds(String ids)throws Exception {
|
|
|
- int result=0;
|
|
|
- String [] idArray=ids.split(",");
|
|
|
- //校验产品表 jsh_material
|
|
|
- List<Material> materialList=null;
|
|
|
- try{
|
|
|
- materialList=materialMapperEx.getMaterialListByUnitIds(idArray);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.readFail(logger, e);
|
|
|
- }
|
|
|
- if(materialList!=null&&materialList.size()>0){
|
|
|
- logger.error("异常码[{}],异常提示[{}],参数,UnitIds[{}]",
|
|
|
- ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,ExceptionConstants.DELETE_FORCE_CONFIRM_MSG,ids);
|
|
|
- throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
|
|
|
- ExceptionConstants.DELETE_FORCE_CONFIRM_MSG);
|
|
|
- }
|
|
|
- //记录日志
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
- sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
|
|
|
- List<Unit> list = getUnitListByIds(ids);
|
|
|
- for(Unit unit: list){
|
|
|
- sb.append("[").append(unit.getName()).append("]");
|
|
|
- }
|
|
|
- logService.insertLog("多单位", sb.toString(),
|
|
|
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
|
|
|
- User userInfo=userService.getCurrentUser();
|
|
|
- //校验通过执行删除操作
|
|
|
- try{
|
|
|
- result=unitMapperEx.batchDeleteUnitByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.writeFail(logger, e);
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public int checkIsNameExist(Long id, String name)throws Exception {
|
|
|
- UnitExample example = new UnitExample();
|
|
|
- example.createCriteria().andIdNotEqualTo(id).andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
|
|
|
- List<Unit> list=null;
|
|
|
- try{
|
|
|
- list=unitMapper.selectByExample(example);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.readFail(logger, e);
|
|
|
- }
|
|
|
- return list==null?0:list.size();
|
|
|
- }
|
|
|
+ int insertUnit(Unit unit, HttpServletRequest request)throws Exception;
|
|
|
|
|
|
/**
|
|
|
- * 根据条件查询单位id
|
|
|
- * @param basicUnit
|
|
|
- * @param otherUnit
|
|
|
- * @param ratio
|
|
|
- * @return
|
|
|
+ * 修改单位
|
|
|
+ * @param unit 单位
|
|
|
*/
|
|
|
- public Long getUnitIdByParam(String basicUnit, String otherUnit, BigDecimal ratio){
|
|
|
- Long unitId = null;
|
|
|
- UnitExample example = new UnitExample();
|
|
|
- example.createCriteria().andBasicUnitEqualTo(basicUnit).andOtherUnitEqualTo(otherUnit).andRatioEqualTo(ratio)
|
|
|
- .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
|
|
|
- List<Unit> list = unitMapper.selectByExample(example);
|
|
|
- if(list!=null && list.size()>0) {
|
|
|
- unitId = list.get(0).getId();
|
|
|
- }
|
|
|
- return unitId;
|
|
|
- }
|
|
|
+ @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
+ int updateUnit(Unit unit, HttpServletRequest request)throws Exception;
|
|
|
|
|
|
/**
|
|
|
- * 根据多单位的比例进行库存换算(保留两位小数)
|
|
|
- * @param stock
|
|
|
- * @param unitInfo
|
|
|
- * @param materialUnit
|
|
|
- * @return
|
|
|
+ * 删除单位
|
|
|
+ * @param id 单位id
|
|
|
+ * @param request
|
|
|
*/
|
|
|
- public BigDecimal parseStockByUnit(BigDecimal stock, Unit unitInfo, String materialUnit) {
|
|
|
- if(materialUnit.equals(unitInfo.getOtherUnit()) && unitInfo.getRatio()!=null && unitInfo.getRatio().compareTo(BigDecimal.ZERO)!=0) {
|
|
|
- stock = stock.divide(unitInfo.getRatio(),2,BigDecimal.ROUND_HALF_UP);
|
|
|
- }
|
|
|
- if(materialUnit.equals(unitInfo.getOtherUnitTwo()) && unitInfo.getRatioTwo()!=null && unitInfo.getRatioTwo().compareTo(BigDecimal.ZERO)!=0) {
|
|
|
- stock = stock.divide(unitInfo.getRatioTwo(),2,BigDecimal.ROUND_HALF_UP);
|
|
|
- }
|
|
|
- if(materialUnit.equals(unitInfo.getOtherUnitThree()) && unitInfo.getRatioThree()!=null && unitInfo.getRatioThree().compareTo(BigDecimal.ZERO)!=0) {
|
|
|
- stock = stock.divide(unitInfo.getRatioThree(),2,BigDecimal.ROUND_HALF_UP);
|
|
|
- }
|
|
|
- return stock;
|
|
|
- }
|
|
|
+ @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
+ int deleteUnit(Long id, HttpServletRequest request);
|
|
|
|
|
|
/**
|
|
|
- * 根据多单位的比例进行单价换算(保留两位小数),变大
|
|
|
- * @param unitPrice
|
|
|
- * @param unitInfo
|
|
|
- * @param materialUnit
|
|
|
- * @return
|
|
|
+ * 批量删除单位
|
|
|
+ * @param ids 单位id字符集
|
|
|
+ * @param request
|
|
|
*/
|
|
|
- public BigDecimal parseUnitPriceByUnit(BigDecimal unitPrice, Unit unitInfo, String materialUnit) {
|
|
|
- if (materialUnit.equals(unitInfo.getOtherUnit()) && unitInfo.getRatio() != null && unitInfo.getRatio().compareTo(BigDecimal.ZERO) != 0) {
|
|
|
- unitPrice = unitPrice.multiply(unitInfo.getRatio());
|
|
|
- }
|
|
|
- if (materialUnit.equals(unitInfo.getOtherUnitTwo()) && unitInfo.getRatioTwo() != null && unitInfo.getRatioTwo().compareTo(BigDecimal.ZERO) != 0) {
|
|
|
- unitPrice = unitPrice.multiply(unitInfo.getRatioTwo());
|
|
|
- }
|
|
|
- if (materialUnit.equals(unitInfo.getOtherUnitThree()) && unitInfo.getRatioThree() != null && unitInfo.getRatioThree().compareTo(BigDecimal.ZERO) != 0) {
|
|
|
- unitPrice = unitPrice.multiply(unitInfo.getRatioThree());
|
|
|
- }
|
|
|
- return unitPrice;
|
|
|
- }
|
|
|
+ @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
+ int batchDeleteUnit(String ids, HttpServletRequest request) throws Exception;
|
|
|
|
|
|
/**
|
|
|
- * 根据多单位的比例进行总金额换算(保留两位小数),变小
|
|
|
- * @param allPrice
|
|
|
- * @param unitInfo
|
|
|
- * @param materialUnit
|
|
|
- * @return
|
|
|
+ * 检查单位名称是否存在
|
|
|
+ * @param name 单位名称
|
|
|
+ * @return 单位数量
|
|
|
*/
|
|
|
- public BigDecimal parseAllPriceByUnit(BigDecimal allPrice, Unit unitInfo, String materialUnit) {
|
|
|
- if (materialUnit.equals(unitInfo.getOtherUnit()) && unitInfo.getRatio() != null && unitInfo.getRatio().compareTo(BigDecimal.ZERO) != 0) {
|
|
|
- allPrice = allPrice.divide(unitInfo.getRatio(), 2, BigDecimal.ROUND_HALF_UP);
|
|
|
- }
|
|
|
- if (materialUnit.equals(unitInfo.getOtherUnitTwo()) && unitInfo.getRatioTwo() != null && unitInfo.getRatioTwo().compareTo(BigDecimal.ZERO) != 0) {
|
|
|
- allPrice = allPrice.divide(unitInfo.getRatioTwo(), 2, BigDecimal.ROUND_HALF_UP);
|
|
|
- }
|
|
|
- if (materialUnit.equals(unitInfo.getOtherUnitThree()) && unitInfo.getRatioThree() != null && unitInfo.getRatioThree().compareTo(BigDecimal.ZERO) != 0) {
|
|
|
- allPrice = allPrice.divide(unitInfo.getRatioThree(), 2, BigDecimal.ROUND_HALF_UP);
|
|
|
- }
|
|
|
- return allPrice;
|
|
|
- }
|
|
|
+ int checkIsNameExist(String name);
|
|
|
|
|
|
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
- public int batchSetStatus(Boolean status, String ids)throws Exception {
|
|
|
- logService.insertLog("多单位",
|
|
|
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ENABLED).toString(),
|
|
|
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
|
|
|
- List<Long> unitIds = StringUtil.strToLongList(ids);
|
|
|
- Unit unit = new Unit();
|
|
|
- unit.setEnabled(status);
|
|
|
- UnitExample example = new UnitExample();
|
|
|
- example.createCriteria().andIdIn(unitIds);
|
|
|
- int result=0;
|
|
|
- try{
|
|
|
- result = unitMapper.updateByExampleSelective(unit, example);
|
|
|
- }catch(Exception e){
|
|
|
- JshException.writeFail(logger, e);
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
+ Long getUnitIdByParam(String basicUnit, String otherUnit, BigDecimal ratio);
|
|
|
+
|
|
|
+ BigDecimal parseStockByUnit(BigDecimal stock, Unit unitInfo, String materialUnit);
|
|
|
+
|
|
|
+ BigDecimal parseUnitPriceByUnit(BigDecimal unitPrice, Unit unitInfo, String materialUnit);
|
|
|
+
|
|
|
+ BigDecimal parseAllPriceByUnit(BigDecimal allPrice, Unit unitInfo, String materialUnit);
|
|
|
|
|
|
/**
|
|
|
- * 根据单位id获取单位值集合
|
|
|
- * @param id 单位id
|
|
|
+ * 批量设置状态
|
|
|
+ * @param status 状态
|
|
|
+ * @param ids 单位id
|
|
|
*/
|
|
|
- public List getUnitListByID(Long id){
|
|
|
- Unit unit = unitMapper.selectByPrimaryKey(id);
|
|
|
- List<UnitListVo> list = new ArrayList();
|
|
|
- UnitListVo unitListVo = new UnitListVo().setName(unit.getBasicUnit()).setRatio(new BigDecimal("1"));
|
|
|
- list.add(unitListVo);
|
|
|
- if (unit.getOtherUnit() != null){
|
|
|
- UnitListVo vo = new UnitListVo().setName(unit.getOtherUnit()).setRatio(unit.getRatio());
|
|
|
- list.add(vo);
|
|
|
- }
|
|
|
- if (unit.getOtherUnitTwo() != null){
|
|
|
- UnitListVo vo = new UnitListVo().setName(unit.getOtherUnitTwo()).setRatio(unit.getRatioTwo());
|
|
|
- list.add(vo);
|
|
|
- }
|
|
|
- if (unit.getOtherUnitThree() != null){
|
|
|
- UnitListVo vo = new UnitListVo().setName(unit.getOtherUnitThree()).setRatio(unit.getRatioThree());
|
|
|
- list.add(vo);
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
+ @Transactional(value = "transactionManager", rollbackFor = Exception.class)
|
|
|
+ int batchSetStatus(Boolean status, String ids);
|
|
|
+
|
|
|
+ List getUnitListByID(Long id);
|
|
|
}
|