const ServiceBase = require("../../sve.base");
const ydzPush = require("./../../../utils/ydzClient");
const settings = require("./../../../../config/settings");
const System = require("../../../system");
const moment = require("moment");
const TOQFT = require("../../../utils/toQft");


class ServicebillService extends ServiceBase {
    constructor() {
        super("agriculture", ServiceBase.getDaoName(ServicebillService));
        this.accountsetSve = System.getObject("service.agriculture.accountsetSve");
    }

    async findAndCountAll(obj) {
        var self = this;
        console.log('---------------findAndCountAll ------------------');
        const apps = await super.findAndCountAll(obj);
        return apps;
    }

    async updateStatus(obj) {
        obj.id = obj.bizId;
        const res = await this.dao.update(obj);
        return res;
    }

    async updateAndPush(obj) { //服务单分配会计推企服通回传会计信息、并推易代账新建帐套
        const res = await this.dao.update(obj);
        const findService = await this.dao.findOne({ bizId: obj.bizId });
        if (findService) {
            let data = {
                custName: findService.contactName, //客户名称
                corpName: findService.companyInfo.companyName, //企业名称
                taxiationArea: findService.companyInfo.companyArea, //报税地区
                taxIndustryId: findService.companyInfo.industry, //行业
                serviceTypeEnum: "ACCOUTING", // 服务类型：枚举型，包括ACCOUTING-记账服务、INDUSTRY_COMMERCE-工商服务、BOTH-记账与工商服务。
                taxNo: findService.companyInfo.taxpayerNumber, // 税号
                accountantEmployeeId: obj.accountantAgentAccountId, //所属会计
                taxClaimMethodEnum: "TAX_DECLARATION", //申报方式：TAX_DECLARATION-税款申报；ZERO_DECLARATION-零申报；
                isBuild: true //是否建账
            }
            console.log(data);
            //TODO: 推企服通回传会计信息
            // dev gsbweb.qifu-dev.gongsibao.com/api/opreceive/service/notice
            // pro gsbweb.qifu.gongsibao.com/api/opreceive/service/notice
            // pushQiFuTong({"actionType":"accountingInfo","actionBody": backData});
            await TOQFT.getClientByType("pannong").pushQiFuTong(obj.username, { "actionType": "accountingInfo", "actionBody": data });

            // TODO: 分配会计并推送易代账新建帐套 回传后新建本地帐套
            const accountset = await ydzPush.pushYiDaZhang(user, data);
            const res = await this.accountsetSve.create(accountset);

            // TODO: 推送企服通帐套信息
            // pushQiFuTong({"actionType":"booksInfo","actionBody": res});
            await TOQFT.getClientByType("pannong").pushQiFuTong(obj.username, { "actionType": "booksInfo", "actionBody": res });
        } else {
            console.log('查无此单：' + obj.bizId);
        }
        return res;
    }

    async updateAndFeedback(obj) { //服务单审核并推送企服通
        const res = await this.dao.update(obj);
        const findOne = await this.dao.findOne({ bizId: obj.bizId });
        const deadline = moment(findOne.updated_at).add(findOne.quantity, 'years');
        let backData = {};
        if (findOne) {
            backData.bizId = findOne.bizId;
            backData.auditResult = findOne.status;
            if (backData.auditResult == 1) { //审核通过
                backData.serviceStartTime = findOne.updated_at;
                backData.serviceEndTime = deadline;
            }
            if (backData.auditResult == 2) { // 审核未通过
                backData.note = findOne.feedback_notes;
            }
            //TODO: 推送企服通
            // pushQiFuTong({"actionType":"feedback","actionBody": backData});
            await TOQFT.getClientByType("pannong").pushQiFuTong(obj.username, { "actionType": "feedback", "actionBody": backData });
        } else {
            console.log('查无此单：' + obj.bizId);
        }
    }

}
module.exports = ServicebillService;