package com.yc.wms.service.impl;

import com.yc.wms.service.Zhengliqingdan;
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 ZhengliqingdanImpl implements Zhengliqingdan {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;


    @Override
    public Result QueryJson(QueryPageUtil query, com.yc.wms.bean.Zhengliqingdan zhengliqingdan) {

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

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

        String sql ="select Row_number() OVER(ORDER BY rd.id) as Row_Num,rd.id,ad.sourceId ,ad.strBill7,ad.strBill8,i.CODE i_CODE,i.NAME,i.UNIT,a.ORDER_DATE,rd.RECEIVE_DATE ,\n" +
                "rd.OPERATOR,w.NAME w_NAME,s.LP,ik.PROP_C1,ik.PROP_C2,\n" +
                "ik.PROP_C3,ik.PROP_C5,ik.PROP_C8,ik.PROP_C9,\n" +
                "rd.BAR_CODE,rd.BE_MOVED,ad.description,a.CODE a_CODE,lc.CODE lc_CODE\n" +
                "\tFROM RECEIVED_RECORD rd \n" +
                "left join ASN_DETAIL ad on ad.id=rd.ASN_DETAIL_ID\n" +
                "left join asn a on a.ID=ad.ASN_ID\n" +
                "left join ITEM_KEY ik on ik.id=rd.ITEM_KEY_ID\n" +
                "left join ITEM i  on i.id=ad.ITEM_ID\n" +
                "left join stock s on s.id=rd.SRC_LP\n" +
                "left join WORKER w on w.id=rd.WORKER_ID\n" +
                "left join [ZONE] z on z.id=rd.WAREHOUSE_AREA_ID\n" +
                "left join LOCATION lc on lc.id=rd.LOCATION_ID"+
                " where 1=1";

        //查询条件
        //采购单号
        if(zhengliqingdan.sourceId!=null){
            if(!zhengliqingdan.sourceId.trim().equals("")){
                sql+=" and ad.sourceId="+"'"+zhengliqingdan.sourceId.trim()+"'";
            }
        }
        //收货凭证
        if(zhengliqingdan.strBill7!=null){
            if(!zhengliqingdan.strBill7.trim().equals("")){
                String[] str=zhengliqingdan.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+=" and ad.strBill7 in ("+sb.toString()+")";
            }

        }

        //料号
        if(zhengliqingdan.i_CODE!=null){
            if(!zhengliqingdan.i_CODE.trim().equals("")){
                String[] str=zhengliqingdan.i_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+=" and i.CODE in ("+sb.toString()+")";
            }
        }

        //物料描述
        if(zhengliqingdan.i_name!=null){
            if(!zhengliqingdan.i_name.trim().equals("")){
                sql+=" and i.NAME like "+"'%"+zhengliqingdan.i_name.trim()+"%'";
            }
        }

        //库区
        if(zhengliqingdan.z_name!=null&& zhengliqingdan.z_name.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< zhengliqingdan.z_name.length; i++){
                if(zhengliqingdan.z_name.length-1==i){
                    sb.append("'"+ zhengliqingdan.z_name[i]+"'");
                }else {
                    sb.append("'"+ zhengliqingdan.z_name[i]+"'"+",");
                }
            }
            sql+=" and z.name in ("+sb.toString()+")";
        }

        //库位号
        if(zhengliqingdan.lc_CODE!=null){
            if(!zhengliqingdan.lc_CODE.trim().equals("")){
                String[] str=zhengliqingdan.lc_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+=" and lc.CODE in ("+sb.toString()+")";
            }
        }

        //批次号
        if(zhengliqingdan.PROP_C1!=null){
            if(!zhengliqingdan.PROP_C1.trim().equals("")){
                sql+=" and ik.PROP_C1="+"'"+zhengliqingdan.PROP_C1.trim()+"'";
            }

        }

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

        //特殊标记
        if(zhengliqingdan.PROP_C3!=null){
            if(!zhengliqingdan.PROP_C3.trim().equals("")){
                sql+=" and ik.PROP_C3="+"'"+zhengliqingdan.PROP_C3.trim()+"'";
            }
        }

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

        //整理人
        if(zhengliqingdan.OPERATOR!=null){
            if(!zhengliqingdan.OPERATOR.trim().equals("")){
                String[] str=zhengliqingdan.OPERATOR.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+=" and rd.OPERATOR in ("+sb.toString()+")";
            }
        }

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

        //整理日期
        if(zhengliqingdan.RECEIVE_DATE!=null&&zhengliqingdan.RECEIVE_DATE.length>0){
            System.out.println("fghjk");
            String startDate = simpleDateFormat.format(zhengliqingdan.RECEIVE_DATE[0]);
            String endDate = simpleDateFormat.format(zhengliqingdan.RECEIVE_DATE[1]);
            sql+=" and rd.RECEIVE_DATE between "+"'"+startDate+"'"+" and "+ "'"+endDate+"'";
        }

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

        //备注
        if(zhengliqingdan.description!=null){
            if(!zhengliqingdan.description.trim().equals("")){
                sql+=" and ad.description like"+"'%"+zhengliqingdan.description.trim()+"%'";
            }
        }

        //容器码
        if(zhengliqingdan.LP!=null){
            if(!zhengliqingdan.LP.trim().equals("")){
                String[] str=zhengliqingdan.LP.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+=" and s.LP in ("+sb.toString()+")";
            }

        }


        String sqlCount =  "select count(1) as Znum from(" + sql+") t";
        String sqlFy = "select t.* from(" + sql+") t";

       // System.out.println(sqlFy);

        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("Znum").toString()),maps);

    }

    @Override
    public Result Plant() {

        String sql = "select distinct PROP_C9 from ITEM_KEY where PROP_C9!='' and PROP_C9 is not null;";
       List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }

    @Override
    public Result LOC() {

        String sql="select distinct PROP_C2 from ITEM_KEY where PROP_C9!='' and PROP_C2 is not null";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }

    @Override
    public Result ZONE() {

        String sql="select distinct name from [ZONE] where name!='' and name is not null;";
        List<Map<String,Object>> list =jdbcTemplate.queryForList(sql,new HashMap<>());

        return Result.success(list);
    }
}
