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

class MerchantService extends ServiceBase {
    constructor() {
        super("all", ServiceBase.getDaoName(MerchantService));
        this.electroniccontractDao = system.getObject("db.all.electroniccontractDao");
        this.loadDao = system.getObject("db.all.loadDao");
        this.recruitDao = system.getObject("db.all.recruitDao");
        this.taxinfoDao = system.getObject("db.all.taxinfoDao");
        this.transactioninDao = system.getObject("db.all.transactioninDao");
    }

    async signPage(params) {
        var currentPage = Number(params.currentPage || 1);
        var pageSize = Number(params.pageSize || 10);

        var where = {};
        if (params.company_name) {
            where.company_name = {
                [this.db.Op.like]: "%" + params.company_name + "%"
            };
        }
        if (params.sign_body) {
            where.sign_body = params.sign_body;
        }
        this.addWhereTime(where, 'begin_time', params.signBegin, params.signEnd, true);
        var orderby = [
            ["id", 'desc']
        ];
        var attributes = [`id`, `company_name`, `type`, `link_man`, `link_no`, `link_email`, `bank_account_name`, `bank_no`, `open_bank`, `sign_body`, `begin_time`, `invalid_time`];
        var page = await this.getPageList(currentPage, pageSize, where, orderby, attributes);
        if (page && page.rows) {
            for (var row of page.rows) {
                this.handleDate(row, ["begin_time", "invalid_time"]);
            }
        }
        return page;
    }

    async dataSummarize(params) {
        var where = {};
        var wheres = {};
        if (params.sign_body) {
            where.sign_body = params.sign_body;
            wheres.sign_body = params.sign_body;
        }
        this.addWhereTime(where, 'begin_time', params.signBegin, params.signEnd, true);


        var addMer = await this.findCount({where: where});//商户新增
        var upMer = await this.findCount(wheres);//累积
        var where_ele = {};
        if (params.sign_body) {
            where_ele.sign_body = params.sign_body;
        }
        this.addWhereTime(where_ele, 'completed_date', params.signBegin, params.signEnd, true);

        var addEle = await this.electroniccontractDao.findCount({where: where_ele});//电子签新增
        var upEle = await this.electroniccontractDao.findCount(wheres);//累积


        var where_loa = {};
        if (params.sign_body) {
            where_ele.sign_body = params.sign_body;
        }
        this.addWhereTime(where_loa, 'affirm_time', params.signBegin, params.signEnd, true);

        var addLoad = await this.loadDao.findCount({where: where_loa});//工作量新增
        var upLoad = await this.loadDao.findCount(wheres);//


        var where_rec = {};
        if (params.sign_body) {
            where_rec.sign_body = params.sign_body;
        }
        this.addWhereTime(where_rec, 'publish_date', params.signBegin, params.signEnd, true);

        var addrec = await this.recruitDao.findCount({where: where_rec});//招聘需求
        var upRec = await this.recruitDao.findCount(wheres);

        var where_tax = {};
        if (params.sign_body) {
            where_tax.sign_body = params.sign_body;
        }
        this.addWhereTime(where_tax, 'tax_time', params.signBegin, params.signEnd, true);

        var addTac = await this.taxinfoDao.findCount({where: where_tax});//完税人次
        var upTac = await this.taxinfoDao.findCount(wheres);

        var addTacSum = await this.taxinfoDao.findSum("income_tax", {where: where_tax});//完税金额
        var upTacSum = await this.taxinfoDao.findSum("income_tax", wheres);

        var where_tra = {};
        if (params.sign_body) {
            where_tra.sign_body = params.sign_body;
        }
        this.addWhereTime(where_tra, 'transaction_time', params.signBegin, params.signEnd, true);
        var addTra = await this.transactioninDao.findCount({where: where_tra});//交易比数
        var upTra = await this.transactioninDao.findCount(wheres);

        var addTraSum = await this.transactioninDao.findSum("actual_amount", {where: where_tra});//交易金额
        var upTraSum = await this.transactioninDao.findSum("actual_amount", wheres);

        var result = {
            addMer: addMer || 0,//商户新增
            upMer: upMer || 0,
            addEle: addEle || 0,//电子签新增
            upEle: upEle || 0,
            addLoad: addLoad || 0,//工作量新增
            upLoad: upLoad || 0,
            addrec: addrec || 0,//招聘需求
            upRec: upRec || 0,
            addTac: addTac || 0,//完税人次
            addTacSum: addTacSum || 0,
            upTacSum: upTacSum || 0,//完税金额
            addTra: addTra || 0,
            upTra: upTra || 0, //交易比数
            addTraSum: addTraSum || 0,
            upTraSum: upTraSum || 0,//交易金额
            upTac: upTac || 0
        };

        return result;
    }


