package com.cnd3b.restcontroller.enterprise.salerwebclient.mainpage.agentwork.businesstrip;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.D3bException;
import com.cnd3b.common.data.Row;
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 businesstrip extends Controller {
    public businesstrip(JSONObject content) {
        super(content);
    }

    /**
     * 出差行程列表查询
     *
     * @return
     */
    public String query_businesstripList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.changedate"};
        String sort = getSort(sortfield, "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(")");
            }
            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_in("tenterprise_userid", getSubUsers());
        costhead.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).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());

            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 businesstripInsertOrModify() throws D3bException, P2Exception {
        long tagentsid = content.getLongValue("tagentsid");//经销商ID
        long tbusinesstripid = content.getLongValue("tbusinesstripid");//出差行程ID，新增时默认为0

        String fbegdate = content.getString("fbegdate");//到达日期
        String fenddate = content.getString("fenddate");//返程日期
        String fprovince = content.getString("fprovince");//省
        String fcity = content.getString("fcity");//市
        String fcounty = content.getString("fcounty");//县
        String fdays = content.getString("fdays");//停留时间
        String fnotes = content.getString("fnotes", "tbusinesstrip.fnotes", "出差备注");//出差备注
        String fothername = content.getString("fothername", "tbusinesstrip.fothername", "随行人员");//随行人员

        PaoSetRemote tbusinesstripSet = getP2ServerSystemPaoSet("tbusinesstrip", "siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and tbusinesstripid='" + tbusinesstripid + "'");
        PaoRemote tbusinesstrip = null;
        if (tbusinesstripid <= 0 || tbusinesstripSet.isEmpty()) {
            tbusinesstrip = tbusinesstripSet.addAtEnd();
            tbusinesstrip.setValue("siteid", siteid, 11L);//企业ID
            tbusinesstrip.setValue("tenterprise_userid", userid, 11L);
            tbusinesstrip.setValue("fstatus", "待提交", 11L);//记录状态
            tbusinesstrip.setValue("fname", username, 11L);//出差人
            tbusinesstrip.setValue("createby", username, 11L);//录入人
            tbusinesstrip.setValue("createdate", sysdate, 11L);//录入时间
        } else {
            tbusinesstrip = tbusinesstripSet.getPao(0);
        }

        tbusinesstrip.setValue("tagentsid", tagentsid, 11L);//经销商ID
        tbusinesstrip.setValue("fbegdate", fbegdate, 11L);//到达日期
        tbusinesstrip.setValue("fenddate", fenddate, 11L);//返程日期
        tbusinesstrip.setValue("fprovince", fprovince, 11L);//省
        tbusinesstrip.setValue("fcity", fcity, 11L);//市
        tbusinesstrip.setValue("fcounty", fcounty, 11L);//县
        tbusinesstrip.setValue("fdays", fdays, 11L);//停留时间
        tbusinesstrip.setValue("fnotes", fnotes, 11L);//出差备注
        tbusinesstrip.setValue("fothername", fothername, 11L);//随行人员

        content.put("tbusinesstripid", tbusinesstrip.getUniqueIDValue());
        saveDataLog(tbusinesstrip);
        tbusinesstripSet.save();

        return query_businesstripMain();
    }

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

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

    /**
     * 出差行程记录提交
     *
     * @return
     */
    public String businesstripSubmit() {
        long tbusinesstripid = content.getLongValue("tbusinesstripid");//出差行程ID
        Rows checkrows = dbConnect.runSqlQuery("select *from tbusinesstrip where siteid='" + siteid + "' and tbusinesstripid=" + tbusinesstripid);
        if (checkrows.isEmpty()) {
            return getErrReturnObject().setErrMsg("工作行程不存在！").toString();
        } else if (!"待提交".equals(checkrows.getRow(0).getString("fstatus"))) {
            return getErrReturnObject().setErrMsg("工作行程已提交，不可重复提交！").toString();
        }
        SQLFactory sqlFactory = new SQLFactory(this, "出差行程记录提交");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tbusinesstripid", tbusinesstripid);
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equalsIgnoreCase(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 经销商选择列表查询
     *
     * @return
     */
    public String query_agentList() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商列表查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter_in("fsaleruserid", getSubUsers());
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

}
