package com.cnd3b.restcontroller.enterprise.webclient.complaint;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DataPool;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

/**
 * 投诉
 */
public class complaint extends Controller {
    public complaint(JSONObject content) {
        super(content);
    }

    /**
     * 投诉查询
     *
     * @return
     */
    public String query() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"createdate desc"};
        String sort = getSort(sortfield, "createdate desc");
        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (content.containsKey("where")) {
            JSONObject whereObject = content.getJSONObject("where");
            if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
                where.append(" and(");
                where.append("ftitle like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or fcontent like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or ftype like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or fphonenumber like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or createby like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }

            if (whereObject.containsKey("fstatus") && !"".equals(whereObject.getString("fstatus"))) {
                where.append(" and(");
                where.append("fstatus ='").append(whereObject.getString("fstatus")).append("' ");
                where.append(")");
            }
        }
        SQLFactory costhead = new SQLFactory(this, "投诉列表查询", pageSize, pageNumber, sort);
        costhead.addParameter("siteid", siteid);
        costhead.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }


    /**
     * 投诉更新
     *
     * @return
     */
    public String update() throws P2Exception {
        long tcomplaintid = content.getLongValue("tcomplaintid");//投诉ID
        String fstatus = content.getString("fstatus");

        PaoSetRemote tcomplaintSet = getP2ServerSystemPaoSet("tcomplaint", "siteid='" + siteid + "' and tcomplaintid=" + tcomplaintid);
        if (!tcomplaintSet.isEmpty()) {
            PaoRemote tcomplaint = tcomplaintSet.getPao(0);
            tcomplaint.setValue("fstatus", fstatus, 11L);
            tcomplaint.setValue("changeby", username, 11L);//修改人
            tcomplaint.setValue("changedate", sysdate, 11L);//修改时间
            saveDataLog(tcomplaint);
            tcomplaintSet.save();
        }
        return getSucReturnObject().toString();
    }

    /**
     * 投诉分析
     *
     * @return
     */
    public String complaint_analysis() {
        int fyear = content.getIntValue("fyear");
        int fmonth = content.getIntValue("fmonth");
        String ftype = content.getString("ftype");
        if (fmonth == 0) {
            SQLFactory factory = new SQLFactory(this, "年度投诉分析");
            factory.addParameter("siteid", siteid);
            factory.addParameter("ftype", ftype);
            factory.addParameter("fyear", fyear);
            Rows rows = dbConnect.runSqlQuery(factory.getSQL());
            return getSucReturnObject().setData(rows).saveToDataPool().toString();
        } else {
            SQLFactory factory = new SQLFactory(this, "月度投诉分析");
            factory.addParameter("siteid", siteid);
            factory.addParameter("ftype", ftype);
            factory.addParameter("fyear", fyear);
            factory.addParameter("fmonth", fmonth);
            Rows rows = dbConnect.runSqlQuery(factory.getSQL());
            return getSucReturnObject().setData(rows).saveToDataPool().toString();
        }
    }

    /**
     * 投诉板块分析
     *
     * @return
     */
    public String complainttype_analysis() {
        int fyear = content.getIntValue("fyear");
        int fmonth = content.getIntValue("fmonth");
        if (fmonth == 0) {
            SQLFactory factory = new SQLFactory(this, "年度投诉板块分析");
            factory.addParameter("siteid", siteid);
            factory.addParameter("fyear", fyear);
            Rows rows = dbConnect.runSqlQuery(factory.getSQL());
            return getSucReturnObject().setData(rows).saveToDataPool().toString();
        } else {
            SQLFactory factory = new SQLFactory(this, "月度投诉板块分析");
            factory.addParameter("siteid", siteid);
            factory.addParameter("fyear", fyear);
            factory.addParameter("fmonth", fmonth);
            Rows rows = dbConnect.runSqlQuery(factory.getSQL());
            return getSucReturnObject().setData(rows).saveToDataPool().toString();
        }
    }
}
