package com.sifang.modules.order.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.order.entity.OrderCallback;
import com.sifang.modules.order.utils.GPSUtil;
import com.sifang.modules.order.utils.GetDistance;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import org.springframework.stereotype.Repository;


@Repository
public class OrdeCallbackDao extends BaseDao<OrderCallback> {

    /**
     * 获取用户距离
    */
    public double getCusaddress(String lnglat,String siteId){
        try {
        Record addre = Db.findFirst(" SELECT * FROM crm_site_order_default_address WHERE site_id=? ",siteId);
        if(addre == null){
            return 0.00;
        }
        String sitelnglat = addre.getStr("lnglat");
        double  fi =  GetDistance.getDistance(sitelnglat,lnglat);
        return fi;
        }catch (Exception e){
            e.printStackTrace();
            return 0.00;
        }

    }

    public OrderCallback getOrderCallback(String orderId){
        return getByHql("from OrderCallback a where a.orderId =:p1 ",new Parameter(orderId));
    }

    public long getOrderFittingCount(String orderId){
        return Db.queryLong(" SELECT COUNT(*) FROM crm_platform_fitting_apply WHERE order_id=? AND old_fitting_flag='1' AND return_fitting='1' ",orderId);
    }

public OrderCallback getCallback(String orderId){
    OrderCallback orderCallback=getOrderCallback(orderId);
        String serviceAttitude=orderCallback.getServiceAttitude();
        if(StringUtil.isNotBlank(serviceAttitude)){
           if(serviceAttitude.equals("5")){
               orderCallback.setServiceAttitude("十分满意");
           }else if(serviceAttitude.equals("4")){
               orderCallback.setServiceAttitude("满意");
           }else if(serviceAttitude.equals("3")){
               orderCallback.setServiceAttitude("一般");
           }else if(serviceAttitude.equals("2")){
               orderCallback.setServiceAttitude("不满意");
           }else if(serviceAttitude.equals("1")){
               orderCallback.setServiceAttitude("十分不满意");
           }else if(serviceAttitude.equals("6")){
               orderCallback.setServiceAttitude("回访无效");
           }else if(serviceAttitude.equals("7")){
               orderCallback.setServiceAttitude("回访未成功");
           }
        }
        String visitMany=orderCallback.getVisitMany();
        if(StringUtil.isNotBlank(visitMany)){
          if(visitMany.equals("0")){
              orderCallback.setVisitMany("否");
          }else if(visitMany.equals("1")){
                orderCallback.setVisitMany("是");
            }
        }
        String result=orderCallback.getResult();
        if(StringUtil.isNotBlank(result)){
            if(result.equals("1")){
                orderCallback.setResult("已回访");
            } if(result.equals("2")){
                orderCallback.setResult("仍需上门");
            }else if(result.equals("3")){
                orderCallback.setResult("无人接听");
            }else if(result.equals("4")){
                orderCallback.setResult("回访不通过");
            }
        }
       return orderCallback;
}


}
