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.CustomerMapper;
import com.th.demo.mapping.maint.StackMapper;
import com.th.demo.mapping.ware.ChangeRecordMapper;
import com.th.demo.mapping.ware.MoveRecordMapper;
import com.th.demo.mapping.ware.StoreMapper;
import com.th.demo.model.maint.Customer;
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.ChangeRecord;
import com.th.demo.model.ware.MoveRecord;
import com.th.demo.model.ware.SplitRecord;
import com.th.demo.model.ware.Store;
import com.th.demo.service.maint.CustomerService;
import com.th.demo.service.ware.MoveService;
import com.th.demo.service.ware.StoreService;
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 java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service("StoreService")
public class StoreServiceImpl implements StoreService {

    @Autowired//自动注入Mapper
    public ChangeRecordMapper changeRecordMapper;
    @Autowired//自动注入Mapper
    public StoreMapper storeMapper;
    @Autowired//自动注入Mapper
    public StackMapper stackMapper;
    @Autowired//自动注入Mapper
    public MoveRecordMapper moveRecordMapper;
    @Autowired//自动注入Mapper
    public CustomerMapper customerMapper;
    String result = Type.FAIL;
    int num = 0;

    @Override
    public String queryLayerByStackId(String stackId, String userId, String belongId) {

        String layer = storeMapper.selectLayerByStackId(stackId, belongId);
        if (layer == null) {
            layer = "0";
        }
        int currentLayer = Integer.parseInt(layer) + 1;
        result = JSONTools.toStringyyyyMMddHHmmss(currentLayer);
        return result;
    }

    @Override
    public String queryByWareIdStackId(String wareId, String stackId, String userId, String belongId) {
       List<Store> list =  storeMapper.selectByWareIdStackId(  wareId,   stackId, belongId);
        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }

    @Override
    public String query(int page, int rows, String wareName, String stackName, String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<Store> list   = storeMapper.select(wareName, stackName, userId,belongId);
        PageInfo<Store> pageInfo = new PageInfo<>(list);
        result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        return result;
    }

    @Override
    public String modify(String json, String userId, String belongId) {
        Store store = JSON.parseObject(json,Store.class);
        int num =storeMapper.updateByPrimaryKey(store);
        result=Tools.moreThanZeroResultJSON(num);
        return result;
    }

    @Override
    public String queryStore(int page, int rows,
                             String wareName,
                             String stackName,
                             String model,
                             String materialName,
                             String standard,
                             String customerName,
                             String plateNo,
                             String ordNo,
                             String remark1,
                             String fkcustmerName,
                             String userId,
                             String belongId) {

        List<String> pltNos =new ArrayList<>() ;
        List<String> ordNos =new ArrayList<>() ;
        if (plateNo.indexOf(",") != -1){
            pltNos = Arrays.asList(plateNo.split(","));
        }
        if (ordNo.indexOf(",") != -1){
            ordNos = Arrays.asList(ordNo.split(","));
        }



        if(page != -1 && rows != -1){
            PageHelper.startPage(page, rows);
            List<Store> list=storeMapper.selectStore(wareName,stackName,model,materialName,standard,
                    customerName, plateNo,ordNo, pltNos,ordNos, userId,belongId,remark1,fkcustmerName);
            PageInfo<Store> pageInfo = new PageInfo<>(list);
            result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        }else {
            List<Store> list=storeMapper.selectStore(wareName,stackName,model,materialName,standard,
                    customerName, plateNo,ordNo, pltNos,ordNos, userId,belongId,remark1,fkcustmerName);
            result = JSONTools.toStringyyyyMMddHHmmss(list);
        }

        return result;
    }

    @Override
    public String modifyCustomer(String id,String customerNameOld,String customerNameNew) {
//        Store store =storeMapper.selectByPrimaryKey(id);
//        store.setCustomerName(customerNameNew);
//        ChangeRecord changeRecord =new ChangeRecord();
//        changeRecord.setWareName(store.getWareName());
//        changeRecord.setStackName(store.getStackName());
//        changeRecord.setId(store.getId());
//        changeRecord.setLayer(store.getLayer());
//        changeRecord.setMaterialName(store.getMaterialName());
//        changeRecord.setMaterialNo(store.getMaterialNo());
//        changeRecord.setStandard(store.getStandard());
//        changeRecord.setModel(store.getModel());
//        changeRecord.setCustomerNameOld(customerNameOld);
//        changeRecord.setCustomerNameNew(customerNameNew);
//        changeRecord.setCount(store.getCount());
//        changeRecord.setRemark(store.getRemark());
//        changeRecord.setAddTime(store.getAddTime());
//        changeRecord.setModifyId(store.getModifyId());
//        changeRecord.setModifyTime(store.getModifyTime());
//        changeRecord.setCancelId(store.getCancelId());
//        changeRecord.setCancelTime(store.getCancelTime());
//        changeRecord.setCancelFlag(store.getCancelFlag());
//        changeRecord.setBelongId(store.getBelongId());
//        changeRecord.setPlateNo(store.getPlateNo());
//        changeRecord.setOutId(store.getOutId());
//        changeRecord.setAddId(store.getAddId());
//
//        changeRecordMapper.insert(changeRecord);
//        num = storeMapper.updateByPrimaryKey(store);
//        result = JSONTools.toString(num);
        return result;
    }

