package com.th.demo.service.impl.ware;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.auth0.jwt.internal.org.bouncycastle.jce.exception.ExtCertificateEncodingException;
import com.th.demo.mapping.maint.*;
import com.th.demo.mapping.system.SysUserMapper;
import com.th.demo.mapping.ware.*;
import com.th.demo.model.finance.AccountDetail;
import com.th.demo.model.maint.*;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.SysUser;
import com.th.demo.model.system.Type;
import com.th.demo.model.ware.*;
import com.th.demo.service.ware.InService;
import com.th.demo.service.ware.OutService;
import com.th.demo.service.ware.StoreService;
import com.th.demo.tools.JSONTools;
import com.th.demo.tools.Tools;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service("InService")
public class InServiceImpl implements InService {

    @Autowired
    public InRecordMapper inRecordMapper;
    @Autowired
    public StoreMapper storeMapper;
    @Autowired
    public WareMapper wareMapper;
    @Autowired
    public StackMapper stackMapper;
    @Autowired
    public CustomerMapper customerMapper;
    @Autowired
    public MaterialMapper materialMapper;
    @Autowired
    public ProdPriceMapper prodPriceMapper;
    @Autowired
    public SysUserMapper sysUserMapper;
    @Autowired
    public PriceMapper priceMapper;
    @Autowired
    public OrdTractMapper ordTractMapper;
    @Autowired
    public OutRecordMapper outRecordMapper;
    @Autowired
    public DistributionMapper distributionMapper;
    @Autowired
    public DistributionDetailMapper distributionDetailMapper;
    String result = Type.FAIL;
    int num = 0;

    @Transactional
    @Override
    public String in(String json, String userId, String belongId) {


        int currentLayer = 0;
        Store store = JSON.parseObject(json, new TypeReference<Store>() {
        });
        int effStoreCnt = storeMapper.selEffStore(store.getPlateNo());
        if(effStoreCnt > 0){
            return JSONTools.toString(1,"库存中已存在板号"+store.getPlateNo()+",请勿重复入库");
        }
        Stack stack = store.getStack();//stackMapper.selectByPrimaryKey(store.getStack().getId());



        if (stack.getIsLayer().equals("1")) {
            currentLayer = Integer.parseInt(storeMapper.selectLayerByStackId(stack.getId(), belongId));
        } else if (stack.getIsLayer().equals("2")){
            currentLayer = store.getLayer()-1;
        }  else{
            currentLayer = -1;
        }

        store.setLayer(currentLayer + 1);
        store.setAddId(userId);
        store.setAddTime(new Date());
        store.setCancelFlag("0");
        store.setOutFlag("0");
        store.setLockFlag("0");
        store.setBelongId(belongId);
        ProdPrice prodPrice = new ProdPrice();

        prodPrice = prodPriceMapper.selectByProdNm(store.getMaterial().getName());
        if(prodPrice == null){
            return JSONTools.toString(1,store.getMaterial().getName()+"价格未维护");
        }
        store.setInvoicePrice(prodPrice.getInvoicePrice());

        InRecord inRecord = getInRecord(store, "1", userId);
        num = inRecordMapper.insert(inRecord);


        store.setInId(inRecord.getId());
        num = storeMapper.insert(store);

        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }


    @Override
    public String inImport(String json, String userId, String belongId) throws Exception {
        long startTime = System.currentTimeMillis();

        List<JSONObject> listJO = JSON.parseObject(json, new TypeReference<List<JSONObject>>() { });

        List<Store> listStore = objToStore(listJO, userId, belongId);
        List storeArray = new ArrayList();
        //检查库存中是否已经有导入的板号或者excel本身板号有重复
        for(int i = 0 ; i < listStore.size();i++){
            if(storeArray.contains(listStore.get(i).getPlateNo())){
                return JSONTools.toString(1,"导入数据板号"+listStore.get(i).getPlateNo()+"重复,请勿重复入库");
            }else{
                storeArray.add(listStore.get(i).getPlateNo());
            }
            int effStoreCnt = storeMapper.selEffStore(listStore.get(i).getPlateNo());
            if(effStoreCnt > 0){
                return JSONTools.toString(1,"库存中已存在板号"+listStore.get(i).getPlateNo()+",请勿重复入库");
            }
        }


        resortAndInsert(listStore, userId, belongId);
        result = Tools.moreThanZeroResultJSON(1);
        long endTime = System.currentTimeMillis();
        float seconds = (endTime - startTime) / 1000F;
        System.out.println(Float.toString(seconds) + " seconds.");

        return result;
    }

