package com.shinsoft.sto.service.impl;

import com.shinsoft.frameworkapi.model.SysDepartment;
import com.shinsoft.frameworkapi.model.SysUser;
import com.shinsoft.frameworkapi.service.FrameworkService;
import com.shinsoft.sto.mapper.*;
import com.shinsoft.sto.model.*;
import com.shinsoft.sto.service.OnhandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinsoft.sto.service.TransRecordService;
import org.springframework.stereotype.Service;


import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.model.common.ResultJSON;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 库存现有量 服务实现类
 * </p>
 *
 * @author system
 * @since 2024-10-08
 */
@Service
public class OnhandServiceImp extends ServiceImpl<OnhandMapper, Onhand> implements OnhandService {


    ResultJSON resultJSON;

    @Autowired
    OnhandMapper onhandMapper;

    @Autowired
    FrameworkService frameworkService;
    @Autowired
    StocktakingMapper stocktakingMapper;
    @Autowired
    TransRecordMapper transRecordMapper;
    @Autowired
    TransRecordService transRecordService;
    @Autowired
    ReverseMapper reverseMapper;
    @Autowired
    PickRecordMapper pickRecordMapper;

    @Override
    public ResultJSON query(int page, int rows, Map<String, Object> map) {

        Page<Onhand> onhandPage = new Page<>(page, rows);
        QueryWrapper<Onhand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cancel_flag", "0");
        queryWrapper.orderByDesc("add_time");
        if (!ObjectUtils.isEmpty(map.get("storeNm"))) {
            queryWrapper.like("store_nm", map.get("storeNm"));
        }
        if (!ObjectUtils.isEmpty(map.get("areaCd"))) {
            queryWrapper.like("area_cd", map.get("areaCd"));
        }
        if (!ObjectUtils.isEmpty(map.get("stackCd"))) {
            queryWrapper.like("stack_cd", map.get("stackCd"));
        }
        if (!ObjectUtils.isEmpty(map.get("itemNm"))) {
            queryWrapper.like("item_nm", map.get("itemNm"));
        }
        if (!ObjectUtils.isEmpty(map.get("itemType"))) {
            queryWrapper.eq("item_type", map.get("itemType"));
        }
        if (!ObjectUtils.isEmpty(map.get("storeType"))) {
            queryWrapper.eq("STORE_TYPE", map.get("storeType"));
        }
        IPage<Onhand> list = onhandMapper.queryList(onhandPage, map);
        resultJSON = JSONTools.toResultJSON(list);
        return resultJSON;
    }

    @Override
    public ResultJSON queryByPK(String id) {
        Onhand onhand = onhandMapper.selectByPK(id);
        resultJSON = JSONTools.toResultJSON(onhand);
        return resultJSON;
    }


    @Override
    public ResultJSON save(String userId, String json) {
        Date date = new Date();
        Onhand onhand = JSON.parseObject(json, new TypeReference<Onhand>() {
        });
        if (StringUtils.isEmpty(onhand.getId())) {
            onhand.setCancelFlag("0");
            onhand.setAddId(userId);
            onhand.setAddTime(date);
            onhandMapper.insert(onhand);
        } else {
            onhand.setModifyId(userId);
            onhand.setModifyTime(date);
            onhandMapper.updateById(onhand);
        }

        resultJSON = JSONTools.toResultJSON(onhand);
        return resultJSON;
    }

    @Override
    public ResultJSON remove(String userId, String id) {
        Date date = new Date();
        Onhand onhand = onhandMapper.selectById(id);
        onhand.setCancelFlag("1");
        onhand.setCancelId(userId);
        onhand.setCancelTime(date);
        onhandMapper.updateById(onhand);
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }

    @Override
    public ResultJSON removeBatch(String userId, String ids) {
        Date date = new Date();
        List<String> list = JSON.parseObject(ids, new TypeReference<List<String>>() {
        });
        Onhand onhand;
        for (String id : list) {
            onhand = onhandMapper.selectById(id);
            onhand.setCancelFlag("1");
            onhand.setCancelId(userId);
            onhand.setCancelTime(date);
            onhandMapper.updateById(onhand);
        }
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }

