const system = require("../../../system");
const ServiceBase = require("../../sve.base")

class StPayService extends ServiceBase {
    constructor() {
        super("trade", ServiceBase.getDaoName(StPayService));
        this.PAY_TYUPE = ["1", '2'];     //支付类型 1线下支付 2...
        this.PAY_STATUS = ["10", "20"];  //支付状态 10待支付 20已支付
    }

    /**
     * 创建交易流水
     * @param params
     * @returns {Promise<void>}
     */
    async saveStPay(params) {
        if (!params.busi_name) {
            return system.getResult(`业务名称不能为空`);
        }
        if (!params.busi_id) {
            return system.getResult(`业务ID不能为空`);
        }

        if (!this.PAY_TYUPE.includes(params.pay_type)) {
            return system.getResult("支付类型错误")
        }
        if (!this.PAY_STATUS.includes(params.pay_status)) {
            return system.getResult("支付状态错误")
        }

        let stpay = {
            busi_id: this.trim(params.busi_id),
            busi_name: this.trim(params.busi_name),
            saas_id: this.trim(params.saas_id),
            saas_merchant_id: this.trim(params.saas_merchant_id),
            pay_type: this.trim(params.pay_type),
            amount: this.trim(params.amount),
            pay_status: this.trim(params.pay_status),
            pay_voucher_img: this.trim(params.pay_voucher_img),
            trade_no: this.trim(params.trade_no),
            account_info: this.trim(params.account_info),
        };
        try {
            let res = await this.dao.create(stpay);
            return system.getResult(res);
        } catch (e) {
            console.log(e);
            return system.getResult(`系统错误`)
        }
    }

    /**
     * 更新支付记录
     * @param pay_status
     * @param pay_voucher_img
     * @param trade_no
     * @returns {Promise<void>}
     */
    async updateStatus (params) {
        let _bean = await this.dao.model.findOne({
            where: {
                id: this.trim(params.id),
            }
        });
        if (!_bean) {
            return system.getResult(null, `业务${_bean.id}不存在`);
        }
        if(params.pay_status){
            if (!this.PAY_STATUS.includes(params.pay_status)) {
                return system.getResult("支付状态错误")
            }
            _bean.pay_status = this.trim(params.pay_status);
        }
        if (params.pay_voucher_img){
            _bean.pay_voucher_img = this.trim(params.pay_voucher_img);
        }
        if (params.trade_no){
            _bean.trade_no = this.trim(params.trade_no);
        }
        try{
            let res = await _bean.save();
            return system.getResult(res);
        }catch (e) {
            console.log(e);
            return system.getResult(null, `系统错误`)
        }
    }

    /**
     * 获取业务支付记录
     * @param busi_name
     * @param busiIds
     * @returns {Promise<void>}
     */
    async mapByBusiIds(params) {
        if (!params.busi_name) {
            return system.getResult(`业务名称不能为空`);
        }
        try {
            let list = await this.dao.model.findAll({
                where: {
                    busi_name: this.trim(params.busi_name),
                    busi_id: {
                        [this.db.Op.in]: params.busiIds,
                    }
                }
            });
            let map = {};
            for (let element of list) {
                if(element.pay_type=="1"){
                    element.pay_type_name = "线下支付";
                }else{
                    element.pay_type_name = "其他支付";
                }
                if(element.pay_status=="10"){
                    element.pay_status_name = "待支付 ";
                }else if(element.pay_status=="20"){
                    element.pay_status_name = "20已支付";
                }else{
                    element.pay_status_name = "";
                }
                let temp = map[element.busi_id] || [];
                temp.push(element);
                map[element.busi_id]=temp;
            }
            return system.getResult(map);
        }catch (e) {
            console.log(e);
            return system.getResult(`系统错误`);
        }
    }
}

module.exports = StPayService;