    @Override
    public String back(String json, String userId, String belongId) {
        List<Store> listStore = JSON.parseObject(json, new TypeReference<List<Store>>() {
        });
        Stack stack = stackMapper.selectByPrimaryKey(listStore.get(0).getStack().getId());

        if (stack.getIsLayer().equals("0")) {
            result = JSONTools.toString("");
        } else {
            Collections.sort(listStore);
            int currentLayer = Integer.parseInt(storeMapper.selectLayerByStackId(listStore.get(0).getStack().getId(), belongId));
            int min = listStore.get(0).getLayer();
            int max = listStore.get(listStore.size() - 1).getLayer();
//            if (currentLayer != max) {
//                result = JSONTools.toString(ResponseCodeMsg.CODE_EX, "必须包含最上层取消出库！");
//            } else if (!WareTools.checkOrder(listStore)) {
//                result = JSONTools.toString(ResponseCodeMsg.CODE_EX, "请选中连续数据取消出库！");
//            } else {
//
//            }
            for (int i = 0; i < listStore.size(); i++) {
                int effStoreCnt = storeMapper.selEffStore(listStore.get(i).getPlateNo());
                if(effStoreCnt == 0){
                    return JSONTools.toString(1,"库存中不存在板号"+listStore.get(i).getPlateNo()+",无法取消出库");
                }
                InRecord inRecord = getInRecord(listStore.get(i), "0", userId);
                num += inRecordMapper.insert(inRecord);

                //listStore.get(i).setOutId(outRecord.getId());
                listStore.get(i).setCancelFlag("1");
                num += storeMapper.updateByPrimaryKey(listStore.get(i));
                num += inRecordMapper.deleteByPrimaryKey(listStore.get(i).getInId());

            }
            result = Tools.moreThanZeroResultJSON(num);
        }
        return result;
    }

