var system = require("../../../system")
const settings = require("../../../../config/settings")
const CtlBase = require("../../ctlms.base");
const moment = require('moment');
class OrdersignlogCtl extends CtlBase {
    constructor() {
        super();
        this.userSve = system.getObject("service.uc.userSve");
        this.ordersignlogSve = system.getObject("service.order.ordersignlogSve");
        this.productSve = system.getObject("service.product.productSve");
    }

    /**
     * fn:保存签约日志
     * ps:测试开发中开放 ,此接口不向外暴露， 认证引擎异步回调将此次操作插入到表中
     * @param pobj  1.spendedBegin消费的开始时间  2.spendedEnd消费的结束时间
     * @param pobj2
     * @param req
     * @param res
     * @returns {Promise<void>}
     */
    async saveEorderSignLog(pobj, pobj2, req, res){
        if(!pobj.order_id){
            return system.getResult(null, `参数错误 订单ID不能为空`);
        }
        if(!pobj.product_id){
            return system.getResult(null, `参数错误 产品ID不能为空`);
        }
        if(!pobj.engine_contract_name){
            return system.getResult(null, `参数错误 签署任务名称不能为空`);
        }
        if(!pobj.engine_contract_id){
            return system.getResult(null, `参数错误 签署ID不能为空`);
        }
        if(!pobj.actual_spend_name){
            return system.getResult(null, `参数错误 实际使用方不能为空`);
        }
        if(!pobj.platform_name){
            return system.getResult(null, `参数错误 平台名称不能为空`);
        }
        pobj.merchant_id = pobj.merchant_id ? this.trim(pobj.merchant_id) : '';
        pobj.spended_num = Number(pobj.spended_num || 0);
        pobj.spended_at = pobj.spended_at?pobj.spended_at:new Date();
        pobj.platform_id=pobj.platform_id?pobj.platform_id:'';
        try{
            return await this.ordersignlogSve.saveEorderSignLog(pobj);
        }catch (e) {
            console.log(e);
            return system.getResult(null, `系统错误`);
        }
    }

    /**
     * fn:签约日志列表（分页）
     * @param pobj
     * @param pobj2
     * @param req
     * @param res
     * @returns {Promise<void>}
     */
    async pageEorderSignLog(pobj, pobj2, req, res){
        //如果存在时间筛选 时间格式 2020-05
        if(pobj.spendedDate){
            let dateRes = this.formatDate(pobj.spendedDate);
            pobj.spendedBegin = dateRes.spendedBegin;
            pobj.spendedEnd = dateRes.spendedEnd;
        }
        try{
            let res= await this.ordersignlogSve.pageEorderSignLog(pobj);
            if(res.status!=0 || res.data.count==0){
                return res;
            }
            //格式化产品信息
            let ids = [],productMap={};
            for (let item of res.data.rows) {
                ids.push(item.product_id);
            }
            let p_listRes = await this.productSve.getByIds({ids:ids});
            if(p_listRes.status!=0){
                return res;
            }
            //将产品列表映射成Map结构
            for (let ele of p_listRes.data) {
                productMap[ele.id]=ele;
            }
            for (let item of res.data.rows) {
                item.product_info = productMap[item.product_id];
            }
            return res;
        }catch (e) {
            console.log(e);
            return system.getResult(null, `系统错误`);
        }
    }

    /**
     * fn：格式化日期
     * @param params
     */
    formatDate(params){
        if(!params){return;}
        let date = moment(params);
        let spendedBegin=date.format("YYYY-MM-DD HH:mm:ss");
        let tempMonth = date.month()+1;
        tempMonth=tempMonth<10?'0'+tempMonth:tempMonth;
        let tempDay = date.daysInMonth();
        tempDay=tempDay<10?'0'+tempDay:tempDay;
        let spendedEnd = date.year() + "-" + tempMonth + '-' + tempDay + " 23:59:59";
        return { spendedBegin,spendedEnd };
    }

    /**
     * fn:合同列表
     * @param pobj
     * @param pobj2
     * @param req
     * @param res
     * @returns {Promise<void>}
     */
    async pageEorderContract(pobj, pobj2, req, res){
        try{
            return await this.ordersignlogSve.pageEorderContract(pobj);
        }catch (e) {
            console.log(e);
            return system.getResult(null, `系统错误`);
        }
    }
}

module.exports = OrdersignlogCtl;