    /**
     * 根据物料，查询库存现有量。
     * @param userId
     * @param params
     * @return
     */
    @Override
    public ResultJSON queryOnhand(String userId, String params) {
        JSONObject jsonObject = JSONObject.parseObject(params);

        if (ObjectUtils.isEmpty(jsonObject) ) {
            resultJSON = JSONTools.toResultJSON(500,"物料编码不允许为空！");
            return resultJSON;
        }
        Map<String, Object> map = new HashMap<>();
        String itemCd = jsonObject.getString("itemCd");
        String storeCd = jsonObject.getString("storeCd");
        String areaCd = jsonObject.getString("areaCd");
        String stackCd = jsonObject.getString("stackCd");
        map.put("itemCd", itemCd);
        map.put("storeCd", storeCd);
        map.put("areaCd", areaCd);
        map.put("stackCd", stackCd);
        if (StringUtils.isEmpty(itemCd)) {
            resultJSON = JSONTools.toResultJSON(500,"物料编码不允许为空！");
            return resultJSON;
        } else
        // 库房
        if (StringUtils.isNotEmpty(storeCd) && StringUtils.isEmpty(areaCd) && StringUtils.isEmpty(stackCd)) {
            Map<String, Object> stringObjectMap = onhandMapper.querySumOnhandKf(map);
            resultJSON = JSONTools.toResultJSON(stringObjectMap);
        } else
        // 区号
        if (StringUtils.isNotEmpty(storeCd) && StringUtils.isNotEmpty(areaCd) && StringUtils.isEmpty(stackCd)) {
            Map<String, Object> stringObjectMap = onhandMapper.querySumOnhandQh(map);
            resultJSON = JSONTools.toResultJSON(stringObjectMap);
        } else
        // 垛位
        if (StringUtils.isNotEmpty(storeCd) && StringUtils.isNotEmpty(areaCd) && StringUtils.isNotEmpty(stackCd)) {
            Map<String, Object> stringObjectMap = onhandMapper.querySumOnhandDw(map);
            resultJSON = JSONTools.toResultJSON(stringObjectMap);
        } else {
            Map<String, Object> stringObjectMap = onhandMapper.querySumOnhand(map);
            resultJSON = JSONTools.toResultJSON(stringObjectMap);
        }

        return resultJSON;
    }



    @Override
    public ResultJSON queryDelvOut(String userId, String params) {
        JSONObject jsonObject = JSONObject.parseObject(params);

        if (ObjectUtils.isEmpty(jsonObject) ) {
            resultJSON = JSONTools.toResultJSON(500,"信息不允许为空！");
            return resultJSON;
        }
        Map<String, Object> map = new HashMap<>();
        String itemCd = jsonObject.getString("itemCd");
        String storeCd = jsonObject.getString("storeCd");

        map.put("itemCd", itemCd);
        map.put("storeCd", storeCd);

        if (StringUtils.isEmpty(itemCd)) {
            resultJSON = JSONTools.toResultJSON(500,"物料编码不允许为空！");
            return resultJSON;
        } else {
            QueryWrapper<Onhand> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("CANCEL_FLAG" , "0");
            queryWrapper.eq("ITEM_CD" , itemCd);
            queryWrapper.eq("STORE_CD" , storeCd);
            List<Onhand> onhands = onhandMapper.selectList(queryWrapper);
//            List<Onhand>list = onhandMapper.selectDelvOut(map);
            resultJSON = JSONTools.toResultJSON(onhands);
        }

        return resultJSON;
    }