    @Override
    public String modifyCustomeradress(String id, String customerAddressOld, String customerAddressNew) {
//        Store store =storeMapper.selectByPrimaryKey(id);
//        store.setReceiveAddress(customerAddressNew);
//        ChangeRecord changeRecord =new ChangeRecord();
//        changeRecord.setWareName(store.getWareName());
//        changeRecord.setStackName(store.getStackName());
//        changeRecord.setId(store.getId());
//        changeRecord.setLayer(store.getLayer());
//        changeRecord.setMaterialName(store.getMaterialName());
//        changeRecord.setMaterialNo(store.getMaterialNo());
//        changeRecord.setStandard(store.getStandard());
//        changeRecord.setModel(store.getModel());
//        changeRecord.setReceiveAddressNew(store.getReceiveAddress());
//        changeRecord.setCustomerNameNew(customerAddressNew);
//        changeRecord.setCustomerNameOld(customerAddressOld);
//        changeRecord.setCount(store.getCount());
//        changeRecord.setRemark(store.getRemark());
//        changeRecord.setAddTime(store.getAddTime());
//        changeRecord.setModifyId(store.getModifyId());
//        changeRecord.setModifyTime(store.getModifyTime());
//        changeRecord.setCancelId(store.getCancelId());
//        changeRecord.setCancelTime(store.getCancelTime());
//        changeRecord.setCancelFlag(store.getCancelFlag());
//        changeRecord.setBelongId(store.getBelongId());
//        changeRecord.setPlateNo(store.getPlateNo());
//        changeRecord.setOutId(store.getOutId());
//        changeRecord.setAddId(store.getAddId());
//
//        changeRecordMapper.insert(changeRecord);
//        num = storeMapper.updateByPrimaryKey(store);
//        result = JSONTools.toString(num);
        return result;
    }

    //



//    @Override
//    public String save(String json, String userId) {
//        Assets  assets = JSON.parseObject(json, Assets.class);
//        Assets isnull = assetsMapper.selectByPrimaryKey(assets.getId());
//        assets.setStatus("正常");
//        if(isnull==null){
//        /*assets.setAddId(userId);
//        assets.setAddTime(new Date());
//        assets.setCancelFlag("0");*/
//            num = assetsMapper.insert(assets);
//            result =JSONTools.toString(num);
//        }else{
//        /*arcd.setModifyId(userId);
//        arcd.setModifyTime(new Date());*/
//            num = assetsMapper.updateByPrimaryKey(assets);
//            result =JSONTools.toString(num);
//        }
//        return result;
//    }

//
//    @Override
//    public String save(String json, String userId, String belongId) {
//        result = Type.FAIL;
//        Customer customer = JSON.parseObject(json, new TypeReference<Customer>() {
//        });
//        if (customer.getId() == null || customer.getId().equals("")) {
//            customer.setAddId(userId);
//            customer.setAddTime(new Date());
//            customer.setModifyId(userId);
//            customer.setModifyTime(new Date());
//            customer.setCancelFlag("0");
//            customer.setBelongId(belongId);
//            num = customerMapper.insert(customer);
//        } else {
//            customer.setModifyId(userId);
//            customer.setModifyTime(new Date());
//            num = customerMapper.updateByPrimaryKey(customer);
//        }
//        result = Tools.moreThanZeroResultJSON(num);
//        return result;
//    }
//
//
//    @Override
//    public String remove(String id, String userId) {
//        Customer customer = customerMapper.selectByPrimaryKey(id);
//        customer.setCancelFlag("9");
//        customer.setCancelId(userId);
//        customer.setCancelTime(new Date());
//        num = customerMapper.updateByPrimaryKey(customer);
//        result = Tools.moreThanZeroResultJSON(num);
//        return result;
//
//    }

