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

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.actionscore.ActionScore;
import com.cnd3b.common.data.Row;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.RowsMap;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DataPool;

public class businesstrip extends Controller {
    public businesstrip(JSONObject content) {
        super(content);
    }

    /**
     * 出差行程列表查询
     *
     * @return
     */
    public String query_businesstripList() {
        String sort = "t1.changedate 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("t1.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t2.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t3.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t4.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("createdate") && !"".equals(whereObject.getString("createdate"))) {
                where.append(" and(");
                where.append("convert(varchar(10),t1.createdate,120) ='").append(whereObject.getString("createdate")).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).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 出差行程主界面查询
     *
     * @return
     */
    public String query_businesstripMain() {
        SQLFactory headsql = new SQLFactory(this, "出差行程主界面查询");
        headsql.addParameter("siteid", siteid);
        headsql.addParameter("tbusinesstripid", content.getString("tbusinesstripid"));
        Rows rows = dbConnect.runSqlQuery(headsql.getSQL());
        for (Row row : rows) {
            SQLFactory bzwork = new SQLFactory(this, "出差行程标准工作查询");
            bzwork.addParameter("siteid", siteid);
            bzwork.addParameter("tbusinesstripid", content.getString("tbusinesstripid"));
            Rows bzworkrows = dbConnect.runSqlQuery(bzwork.getSQL());

            SQLFactory zxwork = new SQLFactory(this, "出差行程自选工作查询");
            zxwork.addParameter("siteid", siteid);
            zxwork.addParameter("tbusinesstripid", content.getString("tbusinesstripid"));
            Rows zxworkrows = dbConnect.runSqlQuery(zxwork.getSQL());


            SQLFactory workmx = new SQLFactory(this, "工作明细查询");
            workmx.addParameter("siteid", siteid);
            workmx.addParameter("tbusinesstripid", content.getString("tbusinesstripid"));
            RowsMap workmxrowMap = dbConnect.runSqlQuery(workmx.getSQL()).toRowsMap("tworkid");

            for (Row bzrow : bzworkrows) {
                bzrow.put("workmx", workmxrowMap.get(bzrow.getString("tworkid")));
            }
            for (Row zxrow : zxworkrows) {
                zxrow.put("workmx", workmxrowMap.get(zxrow.getString("tworkid")));
            }
            row.put("worklist", bzworkrows.toJsonArray());
            row.put("worklist_zx", zxworkrows.toJsonArray());
            row.put("datalog", getDataLog("tbusinesstripid", row.getString("tbusinesstripid")));
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 出差评定
     *
     * @return
     */
    public String updateGrade() {
        long tbusinesstripid = content.getLongValue("tbusinesstripid");//出差行程ID
        if (dbConnect.runSqlQuery("select * from tbusinesstrip where siteid='" + siteid + "' and fstatus='已提交' and tbusinesstripid=" + tbusinesstripid).isEmpty()) {
            return getErrReturnObject().setErrMsg("当前状态不可进行出差评定").toString();
        }
        String fgrade = content.getString("fgrade");//评定结果
        SQLFactory sqlFactory = new SQLFactory(this, "出差行程记录评定");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fgrade", fgrade);
        sqlFactory.addParameter("tbusinesstripid", tbusinesstripid);
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equalsIgnoreCase(status)) {
            SQLFactory headsql = new SQLFactory(this, "出差行程主界面查询");
            headsql.addParameter("siteid", siteid);
            headsql.addParameter("tbusinesstripid", content.getString("tbusinesstripid"));
            Rows rows = dbConnect.runSqlQuery(headsql.getSQL());
            if (!rows.isEmpty()) {
                addActionScore(ActionScore.fsourcetype.行程评优, rows.get(0).getLong("tenterprise_userid"), rows.get(0).getString("fname"), tbusinesstripid, "行程评优");
            }
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 出差行程记录删除
     *
     * @return
     */
    public String businesstripDelete() {
        String tbusinesstripid = content.getString("tbusinesstripid");//出差行程ID
        SQLFactory sqlFactory = new SQLFactory(this, "出差行程记录删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tbusinesstripid", tbusinesstripid);

        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }

}
