let system = require("../../../system")
const http = require("http")
const querystring = require('querystring');
let settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const logCtl = system.getObject("web.common.oplogCtl");
const tmtransactionPath = "/tmtransaction/action/trademarktransaction/springBoard";
const tmurl = settings.tmtransactionurl() + tmtransactionPath;
let pushClient = require("../../../utils/totxClient.js").trademark;

//标源管理
class TrademarkCtl extends CtlBase {
    constructor() {
        super("trademark", CtlBase.getServiceName(TrademarkCtl));
        this.execClient = system.getObject("util.execClient");
    }

    /**
     * @api {post} /web/trademark/trademarkctl/findAndCountAll 获取列表
     * @apiGroup 标源管理
     * @apiParamExample {json} 请求参数
     * {"pageInfo": {"pageNo":1,"pageSize":10}}
     * @apiSuccessExample {json} 成功示例:
     * {"status":0,"msg":""data":{"results":{"rows":[]}}}
     * @apiErrorExample {json} 失败示例:
     * {"error": ""}
     */
    async findAndCountAll(p, q, req) {
        let query = {
            "actionType": "tmInfo",
            "actionBody": {
                channel_code: p.company_id,
                pageSize:p.pageInfo.pageSize,// 每页数量
                pageNumber:p.pageInfo.pageNo,// 页
            }
        };
        if (p.search) {
            if (p.search.name)
                query.actionBody.name = p.search.name;
            else if (p.search.code)
                query.actionBody.code = p.search.code;
            else if (p.search.channel_name)
                query.actionBody.name = p.search.channel_name;
            else if (p.search.tm_start_day)
                query.actionBody.tm_start_day = p.search.tm_start_day;
            else if (p.search.tm_end_day)
                query.actionBody.tm_end_day = p.search.tm_end_day;
        }
        let rtn = await this.execClient.execPost(query, tmurl);
        let data = JSON.parse(rtn.stdout);
        return system.getResultList(data.totalCount, data.data)
    }

    /**
     * @api {post} /web/trademark/trademarkctl/findOne 获取详情
     * @apiGroup 标源管理
     * @apiParamExample {json} 请求参数
     * {"id": "1"}
     * @apiSuccessExample {json} 成功示例:
     * {"success": ""}
     * @apiErrorExample {json} 失败示例:
     * {"error": ""}
     */
    async findOne(p, q, req) {
        let body = {
            "actionType": "tmDetail",
            "actionBody": {
                tmId: 0
            }
        };
        body.actionBody.tmId = p.id;
        let rtn = await this.execClient.execPost(body, tmurl);
        let data = JSON.parse(rtn.stdout);
        return system.getResultSuccess(data.data)
    }

    /**
     * @api {post} /web/trademark/trademarkctl/publish 批量发布
     * @apiGroup 标源管理
     * @apiParamExample {json} 请求参数
     * [{"name":"商标名称","code":"商标申请号","business_quoted_price":"底价","contacts":"联系人","mobile":"手机号"}]
     * @apiSuccessExample {json} 成功示例:
     * {"status":0,"msg":"","data":[],"bizmsg":"empty","requestId":""}
     * @apiErrorExample {json} 失败示例:
     * {"error": ""}
     */
    async publish(p, q, req) {
        let body = {
            "actionType": "tmBatchUpload",
            "actionBody": {
                "tm": p
            }
        };
        let rtn = await this.execClient.execPost(body, tmurl);
        let data = JSON.parse(rtn.stdout);
        if (data.status == 1) {
            let tradedb = [];
            data.data.map(db => tradedb.push({
                opath: p.opath,
                user_id: p.userid,
                company_id: p.company_id,
                trademark_id: db.id,
                tm_code: db.code
            }));
            rtn = await this.service.bulkCreate(tradedb);
        }
        return system.getResult(rtn)
    }

    /**
     * @api {post} /web/trademark/trademarkctl/update 修改
     * @apiDescription (下架/审核/状态修改)
     * @apiGroup 标源管理
     * @apiParamExample {json} 请求参数
     * {"id": "1"}
     * @apiSuccessExample {json} 成功示例:
     * {"success": ""}
     * @apiErrorExample {json} 失败示例:
     * {"error": ""}
     */
    async update(p, q, req) {
        let body = {
            "actionType": "tmInfoUpdate",
            "actionBody": p
        };
        let rtn = await this.execClient.execPost(body, tmurl);
        let data = JSON.parse(rtn.stdout);
        return system.getResult(data.data)
    }

    /**
     * @api {post} /web/trademark/trademarkCtl/delete 删除
     * @apiGroup 标源管理
     * @apiParamExample {json} 请求参数
     * {"id": "1"}
     * @apiSuccessExample {json} 成功示例:
     * {"success": ""}
     * @apiErrorExample {json} 失败示例:
     * {"error": ""}
     */
    async delete(p, q, req) {
        let body = {
            "actionType": "tmInfoDelete",
            "actionBody": {
                id: 0
            }
        };
        body.actionBody.id = p.id;
        let rtn = await this.execClient.execPost(body, tmurl);
        let data = JSON.parse(rtn.stdout);
        if (data.status == 1) {
            let en = await this.service.findOne({trademark_id: p.id});
            if (en != null) {
                en.destroy();
            }
        }
        return system.getResult(data.data)
    }

    //接受队列返回消息
    async notify(p, q, req) {
        let update = [];
        let tradedb = p.actionBody.resultInfo.data;
        tradedb.map(db => update.push({
            trademark_id: db.id,
            tm_code: db.code
        }));
        let res = await this.service.dao.model.bulkCreate(tradedb, {updateOnDuplicate: ["tm_code"]});
        return system.getResult(res)
    }
}

module.exports = TrademarkCtl;