    @Override
    public String changeStack(String json,String newWare,String newStack,String newRemark1,
                              String newRemark,String userId,String belongId) {
        List<Store> listStore = JSON.parseObject(json, new TypeReference<List<Store>>() {});

        int layerNew = 0;
        int layerOld = 0;
        int layer = 0;
        if (!newWare.equals("") && newWare!=null && !newStack.equals("") && newStack!=null){
            Stack toStack = stackMapper.selectByWareNameStackName(newWare,newStack,userId,belongId);
            int currentLayer = Integer.parseInt(storeMapper.selectLayerByStackId(toStack.getId(), belongId));
            if (toStack==null){
                return JSONTools.toString(1,"没有找到该垛位信息");
            }else{
                for (int i = 0; i < listStore.size(); i++) {
                    layerOld = listStore.get(i).getLayer();

                    if (toStack.getIsLayer().equals("0")) {
                        layerNew = 0;
                    } else if (toStack.getIsLayer().equals("1")) {
                        layerNew = currentLayer + 1 + i;
                    } else if (toStack.getIsLayer().equals("2")) {
                        layerNew = layer;
                    }

                    listStore.get(i).setWare(toStack.getWare());
                    listStore.get(i).setStack(toStack);
                    listStore.get(i).setLayer(layerNew);


                    MoveRecord moveRecord = getMoveRecord(listStore.get(i), listStore.get(i).getStack(), toStack, layerNew, layerOld, userId);
                    num = moveRecordMapper.insert(moveRecord);

                    num += storeMapper.updateByPrimaryKey(listStore.get(i));
                }
            }
        }

        for (int i = 0; i < listStore.size(); i++) {
            if (!newRemark1.equals("") && newRemark1!=null){
                if (newRemark1.equals("0")){
                    listStore.get(i).setRemark1("");
                }else {
                    listStore.get(i).setRemark1(newRemark1);
                }
            }
            if (!newRemark.equals("") && newRemark!=null){
                if (newRemark.equals("0")){
                    listStore.get(i).setRemark("");
                }else {
                    listStore.get(i).setRemark(newRemark);
                }
            }
            num = storeMapper.updateByPrimaryKey(listStore.get(i));
        }
        return result;
    }

    @Override
    public String changeCustName(String json,String newCustomername,String newFKCustomername,
                          String userId,String belongId) {
        List<Store> listStore = JSON.parseObject(json, new TypeReference<List<Store>>() {});

        int layerNew = 0;
        int layerOld = 0;
        int layer = 0;
        Customer customer = new Customer();
        Customer fkcustomer = new Customer();
        if (!newCustomername.equals("") && newCustomername!=null){
            customer = customerMapper.selectByName(newCustomername,belongId);
        }
        if (!newFKCustomername.equals("") && newFKCustomername!=null){
            fkcustomer = customerMapper.selectByName(newFKCustomername,belongId);
        }
        for (int i = 0; i < listStore.size(); i++) {
            if (!newCustomername.equals("") && newCustomername!=null){
                listStore.get(i).setCustomer(customer);
                num = storeMapper.updateByPrimaryKey(listStore.get(i));
            }
            if (!newFKCustomername.equals("") && newFKCustomername!=null){
                listStore.get(i).setFkComponyId(fkcustomer.getId());
                num = storeMapper.updateFkById(listStore.get(i));
            }

        }
        return result;
    }

    private MoveRecord getMoveRecord(Store store, Stack fromStack, Stack toStack, int layerNew, int layerOld, String userId) {
        MoveRecord moveRecord = new MoveRecord();
        moveRecord.setWareNameOld(fromStack.getWare().getName());
        moveRecord.setWareNameNew(toStack.getWare().getName());
        moveRecord.setStackNameOld(fromStack.getName());
        moveRecord.setStackNameNew(toStack.getName());
        moveRecord.setLayerOld(layerOld);
        moveRecord.setLayerNew(layerNew);
        moveRecord.setMaterialName(store.getMaterial().getName());
        // moveRecord.setMaterialNo(store.getMaterialNo());
        moveRecord.setStandard(store.getMaterial().getStandard());
        moveRecord.setModel(store.getModel());
        moveRecord.setCustomerName(store.getCustomer().getName());
        // moveRecord.setCustomerNo(store.getCustomerNo());
        moveRecord.setCount(store.getCount());
        moveRecord.setWeight(store.getWeight());
        moveRecord.setRemark(store.getRemark());
        moveRecord.setBelongId(store.getBelongId());
        moveRecord.setPlateNo(store.getPlateNo());
        moveRecord.setAddUser(new SysUser(userId));
        moveRecord.setAddTime(new Date());
        moveRecord.setReceiveAddress(store.getReceiveAddress());
        moveRecord.setWgtDcnMtcCd(store.getWgtDcnMtcCd());
        moveRecord.setEdgeTy(store.getEdgeTy());
        moveRecord.setProductionPlace(store.getProductionPlace());
        moveRecord.setCancelFlag("0");
        return moveRecord;
    }
}
