import { UserModel } from '@db/User';
import { CeAttrDataRole, RoleModel } from '@db/Role';
import { HeroModel } from '@db/Hero';
import { EquipModel } from '@db/Equip';
import { PvpDefenseModel } from '@db/PvpDefense';

import { Service } from 'egg';
import Counter from '@db/Counter';
import { STATUS, HERO_SYSTEM_TYPE } from '@consts';
import { ITID, COUNTER } from '@consts';
import { ItemModel } from '@db/Item';
import { gameData, getExpByLv } from '@pubUtils/data';
import { calPlayerCeAndSave, calculatetopLineup, calEquipSeids } from '@pubUtils/playerCe';
import { AttributeCal } from '@domain/roleField/attribute';
import { smsModel } from '@db/Sms';
import { isString } from 'underscore';
import { addSkin, addEquips, addBags } from '@pubUtils/itemUtils';
import { GiftCodeModel } from '@db/GiftCode';
import { GiftCodeDetailModel } from '@db/GiftCodeDetail';
import { CreateHeroes, deletRole } from '@pubUtils/roleUtil';
import { RScriptRecordModel } from '@db/RScriptRecord';
import { DicWar } from '@pubUtils/dictionary/DicWar';
import { SkinModel } from '@db/Skin';

// import { resResult } from '@pubUtils/util';

// import * as fs from 'fs';

/**
 * Test Service
 */
export default class GMUsers extends Service {

    /**
     * 根据类型等搜索玩家
     */
    public async getuserlist(field: string = 'all', value: string = '') {
        const { ctx } = this;

        let arr = new Array(), flag = 0;
        if (value == '') field = 'all';
        let valueArr = value.split(',');
        for (let i of valueArr) {
            if (field == 'uid') {
                let d = parseInt(i);
                if (isNaN(d)) {
                    flag = 1; break;
                } else {
                    arr.push(d);
                }
            } else if (field == 'username' || field == 'tel') {
                arr.push(i);
            } else if (field == 'all') {
                break;
            } else {
                flag = 1; break;
            }
        }

        if (flag) {
            return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }

        let users = await UserModel.findUserByField(field, valueArr);

        if (users) {
            let list = new Array();
            for (let user of users) {
                let role = await RoleModel.findAllByUid(user.uid);
                let sms = await smsModel.findByTel(user.tel);

                list.push({ ...user, role, isFixed: sms ? sms.isFixed : false, code: sms ? sms.code : "" });
            }

            return ctx.service.utils.resResult(STATUS.SUCCESS, { list })
        } else {
            console.error('userlist not found');
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        }
    }

    public async createRole(uid: number, serverId: number, roleName: string) {
        console.log('enter Auth createRole');
        const ctx = this.ctx;

        const roleId = ctx.service.utils.genCode(10);
        const code = ctx.service.utils.genCode(6);
        const seqId = await Counter.getNewCounter(COUNTER.ROLE) || -1;
        const role = await RoleModel.createRole(uid, serverId, { roleId, code, roleName, seqId });
        if (role) {
            return ctx.service.utils.resResult(STATUS.SUCCESS);
        } else {
            console.error('create role error');
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        }
    }

    public async addAuth(uid: number, auth: number) {
        console.log('enter Auth addAuth');
        const ctx = this.ctx;

        const user = await UserModel.addAuth(uid, auth);
        if (user) {
            return ctx.service.utils.resResult(STATUS.SUCCESS);
        } else {
            console.error('add auth error');
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        }
    }

    public checkTelNo(telNo: string) {
        if (!isString(telNo)) {
            return { status: 1, data: '参数类型错误' };
        }
        if (telNo.length !== 11) {
            return { status: 1, data: '手机号长度错误' };
        }
        return { status: 0, data: '手机号合法' };
    }

