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 = "/api/action/tmQuery/springBoard";
const tmurl = settings.channelApiUrl() + tmtransactionPath;
//标源检索
class searchCtl extends CtlBase {
    constructor() {
        super("trademark", "trademarkSve");
        this.execClient = system.getObject("util.execClient");
    }

    /**
     * @api {post} /web/trademark/searchCtl/findAndCountAll 检索列表
     * @apiGroup 标源检索
     * @apiParamExample {json} 请求参数
     * {"pageInfo": {"pageNo":1,"pageSize":10}}
     * @apiSuccessExample {json} 成功示例:
     * {"success": ""}
     * @apiErrorExample {json} 失败示例:
     * {"error": ""}
     */
    async findAndCountAll(p, q, req) {
        try {

            let query = {
                "actionType": "",
                "actionBody": {
                    pageSize: p.pageInfo.pageSize,
                    currentPage: p.pageInfo.pageNo,
                    "tmName": "",//商标名称
                    "regMan": "",
                    "regNum": "",
                    "sqlbtabkey": "",//商标类别
                    "flzttabkey": "",//法律状态
                    "sqnftabkey": "",//申请年份
                    "ncl": [],//尼斯大类
                    "xcl": [],//尼斯群组
                    "nclNum": [],//尼斯大类
                }
            };
            if (Object.keys(p.search).length > 0) {
                if (p.search.sbzch) {//注册号查询
                    query.actionType = "findTrademarkzchAccurate";
                    query.actionBody.regNum = p.search.sbzch
                } else if (p.search.zcren) {//申请人查询
                    query.actionType = "findTrademarkzcr";
                    query.actionBody.regMan = p.search.zcren
                } else if (p.search.sbmc) {//商标名称查询
                    query.actionType = "findTrademarkNameAccurate";
                    query.actionBody.tmName = p.search.sbmc
                }
            } else return;
            let tokenInfo = await this.service.getToken();
            let rtn = await this.execClient.execPostTK(query, tmurl, tokenInfo.data.token);
            let data = rtn.data;
            let published = await this.service.findAll({company_id: p.company_id});
            data.rows.forEach(item => {
                for (let t of published) {
                    if (t.tm_code == item.sbzch)
                        item._disabled = true;
                }
            });
            return system.getResultList(data.count, data.rows)
        }catch (e) {
            return system.getResultFail(-1, e)
        }
    }
}

module.exports = searchCtl;

