/**
 * 风险提示
 */

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

const enterpriseriskSchema = new Schema({
    id:String,
    uscc:String,
    fuHuaQiUscc:String,
    enterpriseName:String,
    title:String,
    description:String,
    passage:String,
    riskType:String,
    typeCode:Number,
});

var enterpriseriskModel;
export function initModel(){
    enterpriseriskModel = baseDB.model('enterpriserisk', enterpriseriskSchema);
    enterpriseriskModel.selectOnceData = async function (paramater:object) {
        let selectInfo = await enterpriseriskModel.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 enterpriseriskModel.create(param);
}

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

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