    @Override
    @Transactional
    public ResultJSON savePk(String userId, String json) {
        Date date = new Date();
        Onhand hand = JSON.parseObject(json, new TypeReference<Onhand>() {
        });
        ResultJSON userInfoRes = frameworkService.queryUserInfoById(userId, "");
        SysUser user = null;
        List<SysDepartment> deptTreeList = null;
        List<SysDepartment> deptsList = null;
        if (userInfoRes.getCode() == 0) {
            Map<String, Object> mapInfo = JSON.parseObject(userInfoRes.getData().toString(), new TypeReference<Map<String, Object>>() {
            });
            user = JSON.parseObject(mapInfo.get("user").toString(), new TypeReference<SysUser>() {
            });
            deptTreeList = JSON.parseArray(mapInfo.get("deptTree").toString(), SysDepartment.class);
            deptsList = JSON.parseArray(mapInfo.get("depts").toString(), SysDepartment.class);
        }
        if (!StringUtils.isEmpty(hand.getId())) {
            QueryWrapper<Onhand> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ID" , hand.getId());
            List<Onhand> onhands = onhandMapper.selectList(queryWrapper);
            Onhand onhand = onhands.get(0);
            onhand.setModifyId(userId);
            onhand.setModifyTime(date);
            TransRecord transRecord  = new TransRecord();
            if (!org.apache.commons.lang3.ObjectUtils.isEmpty(user)) {
                onhand.setModifyCode(user.getUserCode());
                onhand.setModifyName(user.getUserDesc());
            }
            if (hand.getQtyNew().compareTo(onhand.getQty()) >= 0){
                transRecord.setTransactionType("A7");
                transRecord.setQty(hand.getQtyNew().subtract(onhand.getQty()));
            } else{
                transRecord.setTransactionType("B7");
                transRecord.setQty(hand.getQtyNew().subtract(onhand.getQty()));
            }
//            else {
//                resultJSON = JSONTools.toResultJSON(500,"盘库失败，现有量未发生变化！");
//                return resultJSON;
//            }
            onhand.setQty(hand.getQtyNew());
            onhand.setQtyWet(hand.getQtyWetNew());

            onhandMapper.updateById(onhand);

            Stocktaking stocktaking = new Stocktaking();
            stocktaking.setCancelFlag("0");
            stocktaking.setAddId(userId);
            stocktaking.setAddTime(date);
            stocktaking.setStocktakingDate(date);

            if (!org.apache.commons.lang3.ObjectUtils.isEmpty(user)) {
                stocktaking.setAddCode(user.getUserCode());
                stocktaking.setAddName(user.getUserDesc());
                stocktaking.setStocktakingUser(user.getUserDesc());
            }
            if (!CollectionUtils.isEmpty(deptsList)) {
                stocktaking.setDeptCd(deptsList.get(0).getDeptCode());
                stocktaking.setAddDeptId(deptsList.get(0).getId());
                stocktaking.setAddDeptName(deptsList.get(0).getDeptName());
                stocktaking.setAddDeptCode(deptsList.get(0).getDeptCode());
            }
            if (!CollectionUtils.isEmpty(deptTreeList)) {
                stocktaking.setCompCd(deptTreeList.get(0).getDeptCode());
            }
            stocktaking.setStoreCd(onhand.getStoreCd());
            stocktaking.setStoreNm(hand.getStoreNm());
            stocktaking.setAreaCd(onhand.getAreaCd());
            stocktaking.setStackCd(onhand.getStackCd());
            stocktaking.setItemCd(onhand.getItemCd());
            stocktaking.setItemNm(onhand.getItemNm());
            stocktaking.setItemType(onhand.getItemType());
            stocktaking.setQty(hand.getQtyNew());
            stocktaking.setQtyNew(hand.getQtyNew());
            stocktaking.setQtyWet(hand.getQtyWetNew());
            stocktaking.setQtyWetNew(hand.getQtyWetNew());

            stocktaking.setQtyOld(hand.getQty());
            stocktaking.setQtyWetOld(hand.getQtyWet());

            stocktaking.setSubmitFlag("0");
            stocktaking.setRemarks(hand.getRemarks());

            stocktakingMapper.insert(stocktaking);


            transRecord.setCancelFlag("0");
            transRecord.setAddId(userId);
            transRecord.setAddTime(date);
            transRecord.setAddCode(stocktaking.getAddCode());
            transRecord.setAddName(stocktaking.getAddName());
            transRecord.setDeptCd(stocktaking.getDeptCd());
            transRecord.setAddDeptId(stocktaking.getAddDeptId());
            transRecord.setAddDeptName(stocktaking.getAddDeptName());
            transRecord.setAddDeptCode(stocktaking.getAddDeptCode());
            transRecord.setCompCd(stocktaking.getCompCd());
            transRecord.setItemCd(stocktaking.getItemCd());
            transRecord.setItemNm(stocktaking.getItemNm());
            transRecord.setItemType(stocktaking.getItemType());

            transRecord.setUom(hand.getUom());
            transRecord.setStoreCd(stocktaking.getStoreCd());
            transRecord.setStoreNm(stocktaking.getStoreNm());
            transRecord.setAreaCd(stocktaking.getAreaCd());
            transRecord.setStackCd(stocktaking.getStackCd());
            transRecord.setQtyWet(stocktaking.getQtyWet());
            transRecord.setTransactionDate(date);
            transRecord.setTransactionTable("STO_ONHAND");
            transRecord.setTransactionId(onhand.getId());
            if (transRecord.getTransactionType().equals("A7")){
                transRecord.setRemarks("盘库操作，盘盈:" + transRecord.getQty());
            } else {
                transRecord.setRemarks("盘库操作，盘亏:" + transRecord.getQty());
            }

            transRecordMapper.insert(transRecord);


        } else {
            resultJSON = JSONTools.toResultJSON(500,"盘库失败，当前数据不存在！");
            return resultJSON;
        }

        resultJSON = JSONTools.toResultJSON(hand);
        return resultJSON;
    }