    public async fixSms(tel: string) {
        console.log('enter Auth fix sms');
        const ctx = this.ctx;
        const checkTel = this.checkTelNo(tel);
        if (checkTel.status == 1) {
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR, checkTel.data);
        }
        const sms = await smsModel.fixSms(tel);
        if (sms) {
            return ctx.service.utils.resResult(STATUS.SUCCESS);
        } else {
            console.error('fix sms error');
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        }
    }

    public async deleteRole(roleId: string) {
        console.log('enter Auth deleteRole');
        const ctx = this.ctx;
        let result = await deletRole(roleId);
        if(!result) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }



    /**
     * 根据类型等搜索玩家
     */
    public async getrolelist(field: string = 'all', value: string = '') {
        const { ctx } = this;

        let arr = new Array(), flag = 0;
        let valueArr = value.split(',');
        if (value == '') field = 'all';
        for (let i of valueArr) {
            if (field == 'uid') {
                let d = parseInt(i);
                if (isNaN(d)) {
                    flag = 1; break;
                } else {
                    arr.push(d);
                }
                field = 'userInfo.uid';
            } else if (field == 'roleName') {
                arr.push(i);
            } else if (field == 'tel') {
                arr.push(i);
                field = 'userInfo.tel';
            } else if (field == 'all') {
                break;
            } else {
                flag = 1; break;
            }
        }

        if (flag) {
            return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }

        let roles = await RoleModel.findRoleByField(field, valueArr);

        if (roles) {

            let result = new Array();
            for (let role of roles) {
                let heroCount = await HeroModel.count({ roleId: role.roleId }).lean();
                let equipCount = await EquipModel.count({ roleId: role.roleId }).lean();
                let itemCount = await ItemModel.count({ roleId: role.roleId }).lean();
                let defense = await PvpDefenseModel.findByRoleId(role.roleId);

                let { roleId, roleName, serverId, lv, vLv, gold, coin } = role;
                let { uid, tel } = role.userInfo;
                result.push({
                    key: roleId, roleId, roleName, serverId, lv, vLv, uid, tel, heroCount, equipCount, itemCount, gold, coin,
                    hasDefense: defense ? '是' : '否', defCe: 0
                });
            }
            return ctx.service.utils.resResult(STATUS.SUCCESS, { list: result });
        } else {
            console.error('role list not found');
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        }
    }

    public async createHero(uids: Array<string>, _hid: string, _hlv: string) {
        const { ctx } = this;
        console.log('gm createHero', uids, _hid, _hlv);
        let hlv = parseInt(_hlv);
        if (isNaN(hlv)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        let hids = (_hid as string).split('&').map(cur => parseInt(cur));
        for (let hid of hids) {
            if (isNaN(hid)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }

        for (let roleId of uids) {
            let role = await RoleModel.findByRoleId(roleId);
            if (role) {
                let heroInfos = new Map();
                for (let hid of hids) {
                    let heroInfo = ctx.service.utils.getInitHeroById(hid);
                    heroInfos.set(hid, {...heroInfo});
                }

                let createHero = new CreateHeroes(roleId, role.roleName, role.serverId);
                await createHero.createWithHeroInfo(heroInfos);

            } else {
                return ctx.service.utils.resResult(STATUS.GM_CREATE_ERROR, null, '未找到角色' + roleId)
            }
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS, { uids });
    }

    public async createEquip(uids: Array<string>, _eid: string, _ecount: string, _ehid: string) {
        const { ctx } = this;
        console.log('gm createEquip', uids, _eid, _ecount, _ehid);
        let eids = (_eid as string).split('&').map(cur => parseInt(cur));
        let ecount = parseInt(_ecount);
        let ehid = parseInt(_ehid);
        if (isNaN(ecount) || isNaN(ehid)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        for (let eid of eids) {
            if (isNaN(eid)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }
        let flag = 0, msg = '创建失败';
        for (let roleId of uids) {
            let role = await RoleModel.findByRoleId(roleId);
            if (role) {
                let weapons: { id: number, hid?: number }[] = [];
                for (let eid of eids) {
                    for (let i = 0; i < ecount; i++) {

                        let dicEquip = gameData.goods.get(eid);
                        if (!dicEquip) {
                            flag = 1, msg = "未找到装备" + eid;
                            break;
                        }
                        weapons.push({ id: eid, hid: ehid });
                    }
                }
                await addEquips(roleId, role.roleName, weapons);
            } else {
                flag = 1, msg = '未找到角色' + roleId;
            }
        }

        if (flag) {
            return ctx.service.utils.resResult(STATUS.GM_CREATE_ERROR, null, msg);
        } else {
            return ctx.service.utils.resResult(STATUS.SUCCESS, { uids });
        }
    }

    public async createItem(uids: Array<string>, _itemid: string, _itemcount: string) {
        const { ctx } = this;
        console.log('gm createItem', uids, _itemid, _itemcount);
        let itemids = (_itemid as string).split('&').map(cur => parseInt(cur));
        for (let itemid of itemids) {
            if (isNaN(itemid)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }
        let itemcount = parseInt(_itemcount);
        if (isNaN(itemcount)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

        let flag = 0, msg = '创建失败';
        let datas: {id: number, count: number }[] = []
        for (let itemid of itemids) {
            let dicGoods = gameData.goods.get(itemid);
            let itidObj = ITID.get(dicGoods.itid);

            if (!dicGoods) {
                flag = 1, msg = "未找到道具" + itemid;
            } else if (!itidObj) {
                flag = 1, msg = "未找到道具" + itemid;
            } else {
                datas.push({ id: itemid, count: itemcount });
            }
        }
        if(flag == 0) {
            for (let roleId of uids) {
                let role = await RoleModel.findByRoleId(roleId);
                if (role) {
                    await addBags(roleId, role.roleName, datas);
                } else {
                    flag = 1, msg = '未找到角色' + roleId;
                }
            }
        }

        if (flag) {
            return ctx.service.utils.resResult(STATUS.GM_CREATE_ERROR, null, msg);
        } else {
            return ctx.service.utils.resResult(STATUS.SUCCESS, { uids });
        }
    }

    public async addGold(uids: Array<string>, _count: string) {
        const { ctx } = this;
        console.log('gm addGold', uids, _count);
        let count = parseInt(_count);
        if (isNaN(count)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        for (let roleId of uids) {
            await RoleModel.addGoldFree(roleId, count);
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS, { uids });
    }

    public async addCoin(uids: Array<string>, _count: string) {
        const { ctx } = this;
        console.log('gm addCoin', uids, _count);
        let count = parseInt(_count);
        if (isNaN(count)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        for (let roleId of uids) {
            await RoleModel.addCoin(roleId, count);
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS, { uids });
    }
    public async addSkin(uids: Array<string>, _id: string) {
        const { ctx } = this;
        console.log('gm addSkin', uids, _id);
        let id = parseInt(_id);
        if (isNaN(id)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        for (let roleId of uids) {
            let role = await RoleModel.findByRoleId(roleId);
            await addSkin(roleId, role.roleName, id, false);
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS, { uids });
    }

    public async levelUp(uids: Array<string>, _lv: string) {
        const { ctx } = this;
        console.log('gm levelUp', uids, _lv);
        let lv = parseInt(_lv);
        if (isNaN(lv)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        for (let roleId of uids) {
            let exp = getExpByLv(lv - 1);
            await RoleModel.levelup(roleId, lv, exp ? exp.sum : 0);
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS, { uids });
    }

    public async getPvpDefense(roleId: string) {
        const { ctx } = this;
        let result = await PvpDefenseModel.findByRoleId(roleId);
        return ctx.service.utils.resResult(STATUS.SUCCESS, {
            hasDefense: !!result, defense: result
        })
    }

    //   public async getHeroList(roleId: string) {
    //     const {ctx} = this;
    //     let herolist = await HeroModel.findByRole(roleId);
    //     let defense = await PvpDefenseModel.findByRoleId(roleId);

    //     let result = Array<Hero>();
    //     for(let hero of herolist) {
    //         if(defense) {
    //             let {heroes = []} = defense;
    //             let curHero = heroes.find(cur => cur.actorId == hero.hid);
    //             if(!curHero) {
    //                 result.push(hero);
    //             }
    //         } else{
    //             result.push(hero);
    //         }
    //     }

    //     return ctx.service.utils.resResult(STATUS.SUCCESS, {
    //         heroes: result
    //     })
    //   }

    // public async saveHeroToDefense(roleId: string, _roleName: string, hid: number) {
    //     const { ctx } = this;
    //     let hero = await HeroModel.findByHidAndRole(hid, roleId);
    //     let role = await RoleModel.findByRoleId(roleId);
    //     if (!hero || !role) {
    //         return ctx.service.utils.resResult(STATUS.GM_HERO_NOT_FOUND);
    //     }
    //     let { lv, ce, attr: heroAttrs } = hero;
    //     let { attr: roleAttrs } = role;


    //     let dicHero = gameData.hero.get(hid);

    //     let attribute = new AttributeCal();
    //     attribute.setByDbData(roleAttrs, heroAttrs);

    //     let heroInfo = {
    //         actorId: dicHero.heroId,
    //         actorName: dicHero.name,
    //         attribute, lv,
    //         ce
    //     };

    //     let defense = await PvpDefenseModel.findByRoleId(roleId);
    //     if (!defense) {
    //         // defense = await PvpDefenseModel.createPvpDefense({roleId, roleName, heroes: [], defCe:ce});
    //     } else {
    //         defense = await PvpDefenseModel.addHeroToDefense(roleId, heroInfo, ce);
    //     }
    //     return ctx.service.utils.resResult(STATUS.SUCCESS, {
    //         defense
    //     });
    // }


    // public async removeHeroFromDefense(roleId: string, hid: number) {
    //     const { ctx } = this;

    //     let defense = await PvpDefenseModel.findByRoleId(roleId);
    //     if (!defense) {
    //         return ctx.service.utils.resResult(STATUS.GM_PVP_DEFENSE_NOT_FOUND);
    //     }
    //     let { heroes } = defense;
    //     let curHero = heroes.find(cur => cur.actorId == hid);
    //     if (!curHero) {
    //         return ctx.service.utils.resResult(STATUS.GM_PVP_DEFENSE_HERO_NOT_FOUND);
    //     }
    //     defense = await PvpDefenseModel.removeHeroFromDefense(roleId, hid, curHero.ce);

    //     return ctx.service.utils.resResult(STATUS.SUCCESS, {
    //         defense
    //     });
    // }

    /**
     * 根据类型等搜索玩家
     */
    public async getHeroList(field: string, value: (string | number)) {
        const { ctx } = this;

        let heroes = await HeroModel.findByField(field, value);

        if (heroes) {
            let roleMap = new Map<string, CeAttrDataRole[]>();
            for(let { roleId } of heroes) {
                if(!roleMap.has(roleId)) {
                    let role = await RoleModel.findByRoleId(roleId, 'attr');
                    roleMap.set(roleId, role.attr);
                }
            }
            let list = heroes.map(cur => {
                let cal = new AttributeCal();
                cal.setByDbData(roleMap.get(cur.roleId), cur.attr);
                return {...cur, calculatedAttr: cal.getReduceAttributesToArr()}
            })

            return ctx.service.utils.resResult(STATUS.SUCCESS, { list });
        } else {
            console.error('role list not found');
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        }
    }


    public async deleteHero(roleIdAndHids: string[]) {
        console.log('enter Auth deleteRole');
        const ctx = this.ctx;
        for (let roleIdAndHid of roleIdAndHids) {
            let [roleId, hidStr] = roleIdAndHid.split('|');
            if (isNaN(parseInt(hidStr))) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

            let hid = parseInt(hidStr);

            let hero = await HeroModel.findByHidAndRole(hid, roleId);
            if (!hero) continue;
            await HeroModel.deleteHero(roleId, hid);
            await SkinModel.deleteByHero(roleId, hid);
            let role = await RoleModel.findByRoleId(roleId);
            await calculatetopLineup(role, hid, 0, null);
            await PvpDefenseModel.deleteHero(roleId, hid);
            await RoleModel.updateRoleInfo(roleId, { topLineup: role.topLineup, topLineupCe: role.topLineupCe, ce: role.ce - hero.ce });
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    public async deleteEquip(roleIdAndSeqIds: string[]) {
        console.log('enter Auth deleteEquip');
        const ctx = this.ctx;
        let seqIds = new Map<string, number[]>();
        for (let roleIdAndSeqId of roleIdAndSeqIds) {
            let [roleId, seqIdStr] = roleIdAndSeqId.split('|');
            if (isNaN(parseInt(seqIdStr))) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

            let seqId = parseInt(seqIdStr);
            console.log(seqId);
            let equip = await EquipModel.findbySeqId(seqId);
            if (equip.hid > 0) {
                let hero = await HeroModel.findByHidAndRole(equip.hid, roleId);
                let index = hero.ePlace.findIndex(cur => cur.id == equip.ePlaceId);
                if (index < 0) continue;
                let args = calEquipSeids(hero);
                hero.ePlace[index].equip = null;
                await calPlayerCeAndSave(HERO_SYSTEM_TYPE.EQUIP, roleId, hero, {}, args);
            }
            if(!seqIds.has(roleId)) {
                seqIds.set(roleId, []);
            }
            seqIds.get(roleId).push(seqId);
        }
        for(let [roleId, ids] of seqIds) {
            await EquipModel.deleteEquips(roleId, ids);
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    public async deleteItem(roleIdAndIds: string[]) {
        console.log('enter Auth deleteItem');
        const ctx = this.ctx;
        let map = new Map<string, number[]>();
        for (let roleIdAndId of roleIdAndIds) {
            let [roleId, idStr] = roleIdAndId.split('|');
            if (isNaN(parseInt(idStr))) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

            let id = parseInt(idStr);
            let cur = map.get(roleId);
            if (!cur) {
                cur = new Array<number>();
                map.set(roleId, cur);
            }
            cur.push(id);
        }

        for (let [roleId, ids] of map) {
            await ItemModel.deletebyRoleAndIds(roleId, ids);
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }


    public async setItemCount(roleIdAndIds: string[], count: number) {
        console.log('enter Auth setItemCount');
        const ctx = this.ctx;
        let map = new Map<string, number[]>();
        for (let roleIdAndId of roleIdAndIds) {
            let [roleId, idStr] = roleIdAndId.split('|');
            if (isNaN(parseInt(idStr))) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

            let id = parseInt(idStr);
            let cur = map.get(roleId);
            if (!cur) {
                cur = new Array<number>();
                map.set(roleId, cur);
            }
            cur.push(id);
        }

        for (let [roleId, ids] of map) {
            await ItemModel.updateCountByRoleAndIds(roleId, ids, count);
        }

        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    public async setHeroLv(roleIdAndHids: string[], _hlv: string) {

        try {
            console.log('gm setHeroLv', roleIdAndHids, _hlv);
            const ctx = this.ctx;
            let hlv = parseInt(_hlv);
            if (isNaN(hlv)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

            for (let roleIdAndHid of roleIdAndHids) {
                let [roleId, hidStr] = roleIdAndHid.split('|');
                if (isNaN(parseInt(hidStr))) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

                let hid = parseInt(hidStr);

                let hero = await HeroModel.findByHidAndRole(hid, roleId);
                console.log(hid, roleId, !!hero);
                if (!hero) continue;
                await calPlayerCeAndSave(HERO_SYSTEM_TYPE.LVUP, roleId, hero, { lv: hlv });
            }

            return ctx.service.utils.resResult(STATUS.SUCCESS);
        } catch (e) {
            console.error(e.stack)
        }
    }

    public async setHeroParam(roleIdAndHids: string[], star: number, colorStar: number, quality: number) {
        try {
            const ctx = this.ctx;
            if (typeof star != 'number'||typeof colorStar != 'number'||typeof quality != 'number') return ctx.service.utils.resResult(STATUS.WRONG_PARMS); 

            for (let roleIdAndHid of roleIdAndHids) {
                let [roleId, hidStr] = roleIdAndHid.split('|');
                if (isNaN(parseInt(hidStr))) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

                let hid = parseInt(hidStr);

                let hero = await HeroModel.findByHidAndRole(hid, roleId);
                console.log(hid, roleId, !!hero);
                if (!hero) continue;
                await calPlayerCeAndSave(HERO_SYSTEM_TYPE.LVUP, roleId, hero, { star, colorStar, quality });
            }

            return ctx.service.utils.resResult(STATUS.SUCCESS);
        } catch (e) {
            console.error(e.stack)
        }
    }


    public async setHeroJob(roleIdAndHids: string[], job: number) {
        try {
            const ctx = this.ctx;
            if(!gameData.job.has(job)) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

            for (let roleIdAndHid of roleIdAndHids) {
                let [roleId, hidStr] = roleIdAndHid.split('|');
                if (isNaN(parseInt(hidStr))) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

                let hid = parseInt(hidStr);

                let hero = await HeroModel.findByHidAndRole(hid, roleId);
                console.log(hid, roleId, !!hero);
                if (!hero) continue;
                await calPlayerCeAndSave(HERO_SYSTEM_TYPE.STAGEUP, roleId, hero, { job });
            }

            return ctx.service.utils.resResult(STATUS.SUCCESS);
        } catch (e) {
            console.error(e.stack)
        }
    }

    /**
     * 根据类型等搜索装备
     */
    public async getEquipList(field: string, value: (string | number)) {
        const { ctx } = this;

        let equips = await EquipModel.findByField(field, value);

        if (equips) {

            return ctx.service.utils.resResult(STATUS.SUCCESS, { list: equips });
        } else {
            console.error('role list not found');
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        }
    }


    /**
     * 根据类型等搜索道具
     */
    public async getItemList(field: string, value: (string | number)) {
        const { ctx } = this;

        let items = await ItemModel.findByField(field, value);

        if (items) {

            return ctx.service.utils.resResult(STATUS.SUCCESS, { list: items });
        } else {
            console.error('role list not found');
            return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        }
    }

    public async getGiftCodeList(page: number, pageSize: number, sortField: string, sortOrder: string, form: { name?: string, current?: boolean,  } = {}) {
        const { ctx } = this;

        const list = await GiftCodeModel.findByCondition(page, pageSize, sortField, sortOrder, form);
        const total = await GiftCodeModel.countByCondition( form )
        return ctx.service.utils.resResult(STATUS.SUCCESS, {
            list: list.map(cur => {
                return { ...cur, beginTime: cur.beginTime.getTime(), endTime: cur.endTime.getTime() }
            }), total
        });
    }

    public async updateGiftCode(id: string|number, params: any) {
        const { ctx } = this;
        if(params.beginTime) params.beginTime = new Date(params.beginTime);
        if(params.endTime) params.endTime = new Date(params.endTime);

        try{
            params.goods = JSON.parse(params.goods);
        } catch(e) {
            console.error(e);
            return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }
        const result = await GiftCodeModel.updateData(id, params, ctx.user?.uid);
        if(!result) return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    public async generateGiftCode(id: number, generateType: 1|2, code: string, generateNum: number, codeLen: number = 8) {
        const { ctx } = this;
        
        let giftCode = await GiftCodeModel.findData(id);
        if(!giftCode) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

        let length = 0, isLimit = false, count = 0, generateCode = '';
        if(generateType == 1) {
            let generateResult = await GiftCodeDetailModel.generateMany(giftCode, generateNum, codeLen, ctx.user?.uid);
            length = generateResult.length;
            isLimit = true;
            count = 1;
        } else if (generateType == 2) {
            let generateResult = await GiftCodeDetailModel.generateOne(giftCode, code, ctx.user?.uid);
            length = generateResult.length;
            generateCode = code;
        }
        if(length <= 0) return ctx.service.utils.resResult(STATUS.INTERNAL_ERR);
        giftCode = await GiftCodeModel.updateData(id, { generateCnt: giftCode.generateCnt + length, generateType, isLimit, count, generateCode }, ctx.user?.uid);
        return ctx.service.utils.resResult(STATUS.SUCCESS, { giftCode })
    }

    public async getGiftCodeDetails(id: number) {
        const { ctx } = this;
        
        let giftCode = await GiftCodeModel.findData(id);
        if(!giftCode) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        let { generateCnt } = giftCode;
        let limit = 1000;
        let n = Math.ceil(generateCnt / limit);
        let codes: string[] = [];
        for(let i = 0; i < n; i++) {
            let giftCodeDetails = await GiftCodeDetailModel.findByGiftCode(giftCode);
            for(let {code} of giftCodeDetails) {
                codes.push(code);
            }
        }
        console.log(id, codes.join())

        ctx.set('Content-Type', 'application/octet-stream');

        return Buffer.from(codes.join('\n'));
    }

    public async setWarRecord(roleIds: string[], _warId: string) {
        const { ctx } = this;
        let warId = parseInt(_warId);
        if(warId < 104) return ctx.service.utils.resResult(STATUS.WRONG_PARMS, null, '不可跳到序章以前')
        let dicWar = gameData.war.get(warId);
        if(!dicWar) return ctx.service.utils.resResult(STATUS.WRONG_PARMS);

        for(let roleId of roleIds) {
            await this.setSingleWarRecord(roleId, dicWar);
        }
        return ctx.service.utils.resResult(STATUS.SUCCESS);
    }

    private async setSingleWarRecord(roleId: string, dicWar: DicWar) {
        let { warStar } = await RoleModel.findByRoleId(roleId);
        let deleteIds: number[] = [], myMaxWarId = 0, insertParams: DicWar[] = [];
        for(let i = 0; i < warStar.length; i++) {
            let { id, warType } = warStar[i];
            if(warType == dicWar.warType) {
                if(id >= dicWar.war_id) {
                    deleteIds.push(id);
                }
                if(id > myMaxWarId) {
                    myMaxWarId = id;
                }
            }
        }
        let newWarStar = warStar.filter(cur => {
            return deleteIds.indexOf(cur.id) == -1;
        });
        if(myMaxWarId < dicWar.war_id) {
            for(let id = myMaxWarId + 1; id < dicWar.war_id; id++) {
                let _dicWar = gameData.war.get(id)
                if(_dicWar && _dicWar.warType == dicWar.warType) {
                    newWarStar.push({ id, warType: _dicWar.warType, star: 0 });
                    insertParams.push(_dicWar);
                }
            }
        }
        let preGkIndex = newWarStar.findIndex(cur => cur.id == dicWar.previousGk);
        console.log(dicWar.previousGk)
        if(dicWar.previousGk && preGkIndex == -1) {
            let _dicWar = gameData.war.get(dicWar.previousGk);
            newWarStar.push({ id: _dicWar.war_id, warType: _dicWar.warType, star: 3 });
        }
        if(insertParams.length > 0) {
            await RScriptRecordModel.insertScripts(roleId, insertParams);
        }
        if(deleteIds.length > 0) {
            await RScriptRecordModel.deleteByWarId(roleId, deleteIds);
        }
        let role = await RoleModel.updateRoleInfo(roleId, { warStar: newWarStar });
        return role;
    }
}
