/**
 * 失信核查
 */
import {Schema} from 'mongoose';
import { sasp } from '../db/mongo/dbInit';

const enterprisedishonestPersonSchema = new Schema({
    id:{type:String, index: true},
    uscc:{type:String, index: true},
    enterpriseName:String,
    anno:String,
    executegov:String,
    executestatus:String,
    executeno:String,
    actionRemark:String,
    amount:String,
});

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

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

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

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

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