    @Override
    @Transactional
    public ResultJSON saveDk(String userId, String json) {
        Date date = new Date();
        Onhand hand = JSON.parseObject(json, new TypeReference<Onhand>() {
        });
        // 当前库房
        String id = hand.getId();

        // 数量
        BigDecimal qty = hand.getQty();

        // 湿基量
        BigDecimal qtyWet = hand.getQtyWet();

        // 倒库数量
        BigDecimal qtyDk = hand.getQtyDk();

        // 倒库湿基量
        BigDecimal qtyWetDk = hand.getQtyWetDk();

        BigDecimal subtract = qty.subtract(qtyDk);

        hand.setQty(qty.subtract(qtyDk));
        hand.setQtyWet(qtyWet.subtract(qtyWetDk));

        QueryWrapper<Onhand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Onhand onhand = onhandMapper.selectOne(queryWrapper);

        // 先执行 库存 减少
        JSONObject paramMap = new JSONObject();
        paramMap.put("compCd",onhand.getCompCd());
        paramMap.put("deptCd",onhand.getDeptCd());
        paramMap.put("itemCd",onhand.getItemCd());
        paramMap.put("itemNm",onhand.getItemNm());
        paramMap.put("qty",hand.getQtyDk());
        paramMap.put("qtyWet",hand.getQtyWetDk());
        paramMap.put("uom",onhand.getUom());
        paramMap.put("storeCd",onhand.getStoreCd());
        paramMap.put("areaCd",onhand.getAreaCd());
        paramMap.put("stackCd",onhand.getStackCd());
        paramMap.put("transactionType","B6");
        paramMap.put("transactionDate",date);
        paramMap.put("transactionTable","STO_ONHAND");
        paramMap.put("transactionId", onhand.getId());
        paramMap.put("remarks", "倒库操作，当前库存减少数量："+hand.getQtyDk());
        paramMap.put("userId",userId);

        ResultJSON substract = transRecordService.add(userId, paramMap.toString());
        resultJSON = JSONTools.toResultJSON(hand);
        if (substract.getCode() != 0){
            resultJSON = JSONTools.toResultJSON(500,substract.getMsg());
        }


        // 先执行 库存 增加
        JSONObject paramMapAdd = new JSONObject();
        paramMapAdd.put("compCd",onhand.getCompCd());
        paramMapAdd.put("deptCd",onhand.getDeptCd());
        paramMapAdd.put("itemCd",onhand.getItemCd());
        paramMapAdd.put("itemNm",onhand.getItemNm());
        paramMapAdd.put("qty",hand.getQtyDk());
        paramMapAdd.put("qtyWet",hand.getQtyWetDk());
        paramMapAdd.put("uom",onhand.getUom());
        paramMapAdd.put("storeCd",hand.getStoreCdMB());
        paramMapAdd.put("areaCd",hand.getAreaCdMB());
        paramMapAdd.put("stackCd",hand.getStackCdMB());
        paramMapAdd.put("transactionType","B5");
        paramMapAdd.put("transactionDate",date);
        paramMapAdd.put("transactionTable","STO_ONHAND");
        paramMapAdd.put("transactionId", onhand.getId());
        paramMapAdd.put("userId",userId);
        paramMapAdd.put("remarks", "倒库操作，当前库存增加数量："+hand.getQtyDk() + ";"+hand.getRemarks());
//        paramMapAdd.put("remarksOnhand", hand.getRemarks());

        ResultJSON add = transRecordService.add(userId, paramMapAdd.toString());
        if (add.getCode() != 0){
            resultJSON = JSONTools.toResultJSON(500,add.getMsg());
        }

        ResultJSON userInfoRes = frameworkService.queryUserInfoById(userId, "");
        SysUser user = null;
        List<SysDepartment> deptTreeList = null;
        List<SysDepartment> deptsList = null;
        if (userInfoRes.getCode() == 0) {
            Map<String, Object> mapInfo = JSON.parseObject(userInfoRes.getData().toString(), new TypeReference<Map<String, Object>>() {
            });
            user = JSON.parseObject(mapInfo.get("user").toString(), new TypeReference<SysUser>() {
            });
            deptTreeList = JSON.parseArray(mapInfo.get("deptTree").toString(), SysDepartment.class);
            deptsList = JSON.parseArray(mapInfo.get("depts").toString(), SysDepartment.class);
        }

        // 倒库记录添加。
        Reverse reverse = new Reverse();
        reverse.setCancelFlag("0");
        reverse.setAddId(userId);
        reverse.setAddTime(date);
        if (!org.apache.commons.lang3.ObjectUtils.isEmpty(user)) {
            reverse.setAddCode(user.getUserCode());
            reverse.setAddName(user.getUserDesc());

        }
        if (!CollectionUtils.isEmpty(deptsList)) {
            reverse.setDeptCd(deptsList.get(0).getDeptCode());
            reverse.setAddDeptId(deptsList.get(0).getId());
            reverse.setAddDeptName(deptsList.get(0).getDeptName());
            reverse.setAddDeptCode(deptsList.get(0).getDeptCode());
        }
        if (!CollectionUtils.isEmpty(deptTreeList)) {
            reverse.setCompCd(deptTreeList.get(0).getDeptCode());
        }
        reverse.setStoreCd(onhand.getStoreCd());
        reverse.setStoreNm(hand.getStoreNm());
        reverse.setAreaCd(onhand.getAreaCd());
        reverse.setStackCd(onhand.getStackCd());
        reverse.setStoreCdNew(hand.getStoreCdMB());
        reverse.setStoreNmNew(hand.getStoreNmMB());
        reverse.setAreaCdNew(hand.getAreaCdMB());
        reverse.setStackCdNew(hand.getStackCdMB());
        reverse.setItemCd(onhand.getItemCd());
        reverse.setItemNm(onhand.getItemNm());
        reverse.setItemType(onhand.getItemType());
        reverse.setOum(onhand.getUom());
        reverse.setQty(hand.getQtyDk());
        reverse.setQtyWet(hand.getQtyWetDk());
        reverse.setReverseDate(date);
        reverse.setReverseUser(reverse.getAddName());
        reverse.setSubmitFlag("0");
        reverse.setRemarks(hand.getRemarks());
        reverseMapper.insert(reverse);



        return resultJSON;
    }

