package com.cnd3b.restcontroller.enterprise.agentclient.mainpage.submitedit;

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.RowsMap;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DataPool;

import com.alibaba.fastjson.JSONObject;

/**
 * 提报管理
 */
public class submitedit extends Controller {

    public submitedit(JSONObject content) {
        super(content);
    }

    /**
     * 经销商提报列表查询
     *
     * @return
     */
    public String query_submiteditmodelList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.checkdate"};
        String sort = getSort(sortfield, "t1.checkdate 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.ftitle like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fisnotlimittimes") && !"".equals(whereObject.getString("fisnotlimittimes"))) {
                where.append(" and(");
                where.append("t1.fisnotlimittimes ='").append(whereObject.getString("fisnotlimittimes")).append("' ");
                where.append(")");
            }
        }
        SQLFactory submitedithead = new SQLFactory(this, "提报列表查询", pageSize, pageNumber, sort);
        submitedithead.addParameter("siteid", siteid);
        submitedithead.addParameter("tenterprise_userid", userid);
        submitedithead.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(submitedithead.getSQL());

        SQLFactory editstatuslist = new SQLFactory(this, "提报列表查询_提报状态查询");
        editstatuslist.addParameter("siteid", siteid);
        editstatuslist.addParameter("tenterprise_userid", userid);
        editstatuslist.addParameter_in("tsubmiteditmodelid", rows.toArray("tsubmiteditmodelid"));
        RowsMap editstatusrowsMap = dbConnect.runSqlQuery(editstatuslist).toRowsMap("tsubmiteditmodelid");

        SQLFactory editsubmitusercountlist = new SQLFactory(this, "提报列表查询_提报人数查询");
        editsubmitusercountlist.addParameter("siteid", siteid);
        editsubmitusercountlist.addParameter_in("tsubmiteditmodelid", rows.toArray("tsubmiteditmodelid"));
        RowsMap editsubmitusercountrowsMap = dbConnect.runSqlQuery(editsubmitusercountlist).toRowsMap("tsubmiteditmodelid");

        for (Row row : rows) {
            String tsubmiteditmodelid = row.getString("tsubmiteditmodelid");
            if (editstatusrowsMap.containsKey(tsubmiteditmodelid)) {
                row.put("fisread", editstatusrowsMap.get(tsubmiteditmodelid).get(0).getInteger("fisread") > 0 ? 1 : 0);
                row.put("fissubmit", editstatusrowsMap.get(tsubmiteditmodelid).get(0).getInteger("fissubmit") > 0 ? 1 : 0);
            } else {
                row.put("fisread", 0);
                row.put("fissubmit", 0);
            }
            if (editsubmitusercountrowsMap.containsKey(tsubmiteditmodelid)) {
                row.put("fsubmitqty", editsubmitusercountrowsMap.get(tsubmiteditmodelid).get(0).getInteger("fsubmitqty"));
            } else {
                row.put("fsubmitqty", 0);
            }
        }
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 经销商提报主界面
     *
     * @return
     */
    public String query_submiteditmodelMain() {
        String tsubmiteditmodelid = content.getString("tsubmiteditmodelid");//提报模板ID
        SQLFactory submithead = new SQLFactory(this, "提报主界面查询");
        submithead.addParameter("siteid", siteid);
        submithead.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        Rows headrows = dbConnect.runSqlQuery(submithead.getSQL());
        for (Row row : headrows) {
            SQLFactory submitlist = new SQLFactory(this, "提报主界面查询_提报明细");
            submitlist.addParameter("siteid", siteid);
            submitlist.addParameter("tenterprise_userid", userid);
            submitlist.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
            Rows listrows = dbConnect.runSqlQuery(submitlist.getSQL());

            boolean fissubmit = listrows.getLastRow().getBoolean("fissubmit");
            row.put("fissubmit", fissubmit);
            row.put("submiteditlist", listrows);
        }
        dbConnect.runSqlUpdate("update tsubmitedit set fisread=1 where siteid='" + siteid + "' and tsubmiteditmodelid='" + tsubmiteditmodelid + "' and tenterprise_userid='" + userid + "'");
        return getSucReturnObject().setData(headrows).toString();
    }

    /**
     * 经销商新建提报，针对一事多报的功能
     *
     * @return
     */
    public String newedit() throws D3bException {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");//提报模板ID

        Rows check = dbConnect.runSqlQuery("select tsubmiteditid from tsubmitedit where siteid='" + siteid + "' and fstatus='新建' and tsubmiteditmodelid='" + tsubmiteditmodelid + "' and tenterprise_userid='" + userid + "'");
        if (!check.isEmpty()) {
            return getErrReturnObject().setErrMsg("已存在一个未提交的提报，不可进行新增操作").toString();
        }

        String fcontent = content.getString("fcontent", "tsubmitedit.fcontent", "提报内容");
        SQLFactory sqlFactory = new SQLFactory(this, "新增");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("createby", username);
        sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        sqlFactory.addParameter("tenterprise_userid", userid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("fcontent", fcontent);
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equalsIgnoreCase(status)) {
            return query_submiteditmodelMain();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 经销商提报保存
     *
     * @return
     */
    public String saveedit() throws D3bException {
        long tsubmiteditmodelid = content.getLong("tsubmiteditmodelid");//提报模板ID
        long tsubmiteditid = content.getLong("tsubmiteditid");//提报ID

        Rows check = dbConnect.runSqlQuery("select tsubmiteditid from tsubmitedit where siteid='" + siteid + "' and fstatus='提交' and tsubmiteditmodelid='" + tsubmiteditmodelid + "' and tsubmiteditid='" + tsubmiteditid + "' and tenterprise_userid='" + userid + "'");
        if (!check.isEmpty()) {
            return getErrReturnObject().setErrMsg("当前提报已提交，不可进行修改操作").toString();
        }

        String fcontent = content.getString("fcontent", "tsubmitedit.fcontent", "提报内容");
        SQLFactory sqlFactory = new SQLFactory(this, "保存");
        sqlFactory.addParameter("fcontent", fcontent);
        sqlFactory.addParameter("changeby", username);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        sqlFactory.addParameter("tsubmiteditid", tsubmiteditid);
        sqlFactory.addParameter("tenterprise_userid", userid);
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equalsIgnoreCase(status)) {
            return query_submiteditmodelMain();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 经销商提交
     *
     * @return
     */
    public String submitedit() throws D3bException {
        String tsubmiteditmodelid = content.getString("tsubmiteditmodelid");//提报模板ID
        String tsubmiteditid = content.getString("tsubmiteditid");//提报ID

        Rows check = dbConnect.runSqlQuery("select tsubmiteditid from tsubmitedit where siteid='" + siteid + "' and fstatus='提交' and tsubmiteditmodelid='" + tsubmiteditmodelid + "' and tsubmiteditid='" + tsubmiteditid + "' and tenterprise_userid='" + userid + "'");
        if (!check.isEmpty()) {
            return getErrReturnObject().setErrMsg("当前提报已提交，不可进行提交操作").toString();
        }

        String fcontent = content.getString("fcontent", "tsubmitedit.fcontent", "提报内容");

        SQLFactory sqlFactory = new SQLFactory(this, "提报");
        sqlFactory.addParameter("submitby", username);
        sqlFactory.addParameter("fcontent", fcontent);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        sqlFactory.addParameter("tsubmiteditid", tsubmiteditid);
        sqlFactory.addParameter("tenterprise_userid", userid);
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equalsIgnoreCase(status)) {
            return query_submiteditmodelMain();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }
}
