package com.yc.wms.service.impl;

import com.yc.wms.bean.Rukuzhengli;
import com.yc.wms.until.QueryPageUtil;
import com.yc.wms.until.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RukuzhengliImpl implements com.yc.wms.service.Rukuzhengli {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    public Result readJson(QueryPageUtil query, Rukuzhengli rukuzhengli) {


        int pageNum = query.getPageNum();//当前页
        int pageSize = query.getPageSize();//条数

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        String sql_bz = "SELECT ROW_NUMBER() OVER(ORDER BY ad.id) AS Row_Num,ad.id,ad.sourceId,ad.strBill7,\n" +
                " ad.strBill8,i.code,i.name,i.unit,\n" +
                " ad.strBill11,ad.PROP_C1,ad.PROP_C2,\n" +
                "ad.PROP_C3,ad.PROP_C5,ad.PROP_C8,ad.PROP_C9,\n" +
                "ad.EXPECTED_QUANTITY,ad.RECEIVED_QUANTITY,\n" +
                "a.ORDER_DATE,ad.strBill2,ad.RECEIVE_DATE,\n" +
                "a.USER_NAME,ad.DELIVERY_DATE,ad.RELATED_BILL5,\n" +
                "ad.description,ad.BE_QUALITY,a.CODE a_CODE,\n" +
                "ad.BE_INPUT\n" +
                " from ASN_DETAIL ad \n" +
                " left join asn a on a.id=ad.ASN_ID\n" +
                " left join ITEM i on i.id=ad.ITEM_ID\n" +
                " where 1=1";


        //查询条件
        //采购订单
        if(rukuzhengli.sourceId!=null){
            if(!rukuzhengli.sourceId.trim().equals("")){
                String[] str=rukuzhengli.sourceId.trim().split("\n");
                StringBuffer sb = new StringBuffer();
                for (int i=0;i<str.length;i++){

                    if(str.length-1==i){
                        sb.append("'"+ str[i]+"'");
                    }else {
                        sb.append("'"+ str[i]+"'"+",");
                    }
                }
                sql_bz+=" and ad.sourceId in ("+sb.toString()+")";
            }

        }

        //收货凭证
        if(rukuzhengli.strBill7!=null){
            if(!rukuzhengli.strBill7.trim().equals("")){
                String[] str=rukuzhengli.strBill7.trim().split("\n");
                StringBuffer sb = new StringBuffer();
                for (int i=0;i<str.length;i++){

                    if(str.length-1==i){
                        sb.append("'"+ str[i]+"'");
                    }else {
                        sb.append("'"+ str[i]+"'"+",");
                    }
                }
                sql_bz+=" and ad.strBill7 in ("+sb.toString()+")";
            }

        }

        //料号
        if(rukuzhengli.code!=null){
            if(!rukuzhengli.code.trim().equals("")){
                String[] str=rukuzhengli.code.trim().split("\n");
                StringBuffer sb = new StringBuffer();
                for (int i=0;i<str.length;i++){

                    if(str.length-1==i){
                        sb.append("'"+ str[i]+"'");
                    }else {
                        sb.append("'"+ str[i]+"'"+",");
                    }
                }
                sql_bz+=" and i.code in ("+sb.toString()+")";
            }

        }

        //批次号
        if(rukuzhengli.PROP_C1!=null){
            if(!rukuzhengli.PROP_C1.trim().equals("")){
                String[] str=rukuzhengli.PROP_C1.trim().split("\n");
                StringBuffer sb = new StringBuffer();
                for (int i=0;i<str.length;i++){

                    if(str.length-1==i){
                        sb.append("'"+ str[i]+"'");
                    }else {
                        sb.append("'"+ str[i]+"'"+",");
                    }
                }
                sql_bz+=" and ad.PROP_C1 in ("+sb.toString()+")";
            }

        }

        //收货日期
        if(rukuzhengli.ORDER_DATE!=null&& rukuzhengli.ORDER_DATE.length>0){
            String startDate = simpleDateFormat.format(rukuzhengli.ORDER_DATE[0]);
            String endDate = simpleDateFormat.format(rukuzhengli.ORDER_DATE[1]);
            sql_bz+=" and a.ORDER_DATE between "+"'"+startDate+"'"+" and "+ "'"+endDate+"'";
        }

        //整理日期
        if(rukuzhengli.RECEIVE_DATE!=null&& rukuzhengli.RECEIVE_DATE.length>0){
            String startDate = simpleDateFormat.format(rukuzhengli.RECEIVE_DATE[0]);
            String endDate = simpleDateFormat.format(rukuzhengli.RECEIVE_DATE[1]);
            sql_bz+=" and ad.RECEIVE_DATE between "+"'"+startDate+"'"+" and "+ "'"+endDate+"'";

        }
        //特殊标记
        if(rukuzhengli.PROP_C3!=null){
            if(!rukuzhengli.PROP_C3.trim().equals("")){
                sql_bz+=" and ad.PROP_C3 ="+"'"+ rukuzhengli.PROP_C3.trim()+"'";
            }

        }

        //工厂
        if(rukuzhengli.PROP_C9!=null&& rukuzhengli.PROP_C9.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< rukuzhengli.PROP_C9.length; i++){
                if(rukuzhengli.PROP_C9.length-1==i){
                    sb.append("'"+ rukuzhengli.PROP_C9[i]+"'");
                }else {
                    sb.append("'"+ rukuzhengli.PROP_C9[i]+"'"+",");
                }
            }
            sql_bz+=" and ad.PROP_C9 in ("+sb.toString()+")";
        }
        //LOC
        if(rukuzhengli.PROP_C2!=null&& rukuzhengli.PROP_C2.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< rukuzhengli.PROP_C2.length; i++){
                if(rukuzhengli.PROP_C2.length-1==i){
                    sb.append("'"+ rukuzhengli.PROP_C2[i]+"'");
                }else {
                    sb.append("'"+ rukuzhengli.PROP_C2[i]+"'"+",");
                }
            }
            sql_bz+=" and ad.PROP_C2 in ("+sb.toString()+")";;
        }

        //SAP接口
        if(rukuzhengli.CODE!=null){
            if(!rukuzhengli.CODE.trim().equals("")){
                sql_bz+=" and a.CODE ="+"'"+ rukuzhengli.CODE.trim()+"'";
            }
        }

        //加急标记
        if(rukuzhengli.strBill11!=null){
            if(rukuzhengli.strBill11.trim().equals("")){
                sql_bz+=" and ad.strBill11 ="+"'"+ rukuzhengli.strBill11.trim()+"'";
            }
        }

        String sqlCount = "select count(t.id) as nums from(" + sql_bz+") t";
        String sqlFy = "select t.* from(" + sql_bz+") t";


        sqlFy+= " WHERE Row_Num BETWEEN " + (pageNum-1) * pageSize + " AND " + (pageNum) * pageSize;
        //查询单数据
        Map<String,Object> listCount = jdbcTemplate.queryForMap(sqlCount, new HashMap<>());
        //查询多条数据据
        List<Map<String,Object>> maps = jdbcTemplate.queryForList(sqlFy, new HashMap<>());

        return  Result.success(Long.parseLong(listCount.get("nums").toString()),maps);

    }

    @Override
    public Result Plant(){
        String sql=" select DISTINCT PROP_C9 from ASN_DETAIL where PROP_C9!=''";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new HashMap<>());

        return Result.success(list);
    }

    @Override
    public Result LOC() {
        String sql="  \n" +
                " select DISTINCT PROP_C2 from ASN_DETAIL where PROP_C2!=''";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new HashMap<>());

        return Result.success(list);
    }
}
