/*
 * @Author: YKH
 * @Date: 2022-04-25 11:47:16
 * @LastEditTime: 2022-05-09 13:45:39
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\task\service\flag.ts
 */

import { Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import * as _ from 'lodash';
import { ShuyouTargetFlagEntity } from '../entity/flag';

/**
 * 游戏目标
 */
@Provide()
export class ShuyouTargetFlagService extends BaseService {

    @InjectEntityModel(ShuyouTargetFlagEntity)
    ShuyouTargetFlagEntity: Repository<ShuyouTargetFlagEntity>;

    /**
     * 重载新增接口
     */
    async add(param) {
        // console.log(param)
        const exists = await this.ShuyouTargetFlagEntity.findOne({
            name: param.name,
        });
        if (!_.isEmpty(exists)) {
            //存在则修改
            await this.ShuyouTargetFlagEntity
                .createQueryBuilder()
                .update()
                .set(param)
                .where('name = :name', {
                    name: param.name,
                })
                .execute();
            return 1000
        } else {
            param.createTime = new Date()
            param.updateTime = new Date()
            // console.log(param)
            this.ShuyouTargetFlagEntity.createQueryBuilder()
                .insert()
                .into(ShuyouTargetFlagEntity)
                .values(param)
                .execute();
            return 1000
        }
    }

    /**
     * 查询标记值
     * @param {*} name
     * @return {*} 
     * @memberof ShuyouTargetFlagService
     */
    async getTodayFlag(name: string) {
        const exists = await this.ShuyouTargetFlagEntity.findOne({
            name: name
        });
        // console.log(exists)
        if (_.isEmpty(exists)) {
            return { code: 1000, data: 'exists' }
        } else if (exists.status == 1) {
            return { code: 1000, data: 'success' }
        } else
            return { code: -1, data: exists }
    }
}


