/**
 * 风险提示
 */

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

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

var enterpriseriskModel;
export function initModel(){
    enterpriseriskModel = sasp.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 createDataByParam(param) {
    return await enterpriseriskModel.create(param);
}

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

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


