package com.sifang.modules.order.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.order.entity.CrmOrder;
import com.sifang.modules.supply.order.constant.ApplyOrderConstants;
import com.sifang.modules.supply.stock.constant.MaterialConstants;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Repository
public class OrderDao extends BaseDao<CrmOrder> {
    private Logger logger = LoggerFactory.getLogger(OrderDao.class);

    public List<Record> getFactoryOrdersList(LayuiPage<Record> page, String factId, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.* FROM crm_order a WHERE a.site_id=? AND a.status != '7' ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());

        return Db.find(sf.toString(), factId);
    }

    public long getFactoryOrdersCount(String facId, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE a.site_id=? AND a.status != '7' ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), facId);
    }


    public String getOrderMap(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        //这个查询是为了小程序中的查询条件
        if (StringUtil.checkParamsValid(map.get("condition"))) {
            sf.append(" and (a.number like '%" + map.get("condition").trim() + "%'or a.customer_mobile like '%" + map.get("condition").trim() + "%')");
        }
        //这个条件是为了查询工程师下的工单
        if (StringUtil.checkParamsValid(map.get("employeId"))) {
            sf.append("and  a.employe_id like '%" + map.get("employeId").trim() + "%' ");
         //   sf.append(" and a.employe_id= '" + map.get("employeId").trim() + "'");
        }
        if (StringUtil.checkParamsValid(map.get("number"))) {
            sf.append(" and a.number like '%" + map.get("number").trim() + "%'");
        }
        //标记类型
        if (StringUtil.checkParamsValid(map.get("flag"))) {
            /*String flag = map.get("marks");
            Record flagIds = getmarksettingId(flag);
            String flagId = flagIds.getStr("id");*/
            sf.append(" and a.flag = '" + map.get("flag") + "'");
        }
        if (StringUtil.checkParamsValid(map.get("employe_id"))) {
            String employe_id = map.get("employe_id");
            sf.append(" and a.employe_id like '%" + employe_id + "%'");
        }
        //服务商名称
        if (StringUtil.checkParamsValid(map.get("siteList"))) {
            String siteList = map.get("siteList");
            sf.append(" and a.site_name = '" + siteList + "'");
        }
        //服务类型
        if (StringUtil.checkParamsValid(map.get("service_type"))) {
            sf.append(" and a.service_type = '" + map.get("service_type") + "'");
        }
        //保修类型
        if (StringUtil.checkParamsValid(map.get("warranty_type"))) {
            sf.append(" and a.warranty_type = '" + map.get("warranty_type") + "'");
        }
        if (StringUtil.checkParamsValid(map.get("orderStatus"))) {
            sf.append(" and a.site_status = '" + map.get("orderStatus") + "'");
        }
        //服务措施
        if (StringUtil.checkParamsValid(map.get("service_measures"))) {
            sf.append(" and a.service_measures ='" + map.get("service_measures") + "'");
        }
        //用户姓名
        if (StringUtil.checkParamsValid(map.get("customer_name"))) {
            sf.append(" and a.customer_name like '%" + map.get("customer_name").trim() + "%'");
        }
        //用户电话
        if (StringUtil.checkParamsValid(map.get("customer_mobile"))) {
            sf.append(" and (a.customer_mobile like '%" + map.get("customer_mobile").trim() + "%' or a.customer_telephone like '%" + map.get("customer_mobile") + "%' )");
        }
        //用户地址
        if (StringUtil.checkParamsValid(map.get("customer_address"))) {
            sf.append(" and concat_ws('',a.province,a.city,a.area,a.customer_address) like '%" + map.get("customer_address").trim() + "%'");
        }
        //设备类型
        if (StringUtil.checkParamsValid(map.get("appliance_category"))) {
            sf.append(" and a.appliance_category = '" + map.get("appliance_category") + "'");
        }
        if (StringUtil.checkParamsValid(map.get("appliance_brand"))) {
            sf.append(" and a.appliance_brand = '" + map.get("appliance_brand") + "'");
        }
        //设备型号
        if (StringUtil.checkParamsValid(map.get("appliance_model"))) {
            sf.append(" and a.appliance_model like '%" + map.get("appliance_model").trim() + "%'");
        }
        //电站名称
        if (StringUtil.checkParamsValid(map.get("power_station_name"))) {
            sf.append(" and a.power_station_name like '%" + map.get("power_station_name") + "%'");
        }
        // 设备序列号
        if (StringUtil.checkParamsValid(map.get("appliance_machine_code"))) {
            sf.append(" and a.appliance_machine_code like '%" + map.get("appliance_machine_code") + "%'");
        }
        //服务商名称
        if (StringUtil.checkParamsValid(map.get("siteId"))) {
            sf.append(" and a.site_id = '" + map.get("siteId") + "'");
        }

        //报单时间
        if (StringUtil.checkParamsValid(map.get("repairTimeSta"))) {
            String repair_time = map.get("repairTimeSta");
            sf.append(" and a.repair_time >= '" + repair_time + "'");
        }
        if (StringUtil.checkParamsValid(map.get("repairTimeEnd"))) {
            String repairEnd = map.get("repairTimeEnd");
            sf.append(" and a.repair_time <= '" + repairEnd + "'");
        }

        if(StringUtil.checkParamsValid(map.get("status"))){
            String[] status = map.get("status").split(",");
            if (status.length > 0) {
                sf.append(" and  a.status in (" + StringUtil.joinInSql(status) + ") ");
            }
        }
        //回访评价
        if(StringUtil.checkParamsValid(map.get("serviceAttitude"))){
            String[] serviceAttitude = map.get("serviceAttitude").split(",");
            if (serviceAttitude.length > 0) {
                sf.append(" and  c.service_attitude in (" + StringUtil.joinInSql(serviceAttitude) + ") ");
            }
        }

        //是否预约
        if (StringUtil.checkParamsValid(map.get("promise_flag"))) {
            sf.append(" and a.promise_flag = '" + map.get("promise_flag") + "'");
        }
        //故障描述
        if (StringUtil.checkParamsValid(map.get("remarks"))) {
            sf.append(" and a.remarks like '%" + map.get("remarks") + "%'");
        }
        //完工时间
        if (StringUtil.checkParamsValid(map.get("endTimeSta"))) {
            String endStartTime = map.get("endTimeSta");
            sf.append(" and a.end_time >= '" + endStartTime + "'");
        }
        if (StringUtil.checkParamsValid(map.get("endTimeEnd"))) {
            String endEndTime = map.get("endTimeEnd");
            sf.append(" and a.end_time <= '" + endEndTime + "'");
        }
        if (StringUtil.checkParamsValid(map.get("site_status"))) {
            sf.append(" and a.site_status = '" + map.get("site_status") + "'");
        }
        //预约日期
        if (StringUtil.checkParamsValid(map.get("promiseTime"))) {
            sf.append(" and a.promise_time = '" + map.get("promiseTime") + "'");
        }
        //预约时间是一个时间段
        if (StringUtil.checkParamsValid(map.get("promiseStratTime"))) {
            sf.append(" and a.promise_time >= '" + map.get("promiseStratTime") + "' ");
        }
        if (StringUtil.checkParamsValid(map.get("promiseEndTime"))) {
            sf.append(" and a.promise_time <= '" + map.get("promiseEndTime") + "' ");
        }
        //平台派工时间
        if (StringUtil.checkParamsValid(map.get("dispatchTimeSta"))) {
            sf.append(" and a.dispatch_time >= '" + map.get("dispatchTimeSta") + "' ");
        }
        if (StringUtil.checkParamsValid(map.get("dispatchTimeEnd"))) {
            sf.append(" and a.dispatch_time <= '" + map.get("dispatchTimeEnd") + "' ");
        }
        //派工时间
        if (StringUtil.checkParamsValid(map.get("site_dispatch_timeMin"))) {
            sf.append(" and a.site_dispatch_time >= '" + map.get("site_dispatch_timeMin") + " 00:00:00' ");
        }
        if (StringUtil.checkParamsValid(map.get("site_dispatch_timeMax"))) {
            sf.append(" and a.site_dispatch_time <= '" + map.get("site_dispatch_timeMax") + " 23:59:59' ");
        }
        //服务商接单时间
        if (StringUtil.checkParamsValid(map.get("processTimeSta"))) {
            String processTimeSta = map.get("processTimeSta");
            sf.append(" and a.process_time >= '" + processTimeSta + " 00:00:00' ");
        }
        if (StringUtil.checkParamsValid(map.get("processTimeEnd"))) {
            String processTimeEnd = map.get("processTimeEnd");
            sf.append(" and a.process_time <= '" + processTimeEnd + " 23:59:59' ");
        }
        //回访时间
        if (StringUtil.checkParamsValid(map.get("cellbackTimeSta"))) {
            String cellbackTimeSta = map.get("cellbackTimeSta");
            sf.append(" and c.create_time >= '" + cellbackTimeSta + " 00:00:00'");
        }
        if (StringUtil.checkParamsValid(map.get("cellbackTimeEnd"))) {
            String cellbackTimeEnd = map.get("cellbackTimeEnd");
            sf.append(" and c.create_time <= '" + cellbackTimeEnd + "  23:59:59'");
        }
        //结算时间
        if (StringUtil.checkParamsValid(map.get("settlementTimeSta"))) {
            String cellbackTimeSta = map.get("settlementTimeSta");
            sf.append(" and s.settlement_time >= '" + cellbackTimeSta + "  00:00:00'");
        }
        if (StringUtil.checkParamsValid(map.get("settlementTimeEnd"))) {
            String cellbackTimeEnd = map.get("settlementTimeEnd");
            sf.append(" and s.settlement_time <= '" + cellbackTimeEnd + "  23:59:59'");
        }

        //修改时间(无效时间)
        if (StringUtil.checkParamsValid(map.get("updateTimeSta"))) {
            String updateTimeSta = map.get("updateTimeSta");
            sf.append(" and a.update_time >= '" + updateTimeSta + "'");
        }
        if (StringUtil.checkParamsValid(map.get("updateTimeEnd"))) {
            String updateTimeEnd = map.get("updateTimeEnd");
            sf.append(" and a.update_time <= '" + updateTimeEnd + "'");
        }
        //上门时间
        if (StringUtil.checkParamsValid(map.get("drop_in_timeMin"))) {
            String drop_in_timeMin = map.get("drop_in_timeMin");
            sf.append(" and a.drop_in_time >= '" + drop_in_timeMin + "'");
        }
        if (StringUtil.checkParamsValid(map.get("drop_in_timeMax"))) {
            String drop_in_timeMax = map.get("drop_in_timeMax");
            sf.append(" and a.drop_in_time <= '" + drop_in_timeMax + "'");
        }

        //网点查询工单状态
        if (StringUtil.checkParamsValid(map.get("siteStatus"))) {
            String statusSplit = map.get("siteStatus");
            String[] status = map.get("siteStatus").split(",");
            sf.append(" and a.site_status in(");
            if (status.length > 0) {
                int count = 0;
                if (statusSplit.indexOf("1") >= 0) {//存在1
                    sf.append("'1'");
                    count++;
                }
                if (statusSplit.indexOf("3") >= 0) {
                    if (count == 0) {
                        sf.append("'3','8' ");
                    } else {
                        sf.append(",'3','8' ");
                    }
                    count++;
                }
                if (statusSplit.indexOf("4") >= 0) {
                    if (count == 0) {
                        sf.append("'6','4','5'");
                    } else {
                        sf.append(",'6','4','5'");
                    }
                    count++;
                }
                if (statusSplit.indexOf("9") >= 0) {
                    if (count == 0) {
                        sf.append("'9' ");
                    } else {
                        sf.append(",'9' ");
                    }
                    count++;
                }
                if (statusSplit.indexOf("7") >= 0) {
                    if (count == 0) {
                        sf.append("'7' ");
                    } else {
                        sf.append(",'7' ");
                    }
                    count++;
                }
                if (statusSplit.indexOf("8") >= 0) {
                    if (count == 0) {
                        sf.append("'7' ) and a.status = '5' ");
                    } else {
                        sf.append(",'7') and a.status = '5' ");
                    }
                    count++;
                } else {
                    sf.append(")");
                }
            }
        }

        return sf.toString();
    }

    /**
     * 根据标记名称取标记id
     *
     * @param name
     * @return
     */
    public Record getmarksettingId(String name) {
        String sql = "select id from crm_order_mark_settings where name=? and status='0' and role_type='0' and use_type='1'  ";
        return Db.findFirst(sql, name);
    }

    /**
     * 其他状态工单列表
     */
    public List<Record> getOrderLists(LayuiPage<Record> page, Map<String, String> map, String status) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.* FROM crm_order a WHERE 1=1 ");
        if ("5".equals(status)) {
            sf.append(" AND a.status in ('4','5')  ");
        } else {
            sf.append(" AND a.status = '" + status + "'  ");
        }
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }


    public long getOrderCount(Map<String, String> map, String status) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE 1=1 ");
        if ("5".equals(status)) {
            sf.append(" AND a.status in ('4','5')  ");
        } else {
            sf.append(" AND a.status = '" + status + "'  ");
        }
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString());
    }

    /**
     * 配件单
     */
    public List<Record> getPartsOrderList(LayuiPage<Record> page, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.* FROM crm_order a WHERE a.site_id=? AND a.status != '7' and a.fitting_flag != '0' ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getPartsOrderCount(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE   a.status != '7' and a.fitting_flag != '0'  ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString());
    }


    /**
     * 平台查看全部工单
     */
    public List<Record> getPlatformOrdersList(LayuiPage<Record> page, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type,c.create_time as callbackTime,s.settlement_time");
        sf.append(" ,IFNULL((SELECT e.name FROM `crm_platform_employee` e WHERE e.user_id=a.`create_by`),ifnull((select a.customer_name from crm_customer cus where cus.user_id=a.`create_by` ),'system'))AS createName");
        sf.append(" FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" LEFT JOIN `crm_order_callback` c on c.order_id=a.id  and c.result in('1','4') ");
        sf.append(" LEFT JOIN `crm_order_site_settlement` s on s.order_id=a.id  ");
        sf.append(" WHERE  a.status != '7' and a.special_installation ='1'");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getPlatformOrdersCount(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a ");
        sf.append(" LEFT JOIN `crm_order_callback` c on c.order_id=a.id  and c.result in('1','4') ");
        sf.append(" LEFT JOIN `crm_order_site_settlement` s on s.order_id=a.id  ");
        sf.append(" WHERE  a.status != '7' and a.special_installation ='1'");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString());
    }


    /***
     * 平台查看渠道商工单
     * @param map
     * @return
     */
    public List<Record> getChannelBusinessList(LayuiPage<Record> page, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type,IFNULL((SELECT e.name FROM `crm_platform_employee` e WHERE e.user_id=a.`create_by`),ifnull((select a.customer_name from crm_customer cus where cus.user_id=a.`create_by` ),'system'))AS createName ");
        sf.append(" FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.status != '7' and a.special_installation ='2'");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getChannelBusinessCount(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE  a.status != '7' and a.special_installation ='2'");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString());
    }

    /**
     * 平台查看待接单
     */
    public List<Record> getWaitingforOrdersList(LayuiPage<Record> page, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type FROM crm_order a  ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag`  AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.status != '7' and a.site_status ='1' and a.special_installation ='1'");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getWaitingforOrdersCount(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE  a.status != '7'  and a.site_status ='1' and a.special_installation ='1' ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString());
    }

    /**
     * 平台查看其他状态的工单
     * status :1、2、3、4、5
     */
    public List<Record> getPlatformOrdersStatus(LayuiPage<Record> page, Map<String, String> map, String status) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type ,c.create_time AS callbackTime");
        sf.append(" ,IFNULL((SELECT e.name FROM `crm_platform_employee` e WHERE e.user_id=a.`create_by`),ifnull((select a.customer_name from crm_customer cus where cus.user_id=a.`create_by` ),'system'))AS createName,s.settlement_time");
        sf.append(" FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag`  AND m.status='0' and m.use_type='1' ");
        sf.append("  LEFT JOIN `crm_order_callback` c ON c.order_id=a.id  and c.result in('1','4') ");
        sf.append("  LEFT JOIN `crm_order_site_settlement` s ON s.order_id=a.id");
        sf.append(" WHERE  a.status =?  and a.special_installation ='1' ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString(), status);
    }

    public long getPlatformOrdersStatusCount(Map<String, String> map, String status) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a ");
        sf.append(" LEFT JOIN `crm_order_callback` c ON c.order_id=a.id and c.result in('1','4')");
        sf.append("  LEFT JOIN `crm_order_site_settlement` s ON s.order_id=a.id");
        sf.append(" WHERE  a.status =?  and a.special_installation ='1'");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), status);
    }

    /**
     * 服务端查看无效工单
     */
    public List<Record> getWorkInvalidData(LayuiPage<Record> page, Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*  ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append("  ,m.name AS flag_name,m.type AS flag_type FROM crm_order a  ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag`  AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.site_status ='9'  and a.site_id=? ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString(), siteId);
    }

    public long getWorkInvalidDataCount(Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE  a.site_status ='9'  and a.site_id=? ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), siteId);
    }

    /**
     * 平台配件单工单
     */
    public List<Record> getPlatformFittingOrderList(LayuiPage<Record> page, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type FROM crm_order a  ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag`  AND m.status='0' and m.use_type='1' ");
        sf.append("  WHERE a.status != '7' and a.fitting_flag != '0' ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getPlatformFittingOrderCount(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE  a.status != '7' and a.fitting_flag != '0'  ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString());
    }

    /**
     * 平台投诉工单
     */
    public List<Record> getComplaintOrderList(LayuiPage<Record> page, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type FROM crm_order a  ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag`  AND m.status='0' and m.use_type='1' ");
        sf.append("  WHERE a.status != '7' and a.complaint_flag = '1' ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getComplaintOrderCount(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE  a.status != '7' and a.complaint_flag = '1'  ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString());
    }

    /**
     * 服务商查看全部工单
     */
    public List<Record> getSiteOrdersList(LayuiPage<Record> page, Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.* ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append(" ,m.name AS flag_name,m.type AS flag_type,  ");
        sf.append(" ifnull((SELECT e.name FROM `crm_platform_employee` e WHERE e.user_id=a.`create_by`),'system') AS createName FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.site_status not in(0,2) and a.site_id=? ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        logger.info("全部工单 sql： {} , siteId :{}",sf.toString(),siteId);
        return Db.find(sf.toString(), siteId);
    }

    public long getSiteOrdersCount(Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE  a.site_status not in(0,2)  and a.site_id=?  ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), siteId);
    }

    /**
     * 服务中工单
     * siteId
     */
    public List<Record> getInServiceOrderBySiteId(LayuiPage<Record> page, Map<String, String> map, String siteId, String type) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type  ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append("  FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE   a.site_id=? and a.status='2' ");
        if (StringUtil.isEmpty(type)) {
            sf.append(" and  a.site_status in(4,5,6) ");
        } else {
            sf.append(" and  a.site_status in(5,6)");

        }
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        logger.info("服务中 sql： {} , siteId :{}",sf.toString(),siteId);
        return Db.find(sf.toString(), siteId);
    }


    public long getInServiceOrderCount(Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE a.site_id=? AND a.site_status in(4,5,6) ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), siteId);
    }

    /**
     * 服务工程师查看待接单
     */
    public List<Record> getWaitingForOrders(LayuiPage<Record> page, Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type  ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append("  FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.site_status ='4' and a.status='2' and a.site_id=? ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());

        logger.info("待接单 sql： {} , siteId :{}",sf.toString(),siteId);
        return Db.find(sf.toString(), siteId);
    }

    /**
     * 待接收工单
     * siteId
     */
    public List<Record> getWaitReceiveData(LayuiPage<Record> page, Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type  ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append("  FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.site_status ='1' and a.status='2' and a.site_id=? ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString(), siteId);
    }

    public long getWaitReceiveCount(Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE a.site_id=? AND a.site_status='1' and a.status='2'");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), siteId);
    }

    /**
     * 待派工工单
     * siteId
     */
    public List<Record> getWaitWorkData(LayuiPage<Record> page, Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append("  FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.site_status ='3' and a.site_id=? ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString(), siteId);
    }

    public long getWaitWorkCount(Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE a.site_id=? and a.site_status='3' ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), siteId);
    }

    /**
     * 已完工工单
     * siteId
     */
    public List<Record> getWorkFinishData(LayuiPage<Record> page, Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append("  FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.site_status ='7' and a.status in ('3','4') and a.site_id=? ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString(), siteId);
    }

    public long getWorkFinishCount(Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE a.site_id=? and a.status in ('3','4')  AND a.site_status='7' ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), siteId);
    }

    /**
     * 已完成工单
     * siteId
     */
    public List<Record> getWorkSettlementData(LayuiPage<Record> page, Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type ");
        sf.append("  FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.site_status ='7' and a.status='5' and a.site_id=? ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString(), siteId);
    }

    public long getWorkSettlementCount(Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE a.site_id=? AND a.site_status='7' and a.status ='5' ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), siteId);
    }


    public List<Record> getEmpCompleted(LayuiPage<Record> page, Map<String, String> map, String siteId) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append("  FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag` AND m.status='0' and m.use_type='1' ");
        sf.append(" WHERE  a.site_status ='7' and a.status in('3','4','5') and a.site_id=? ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        System.out.println("已完工工单sql :"+sf.toString());
        return Db.find(sf.toString(), siteId);
    }


    /**
     * 服务商配件单
     */
    public List<Record> getSitePartsOrderList(LayuiPage<Record> page, String siteId, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,a.status as orderstatus ");
        sf.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sf.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sf.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sf.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sf.append("  FROM crm_order a WHERE a.site_id=? AND a.status <6 and a.fitting_flag != '0' ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString(), siteId);
    }

    public long getSitePartsOrderCount(String siteId, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a WHERE  a.site_id=? AND a.status <6 and a.fitting_flag != '0'  ");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString(), siteId);
    }

    /**
     * 首页统计工单数量
     */
    public Record getPageOrderCount(String factor, String siteId, String employeId) {
        String sta;
        User user = UserUtils.getUser();
        if (user != null && UserConstants.TYPE_ADMIN.equals(user.getUserType())) {
             sta = "('1','4','5','6')";
        } else {
             sta = "('4','5','6')";
        }

        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT ");
        sf.append(" COUNT(a.id) AS newCount, ");
        sf.append(" COUNT(CASE WHEN a.site_status ='1' AND STATUS='2' THEN 1 END) AS siteDjsCount, ");
        sf.append(" COUNT(CASE WHEN a.site_status IN "+sta+" AND STATUS ='2' THEN 1 END) AS fuzCount, ");
        sf.append(" COUNT(CASE WHEN a.site_status IN ('5','6') AND STATUS='2' THEN 1 END) AS empfuzCount, ");
        sf.append(" COUNT(CASE WHEN a.status IN ('5','4','3') AND a.site_status='7' THEN 1 END) AS empywgCount,   ");
        sf.append(" COUNT(CASE WHEN a.site_status ='4' AND STATUS='2' THEN 1 END) AS djdCount, ");
        sf.append(" COUNT(CASE WHEN a.status = '5' THEN 1 END) AS ywcCount ");
        sf.append(" FROM crm_order a WHERE  a.status != '7' and a.special_installation!='2' ");
        //查询工程师下的工单数量
        if (StringUtil.isNotBlank(employeId)) {
            sf.append("and  a.employe_id like '%" + employeId + "%' ");
        }
        if (StringUtil.isNotBlank(siteId)) {
            sf.append(" and  a.site_id ='" + siteId + "'  ");
        }
        if ("week".equals(factor)) {//周
            sf.append(" and  YEARWEEK(date_format(a.create_time,'%Y-%m-%d'),1) = YEARWEEK(now(),1) ");
        } else if ("month".equals(factor)) {//月
            sf.append(" and  DATE_FORMAT(a.create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ");
        } else {
            sf.append(" and  TO_DAYS(a.create_time) = TO_DAYS(NOW()) ");
        }

        logger.info("首页统计 sql： {} ",sf.toString());
        return Db.findFirst(sf.toString());
    }

    public List<Record> getSiteOrderList(String ids, String siteId) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT a.*  ");
        sql.append("  ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') createTime ,DATE_FORMAT(a.repair_time,'%Y-%m-%d %H:%i:%s') repairTime ");
        sql.append("  ,DATE_FORMAT(a.appliance_buy_time,'%Y-%m-%d') appliance_buyTime ,DATE_FORMAT(a.site_dispatch_time,'%Y-%m-%d %H:%i:%s') site_dispatchTime ");
        sql.append("  ,DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%i:%s') endTime ,DATE_FORMAT(a.site_process_time,'%Y-%m-%d %H:%i:%s') site_processTime ");
        sql.append("  ,DATE_FORMAT(a.latest_process_time,'%Y-%m-%d %H:%i:%s') latest_processTime ,DATE_FORMAT(a.promise_time,'%Y-%m-%d') promiseTime ");
        sql.append(" FROM crm_order a WHERE a.site_id=? ");
        if (StringUtil.checkParamsValid(ids)) {
            sql.append(" and  a.id in (" + ids + ") ");
        }

        return Db.find(sql.toString(), siteId);
    }

    public void updateFittingFlag(String orderId,String siteId) {
        List<Record> rds = Db.find("SELECT id,status FROM crm_site_fitting_apply WHERE order_id=? AND site_id=? AND STATUS in ('1','2') ", orderId, siteId);
        CrmOrder order = get(orderId);
        if (rds.size() > 0) {
            for (Record rd : rds) {
                String status = rd.get("status");
                if (MaterialConstants.APPLY_STATUS_ONE.equals(status)) {
                    order.setFittingFlag(ApplyOrderConstants.FITTING_FLAG_WAIT);
                    break;
                } else if (MaterialConstants.APPLY_STATUS_TWO.equals(status)) {
                    order.setFittingFlag(ApplyOrderConstants.FITTING_FLAG_WAY);
                    break;
                } else {
                    order.setFittingFlag(ApplyOrderConstants.FITTING_FLAG_NO);
                }
            }
        } else {
            order.setFittingFlag(ApplyOrderConstants.FITTING_FLAG_NO);
        }
        save(order);
    }

    /**
     * 平台查看其他状态的工单
     * status :1、2、3、4、5
     */
    public List<Record> getManyTimesPlatformOrders(LayuiPage<Record> page, Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.*,m.name AS flag_name,m.type AS flag_type ,c.create_time AS callbackTime");
        sf.append(" ,IFNULL((SELECT e.name FROM `crm_platform_employee` e WHERE e.user_id=a.`create_by`),ifnull((select a.customer_name from crm_customer cus where cus.user_id=a.`create_by` ),'system'))AS createName,s.settlement_time");
        sf.append(" FROM crm_order a ");
        sf.append(" LEFT JOIN crm_order_mark_settings m ON m.`id`=a.`flag`  AND m.status='0' and m.use_type='1' ");
        sf.append("  LEFT JOIN `crm_order_callback` c ON c.order_id=a.id  ");
        sf.append("  LEFT JOIN `crm_order_site_settlement` s ON s.order_id=a.id");
        sf.append(" WHERE  a.status ='3' and c.result='5' and a.special_installation ='1' ");
        sf.append(getOrderMap(map));
        sf.append(" ORDER BY a.create_time DESC  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public long getgetManyTimesPlatformOrdersCount(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT count(a.id) FROM crm_order a ");
        sf.append(" LEFT JOIN `crm_order_callback` c ON c.order_id=a.id and c.result='5'");
        sf.append("  LEFT JOIN `crm_order_site_settlement` s ON s.order_id=a.id");
        sf.append(" WHERE  a.status ='3' and c.result='5'  and a.special_installation ='1'");
        sf.append(getOrderMap(map));
        return Db.queryLong(sf.toString());
    }
}
