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


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.th.demo.mapping.ware.DeficitRecordMapper;
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.DeficitRecord;
import com.th.demo.model.ware.Store;
import com.th.demo.service.ware.DeficitService;
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.Collections;
import java.util.Date;
import java.util.List;


@Service("DeficitService")
public class DeficitServiceImpl implements DeficitService {

    @Autowired
    public DeficitRecordMapper deficitRecordMapper;
    @Autowired
    public StoreMapper storeMapper;

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

    @Transactional
    @Override
    public String deficit(String json, String reason, String userId, String belongId) {
        String recordType = "1";
        String backFlag = "0";

        List<Store> listStore = JSON.parseObject(json, new TypeReference<List<Store>>() {
        });
        Collections.sort(listStore);
        for (int i = 0; i < listStore.size(); i++) {
            listStore.get(i).setCancelFlag("5");
            num += storeMapper.updateByPrimaryKey(listStore.get(i));
            DeficitRecord record = getDeficitRecord(listStore.get(i), reason, recordType,backFlag,  userId, belongId);
            num += deficitRecordMapper.insert(record);
        }
        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();

        List<Store> listStoreMiddle = storeMapper.selectForUpDown(listStore.get(0).getWare().getId(), listStore.get(0).getStack().getId(), userId, belongId, min, max);
        for (int i = 0; i < listStoreMiddle.size(); i++) {
            listStoreMiddle.get(i).setLayer(min + i);
            storeMapper.updateByPrimaryKey(listStoreMiddle.get(i));
        }

        num += storeMapper.updateForUpDownByStep(listStore.get(0).getWare().getId(), listStore.get(0).getStack().getId(), userId, belongId, max + 1, currentLayer, -1 * listStore.size());
        return Tools.moreThanZeroResultJSON(num);
    }

    @Override
    public String cancel(String json, String reason, String toStack,int layer, String userId, String belongId) {
        int currentLayer = 0;
        String recordType = "0";
        String backFlag = "1";
        List<DeficitRecord> list = JSON.parseObject(json, new TypeReference<List<DeficitRecord>>() {
        });

        Stack stack =  JSON.parseObject(toStack, new TypeReference<Stack>() {
        });

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

        for (int i = 0; i < list.size(); i++) {
            Store store = storeMapper.selectByPrimaryKey(list.get(i).getStoreId());
            store.setCancelFlag("0");
            store.setLayer(currentLayer+1+i);
            store.setStack(stack);
            store.setAddId(userId);
            store.setRemark(store.getRemark()+";"+reason);
            num += storeMapper.insert(store);

            list.get(i).setBackFlag(backFlag);
            num+= deficitRecordMapper.updateByPrimaryKey(list.get(i));
            DeficitRecord record = getDeficitRecord(store, reason, recordType,backFlag, userId, belongId);
            num += deficitRecordMapper.insert(record);
        }
        return Tools.moreThanZeroResultJSON(num);

    }

    private DeficitRecord getDeficitRecord(Store store, String reason, String recordType, String backFlag, String userId, String belongId) {
        DeficitRecord record = new DeficitRecord();
        record.setWareName(store.getWare().getName());
        record.setStackName(store.getStack().getName());
        record.setLayer(store.getLayer());
        record.setMaterialName(store.getMaterial().getName());
        record.setStandard(store.getMaterial().getStandard());
        record.setModel(store.getModel());
        record.setCustomerName(store.getCustomer().getName());
        if (recordType.equals("0")) {
            record.setCount(-1 * store.getCount());
            record.setWeight(-1 * store.getWeight());
        } else {
            record.setCount(store.getCount());
            record.setWeight(store.getWeight());
        }
        record.setReceiveAddress(store.getReceiveAddress());
        record.setRemark(store.getRemark());
        record.setBelongId(belongId);
        record.setPlateNo(store.getPlateNo());
        record.setAddUser(new SysUser(userId));
        record.setAddTime(new Date());
        record.setRecordType(recordType);
        record.setReason(reason);
        record.setStoreId(store.getId());
        record.setBackFlag(backFlag);
        record.setWgtDcnMtcCd(store.getWgtDcnMtcCd());
        record.setEdgeTy(store.getEdgeTy());
        record.setProductionPlace(store.getProductionPlace());
        return record;


    }
}
