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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.D3bException;
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;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

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

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

    /**
     * 新建或修改提报模板
     */
    public String insertOrModify() throws D3bException, P2Exception {
        long tsubmiteditmodelid = content.getLong("tsubmiteditmodelid"); //新增时默认传0
        String ftitle = content.getString("ftitle", "tsubmiteditmodel.ftitle", "标题");//标题
        Date fbegdate = content.getDate("fbegdate");//开始时间
        Date fenddate = content.getDate("fenddate");//结束时间
        String fnotes = content.getString("fnotes", "tsubmiteditmodel.fnotes", "提报要求");//提报要求
        boolean fisattrequired = content.getBooleanValue("fisattrequired");//附件必填
        boolean fistextrequired = content.getBooleanValue("fistextrequired");//文字必填
        boolean fisnotlimittimes = content.getBooleanValue("fisnotlimittimes");//是否允许一事多报

        PaoSetRemote tsubmiteditmodelSet = getP2ServerSystemPaoSet("tsubmiteditmodel", "siteid='" + siteid + "' and tsubmiteditmodelid='" + tsubmiteditmodelid + "'");
        PaoRemote tsubmiteditmodel = null;
        if (tsubmiteditmodelid <= 0 || tsubmiteditmodelSet.isEmpty()) {
            tsubmiteditmodel = tsubmiteditmodelSet.addAtEnd();
            tsubmiteditmodel.setValue("siteid", siteid, 11L);//企业ID
            tsubmiteditmodel.setValue("createby", username, 11L);//录入人
            tsubmiteditmodel.setValue("createdate", sysdate, 11L);//录入时间
            tsubmiteditmodel.setValue("fstatus", "新建", 11L);
        } else {
            tsubmiteditmodel = tsubmiteditmodelSet.getPao(0);
        }
        tsubmiteditmodel.setValue("fisnotlimittimes", fisnotlimittimes, 11L);//是否允许一事多报
        tsubmiteditmodel.setValue("ftitle", ftitle, 11L);//标题
        tsubmiteditmodel.setValue("fbegdate", fbegdate, 11L);//开始时间
        tsubmiteditmodel.setValue("fenddate", fenddate, 11L);//结束时间
        tsubmiteditmodel.setValue("fnotes", fnotes, 11L);//提报要求
        tsubmiteditmodel.setValue("fisattrequired", fisattrequired, 11L);//附件必填
        tsubmiteditmodel.setValue("fistextrequired", fistextrequired, 11L);//文字必填
        tsubmiteditmodel.setValue("changeby", username, 11L);//修改人
        tsubmiteditmodel.setValue("changedate", sysdate, 11L);//修改时间

        content.put("tsubmiteditmodelid", tsubmiteditmodel.getUniqueIDValue());
        saveDataLog(tsubmiteditmodel);
        tsubmiteditmodelSet.save();

        return query_submitEditMain();
    }

    public String query_submitEditMain() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        SQLFactory noticehead = new SQLFactory(this, "提报主界面查询");
        noticehead.addParameter("siteid", siteid);
        noticehead.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        Rows rows = dbConnect.runSqlQuery(noticehead.getSQL());
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 提报模板发布
     */
    public String check() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        boolean fischeck = content.getBooleanValue("fischeck");//是否发布
        SQLFactory sqlFactory = null;
        if (fischeck) {
            sqlFactory = new SQLFactory(this, "提报模板发布");
            sqlFactory.addParameter("checkby", username);
        } else {
            sqlFactory = new SQLFactory(this, "提报模板取消发布");
        }
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equalsIgnoreCase(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }


    /**
     * 提报列表查询
     *
     * @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 sqlFactory = new SQLFactory(this, "提报列表查询", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }


    /**
     * 提报明细查询
     *
     * @return
     */
    public String query_submiteditList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.submitdate"};
        String sort = getSort(sortfield, "t1.submitdate 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("t2.fphonenumber like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.createby like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t3.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("tenterprise_userid") && !"".equals(whereObject.getString("tenterprise_userid"))) {
                where.append(" and(");
                where.append("t1.tenterprise_userid ='").append(whereObject.getString("tenterprise_userid")).append("' ");
                where.append(")");
            }
        }
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");//提报模板ID
        SQLFactory costhead = new SQLFactory(this, "提报明细列表查询", pageSize, pageNumber, sort);
        costhead.addParameter("siteid", siteid);
        costhead.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        costhead.addParameter_SQL("where", where);

        Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }


    /**
     * 提报退回
     *
     * @return
     */
    public String returnedit() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");//提报模板ID
        long tsubmiteditid = content.getLongValue("tsubmiteditid");

        SQLFactory sqlFactory = new SQLFactory(this, "提报退回");
        sqlFactory.addParameter("returnby", username);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        sqlFactory.addParameter("tsubmiteditid", tsubmiteditid);

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


    /**
     * 提报模板删除
     *
     * @return
     */
    public String delete() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");//提报模板ID
        if (dbConnect.runSqlQuery("select * from tsubmiteditmodel where siteid='" + siteid + "' and fstatus='新建' and tsubmiteditmodelid=" + tsubmiteditmodelid).isEmpty()) {
            return getErrReturnObject().setErrMsg("当前状态不可删除!").toString();
        }
        SQLFactory sqlFactory = new SQLFactory(this, "提报模板删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tsubmiteditmodelid", content.getString("tsubmiteditmodelid"));

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

    /**
     * 查询提报人员
     *
     * @return
     */
    public String query_submitedituserList() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tsubmiteditid"};
        String sort = getSort(sortfield, "t1.tsubmiteditid");
        /**
         * 过滤条件设置
         */
        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("t2.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("t3.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fusertype") && !"".equals(whereObject.getString("fusertype"))) {
                where.append(" and(");
                where.append("t2.fusertype ='").append(whereObject.getString("fusertype")).append("' ");
                where.append(")");
            }
        }
        SQLFactory sqlFactory = new SQLFactory(this, "查询提报人员", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        sqlFactory.addParameter_SQL("where", where);

        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 添加提报人员
     *
     * @return
     */
    public String adduser() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        JSONArray idarray = content.getJSONArray("tenterprise_userid");
        Iterator it = idarray.iterator();
        ArrayList<String> list = new ArrayList<>();
        while (it.hasNext()) {
            SQLFactory sqlFactory = new SQLFactory(this, "添加提报人员");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
            sqlFactory.addParameter("tenterprise_userid", (String) it.next());
            list.add(sqlFactory.getSQL());
        }
        if (!list.isEmpty()) {
            String status = dbConnect.runSqlUpdate(list);
            if ("true".equals(status)) {
                return getSucReturnObject().toString();
            } else {
                return getErrReturnObject().setErrMsg(status).toString();
            }
        }
        return getErrReturnObject().setErrMsg("没有选择添加人员!").toString();
    }

    /**
     * 添加提报人员
     *
     * @return
     */
    public String addusergroup() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        long tusergroupid = content.getLongValue("tusergroupid");

        SQLFactory sqlFactory = null;
        if (tusergroupid == 1) {
            sqlFactory = new SQLFactory(this, "添加提报人员组_所有业务员");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
            sqlFactory.addParameter("tusergroupid", tusergroupid);
        } else if (tusergroupid == 2) {
            sqlFactory = new SQLFactory(this, "添加提报人员组_所有经销商主账号");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
            sqlFactory.addParameter("tusergroupid", tusergroupid);
        } else {
            sqlFactory = new SQLFactory(this, "添加提报人员组");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
            sqlFactory.addParameter("tusergroupid", tusergroupid);
        }
        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 删除提报人员
     *
     * @return
     */
    public String deleteuser() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        JSONArray idarray = content.getJSONArray("tenterprise_userid");
        Iterator<Object> it = idarray.iterator();
        ArrayList<String> list = new ArrayList<>();
        while (it.hasNext()) {
            SQLFactory sqlFactory = new SQLFactory(this, "删除提报人员");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("createby", username);
            sqlFactory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
            sqlFactory.addParameter("tenterprise_userid", (String) it.next());
            list.add(sqlFactory.getSQL());
        }
        if (!list.isEmpty()) {
            String status = dbConnect.runSqlUpdate(list);
            if ("true".equals(status)) {
                return getSucReturnObject().toString();
            } else {
                return getErrReturnObject().setErrMsg(status).toString();
            }
        }
        return getErrReturnObject().setErrMsg("没有选择删除人员!").toString();
    }


    /**
     * 未提报用户列表
     *
     * @return
     */
    public String havenoreaduserlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tsubmiteditid"};
        String sort = getSort(sortfield, "t1.tsubmiteditid");
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        SQLFactory factory = new SQLFactory(this, "分析_未提报经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 未提报用户列表
     *
     * @return
     */
    public String havenoreaduserlist_saler() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tsubmiteditid"};
        String sort = getSort(sortfield, "t1.tsubmiteditid");
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        SQLFactory factory = new SQLFactory(this, "分析_未提报业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 已提报用户列表
     *
     * @return
     */
    public String havereaduserlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tenterprise_userid"};
        String sort = getSort(sortfield, "t1.tenterprise_userid");
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        SQLFactory factory = new SQLFactory(this, "分析_已提报经销商", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 已提报用户列表
     *
     * @return
     */
    public String havereaduserlist_saler() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tenterprise_userid"};
        String sort = getSort(sortfield, "t1.tenterprise_userid");
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        SQLFactory factory = new SQLFactory(this, "分析_已提报业务员", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 提报分析
     *
     * @return
     */
    public String submit_analysis() {
        long tsubmiteditmodelid = content.getLongValue("tsubmiteditmodelid");
        SQLFactory factory = new SQLFactory(this, "分析_提报占比分析");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tsubmiteditmodelid", tsubmiteditmodelid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }
}