    async dataChart(params) {
        var result = [];
        var sql = "SELECT DATE_FORMAT(invalid_time,'%Y-%m-%d') days,COUNT(id) COUNT FROM merchant_info WHERE 1=1";
        var sqls = "SELECT DATE_FORMAT(transaction_time,'%Y-%m-%d') days,COUNT(id) COUNT FROM transaction_info WHERE 1=1";
        if (params.company_name) {
            sql = sql + " ANN company_name = " + params.company_name;
            sqls = sqls + " ANN company_name = " + params.company_name;
        }
        if (params.sign_body) {
            sql = sql + " AND sign_body = " + params.sign_body;
            sqls = sqls + " AND sign_body = " + params.sign_body;
        }
        if (params.signBegin || params.signEnd) {
            sql = sql + " AND invalid_time >= '" + params.signBegin + " 00:00:00' AND invalid_time <= '" + params.signEnd + " 23:59:59'";
            sqls = sqls + " AND transaction_time >= '" + params.signBegin + " 00:00:00' AND transaction_time <= '" + params.signEnd + " 23:59:59'";
        }
        sql = sql + " GROUP BY days ORDER BY days ASC";
        sqls = sqls + " GROUP BY days ORDER BY days ASC";
        var list = await this.customQuery(sql);
        var lists = await this.customQuery(sqls);
        var m = {
            mx: [],
            my: []
        };
        for (let i = 0; list.length > i; i++) {
            m.mx.push(list[i].days);
            m.my.push(list[i].COUNT);
        }
        result.push(m);
        var t = {
            tx: [],
            ty: []
        };
        for (let j = 0; lists.length > j; j++) {
            t.tx.push(lists[j].days);
            t.ty.push(lists[j].COUNT);
        }
        result.push(t);
        return result || [];
    }

    async commercialCount(params){
        let page = await this.signPage(params);

        let rows = page.rows;
        if (!page || !page.rows || page.rows.length == 0) {
            return page;
        }

        let companyNames = [];
        for (var row of rows) {
            companyNames.push(row.company_name);
        }

        // 电子签约数量  company_name --> num
        let electronMap = await this.electroniccontractDao.statCount(companyNames);

        // 招工需求
        let recruitMap = {};

        // 工作量确认单
        let loadMap = {};

        // 交易笔数、交易金额

        // 完税人次

        // 完税金额


        for (var row of rows) {
            row.signNum = electronMap[row.company_name] || 0;
            row.xuqiushuliang = electronMap[row.company_name] || 0;
            row.signNum = electronMap[row.company_name] || 0;
            row.signNum = electronMap[row.company_name] || 0;
            row.signNum = electronMap[row.company_name] || 0;
            row.signNum = electronMap[row.company_name] || 0;
        }
    }

    async commercialList(params){
        var result = [];
        var currentPage = Number(params.currentPage || 1);
        var pageSize = Number(params.pageSize || 10);
        var sql = "";
        if (params.company_name) {
            sql = sql + " ANN company_name = " + params.company_name;
        }
        if (params.sign_body) {
            sql = sql + " AND sign_body = " + params.sign_body;
        }


    }



}

module.exports = MerchantService;