UseService.java 21.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
package cn.csbr.app.service;

import cn.csbr.app.config.FxConfigure;

import cn.csbr.springboot.dao.mapper.HisGoodsMapper;
import cn.csbr.app.gui.GUIContext;
import cn.csbr.app.model.InboundItem;
import cn.csbr.app.model.RfidDataModel;
import cn.csbr.app.util.CommonUtil;
import cn.csbr.springboot.dao.mapper.*;
import cn.csbr.springboot.dao.model.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.date.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.csbr.mybatis.CommonMapper;
import com.csbr.util.ArrayUtil;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * 取用业务类
 */
@Service
public class UseService {
    private static Logger logger = LoggerFactory.getLogger(UseService.class);
    private static Logger log = LoggerFactory.getLogger(InboundService.class);
    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private GUIContext guiContext;

    @Autowired
    private FxConfigure fxConfigure;
    @Autowired
    private MfcubestockdetailMapper mfcubestockdetailMapper;
    @Autowired
    private MfcubestockstatementMapper mfcubestockstatementMapper;
    @Autowired
    private MfcubestockMapper mfcubestockMapper;
    @Autowired
    private BaseService baseService;
    @Autowired
    private TrchargingMapper trchargingMapper;
    @Autowired
    private TrchargingdetailMapper trchargingdetailMapper;
    @Autowired
    private MfcubestockdetailOldMapper2 mfcubestockdetailOldMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;
    @Autowired
    private HisGoodsMapper hisGoodsMapper;
    /*
    获取超时的取货操作
     */
    @Transactional
    public synchronized String getretruntimeout(String time) {
        String sql = "select count(*) num FROM mfcubestockdetail WHERE BillState='O' and CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "')  AND DepotGuid='" + guiContext.getLoginUser().getOrgGuid() +"' and use_time is not null and use_time<=DATE_SUB(NOW(), interval "+time+" hour)";
        // Integer num =Integer.parseInt();
          // System.out.println(sql);
        return commonMapper.queryOne(sql).get("num").toString();
    }
    /*
    获取超时48的取货数量
     */
    @Transactional
    public synchronized String getretruntimeout2(String time) {
        String sql = "select count(*) num FROM mfcubestockdetail WHERE BillState='O' and CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "')  AND DepotGuid='" + guiContext.getLoginUser().getOrgGuid() +"' and use_time is not null and use_time<=DATE_SUB(NOW(), interval "+time+" hour)";
        // Integer num =Integer.parseInt();
        //   System.out.println(sql);
        return commonMapper.queryOne(sql).get("num").toString();
    }
    @Transactional
    public  void getMfcubestDetail(String time) {
        try {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sql = "select * FROM mfcubestockdetail WHERE BillState='O' and CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "')  AND DepotGuid='" + guiContext.getLoginUser().getOrgGuid() + "' and use_time is not null and use_time<=DATE_SUB(NOW(), interval " + time + " hour) limit 30";
            System.out.println(sql);
            List<Map> maps = commonMapper.queryList(sql);

            if (maps.size() > 0) {
                for (int i = 0; i < maps.size(); i++) {
                    Map map = maps.get(i);
                    log.info("map" + map.toString());
                    //根据追溯码在库存表里查找
                    Example example1 = new Example(Mfcubestockdetail.class);
                    Example.Criteria criteria1 = example1.createCriteria();
                    criteria1.andEqualTo("labGuid", map.get("LabGuid").toString());
                    //执行查询操作
                    List<Mfcubestockdetail> mfcubestockdetails = mfcubestockdetailMapper.selectByExample(example1);

                    Mfcubestockdetail mfcubestockdetail = mfcubestockdetails.get(0);
                    log.info("data" + mfcubestockdetail);
                    Trcharging tg = new Trcharging();
                    tg.setGuid(CommonUtil.getGuid());
                    tg.setOperationno(CommonUtil.getGuid());//手术单号
                    tg.setInpatienno(CommonUtil.getGuid());//住院号

                    log.info("test111");
                    //科室名称添加
                    Example exampleOrg = new Example(SysOrg.class);
                    Example.Criteria criteriaOrg = exampleOrg.createCriteria();
                    criteriaOrg.andEqualTo("guid", guiContext.getLoginUser().getOrgGuid());//科室guid
                    List<SysOrg> sysOrgs = sysOrgMapper.selectByExample(exampleOrg);
                    tg.setOrgguid(sysOrgs.get(0).getGuid());//科室id
                    tg.setOrgName(sysOrgs.get(0).getOrgname());//消耗科室名称
                    tg.setName("不详");//患者姓名
                    tg.setOperationdate(CommonUtil.nowTs());//手术日期
                    tg.setCreatetime(CommonUtil.nowTs());//下单时间
                    tg.setStoreguid(guiContext.getStoreguid());
                    log.info("test222");
                    //执行弹头添加操作
                    trchargingMapper.insert(tg);
                    //添加明细表
                    //获取guid值赋值给deatil表
                    Trchargingdetail trchargingdetail = new Trchargingdetail();
                    trchargingdetail.setGuid(CommonUtil.getGuid());
                    trchargingdetail.setTguid(tg.getGuid());//关联guid
                    Example exampleGoods = new Example(HisGoods.class);
                    Example.Criteria criteriaGoods = exampleGoods.createCriteria();
                    criteriaGoods.andEqualTo("guid", map.get("GoodsGUID"));
                    List<HisGoods> hisGoods = hisGoodsMapper.selectByExample(exampleGoods);

                    if (hisGoods.size()>0&&hisGoods != null) {
                        HisGoods hisGood = hisGoods.get(0);
                        trchargingdetail.setGoodsguid(hisGood.getGuid());
                        trchargingdetail.setGoodsCode(hisGood.getGoodscode());//goodscode
                        trchargingdetail.setGoodsname(hisGood.getGoodsname());
                        trchargingdetail.setGoodsSpec(hisGood.getGoodsspec());
                        trchargingdetail.setUnit(hisGood.getUnitstyle());//单位
                        trchargingdetail.setPrice(hisGood.getPrice().doubleValue());//价格
                    }
                    log.info("test333");
                    trchargingdetail.setLot(map.get("Lot").toString());//批号
                    trchargingdetail.setInputdate(mfcubestockdetail.getUseTime());//手术日期
                    trchargingdetail.setQty(new BigDecimal("1"));//数量

                    //生产日期
                    Date productionDate = null;
                    Date expireDate = null;
                    try {
                        productionDate = (Date) f.parse(map.get("Productiondate").toString());
                        expireDate = (Date) f.parse(map.get("ExpireDate").toString());

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    trchargingdetail.setOperatername(mfcubestockdetail.getUseUsername());
                    trchargingdetail.setSupplierguid(mfcubestockdetail.getSupplierGUID());
                    trchargingdetail.setSuppliername(mfcubestockdetail.getSupplierName());
                    trchargingdetail.setProductiondate(new Timestamp(productionDate.getTime()));
                    trchargingdetail.setExpiredate(new Timestamp(expireDate.getTime()));//有效期
                    trchargingdetail.setLabguid(map.get("LabGuid").toString());//追溯码
                    trchargingdetail.setBarcode(map.get("LabGuid").toString());//追数码
                    trchargingdetail.setCubeguid(map.get("CubeGuid").toString());
                    trchargingdetail.setCubename(map.get("CubeName").toString());
                    //耗材科室
                    trchargingdetail.setStoreguid(guiContext.getStoreguid());
                    trchargingdetail.setDepotguid(map.get("depotGuid").toString());
                    trchargingdetail.setDepotname(map.get("depotName").toString());
                    trchargingdetail.setRfiduid(map.get("RfidUid").toString());
                    log.info("test444");
                    //执行添加操作
                    trchargingdetailMapper.insert(trchargingdetail);
                    log.info("明细添加成功=========》" + trchargingdetail.toString());

                    //System.out.println(mfcubestockdetail.toString());
                    MfcubestockdetailOld2 history = new MfcubestockdetailOld2();
                    BeanUtils.copyProperties(mfcubestockdetail, history);
                    history.setUpdateTime(CommonUtil.nowTs());
                    //执行库存历史表添加
                    int insert = mfcubestockdetailOldMapper.insert(history);
                    log.info("库存历史表添加成功=========》" + history);
                    if (insert > 0) {
                        //库存历史表添加成功则删除库存表
                        System.out.println("追溯码" + mfcubestockdetail.getLabGuid());
                        mfcubestockdetailMapper.mfcubestockdetaildel(mfcubestockdetail.getLabGuid());
                        log.info("库存表删除成功=========》" + mfcubestockdetail);
                    } else {
                        log.info("库存历史添加失败=========》");
                    }
                }
            }
        }
        catch (Exception ex)
        {  logger.info("还货数据处理异常"+ex.getMessage());}


    }
    public synchronized List<InboundItem> queryInboundItem(String inboundNo,
                                                           Map<Cabinet, List<String>> scanData,
                                                           String goodsInfo,
                                                           boolean ifNeedMacthRfid,
                                                           List<InboundItem> outinbounditems) {
        String goodsInfol = "";

        String sql = " select * from mfcubestockdetail where billstate!='O' AND CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "') " +
                " AND DepotGuid='" + guiContext.getLoginUser().getOrgGuid() + "'";

        if (!StringUtils.isEmpty(goodsInfo)) {
            goodsInfol = "%" + goodsInfo + "%";
            sql = sql + " AND GoodsName like '" + goodsInfol + "' or LabGuid like'" + goodsInfol + "'";

        }
        Set<Cabinet> cabinets = scanData.keySet();
        String strtmp = "";
        int cabinetcount = 0;
        int ncabinetlength = cabinets.size();
        String strcabinet = "";
        for (Cabinet cabinet : cabinets) {

            List<String> results = scanData.get(cabinet);
            int ncount = 0;
            int nlength = results != null ? results.size() : 0;
            if (nlength > 0) {
                strcabinet = strcabinet + "'" + cabinet.getGuid() + "'" + ",";

            }
            for (String barcode : results) {
                if (ncount == nlength - 1) {
                    strtmp = strtmp + "'" + barcode + "'";
                } else {
                    strtmp = strtmp + "'" + barcode + "'" + ",";
                }
                ncount++;
            }
        }
        //if (strtmp.length()>0) {
        //     sql=sql+" AND  rfiduid  NOT IN ("+strtmp +")";
        //   }
        if (strcabinet.length() > 0) {
            sql = sql + " AND CUBEGUID IN (" + strcabinet.substring(0, strcabinet.length() - 1) + ")";

        }

        List<Map> maps = commonMapper.queryList(sql);
        List<InboundItem> inboundItems = new ArrayList<>();

        for (Map map : maps) {
            InboundItem inboundItem = new InboundItem();
            Mfcubestockdetail data = JSON.parseObject(JSON.toJSONString(map), Mfcubestockdetail.class);
            inboundItem.setScanQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setSrcOrderCode(data.getRkBillNo());
            inboundItem.setQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setGoodsguid(data.getGoodsGUID());
//                        inboundItem.setGoodscode(data.getG);
            inboundItem.setGoodsname(data.getGoodsName());
//                        inboundItem.setGoodsspec(data.get);
            inboundItem.setUnit(data.getUnit());
            inboundItem.setIfScand((byte) 0);
            inboundItem.setSupplierName(data.getSupplierName());
            inboundItem.setSupplierGuid(data.getSupplierGUID());
            inboundItem.setBacthCode(data.getLot());
//                        inboundItem.setSrcInboundDate(data.getMvdate());
            inboundItem.setTraceCode(data.getLabGuid());
            inboundItem.setBarcode(data.getLabGuid());
            inboundItem.setGuid(data.getGuid());
//                        inboundItem.setRegistkey(data.get);
//                        inboundItem.setManufacturer(data.getManufacturer());
            inboundItem.setAllocation(data.getCubeName());
            inboundItem.setAllocationGuid(data.getCubeGuid());
            inboundItem.setIfInCabinet((byte) 1);
            inboundItem.setIfScand((byte) 1);
            inboundItem.setProductiondate(data.getProductiondate());
            inboundItem.setExpiredate(data.getExpireDate());
            inboundItem.setRfiduid(data.getRfidUid());
            boolean match = matchScanBarcode(inboundItem, scanData);
            if (ifNeedMacthRfid) {
                //手动
                if (ArrayUtil.isNotEmpty(outinbounditems)) {
                    for (InboundItem outinbounditem : outinbounditems) {
                        if (!outinbounditem.getGuid().equals(inboundItem.getGuid())) {
                            inboundItems.add(inboundItem);
                        }
                    }

                }
            } else {
                //读码
                if (!match) {
                    inboundItems.add(inboundItem);
                }

            }
        }

        return inboundItems;
    }

    @Transactional
    public synchronized List<InboundItem> ActionqueryInboundItem(RfidDataModel rfidDataModel, String rfidData) {
        String goodsInfol = "";
        // ActionBacknboundItem(cubeGuid,rfidData);
        //设置操作人人是从柜子柜子对象中取得
        String cubeGuid = rfidDataModel.getCabinetCd();
        String operaterId = rfidDataModel.getUserCd();
        String operaterName = rfidDataModel.getUserName();
        System.out.println(rfidData);
        String sql = " select * from mfcubestockdetail where billstate!='O' AND CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "') " +
                " AND DepotGuid='" + guiContext.getLoginUser().getOrgGuid() + "' AND CubeName=" + "'" + cubeGuid + "'  AND  rfiduid  NOT IN (" + rfidData + ")";
        List<Map> maps = commonMapper.queryList(sql);
        List<InboundItem> inboundItems = new ArrayList<>();

        for (Map map : maps) {
            InboundItem inboundItem = new InboundItem();
            Mfcubestockdetail data = JSON.parseObject(JSON.toJSONString(map), Mfcubestockdetail.class);
            inboundItem.setScanQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setSrcOrderCode(data.getRkBillNo());
            inboundItem.setQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setGoodsguid(data.getGoodsGUID());
            inboundItem.setGoodsname(data.getGoodsName());
            inboundItem.setUnit(data.getUnit());
            inboundItem.setIfScand((byte) 0);
            inboundItem.setSupplierName(data.getSupplierName());
            inboundItem.setSupplierGuid(data.getSupplierGUID());
            inboundItem.setBacthCode(data.getLot());
            inboundItem.setTraceCode(data.getLabGuid());
            inboundItem.setBarcode(data.getLabGuid());
            inboundItem.setGuid(data.getGuid());

            inboundItem.setAllocation(data.getCubeName());
            inboundItem.setAllocationGuid(data.getCubeGuid());
            inboundItem.setIfInCabinet((byte) 1);
            inboundItem.setIfScand((byte) 1);
            inboundItem.setProductiondate(data.getProductiondate());
            inboundItem.setExpiredate(data.getExpireDate());
            inboundItem.setRfiduid(data.getRfidUid());
            inboundItem.setOperaterId(operaterId);
            inboundItem.setOperaterName(operaterName);
            inboundItems.add(inboundItem);

        }

        return inboundItems;
    }

    public synchronized void ActionBacknboundItem(String cubeGuid, String rfidData) {

        String sql = " select * from mfcubestockdetail where  CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "') " +
                " AND DepotGuid='" + guiContext.getLoginUser().getOrgGuid() + "' AND  rfiduid   IN (" + rfidData + ")";
        List<Map> maps = commonMapper.queryList(sql);


        for (Map map : maps) {
            InboundItem inboundItem = new InboundItem();
            Mfcubestockdetail data = JSON.parseObject(JSON.toJSONString(map), Mfcubestockdetail.class);
            inboundItem.setScanQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setSrcOrderCode(data.getRkBillNo());
            inboundItem.setQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setGoodsguid(data.getGoodsGUID());
            inboundItem.setGoodsname(data.getGoodsName());
            inboundItem.setUnit(data.getUnit());
            inboundItem.setIfScand((byte) 0);
            inboundItem.setSupplierName(data.getSupplierName());
            inboundItem.setSupplierGuid(data.getSupplierGUID());
            inboundItem.setBacthCode(data.getLot());
            inboundItem.setTraceCode(data.getLabGuid());
            inboundItem.setBarcode(data.getLabGuid());
            inboundItem.setGuid(data.getGuid());

            inboundItem.setAllocation(data.getCubeName());
            inboundItem.setAllocationGuid(data.getCubeGuid());
            inboundItem.setIfInCabinet((byte) 1);
            inboundItem.setIfScand((byte) 1);
            inboundItem.setProductiondate(data.getProductiondate());
            inboundItem.setExpiredate(data.getExpireDate());
            inboundItem.setRfiduid(data.getRfidUid());

            if (!data.getCubeName().equals(cubeGuid)) {
                for (Cabinet cabinet : guiContext.getCabinets()) {
                    if (cabinet.getCabinetcode().equals(cubeGuid)) {
                        data.setLocGuid(cabinet.getGuid());
                        data.setLocName(cabinet.getCabinetcode());
                        data.setCubeName(cabinet.getCabinetcode());
                        data.setCubeGuid(cabinet.getGuid());
                        if (data.getBillState() != "Y") {
                            baseService.writeLog(inboundItem, data, "6");
                            data.setBillState("Y");
                        }
                        mfcubestockdetailMapper.updateByPrimaryKeySelective(data);

                        break;

                    }

                }
            }


        }

    }

    private synchronized boolean matchScanBarcode(InboundItem item, Map<Cabinet, List<String>> scanData) {
        System.out.println(item.getRfiduid() + "~~~~~~~~~~~~~~~~~~");

        Set<Cabinet> cabinets = scanData.keySet();
        for (Cabinet cabinet : cabinets) {
            List<String> results = scanData.get(cabinet);
            if (results != null) {
                for (String barCode : results) {
                    if (barCode.equalsIgnoreCase(item.getRfiduid())) {
                        return true;
                    }
                }
            }

        }
        return false;
    }

    public void saveUseCabinetInfo(List<InboundItem> inboundItems) {
        if (inboundItems == null) {
            return;
        }

        for (InboundItem item : inboundItems) {
            Mfcubestockdetail cubedetail = mfcubestockdetailMapper.selectByPrimaryKey(item.getGuid());
            //标记为取货状态
            cubedetail.setBillState("O");

            Timestamp result = new Timestamp(System.currentTimeMillis());
            java.sql.Date sqlExpiryDate = new java.sql.Date(result.getTime());
            cubedetail.setUseTime(new DateTime());
            cubedetail.setUseUsername(item.getOperaterName());
            cubedetail.setStoreguid(guiContext.getStoreguid());
            cubedetail.setStorename(guiContext.getStorename());
            mfcubestockdetailMapper.updateByPrimaryKeySelective(cubedetail);
            baseService.writeLog(item, cubedetail, "2");   //取货

            //处理信息
//            mfcubestockdetailMapper;

            //处理台账信息
//            mfcubestockstatementMapper;
            //处理汇总信息
//        mfcubestockMapper;
//        逻辑联合主键 是    科室id+耗材柜id+商品id+批号+有效期至
//            baseService.sumNumber(item, cubedetail);
        }
    }
}