package com.cnd3b.restcontroller.enterprise.salerclient.mainpage.action.storeinput;

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

import java.util.Date;

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

    /**
     * 业务员经销商店铺列表查询
     *
     * @return
     */
    public String query_storeList() {
        String sort = "t1.tagentsid,t1.fagentname";
        /**
         * 过滤条件设置
         */
        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.fstorename like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
        }
        SQLFactory costhead = new SQLFactory(this, "经销商门店列表查询", pageSize, pageNumber, sort);
        costhead.addParameter("siteid", siteid);
        costhead.addParameter_in("fsaleruserid", getSubUsers());
        costhead.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 经销商门店主界面查询
     *
     * @return
     */
    public String query_storeMain() {
        SQLFactory costhead = new SQLFactory(this, "经销商门店主界面查询");
        costhead.addParameter("siteid", siteid);
        costhead.addParameter("tagentsid", content.getString("tagentsid"));
        costhead.addParameter("tstoreid", content.getString("tstoreid"));

        Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
        for (Row row : rows) {
            row.put("datalog", getDataLog("tstore", row.getString("tstoreid")));
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 经销商门店新增、修改
     *
     * @return
     */
    public String storeInsertOrModify() throws D3bException, P2Exception {
        long tagentsid = content.getLongValue("tagentsid");//经销商ID
        long tstoreid = content.getLongValue("tstoreid");//门店ID，新增时默认传0

        String fstorenum = content.getString("fstorenum", "tstore.fstorenum", "门店编号");//门店编号
        String fstorename = content.getString("fstorename", "tstore.fstorename", "门店名称");//门店名称
        double farea = content.getDoubleValue("farea");//门店面积
        String fstoretype = content.getString("fstoretype");//门店类型
        String fmarket = content.getString("fmarket");//归属卖场

        String fstartdate = content.getString("fstartdate");//建店日期
        String fstatus = content.getString("fstatus");//门店状态
        String fprovince = content.getString("fprovince");//省份
        String fcity = content.getString("fcity");//城市
        String fcounty = content.getString("fcounty");//区县
        String faddress = content.getString("faddress", "tstore.faddress", "详细地址");//门店地址

        String fcontact = content.getString("fcontact", "tstore.fcontact", "门店负责人");//联系人
        String fphonenumber = content.getString("fphonenumber", "tstore.fphonenumber", "手机号码");//联系方式
        String femail = content.getString("femail", "tstore.femail", "电子邮箱");//邮箱
        String fwechatno = content.getString("fwechatno", "tstore.fwechatno", "微信");//微信号

        PaoSetRemote tstoreSet = getP2ServerSystemPaoSet("tstore", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tstoreid='" + tstoreid + "'");
        PaoRemote tstore = null;
        if (tstoreid <= 0 || tstoreSet.isEmpty()) {
            tstore = tstoreSet.addAtEnd();
            tstore.setValue("siteid", siteid, 11L);//企业ID
            tstore.setValue("tagentsid", tagentsid, 11L);//经销商ID
            tstore.setValue("createby", username, 11L);//录入人
            tstore.setValue("createdate", sysdate, 11L);//录入时间

            addActionScore(ActionScore.fsourcetype.店铺录入, tstore.getUniqueIDValue(), "店铺录入");
        } else {
            tstore = tstoreSet.getPao(0);
        }
        tstore.setValue("fstorenum", fstorenum, 11L);//门店编号
        tstore.setValue("fstorename", fstorename, 11L);//门店名称
        tstore.setValue("farea", farea, 11L);//门店面积
        tstore.setValue("fstoretype", fstoretype, 11L);//门店类型
        tstore.setValue("fmarket", fmarket, 11L);//归属卖场
        tstore.setValue("fstartdate", fstartdate, 11L);//建店日期
        tstore.setValue("fstatus", fstatus, 11L);//门店状态
        tstore.setValue("fprovince", fprovince, 11L);//省份
        tstore.setValue("fcity", fcity, 11L);//城市
        tstore.setValue("fcounty", fcounty, 11L);//区县
        tstore.setValue("faddress", faddress, 11L);//门店地址
        tstore.setValue("fcontact", fcontact, 11L);//联系人
        tstore.setValue("fphonenumber", fphonenumber, 11L);//联系方式
        tstore.setValue("femail", femail, 11L);//邮箱
        tstore.setValue("fwechatno", fwechatno, 11L);//微信
        tstore.setValue("changeby", username, 11L);//修改人
        tstore.setValue("changedate", sysdate, 11L);//修改时间

        content.put("tstoreid", tstore.getUniqueIDValue());
        saveDataLog(tstore);
        tstoreSet.save();

        return query_storeMain();
    }


    /**
     * 经销商门店删除
     *
     * @return
     */
    public String storeDelete() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商门店删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", content.getString("tagentsid"));
        sqlFactory.addParameter("tstoreid", content.getString("tstoreid"));

        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).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();
    }

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


    /**
     * 门店类型选择
     *
     * @return
     */
    public String query_storetypeList() {
        SQLFactory sqlFactory = new SQLFactory(this, "门店类型选择");
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 门店状态选择
     *
     * @return
     */
    public String query_storestatusList() {
        SQLFactory sqlFactory = new SQLFactory(this, "门店状态选择");
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 门店改建
     *
     * @return
     */
    public String storeRebuild() throws D3bException, P2Exception {
        long tagentsid = content.getLongValue("tagentsid");//经销商ID
        long tstoreid = content.getLongValue("tstoreid");//门店ID
        String fdate = content.getString("fdate");//改建日期
        String ftype = content.getString("ftype");//改建类型
        double farea = content.getDoubleValue("farea");//门店面积
        String fnotes = content.getString("fnotes", "tstore_rebuildlog.fnotes", "改建说明");//改建说明

        PaoSetRemote tstore = getP2ServerSystemPaoSet("tstore", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tstoreid='" + tstoreid + "'");
        if (!tstore.isEmpty()) {
            PaoSetRemote tstore_rebuildlogSet = tstore.getPao(0).getPaoSet("tstore_rebuildlog");
            tstore_rebuildlogSet.setOrderBy("fdate desc");
            tstore_rebuildlogSet.reset();
            Date lasttime;//上一次翻新时间
            if (tstore_rebuildlogSet.isEmpty()) {
                //如果之前没有翻新过，则以建店日期为上一次翻新时间
                lasttime = tstore.getPao(0).getDate("fstartdate");
            } else {
                lasttime = tstore_rebuildlogSet.getPao(0).getDate("fdate");
            }

            PaoRemote tstore_rebuildlog = tstore_rebuildlogSet.addAtEnd();
            tstore_rebuildlog.setValue("siteid", siteid, 11L);//企业ID
            tstore_rebuildlog.setValue("tagentsid", tagentsid, 11L);//经销商ID
            tstore_rebuildlog.setValue("tstoreid", tstoreid, 11L);//门店ID

            tstore_rebuildlog.setValue("fdate", fdate, 11L);//改建日期
            tstore_rebuildlog.setValue("farea", farea, 11L);//门店面积
            tstore_rebuildlog.setValue("ftype", ftype, 11L);//改建类型
            tstore_rebuildlog.setValue("fnotes", fnotes, 11L);//改建说明

            tstore_rebuildlog.setValue("createby", username, 11L);//录入人
            tstore_rebuildlog.setValue("createdate", sysdate, 11L);//录入时间
            tstore_rebuildlog.setValue("changeby", username, 11L);//修改人
            tstore_rebuildlog.setValue("changedate", sysdate, 11L);//修改时间

            float times = tstore_rebuildlog.getDate("fdate").getTime() - lasttime.getTime();
            tstore_rebuildlog.setValue("fintervaltime", times / (float) (1000 * 60 * 60 * 24));

            tstore.getPao(0).setValue("farea", farea, 11L);
            tstore.save();
        }
        return getSucReturnObject().toString();
    }

}
