/**
 * Created by zhang on 2017/11/24.
 */

var quantity_detail_data = require('../facade/quantity_detail_facade');
let logger = require("../../../logs/log_helper").logger;
module.exports={
    save:save,
    update:update,
    updateRegex:updateRegex,
    insertRecode:insertRecode,
    deleteRecode:deleteRecode,
    swapRow:swapRow
}

async function save(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        if(typeof data === 'object'){
            data = JSON.stringify(data);
        }
        data = JSON.parse(data);
        let datas= await quantity_detail_data.saveQuantityDetail(data);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function update(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let datas= await quantity_detail_data.update(data);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function updateRegex(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let datas= await quantity_detail_data.updateRegex(data);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function insertRecode(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let datas= await quantity_detail_data.insertRecode(data);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function deleteRecode(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let datas= await quantity_detail_data.deleteRecode(data);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function swapRow(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let datas= await quantity_detail_data.swapRow(data);
        result.data=datas;
    }catch (err){
        logger.err(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}