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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.maint.StackMapper;
import com.th.demo.mapping.ware.SplitRecordMapper;
import com.th.demo.mapping.ware.StoreMapper;
import com.th.demo.model.maint.Stack;
import com.th.demo.model.system.SysUser;
import com.th.demo.model.system.Type;
import com.th.demo.model.ware.InRecord;
import com.th.demo.model.ware.SplitRecord;
import com.th.demo.model.ware.Store;
import com.th.demo.service.ware.SplitService;
import com.th.demo.tools.JSONTools;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("SplitService")
public class SplitServiceImpl implements SplitService {

    @Autowired
    public SplitRecordMapper splitRecordMapper;
    @Autowired
    public StoreMapper storeMapper;
    @Autowired
    public StackMapper stackMapper;

    String result = Type.FAIL;
    int num = 0;

    @Transactional
    @Override
    public String split(String json, String splitCount, String splitWeight, String userId, String belongId) {
        List<Store> listStore = JSON.parseObject(json, new TypeReference<List<Store>>() {
        });
        Store store = listStore.get(0);
        int currentLayer = Integer.parseInt(storeMapper.selectLayerByStackId(store.getStack().getId(), belongId));
        Stack stack = store.getStack();//stackMapper.selectByPrimaryKey(store.getStack().getId());
        Double d_splitCount = Double.parseDouble(splitCount);
        Double d_splitWeight = Double.parseDouble(splitWeight);
        Double d_orginalCount = store.getCount();
        Double d_orginalWeight = store.getWeight();
        Double d_remainCount = d_orginalCount - d_splitCount;
        Double d_remainWeight = d_orginalWeight - d_splitWeight;
        int orignalLayer = 0;
        int splitLayer = 0;
        int remainLayer = 0;
        String orginalStoreId = "";
        String splitStoreId = "";
        String remainStoreId = "";


        store.setCancelFlag("7");
        num += storeMapper.updateByPrimaryKey(store);

        orginalStoreId = store.getId();
        orignalLayer = store.getLayer();

        if (stack.getIsLayer().equals("0")) {
            store.setLayer(0);
        } else if (stack.getIsLayer().equals("1")) {
            //store.setLayer(0);
        } else if (stack.getIsLayer().equals("2")) {
            //  store.setLayer(0);
        }
        store.setCancelFlag("0");
        store.setOutFlag("0");
        store.setCount(d_remainCount);
        store.setWeight(d_remainWeight);
        store.setModifyId(userId);
        store.setModifyTime(new Date());
        num += storeMapper.insert(store);
        remainStoreId = store.getId();
        remainLayer = store.getLayer();



        if (stack.getIsLayer().equals("0")) {
            store.setLayer(0);
        } else if (stack.getIsLayer().equals("1")) {
            List<Store> listUp = storeMapper.selectForUpDown(store.getWare().getId(), store.getStack().getId(), userId, belongId, store.getLayer() + 1, currentLayer);
            for (int i = 0; i < listUp.size(); i++) {
                listUp.get(i).setLayer(listUp.get(i).getLayer() + 1);
                num += storeMapper.updateByPrimaryKey(listUp.get(i));
            }
            store.setLayer(store.getLayer() + 1);
        } else if (stack.getIsLayer().equals("2")) {

        }
        store.setCancelFlag("0");
        store.setOutFlag("0");
        store.setCount(d_splitCount);
        store.setWeight(d_splitWeight);
        store.setModifyId(userId);
        store.setModifyTime(new Date());
        num += storeMapper.insert(store);
          splitStoreId = store.getId();
        splitLayer = store.getLayer();

        SplitRecord splitRecordSplit = getSplitRecord(store,
                d_orginalCount, d_orginalWeight,
                d_splitCount, d_splitWeight,
                d_remainCount, d_remainWeight,
                orginalStoreId, splitStoreId, remainStoreId,
                orignalLayer,splitLayer,remainLayer,
                userId);
        splitRecordMapper.insert(splitRecordSplit);


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

    private SplitRecord getSplitRecord(Store store,
                                       Double orginalCount, Double orginalWeight,
                                       Double splitCount, Double splitWeight,
                                       Double remainCount,Double remainWeight,
                                       String orginalStoreId, String splitStoreId, String remainStoreId,
                                       int orignalLayer ,  int splitLayer ,  int remainLayer ,
                                       String userId) {

        SplitRecord record = new SplitRecord();
        record.setWareName(store.getWare().getName());
        record.setStackName(store.getStack().getName());
        record.setOrignalLayer(orignalLayer);
        record.setSplitLayer(splitLayer);
        record.setRemainLayer(remainLayer);
        record.setMaterialName(store.getMaterial().getName());
        //record.setMaterialNo(store.getMaterialNo());
        record.setStandard(store.getMaterial().getStandard());
        record.setModel(store.getModel());
        record.setCustomerName(store.getCustomer().getName());
        // record.setCustomerNo(store.getCustomerNo());
        record.setOrignalCount(orginalCount);
        record.setOrignalWeight(orginalWeight);
        record.setSplitCount(splitCount);
        record.setSplitWeight(splitWeight);
        record.setRemainCount(remainCount);
        record.setRemainWeight(remainWeight);
        record.setRemark(store.getRemark());
        record.setBelongId(store.getBelongId());
        record.setPlateNo(store.getPlateNo());
        record.setAddUser(new SysUser(userId));
        record.setAddTime(new Date());
        record.setInId(store.getInId());
        record.setOrignalStoreId(orginalStoreId);
        record.setSplitStoreId(splitStoreId);
        record.setRemainStoreId(remainStoreId);
        record.setReceiveAddress(store.getReceiveAddress());
        record.setCancelFlag("0");
        record.setWgtDcnMtcCd(store.getWgtDcnMtcCd());
        record.setEdgeTy(store.getEdgeTy());
        record.setProductionPlace(store.getProductionPlace());
        return record;
    }


}
