
import {Schema} from 'mongoose';
import { baseDB } from '../../db/mongo/dbInit';

const illegalitySchema = new Schema({
    id:String,
    uscc:String,
    fuHuaQiUscc:String,
    enterpriseName:String,
    type:String,
    addReason:String,
    addOffice:String,
    removeReason:String,
    removeOffice:String,
});

var illegalityModel;
export function initModel(){
    illegalityModel = baseDB.model('illegality', illegalitySchema);
    illegalityModel.selectOnceData = async function (paramater:object) {
        let selectInfo = await illegalityModel.findOne(paramater).exec();
        if (selectInfo) {
            if (!selectInfo.runSave) {
                selectInfo.runSave = selectInfo.save;
                selectInfo.save = save.bind(selectInfo)
            }
        }
        return selectInfo;
    }
}
export async function save(throwError=false) {
    if (!this.isModified()) return;
    await this.runSave({validateBeforeSave:false}).catch(err=>{
        console.log(err);
    });
}

export async function createOnce(param) {
    return await illegalityModel.create(param);
}

export async function findOnce(param) {
    return await illegalityModel.selectOnceData(param);
}

export async function findList(param) {
    return await illegalityModel.find(param);
}

export async function removeMany(param) {
    return await illegalityModel.deleteMany(param);
}

export async function addMany(params) {
    await illegalityModel.insertMany(params);
}