GoodsService.java 5.87 KB
package cn.csbr.app.service;

import cn.csbr.app.gui.GUIContext;
import cn.csbr.springboot.dao.mapper.HisGoodsBoundMapper;
import cn.csbr.springboot.dao.mapper.HisGoodsMapper;
import cn.csbr.springboot.dao.model.HisGoods;
import cn.csbr.springboot.dao.model.HisGoodsBound;
import com.alibaba.fastjson.JSON;
import com.csbr.mybatis.CommonMapper;
import com.csbr.util.IdUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService {
    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private GUIContext guiContext;

    @Autowired
    private HisGoodsMapper hisGoodsMapper;

    @Autowired
    private HisGoodsBoundMapper hisGoodsBoundMapper;
    public synchronized String getProducer(String guid){
        String sql = "select Producer from his_goods where guid='"+guid+"'";

        return commonMapper.queryOne(sql).get("Producer").toString();
    }
    public synchronized List<HisGoods> getStockedGoodsData() {
        String sql = "select *,hgb.StockLowerLimit as lowerlimit,hgb.StockUpperLimit as upperlimit" +
                " from his_goods hg LEFT JOIN his_goods_bound hgb on hg.GUID = hgb.HisGoodsGUID and hgb.orgGUID " +
                "= '"+guiContext.getLoginUser().getOrgGuid()+"' ORDER BY hgb.UPDATETIME desc;";
        List<Map> list = commonMapper.queryList(sql);
        List<HisGoods> goodsList = new ArrayList<>();
        for(Map map:list) {
            HisGoods goods = JSON.parseObject(JSON.toJSONString(map), HisGoods.class);
            goods.setStockupperlimit(map.get("upperlimit")==null?BigDecimal.ZERO:new BigDecimal(map.get("upperlimit").toString()));
            goods.setStocklowerlimit(map.get("lowerlimit")==null?BigDecimal.ZERO:new BigDecimal(map.get("lowerlimit").toString()));
            goodsList.add(goods);
        }
       return goodsList;
    }
    private static Logger log = LoggerFactory.getLogger(GoodsService.class);

    public synchronized List<HisGoods> getStockedGoodsData(String goodscode) {
        String sql = "select *,hgb.StockLowerLimit as lowerlimit,hgb.StockUpperLimit as upperlimit" +
                " from his_goods hg LEFT JOIN his_goods_bound hgb on hg.GUID = hgb.HisGoodsGUID and hgb.orgGUID " +
                "= '"+guiContext.getLoginUser().getOrgGuid()+"' where ( hg.goodscode like '%"+goodscode+"%' or hg.goodsname like "+"'%"+goodscode+"%'"+" or hg.goodsspec like " +"'%"+goodscode+"%'"+" or hg.model like "+"'%"+goodscode+"%'"+" ) ORDER BY hgb.UPDATETIME desc";
       log.info(sql);
        List<Map> list = commonMapper.queryList(sql);
        List<HisGoods> goodsList = new ArrayList<>();
        log.info(list.toString());
        for(Map map:list) {
            HisGoods goods = JSON.parseObject(JSON.toJSONString(map), HisGoods.class);
            goods.setStockupperlimit(map.get("upperlimit")==null?BigDecimal.ZERO:new BigDecimal(map.get("upperlimit").toString()));
            goods.setStocklowerlimit(map.get("lowerlimit")==null?BigDecimal.ZERO:new BigDecimal(map.get("lowerlimit").toString()));
            goodsList.add(goods);

        }
        return goodsList;
    }

    //保存上下限修改
    @Transactional
   public synchronized void updateGoodsUpLower(List<HisGoods> goodsList) {
        List<HisGoodsBound> hisGoodsBounds = new ArrayList<>();
        for (HisGoods hisGoods : goodsList) {
            HisGoodsBound hisGoodsBound = new HisGoodsBound();
            hisGoodsBound.setHisgoodguid(hisGoods.getGuid());
            hisGoodsBound.setOrgguid(guiContext.getLoginUser().getOrgGuid());
            List<HisGoodsBound> list = hisGoodsBoundMapper.select(hisGoodsBound);
            hisGoodsBound.setStocklowerlimit(hisGoods.getStocklowerlimit());
            hisGoodsBound.setStockupperlimit(hisGoods.getStockupperlimit());
            //能在his_goods_bound表找到,对比数据是否有变化,有则做更新操作,否则做插入操作
            if(CollectionUtils.isNotEmpty(list)) {
                if(hisGoodsBound.getStockupperlimit().compareTo(BigDecimal.ZERO) == 0 && hisGoodsBound.getStocklowerlimit().compareTo(BigDecimal.ZERO) == 0) {
                    hisGoodsBound.setGuid(list.get(0).getGuid());
                    hisGoodsBoundMapper.deleteByPrimaryKey(hisGoodsBound);
                    continue;
                }
                if(list.get(0).getStocklowerlimit().compareTo(hisGoodsBound.getStocklowerlimit())!=0 || list.get(0).getStockupperlimit().compareTo(hisGoodsBound.getStockupperlimit())!=0) {
                    hisGoodsBound.setUpdatetime(new Date());
                    hisGoodsBound.setGuid(list.get(0).getGuid());
                    hisGoodsBound.setUpdaterid(guiContext.getLoginUser().getGuid());
                    hisGoodsBoundMapper.updateByPrimaryKeySelective(hisGoodsBound);
                }
                continue;
            }
            //上下限存在不为0则做插入操作
            if(hisGoods.getStockupperlimit().compareTo(BigDecimal.ZERO) != 0 || hisGoods.getStocklowerlimit().compareTo(BigDecimal.ZERO)!=0) {
                hisGoodsBound.setGuid(IdUtils.getUUID());
                hisGoodsBound.setCreaterid(guiContext.getLoginUser().getGuid());
                hisGoodsBound.setUpdaterid(guiContext.getLoginUser().getGuid());
                hisGoodsBound.setUpdatetime(new Date());
                hisGoodsBound.setCreatetime(new Date());
                hisGoodsBounds.add(hisGoodsBound);
            }
        }
        if(CollectionUtils.isNotEmpty(hisGoodsBounds)) {
            hisGoodsBoundMapper.insertList(hisGoodsBounds);
        }
    }
}