package com.nmgs.service.impl;

import com.nmgs.mapper.Mapper;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PubTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SerivceAreaOperationMealServiceImpl {
    @Autowired
    public Mapper mapperI;
    public static String sql="SELECT\n" +
            "\tsaot.JOBNUMBER,\n" +
            "\tsaot.PHONE,\n" +
            "\tsaom.ID,\n" +
            "\tsaom.MEALNAME,\n" +
            "\tsaom.MEALINFO,\n" +
            "\tsaom.MEALPIC,\n" +
            "\t(SELECT IPS.BAK1 FROM INTEGRAL_PUBLIC_SET ips WHERE IPS.PARAMCODE='PICURL') AS PICURL,\n" +
            "\tsaom.MEALPRICE \n" +
            "FROM\n" +
            "\tservice_area_operation_meal_table saom,\n" +
            "\tservice_area_operators_table saot,\n" +
            "\tservice_area_table sat \n" +
            "WHERE\n" +
            "\tsaom.JOBNUMBER = saot.JOBNUMBER \n" +
            "\tAND saot.SERVICEAREAID = sat.ID ";


    public List getAreaMealList( String jobNumber) {
        List list=null;
        String sqlTemp=sql;
        List retList=new ArrayList();
        try{
            if(!PubTools.isNull(jobNumber)){
                sqlTemp += " and saot.JOBNUMBER = '"+jobNumber+"'";
            }
            sqlTemp +=  " order by saom.MEALNAME";
            list= this.mapperI.selectforlist(sqlTemp);
            if(list != null && list.size()>0){
                for(Object obj:list){
                    Map<String, Object> obj1 = (Map<String, Object>) obj;
                    Object o=obj1.get("MEALPIC");
                    if(!PubTools.isEmpty(o)){
                        String s = PubTools.ClobToString( o);
                        if(!PubTools.isNull(s)){
                            obj1.remove("MEALPIC");
                            obj1.put("MEALPIC",s);
                        }else{
                            obj1.remove("MEALPIC");
                            obj1.put("MEALPIC","");
                        }
                    }else{
                        obj1.put("MEALPIC","");
                    }
                    retList.add(obj1);
                }
            }else{
                retList=list;
            }
        }catch (Exception e){
            LogUtil.WriteLog_Error("查询服务区套餐列表失败：===>"+e.getMessage(),"ServiceAreaServiceImpl");
            return retList=null;
        }
        return retList;
    }

    public List getMealInfoById( String mealIds) {
        List list=null;
        String sqlTemp=sql;
        List retList=new ArrayList();
        try{
            if(!PubTools.isNull(mealIds)){
                sqlTemp += " and saom.ID in( "+mealIds+")";
            }
            list= this.mapperI.selectforlist(sqlTemp);
            if(list != null && list.size()>0){
                for(Object obj:list){
                    Map<String, Object> obj1 = (Map<String, Object>) obj;
                    Object o=obj1.get("MEALPIC");
                    if(!PubTools.isEmpty(o)){
                        String s = PubTools.ClobToString( o);
                        if(!PubTools.isNull(s)){
                            obj1.remove("MEALPIC");
                            obj1.put("MEALPIC",s);
                        }else{
                            obj1.remove("MEALPIC");
                            obj1.put("MEALPIC","");
                        }
                    }else{
                        obj1.put("MEALPIC","");
                    }
                    retList.add(obj1);
                }
            }else{
                retList=list;
            }
        }catch (Exception e){
            LogUtil.WriteLog_Error("根据套餐ID查询套餐列表失败：===>"+e.getMessage(),"ServiceAreaServiceImpl");
            return retList=null;
        }
        return retList;
    }
}
