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

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.ware.ChangeRecordMapper;
import com.th.demo.mapping.ware.StoreMapper;
import com.th.demo.mapping.ware.InStoreMapper;

import com.th.demo.model.system.Type;
import com.th.demo.model.ware.Store;
import com.th.demo.model.ware.InStore;
import com.th.demo.service.ware.StoreService;
import com.th.demo.service.ware.InStoreService;
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.util.List;

@Service("InStoreService")
public class InStoreServiceImpl implements InStoreService {

    @Autowired//自动注入Mapper
    public ChangeRecordMapper changeRecordMapper;
    @Autowired//自动注入Mapper
    public StoreMapper storeMapper;
    @Autowired//自动注入Mapper
    public InStoreMapper inStoreMapper;
    String result = Type.FAIL;
    int num = 0;

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

        String layer = inStoreMapper.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<InStore> list =  inStoreMapper.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<InStore> list   = inStoreMapper.select(wareName, stackName, userId,belongId);
        PageInfo<InStore> 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 =inStoreMapper.updateByPrimaryKey(store);
        result=Tools.moreThanZeroResultJSON(num);
        return result;
    }

    @Override
    public String queryStore(int page, int rows,
                             String wareName,
                             String areaName,
                             String stackName,
                             String plateNo,
                             String ordNo,
                             String userId,
                             String belongId) {
        PageHelper.startPage(page, rows);
        List<InStore> list=inStoreMapper.selectStore(wareName, areaName, stackName, plateNo, ordNo, userId,belongId);
        PageInfo<InStore> pageInfo = new PageInfo<>(list);
        result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        return result;
    }

    @Override
    public String modifyCustomer(String id,String customerNameOld,String customerNameNew) {
//        Store store =inStoreMapper.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 = inStoreMapper.updateByPrimaryKey(store);
//        result = JSONTools.toString(num);
        return result;
    }

    @Override
    public String modifyCustomeradress(String id, String customerAddressOld, String customerAddressNew) {
//        Store store =inStoreMapper.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 = inStoreMapper.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;
//
//    }
}