    @Override
    public ResultJSON savePick(String userId, String json) {
        Date date = new Date();
        Onhand hand = JSON.parseObject(json, new TypeReference<Onhand>() {
        });
        ResultJSON userInfoRes = frameworkService.queryUserInfoById(userId, "");
        SysUser user = null;
        List<SysDepartment> deptTreeList = null;
        List<SysDepartment> deptsList = null;
        if (userInfoRes.getCode() == 0) {
            Map<String, Object> mapInfo = JSON.parseObject(userInfoRes.getData().toString(), new TypeReference<Map<String, Object>>() {
            });
            user = JSON.parseObject(mapInfo.get("user").toString(), new TypeReference<SysUser>() {
            });
            deptTreeList = JSON.parseArray(mapInfo.get("deptTree").toString(), SysDepartment.class);
            deptsList = JSON.parseArray(mapInfo.get("depts").toString(), SysDepartment.class);
        }
        String id = hand.getId();
        QueryWrapper<Onhand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Onhand onhand = onhandMapper.selectOne(queryWrapper);

        // 领料记录添加。
        PickRecord record = new PickRecord();
        record.setCancelFlag("0");
        record.setAddId(userId);
        record.setAddTime(date);
        if (!org.apache.commons.lang3.ObjectUtils.isEmpty(user)) {
            record.setAddCode(user.getUserCode());
            record.setAddName(user.getUserDesc());
        }
        StringBuffer stringBuffer = new StringBuffer("LL");
        if (!CollectionUtils.isEmpty(deptsList)) {
            record.setDeptCd(deptsList.get(0).getDeptCode());
            record.setAddDeptId(deptsList.get(0).getId());
            record.setAddDeptName(deptsList.get(0).getDeptName());
            record.setAddDeptCode(deptsList.get(0).getDeptCode());
            stringBuffer.append(deptsList.get(0).getDeptCode());
        }
        if (!CollectionUtils.isEmpty(deptTreeList)) {
            record.setCompCd(deptTreeList.get(0).getDeptCode());
        }
        record.setStoreCd(onhand.getStoreCd());
        record.setStoreNm(onhand.getStoreNm());
        record.setAreaCd(onhand.getAreaCd());
        record.setStackCd(onhand.getStackCd());

        record.setItemCd(onhand.getItemCd());
        record.setItemNm(onhand.getItemNm());
        record.setItemType(onhand.getItemType());
        record.setUom(onhand.getUom());
        record.setQty(hand.getQtyNew());
        record.setQtyWet(hand.getQtyWetNew());
        record.setRemarks(hand.getRemarks());
        record.setPickType("1"); // 0退料/1领料
        record.setPickDate(date);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(date);
        String startTime = format + " 00:00:00";
        String endTime = format + " 23:59:59";
        String max = pickRecordMapper.getMax(startTime, endTime);

        String dateStr = new SimpleDateFormat("yyyyMMdd").format(date);

        stringBuffer.append(dateStr);
        if (!StringUtils.isEmpty(max)) {
            Long count = Long.valueOf(max);
            String lsh = "";
            if (max.length() > 4) {
                Long lCount = (count + 1l);
                lsh = lCount.toString();
            } else {
                lsh = String.format("%04d", count + 1l);
            }
            stringBuffer.append(lsh);
        } else {
            stringBuffer.append("0001");
        }
        record.setPickRecordNo(stringBuffer.toString());
        pickRecordMapper.insert(record);

        // 执行 库存 减少
        JSONObject paramMap = new JSONObject();
        paramMap.put("compCd",onhand.getCompCd());
        paramMap.put("deptCd",onhand.getDeptCd());
        paramMap.put("itemCd",onhand.getItemCd());
        paramMap.put("itemNm",onhand.getItemNm());
        paramMap.put("qty",hand.getQtyNew());
        paramMap.put("qtyWet",hand.getQtyWetNew());
        paramMap.put("uom",onhand.getUom());
        paramMap.put("storeCd",onhand.getStoreCd());
        paramMap.put("areaCd",onhand.getAreaCd());
        paramMap.put("stackCd",onhand.getStackCd());
        paramMap.put("transactionType","B8");
        paramMap.put("transactionDate",date);
        paramMap.put("transactionTable","STO_PICK_RECORD");
        paramMap.put("transactionId", record.getId());
        paramMap.put("remarks", "库房领用操作，当前库存减少数量："+hand.getQtyNew());
        paramMap.put("userId",userId);

        ResultJSON substract = transRecordService.add(userId, paramMap.toString());
        resultJSON = JSONTools.toResultJSON(hand);
        if (substract.getCode() != 0){
            resultJSON = JSONTools.toResultJSON(500,substract.getMsg());
            return resultJSON;
        }





        return resultJSON;
    }
}
