package com.sifang.modules.platform.utils;

import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.RandomUtil;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.entity.User;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PlantformUtils {


    /**
     * 获取所有服务商
     * @return
     */
    public static List<Record> getAllSiteList(){
        return Db.find("select * from crm_site a where a.status='0' order by a.create_time asc ");
    }
    /***
     * 获取所以服务商 及默认的计算地址
     */
    public static List<Record> getAllSiteDefaultAddress(){
        return Db.find(" SELECT a.id,a.name,d.province,d.city,d.area,d.address,d.lnglat FROM crm_site a LEFT JOIN `crm_site_order_default_address` d ON d.site_id= a.id WHERE a.status='0' ORDER BY a.create_time ASC");
    }

    /***
     * 获取所以服务商 及默认的计算地址
     */
    public static List<Record> getSiteDefaultAddressList(String province,String city){
        return Db.find(" SELECT a.id,a.name,d.province,d.city,d.area,d.address,d.lnglat FROM crm_site a LEFT JOIN `crm_site_order_default_address` d ON d.site_id= a.id WHERE a.status='0' and a.province=? and a.city=? ORDER BY a.create_time ASC",province,city);
    }

    /***
     * 获取根据名称搜索 服务商
     */
    public static List<Record> getSiteBySiteNameList(String siteName){
        return Db.find(" SELECT a.id,a.name,d.province,d.city,d.area,d.address,d.lnglat FROM crm_site a LEFT JOIN `crm_site_order_default_address` d ON d.site_id= a.id WHERE a.status='0' and a.name like '%"+siteName+"%'  ORDER BY a.create_time ASC");
    }


    /**
     * 生成随机数
     * @return
     */
    public static synchronized String getSiteNumber() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date()) + RandomUtil.getPositiveRandomWithRang(4);
    }

    /**
     * 生成随机数--工单编号
     * @return
     */
    public static synchronized String getOrderNumber() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
        return sdf.format(new Date()) + RandomUtil.getPositiveRandomWithRang(4);
    }

    /**
     * 根据指定规则生成单号
    */
    public static synchronized String findOrderNumber(String start){
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
        return start+sdf.format(new Date()) + RandomUtil.getPositiveRandomWithRang(4);
    }

    /**
     * 生成合同编号
     *
     * @return
     */
    public static synchronized String getContractNumber(String profix, int index) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyHHmmss");
        StringBuilder s = new StringBuilder(String.valueOf(index));
        while (s.toString().length() < 4) {
            s.insert(0, "0");
        }
        return profix + sdf.format(new Date()) + s.toString();
    }


    /**
     * 根据ID查询修改地址选中的数据
     * @return
     */
    public static Record getSelectAddress(String id){
        String sql = "select * from crm_site_receiving_info a where a.id=? ";
        return Db.findFirst(sql,id);
    }


    public static List<String> getPromiseLimit(){
        List<String> list = Lists.newArrayList();
        list.add("6:00-8:00");
        list.add("8:00-10:00");
        list.add("10:00-12:00");
        list.add("12:00-14:00");
        list.add("14:00-16:00");
        list.add("16:00-18:00");
        list.add("18:00-20:00");
        list.add("20:00-22:00");
        return list;
    }

    //根据工单查询对应的备件信息
    public static List<Record> getFittingApply(String orderId){
        String sql  = "SELECT c.status FROM crm_platform_fitting_apply c WHERE c.order_id = ? AND c.`status` IN(1,2) ";
        return Db.find(sql,orderId);
    }

    //获取所有的服务措施
    public static List<Record> getAllMeasutes(){
        String sql=" SELECT DISTINCT service_measures  FROM `crm_order_service_measures` ";
        return Db.find(sql.toString());
    }
}