    @Override
    public String inImportAll(String json, String userId, String belongId) throws ParseException {
        List<JSONObject> listJO = JSON.parseObject(json, new TypeReference<List<JSONObject>>() { });
        Store store = new Store();
        Distribution distribution = new Distribution();
        String err = Type.SUCCESS;
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < listJO.size(); i++){
            String wareName = listJO.get(i).get("仓库名称").toString();
            String stackName = listJO.get(i).get("垛位号").toString();
            String materialName = listJO.get(i).get("品名").toString();
            String standard = listJO.get(i).get("钢种").toString();
            String customerName = listJO.get(i).get("客户名称").toString();
            String fk_compony_name = listJO.get(i).get("货权单位").toString();
            //验证信息增加多条信息验证
            Ware ware = wareMapper.selectByWareName(wareName, userId, belongId);
            if (ware == null) {
                err += "未找到名为\"" + wareName + "\"的库房；";
            }
            Stack stack = stackMapper.selectByWareNameStackName(wareName, stackName, userId, belongId);
            if (stack == null) {
                err += "未找到名为\"" + stackName + "\"的垛位号；";
            }
            // 将传入的材料品名和钢种进行校验
            Material material = new Material();
            List<Material> materialList = materialMapper.selectListByNameStandard(materialName, standard, belongId);
            if (materialList.size() == 0 ){
                err += "未找到名为\"" + materialName + "，" + standard + "\"的物料类型；";
            } else if (materialList.size() > 1) {
                err += "名为\"" + materialName + "，" + standard + "\"的物料类型维护多条；";
            }else {
                material= materialList.get(0);
            }

            // 将传入的客户名称进行校验
            Customer customer = new Customer();
            List<Customer> customerList = customerMapper.selectByNameCode(customerName,customerName,belongId);
            if (customerList.size() == 0){
                err += "未找到全名或简称为\"" + customerName + "\"的客户名称；";
            } else if (customerList.size() > 1) {
                err += "全名或简称为\"" + customerName + "\"的客户名称维护多条；";
            }else{
                customer = customerList.get(0);
            }

            // 将传入的客户名称进行校验
            Customer fk_compony = new Customer();
            List<Customer> customerList2 = customerMapper.selectByNameCode(fk_compony_name,fk_compony_name,belongId);
            if (customerList2.size() == 0){
                err += "未找到全名或简称为\"" + fk_compony_name + "\"的客户名称；";
            } else if (customerList2.size() > 1) {
                err += "全名或简称为\"" + fk_compony_name + "\"的客户名称维护多条；";
            }else{
                fk_compony = customerList2.get(0);
            }
            store.setWare(ware);
            store.setStack(stack);
            store.setMaterial(material);
            store.setCustomer(customer);
            store.setFkComponyId(fk_compony.getId());
            store.setLayer(Integer.parseInt(listJO.get(i).get("层号").toString()));
            store.setPlateNo(listJO.get(i).get("产品编号").toString());
            store.setModel(listJO.get(i).get("规格型号").toString());
            store.setCount(Double.parseDouble(listJO.get(i).get("数量").toString()));
            store.setWeight(Double.parseDouble(listJO.get(i).get("重量").toString()));
            if (listJO.get(i).get("收货地址") != null && !listJO.get(i).get("收货地址").toString().equals("")) {
                store.setReceiveAddress(listJO.get(i).get("收货地址").toString());
            }

            if(listJO.get(i).get("备注") != null && !listJO.get(i).get("备注").toString().equals("")){
                store.setRemark(listJO.get(i).get("备注").toString());
            }

            store.setAddId(userId);
            store.setAddTime(df2.parse(listJO.get(i).get("入库时间").toString()));
            store.setCancelFlag("0");
            store.setBelongId(belongId);
            if(listJO.get(i).get("计量方式") != null && !listJO.get(i).get("计量方式").toString().equals("")){
                store.setWgtDcnMtcCd(listJO.get(i).get("计量方式").toString());
            }
            if(listJO.get(i).get("产地") != null && !listJO.get(i).get("产地").toString().equals("")){
                store.setProductionPlace(listJO.get(i).get("产地").toString());
            }
            if(listJO.get(i).get("合约号") != null && !listJO.get(i).get("合约号").toString().equals("")){
                store.setContractNo(listJO.get(i).get("合约号").toString());
            }
            if(listJO.get(i).get("订单号") != null && !listJO.get(i).get("订单号").toString().equals("")){
                store.setOrdNo(listJO.get(i).get("订单号").toString());
            }
            if(listJO.get(i).get("入库车号") != null && !listJO.get(i).get("入库车号").toString().equals("")){
                store.setCarNo(listJO.get(i).get("入库车号").toString());
            }
            if(listJO.get(i).get("吊装工") != null && !listJO.get(i).get("吊装工").toString().equals("")){
                store.setTallyPeople(listJO.get(i).get("吊装工").toString());
            }
            if(listJO.get(i).get("捆包号") != null && !listJO.get(i).get("捆包号").toString().equals("")){
                store.setPackNo(listJO.get(i).get("捆包号").toString());
            }
            InRecord inRecord = getInRecord(store, "1", userId);
            inRecord.setAddTime(df2.parse(listJO.get(i).get("入库时间").toString()));
            num += inRecordMapper.insert(inRecord);

            store.setOutFlag("0");
            store.setLockFlag("0");
            store.setOutId("");
            store.setInId(inRecord.getId());
            num += storeMapper.insert(store);

            if(listJO.get(i).get("出库时间")!= null && !listJO.get(i).get("出库时间").equals("")){
                Distribution isExist = distributionMapper.selectByDisNo(listJO.get(i).get("出库单号").toString(),belongId);
                if (isExist == null) {
                    distribution.setTruckNo(listJO.get(i).get("出库车号").toString());
                    distribution.setAddId(userId);
                    distribution.setAddTime(new Date());
                    distribution.setOutFlag("1");
                    distribution.setBelongId(belongId);
                    distribution.setSumCount(store.getCount());
                    distribution.setSumWeight(store.getWeight());
                    num += distributionMapper.insert(distribution);
                    DistributionDetail detail = getDistributionDetail(distribution, store, userId, belongId);
                    num += distributionDetailMapper.insert(detail);
                } else {
                    distribution.setModifyId(userId);
                    distribution.setModifyTime(new Date());
                    distribution.setSumCount(distribution.getSumCount()+store.getCount());
                    distribution.setSumWeight(distribution.getSumWeight()+store.getWeight());
                    num += distributionMapper.updateByPrimaryKey(distribution);
                    DistributionDetail detail = getDistributionDetail(distribution, store, userId, belongId);
                    num += distributionDetailMapper.insert(detail);
                }

                OutRecord outRecord = getOutRecord(store, distribution.getId(), "1", userId);
                outRecord.setAddTime(df2.parse(listJO.get(i).get("出库时间").toString()));
                num += outRecordMapper.insert(outRecord);

                store.setOutId(outRecord.getId());
                store.setOutFlag("1");
                store.setLockFlag("1");
                num += storeMapper.updateByPrimaryKey(store);
            }


        }
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }

    private List<Store> resortAndInsert(List<Store> listStore, String userId, String belongId) throws Exception {
        for (int i = 0; i < listStore.size(); i++) {
            Store temp = listStore.get(i);
            List<Store> listSameStack = new ArrayList<>();

            for (int j = 0; j < listStore.size(); j++) {
                if (listStore.get(j).getWare().equals(temp.getWare()) && listStore.get(j).getStack().equals(temp.getStack())) {
                    listSameStack.add(listStore.get(j));
                    listStore.remove(j);
                    j--;
                }
            }

            Collections.sort(listSameStack);
            Stack stack = stackMapper.selectByPrimaryKey(listSameStack.get(0).getStack().getId());
            num = insert(listSameStack, stack, userId, belongId);
            i--;


            for (int j = 0; j < listStore.size(); j++) {
                if (i != j && listStore.get(j).getWare().equals(temp.getWare()) && listStore.get(j).getStack().equals(temp.getStack())) {
                    if (temp.getLayer() > listStore.get(j).getLayer()) {
                        temp = listStore.get(j);
                    }
                }
            }

            int currentLayer = Integer.parseInt(storeMapper.selectLayerByStackId(temp.getStack().getId(), belongId));
            if (currentLayer + 1 != temp.getLayer()) {
                int c = temp.getLayer() - currentLayer - 1;
                for (int a = 0; a < listStore.size(); a++) {
                    if (listStore.get(a).getWare().equals(temp.getWare()) && listStore.get(a).getStack().equals(temp.getStack())) {
                        listStore.get(i).setLayer(listStore.get(i).getLayer() + c);
                    }
                }
            }


        }
        return listStore;
    }

    private int insert(List<Store> listSameStack, Stack stack, String userId, String belongId) throws Exception{
        int currentLayer = 0;
        if (stack.getIsLayer().equals("1")) {
            currentLayer = Integer.parseInt(storeMapper.selectLayerByStackId(stack.getId(), belongId));
        } else {
            currentLayer = -1;
        }
        ProdPrice prodPrice = new ProdPrice();
        for (int m = 0; m < listSameStack.size(); m++) {

            if (stack.getIsLayer().equals("1")) {
                listSameStack.get(m).setLayer(currentLayer + m + 1);
            } else {
                listSameStack.get(m).setLayer(0);
            }
            listSameStack.get(m).setAddId(userId);
            listSameStack.get(m).setAddTime(new Date());
            listSameStack.get(m).setCancelFlag("0");
            listSameStack.get(m).setBelongId(belongId);
//            prodPrice = prodPriceMapper.selectByProdNm(listSameStack.get(m).getMaterial().getName());
//            if(prodPrice == null){
//                throw new Exception(listSameStack.get(m).getMaterial().getName()+"价格未维护");
//            }
            // listSameStack.get(m).setInvoicePrice(prodPrice.getInvoicePrice());
            List<Price> priceList = priceMapper.selectListByCustomer(listSameStack.get(m).getFkComponyId(),belongId,listSameStack.get(m).getMaterial().getName());
            if (priceList.size() >= 1){
                listSameStack.get(m).setKeepDay(priceList.get(0).getKeepDay());
                listSameStack.get(m).setOrdPrice(priceList.get(0).getOrdPrice());
                listSameStack.get(m).setOutOrdPrice(priceList.get(0).getOutOrdPrice());
            }
            List<OrdTract> ordTractList = ordTractMapper.selectByOrdNo(listSameStack.get(m).getOrdNo(),belongId);
            if (ordTractList.size() >= 1){
                listSameStack.get(m).setPayWay(ordTractList.get(0).getPayWay());
            }
            InRecord inRecord = getInRecord(listSameStack.get(m), "1", userId);
            num += inRecordMapper.insert(inRecord);

            listSameStack.get(m).setInId(inRecord.getId());
            num += storeMapper.insert(listSameStack.get(m));

        }

        return  num;
    }


    private List<Store> objToStore(List<JSONObject> listJO, String userId, String belongId) throws Exception {
        String err = Type.SUCCESS;
        List<Store> listStore = new ArrayList<>();
        String wareName = listJO.get(0).get("仓库名称").toString();
        String stackName = listJO.get(0).get("垛位号").toString();
        String materialName = listJO.get(0).get("品名").toString();
        String standard = listJO.get(0).get("钢种").toString();
        String customerName = listJO.get(0).get("客户名称").toString();
        String fk_compony_name = listJO.get(0).get("货权单位").toString();
        //验证信息增加多条信息验证
        Ware ware = wareMapper.selectByWareName(wareName, userId, belongId);
        if (ware == null) {
            err += "未找到名为\"" + wareName + "\"的库房；";
            throw new Exception(err);
        }
        Stack stack = stackMapper.selectByWareNameStackName(wareName, stackName, userId, belongId);
        if (stack == null) {
            err += "未找到名为\"" + stackName + "\"的垛位号；";
            throw new Exception(err);
        }
        // 将传入的材料品名和钢种进行校验
        Material material = new Material();
        List<Material> materialList = materialMapper.selectListByNameStandard(materialName, standard, belongId);
        if (materialList.size() == 0 ){
            err += "未找到名为\"" + materialName + "，" + standard + "\"的物料类型；";
            throw new Exception(err);
        } else if (materialList.size() > 1) {
            err += "名为\"" + materialName + "，" + standard + "\"的物料类型维护多条；";
            throw new Exception(err);
        }else {
            material= materialList.get(0);
        }

        // 将传入的客户名称进行校验
        Customer customer = new Customer();
        List<Customer> customerList = customerMapper.selectByNameCode(customerName,customerName,belongId);
        if (customerList.size() == 0){
            err += "未找到全名或简称为\"" + customerName + "\"的客户名称；";
            throw new Exception(err);
        } else if (customerList.size() > 1) {
            err += "全名或简称为\"" + customerName + "\"的客户名称维护多条；";
            throw new Exception(err);
        }else{
            customer = customerList.get(0);
        }

        // 将传入的客户名称进行校验
        Customer fk_compony = new Customer();
        List<Customer> customerList2 = customerMapper.selectByNameCode(fk_compony_name,fk_compony_name,belongId);
        if (customerList2.size() == 0){
            err += "未找到全名或简称为\"" + fk_compony_name + "\"的客户名称；";
            throw new Exception(err);
        } else if (customerList2.size() > 1) {
            err += "全名或简称为\"" + fk_compony_name + "\"的客户名称维护多条；";
            throw new Exception(err);
        }else{
            fk_compony = customerList2.get(0);
        }

//        //检查入库操作人
//        SysUser sysUser = new SysUser();
//        List<SysUser> sysUserList = sysUserMapper.selectUser(addUserName,"");

        for (int i = 0; i < listJO.size(); i++) {
            Store store = new Store();
            if (listJO.get(i).get("仓库名称").toString().equals(wareName)) {
                store.setWare(ware);
            } else {
                wareName = listJO.get(i).get("仓库名称").toString();
                ware = wareMapper.selectByWareName(wareName, userId, belongId);
                if (ware == null) {
                    err += "未找到名为\"" + wareName + "\"的仓库名称；";
                    throw new Exception(err);
                }
                store.setWare(ware);
            }
            if (listJO.get(i).get("垛位号").toString().equals(stackName)) {
                store.setStack(stack);
            } else {
                stackName = listJO.get(i).get("垛位号").toString();
                stack = stackMapper.selectByWareNameStackName(wareName, stackName, userId, belongId);
                if (stack == null) {
                    err += "未找到名为\"" + stackName + "\"的垛位号；";
                    throw new Exception(err);
                }
                store.setStack(stack);
            }
            if (listJO.get(i).get("品名").toString().equals(materialName) && listJO.get(i).get("钢种").toString().equals(standard)) {
                store.setMaterial(material);
            } else {
                materialList = materialMapper.selectListByNameStandard(listJO.get(i).get("品名").toString(), listJO.get(i).get("钢种").toString(), belongId);
                if (materialList.size() == 0 ){
                    err += "未找到名为\"" + listJO.get(i).get("品名").toString() + "，" + listJO.get(i).get("钢种").toString() + "\"的物料类型；";
                    throw new Exception(err);
                } else if (materialList.size() > 1) {
                    err += "名为\"" + listJO.get(i).get("品名").toString() + "，" + listJO.get(i).get("钢种").toString() + "\"的物料类型维护多条；";
                    throw new Exception(err);
                }else {
                    store.setMaterial(materialList.get(0));
                }
            }

            if (listJO.get(i).get("客户名称").toString().equals(customerName)) {
                store.setCustomer(customer);
            } else {
                customerName = listJO.get(i).get("客户名称").toString();
                customerList = customerMapper.selectByNameCode(customerName,customerName,belongId);
                if (customerList.size() == 0){
                    err += "未找到全名或简称为\"" + customerName + "\"的客户名称；";
                    throw new Exception(err);
                } else if (customerList.size() > 1) {
                    err += "全名或简称为\"" + customerName + "\"的客户名称维护多条；";
                    throw new Exception(err);
                }else{
                    store.setCustomer(customerList.get(0));
                }
            }

            if (listJO.get(i).get("货权单位").toString().equals(fk_compony_name)) {
                store.setFkComponyId(fk_compony.getId());
            } else {
                fk_compony_name = listJO.get(i).get("货权单位").toString();
                customerList2 = customerMapper.selectByNameCode(fk_compony_name,fk_compony_name,belongId);
                if (customerList2.size() == 0){
                    err += "未找到全名或简称为\"" + fk_compony_name + "\"的客户名称；";
                    throw new Exception(err);
                } else if (customerList2.size() > 1) {
                    err += "全名或简称为\"" + fk_compony_name + "\"的客户名称维护多条；";
                    throw new Exception(err);
                }else {
                    store.setFkComponyId(customerList2.get(0).getId());
                }

            }
            store.setLayer(Integer.parseInt(listJO.get(i).get("层号").toString()));
            store.setPlateNo(listJO.get(i).get("产品编号").toString());
            store.setModel(listJO.get(i).get("规格型号").toString());
            store.setCount(Double.parseDouble(listJO.get(i).get("数量").toString()));
            store.setWeight(Double.parseDouble(listJO.get(i).get("重量").toString()));
            if (listJO.get(i).get("收货地址") != null && !listJO.get(i).get("收货地址").toString().equals("")) {
                store.setReceiveAddress(listJO.get(i).get("收货地址").toString());
            }

            if(listJO.get(i).get("备注") != null && !listJO.get(i).get("备注").toString().equals("")){
                store.setRemark(listJO.get(i).get("备注").toString());
            }

            store.setAddId(userId);
            store.setAddTime(new Date());
            store.setCancelFlag("0");
            store.setOutFlag("0");
            store.setLockFlag("0");
            store.setBelongId(belongId);
            if(listJO.get(i).get("计量方式") != null && !listJO.get(i).get("计量方式").toString().equals("")){
                store.setWgtDcnMtcCd(listJO.get(i).get("计量方式").toString());
            }
            if(listJO.get(i).get("产地") != null && !listJO.get(i).get("产地").toString().equals("")){
                store.setProductionPlace(listJO.get(i).get("产地").toString());
            }
            if(listJO.get(i).get("合约号") != null && !listJO.get(i).get("合约号").toString().equals("")){
                store.setContractNo(listJO.get(i).get("合约号").toString());
            }

            if(listJO.get(i).get("订单号") != null && !listJO.get(i).get("订单号").toString().equals("")){
                store.setOrdNo(listJO.get(i).get("订单号").toString());
            }
            if(listJO.get(i).get("入库车号") != null && !listJO.get(i).get("入库车号").toString().equals("")){
                store.setCarNo(listJO.get(i).get("入库车号").toString());
            }
            if(listJO.get(i).get("吊装工") != null && !listJO.get(i).get("吊装工").toString().equals("")){
                store.setTallyPeople(listJO.get(i).get("吊装工").toString());
            }
            if(listJO.get(i).get("捆包号") != null && !listJO.get(i).get("捆包号").toString().equals("")){
                store.setPackNo(listJO.get(i).get("捆包号").toString());
            }
            listStore.add(store);
        }
        if (err.equals(Type.SUCCESS)) {
            return listStore;
        } else {
            throw new Exception(err);
        }
    }




    private InRecord getInRecord(Store store, String recordType, String userId) {
        InRecord inRecord = new InRecord();
        Ware ware =  wareMapper.selectByPrimaryKey(store.getWare().getId());
        inRecord.setWareName(ware.getName());
        inRecord.setStackName(store.getStack().getName());
        inRecord.setLayer(store.getLayer());
        inRecord.setMaterialName(store.getMaterial().getName());
        inRecord.setStandard(store.getMaterial().getStandard());
        inRecord.setModel(store.getModel());
        inRecord.setCustomerName(store.getCustomer().getName());
        inRecord.setFkComponyName(store.getFkComponyId());

        if (recordType.equals("0")) {
            inRecord.setCount(-1 * store.getCount());
            inRecord.setWeight(-1 * store.getWeight());
        } else {
            inRecord.setCount(store.getCount());
            inRecord.setWeight(store.getWeight());
        }
        inRecord.setRemark(store.getRemark());
        inRecord.setBelongId(store.getBelongId());
        inRecord.setPlateNo(store.getPlateNo());
        inRecord.setAddUser(new SysUser(userId));
        inRecord.setAddTime(new Date());
        inRecord.setRecordType(recordType);
        if (recordType.equals("0")){
            inRecord.setCancelFlag("9");
        }else {
            inRecord.setCancelFlag("0");
        }
        inRecord.setOutFlag("0");
        inRecord.setPackNo(store.getPackNo());
        inRecord.setContractNo(store.getContractNo());

        inRecord.setReceiveAddress(store.getReceiveAddress());

        inRecord.setWgtDcnMtcCd(store.getWgtDcnMtcCd());
        inRecord.setEdgeTy(store.getEdgeTy());
        inRecord.setProductionPlace(store.getProductionPlace());
        inRecord.setOrdNo(store.getOrdNo());
        inRecord.setCarNo(store.getCarNo());
        inRecord.setTallyPeople(store.getTallyPeople());

        return inRecord;
    }

    private OutRecord getOutRecord(Store store, String distributionId, String recordType, String userId) {
        OutRecord outRecord = new OutRecord();
        outRecord.setWareName(store.getWare().getName());
        outRecord.setStackName(store.getStack().getName());
        outRecord.setLayer(store.getLayer());
        outRecord.setMaterialName(store.getMaterial().getName());
        outRecord.setStandard(store.getMaterial().getStandard());
        outRecord.setModel(store.getModel());
        outRecord.setCustomerName(store.getCustomer().getName());
        if (recordType.equals("0")) {
            outRecord.setCount(-1 * store.getCount());
            outRecord.setWeight(-1 * store.getWeight());
        } else {
            outRecord.setCount(store.getCount());
            outRecord.setWeight(store.getWeight());
        }
        if (recordType.equals("0")) {
            outRecord.setCancelFlag("9");
        } else {
            outRecord.setCancelFlag("0");
        }

        outRecord.setPackNo(store.getPackNo());
        outRecord.setContractNo(store.getContractNo());

        outRecord.setRemark(store.getRemark());
        outRecord.setBelongId(store.getBelongId());
        outRecord.setPlateNo(store.getPlateNo());
        outRecord.setAddUser(new SysUser(userId));
        outRecord.setAddTime(new Date());
        outRecord.setInId(store.getInId());
        outRecord.setRecordType(recordType);
        outRecord.setReceiveAddress(store.getReceiveAddress());
        outRecord.setDistributionId(distributionId);

        outRecord.setWgtDcnMtcCd(store.getWgtDcnMtcCd());
        outRecord.setEdgeTy(store.getEdgeTy());
        outRecord.setProductionPlace(store.getProductionPlace());
        outRecord.setInvoicePrice(store.getInvoicePrice());
        return outRecord;
    }

    public DistributionDetail getDistributionDetail(Distribution distribution, Store store, String userId, String belongId) {
        DistributionDetail detail = new DistributionDetail();
        detail.setDistributionId(distribution.getId());
        detail.setStore(store);
        detail.setAddId(userId);
        detail.setAddTime(new Date());
        detail.setCancelFlag("0");
        detail.setBelongId(belongId);
        return detail;
    }
}
