const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");
const moment = require("moment");
const System = require("../../../system");
const pushQft = require("../../../utils/toqifutongClient")


class WangwenService extends ServiceBase {
    constructor() {
        super("delivery", ServiceBase.getDaoName(WangwenService));
        this.cacheinfoDao = system.getObject("db.delivery.cacheinfoDao");
        this.deliveryDao = system.getObject("db.delivery.deliverDao");
        this.wangwenmaterialDao = system.getObject("db.delivery.wangwenmaterialDao");
        this.statuslogDao = system.getObject("db.bizchance.statuslogDao");

    }
    async findInfo(pobj, qobj, req) {
        let result = await this.deliveryDao.findInfoByWangwen({
            id: pobj.id
        });
        if (!result) {
            throw new Error("交付单不可查看");
        }
        return result;
    }

    async submitMaterials(pobj, qobj, req) {
        const deliverData = await this.deliveryDao.findOne({
            id: pobj.deliver_id
        });

        if (!deliverData) {
            throw new Error("查不到此交付单");
        }
        if ([system.SERVERSESTATUS.COLLECTING, system.SERVERSESTATUS.USERCONFIRMATIONREJECT].includes(deliverData.delivery_status)) {
            await pushQft.wangwenSubmitOption(deliverData, pobj.cache_info);
        }

        return this.db.transaction(async (t) => {
            await this.cacheinfoDao.createOrUpdate(pobj, t);
            await this.wangwenmaterialDao.createOrUpdate(pobj, t);
            if ([system.SERVERSESTATUS.COLLECTING, system.SERVERSESTATUS.USERCONFIRMATIONREJECT].includes(deliverData.delivery_status)) {
                await this.deliveryDao.updateByWhere({
                    delivery_status: system.SERVERSESTATUS.USERCONFIRMATIONWAIT
                }, {
                    id: pobj.deliver_id
                }, t);
                this.statuslogDao.create({
                    flow_type: system.FLOWCODE.DELIVERY,
                    flow_id: pobj.deliver_id,
                    status_code: system.SERVERSESTATUS.USERCONFIRMATIONWAIT,
                    salesman_id: pobj.userid,
                    salesman_name: pobj.username
                });
            }
            return "SUCCESS"
        });
    }

    async changeDeliveryStatus(pobj, qobj, req) {
        const deliverData = await this.deliveryDao.findOne({
            id: pobj.deliver_id
        });
        if (!deliverData) {
            throw new Error("没有此交付单");
        }
        let status;
        switch (deliverData.delivery_status) {
            case system.SERVERSESTATUS.USERCONFIRMATIONRESOLVE:
                status = system.SERVERSESTATUS.ACCOUNTREGISTRATION;
                await pushQft.wangwenSubmitOption(deliverData);
                break
            case system.SERVERSESTATUS.ACCOUNTREGISTRATION:
                status = system.SERVERSESTATUS.SUBMITING;
                await pushQft.wangwenSubmitOption(deliverData);
                break
            case system.SERVERSESTATUS.SUBMITING:
                status = system.SERVERSESTATUS.DISPOSEING;
                await pushQft.wangwenSubmitOption(deliverData);
                break
            case system.SERVERSESTATUS.DISPOSEING:
                status = system.SERVERSESTATUS.THROUGH;
                await pushQft.wangwenSubmitOption(deliverData);
                break
            case system.SERVERSESTATUS.THROUGH:
                status = system.SERVERSESTATUS.SUCCESS;
                break
            default:
                throw new Error("此状态手动不能更改");
                break
        }
        await this.deliveryDao.updateByWhere({
            delivery_status: status
        }, {
            id: pobj.deliver_id
        })
        this.statuslogDao.create({
            flow_type: system.FLOWCODE.DELIVERY,
            flow_id: pobj.deliver_id,
            status_code: status,
            salesman_id: pobj.userid,
            salesman_name: pobj.username
        });
        return "success"
    }
}
module.exports = WangwenService;
