package com.cnd3b.restcontroller.singleagent.webclient.products;

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 p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

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

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

    /**
     * 商品列表查询
     *
     * @return
     */
    public String queryList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.fonsaledate desc"};
        String sort = getSort(sortfield, "t1.fonsaledate 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.fprodname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fprodnum like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fisonsale") && !"".equals(whereObject.getString("fisonsale"))) {
                where.append(" and(");
                where.append("t1.fisonsale =").append(whereObject.getString("fisonsale")).append(" ");
                where.append(")");
            }
        }
        SQLFactory productSQL = new SQLFactory(this, "商品列表查询", pageSize, pageNumber, sort);
        productSQL.addParameter("siteid", siteid);
        productSQL.addParameter("tagentsid", tagentsid);
        productSQL.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(productSQL.getSQL());

        rows = addAttachmentUrl(rows, "tagents_product", "default", "docinfos");
        return getSucReturnObject().setDataByPaging(rows, sortfield).saveToDataPool().preloading(1).toString();
    }

    /**
     * 主界面查询
     *
     * @return
     */
    public String queryMain() {
        long tagents_productid = content.getLongValue("tagents_productid");
        SQLFactory sqlFactory = new SQLFactory(this, "商品主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        sqlFactory.addParameter("tagents_productid", tagents_productid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 商品录入
     *
     * @return
     */
    public String insertOrModify() throws D3bException, P2Exception {
        long tagents_productid = content.getLongValue("tagents_productid");// 新建时为0
        String fprodname = content.getString("fprodname", "tagents_product.fprodname", "产品名称");
        String fprodnum = content.getString("fprodnum", "tagents_product.fprodnum", "产品编码");
        double fprice = content.getDoubleValue("fprice");
        String fnotes = content.getString("fnotes", "tagents_product.fnotes", "备注");
        boolean fisonsale = content.getBoolean("fisonsale");

        PaoSetRemote tagents_productSet = getP2ServerSystemPaoSet("tagents_product", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tagents_productid='" + tagents_productid + "'");
        PaoRemote tagents_product = null;
        if (tagents_productid == 0 || tagents_productSet.isEmpty()) {
            tagents_product = tagents_productSet.addAtEnd();
            tagents_product.setValue("siteid", siteid, 11L);
            tagents_product.setValue("tagentsid", tagentsid, 11L);
            tagents_product.setValue("createby", username, 11L);
            tagents_product.setValue("createdate", sysdate, 11L);
        } else {
            tagents_product = tagents_productSet.getPao(0);
        }
        tagents_product.setValue("fprodname", fprodname, 11L);
        tagents_product.setValue("fprodnum", fprodnum, 11L);
        tagents_product.setValue("fprice", fprice, 11L);
        tagents_product.setValue("fnotes", fnotes, 11L);

        if (fisonsale && !tagents_product.getBoolean("fisonsale")) {
            tagents_product.setValue("fisonsale", true, 11L);
            tagents_product.setValue("fonsaleby", username, 11L);
            tagents_product.setValue("fonsaledate", sysdate, 11L);
            tagents_product.setValue("foffsaleby", "", 11L);
            tagents_product.setValue("foffsaledate", "", 11L);
        }
        if (!fisonsale && tagents_product.getBoolean("fisonsale")) {
            tagents_product.setValue("fisonsale", false, 11L);
            tagents_product.setValue("fonsaleby", "", 11L);
            tagents_product.setValue("fonsaledate", "", 11L);
            tagents_product.setValue("foffsaleby", username, 11L);
            tagents_product.setValue("foffsaledate", sysdate, 11L);
        }

        tagents_product.setValue("changeby", username, 11L);
        tagents_product.setValue("changedate", sysdate, 11L);
        content.put("tagents_productid", tagents_product.getUniqueIDValue());
        tagents_productSet.save();

        return queryMain();
    }

    /**
     * 批量上架
     *
     * @return
     */
    public String batch_updatesalestate() {
        JSONArray tagents_productids = content.getJSONArray("tagents_productids");
        boolean fisonsale = content.getBoolean("fisonsale");
        Iterator it = tagents_productids.iterator();
        ArrayList<String> sqllist = new ArrayList<>();
        while (it.hasNext()) {
            int tagents_productid = (int) it.next();
            SQLFactory sqlFactory = null;
            if (fisonsale) {
                sqlFactory = new SQLFactory(this, "商品批量上架");
            } else {
                sqlFactory = new SQLFactory(this, "商品批量下架");
            }
            sqlFactory.addParameter("username", username);
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("tagentsid", tagentsid);
            sqlFactory.addParameter("tagents_productid", tagents_productid);
            sqllist.add(sqlFactory.getSQL());
        }
        String status = dbConnect.runSqlUpdate(sqllist);
        if ("true".equals(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg("失败").toString();
        }
    }

    /**
     * 删除?
     *
     * @return
     */
    public String delete() {
        String tagents_productid = content.getString("tagents_productid");
        Rows rows = dbConnect.runSqlQuery("select *from tagents_product where siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tagents_productid='" + tagents_productid + "'");
        if (rows.isEmpty()) {
            return getErrReturnObject().setErrMsg("当前商品不存在").toString();
        }
        if (rows.get(0).getBoolean("fisonsale")) {
            return getErrReturnObject().setErrMsg("已上架的商品不可删除").toString();
        }
        Rows usedcheck = dbConnect.runSqlQuery("select * from tcustomer_order where siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tagents_productid='" + tagents_productid + "'");
        if (!usedcheck.isEmpty()) {
            return getErrReturnObject().setErrMsg("该商品存在销售订单，不可删除").toString();
        }

        String status = dbConnect.runSqlUpdate("delete from tagents_product where siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tagents_productid='" + tagents_productid + "'");
        if ("true".equals(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg("删除失败").toString();
        }
    }


}
