package com.yc.wms.service.impl;

import com.yc.wms.service.Jianhuojiemian;
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;

@Slf4j
@Service
public class JianhuojiemianImpl implements Jianhuojiemian {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

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

         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 t.id) as Row_Num, \n" +
                "t.id, pt.CODE pt_code,pt.ORDER_DATE, t.END_REGION,s.name s_name,z.NAME z_name,\n" +
                "pt.sourceType,bt.NAME bt_name,t.BILLCODE,i.NAME i_name,i.CODE i_code,i.UNIT,i.SHIP_RULES,\n" +
                "lc1.CODE lc1_code,st.LP ,lc2.CODE lc2_code,t.strBill4 ,t.yc_qty ,t.PLAN_QUANTITY_MU ,\n" +
                "t.MOVED_QUANTITY_MU , \n" +
                "case WHEN t.PLAN_QUANTITY_MU-t.MOVED_QUANTITY_MU <=0 then '处理完成' WHEN t.MOVED_QUANTITY_MU =0 then '未处理' else '处理中' end as Status ,\n" +
                "ik.PROP_C1 ,ik.PROP_C2 ,ik.PROP_C3 ,ik.PROP_C5 ,ik.PROP_C6 ,ik.PROP_C7 ,ik.PROP_C8 ,ik.PROP_C9 ,\n" +
                "pt.equated_Quantity ,t.BAR_CODE ,t.INVENTORY_STATUS ,pd.LINE_NUMBER ,\n" +
                "t.strBill1 ,w1.NAME w1_name,w4.NAME w4_name,t.OPERATE_TIME,t.CREATE_TIME ,t.CREATE_TIME t_date,wd.CODE wd_code,t.statusCode ,\n" +
                "t.agv_Status ,t.popular ,t.CODE t_code\n" +
                " from TASK t\n" +
                " left join PICK_TICKET_DETAIL pd on pd.id=t.PICK_TICKET_DETAIL_ID\n" +
                " left join PICK_TICKET pt on pt.id=pd.PICK_TICKET_ID\n" +
                " left join Seed_Wall s on s.id=t.Seed_Wall_ID\n" +
                " left join BILL_TYPE bt on bt.id=pt.BILL_TYPE_ID\n" +
                " left join ITEM_KEY ik on ik.id=t.ITEM_KEY_ID\n" +
                " left join ITEM i on i.id=ik.ITEM_ID\n" +
                " left join LOCATION lc1 on lc1.id=t.SRC_LOC_ID\n" +
                " left join [ZONE] z on z.id=lc1.ZONE_ID\n" +
                " left join LOCATION lc2 on lc2.id=t.DST_LOC_ID\n" +
                " left join WAVE_DOC  wd on wd.id=t.WAVE_DOC_ID\n" +
                " left join WORKER w1 on w1.id=t.WORKER1_ID\n" +
                " left join WORKER w4 on w4.id=t.WORKER4_ID\n" +
                " left join stock st on st.id=t.DST_LP\n" +
                "where 1=1  and t.TYPE='MV_PICKTICKET' and t.PLAN_QUANTITY>0";

        //子任务号
        if(jianhuojiemian.BILLCODE!=null){
            if(!jianhuojiemian.BILLCODE.trim().equals("")){
                String[] str=jianhuojiemian.BILLCODE.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 t.BILLCODE in ("+sb.toString()+")";
            }
        }

        //工单号
        if(jianhuojiemian.pt_code!=null){
            if(!jianhuojiemian.pt_code.trim().equals("")){
                String[] str=jianhuojiemian.pt_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 pt.CODE in ("+sb.toString()+")";
            }
        }

        //SAP类型
        if(jianhuojiemian.sourceType!=null){
            if(!jianhuojiemian.sourceType.trim().equals("")){
                sql+=" and item.name like "+"'%"+jianhuojiemian.sourceType.trim()+"%'";
            }
        }

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

        //所在库位
        if(jianhuojiemian.lc2_code!=null){
            if(!jianhuojiemian.lc2_code.trim().equals("")){
                String[] str=jianhuojiemian.lc2_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 lc2.CODE in ("+sb.toString()+")";
            }
        }

        //料号
        if(jianhuojiemian.i_code!=null){
            if(!jianhuojiemian.i_code.trim().equals("")){
                String[] str=jianhuojiemian.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(jianhuojiemian.bt_name!=null&& jianhuojiemian.bt_name.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< jianhuojiemian.bt_name.length; i++){
                if(jianhuojiemian.bt_name.length-1==i){
                    sb.append("'"+ jianhuojiemian.bt_name[i]+"'");
                }else {
                    sb.append("'"+ jianhuojiemian.bt_name[i]+"'"+",");
                }
            }
            sql+=" and bt.NAME in ("+sb.toString()+")";;
        }

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

        //拣货人
        if(jianhuojiemian.w4_name!=null&& jianhuojiemian.w4_name.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< jianhuojiemian.w4_name.length; i++){
                if(jianhuojiemian.w4_name.length-1==i){
                    sb.append("'"+ jianhuojiemian.w4_name[i]+"'");
                }else {
                    sb.append("'"+ jianhuojiemian.w4_name[i]+"'"+",");
                }
            }
            sql+=" and w4.NAME in ("+sb.toString()+")";;
        }

        //波次号
        if(jianhuojiemian.wd_code!=null){
            if(!jianhuojiemian.wd_code.trim().equals("")){
                sql+=" and wd.CODE="+"'"+jianhuojiemian.wd_code.trim()+"'";
            }
        }

        //容器码
        if(jianhuojiemian.LP!=null){
            if(!jianhuojiemian.LP.trim().equals("")){
                sql+=" and wd.CODE="+"'"+jianhuojiemian.LP.trim()+"'";
            }
        }

        //拣货库位
        if(jianhuojiemian.lc1_code!=null&& jianhuojiemian.lc1_code.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< jianhuojiemian.lc1_code.length; i++){
                if(jianhuojiemian.lc1_code.length-1==i){
                    sb.append("'"+ jianhuojiemian.lc1_code[i]+"'");
                }else {
                    sb.append("'"+ jianhuojiemian.lc1_code[i]+"'"+",");
                }
            }
            sql+=" and lc1.CODE in ("+sb.toString()+")";;
        }

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

        //分配日期
        if(jianhuojiemian.CREATE_TIME!=null&&jianhuojiemian.CREATE_TIME.length>0){
            String startDate = simpleDateFormat.format(jianhuojiemian.CREATE_TIME[0]);
            String endDate = simpleDateFormat.format(jianhuojiemian.CREATE_TIME[1]);
            sql+=" and t.CREATE_TIME between "+"'"+startDate+"'"+" and "+ "'"+endDate+"'";
        }

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

        //拣货状态
        if(jianhuojiemian.Status!=null&&!jianhuojiemian.Status.equals("")){
            if(jianhuojiemian.Status.equals("处理完成")){
                sql+=" and t.PLAN_QUANTITY_MU-t.MOVED_QUANTITY_MU <=0";
            }else if(jianhuojiemian.Status.equals("未处理")){
                sql+=" and t.MOVED_QUANTITY_MU =0";
            }else {
                sql+=" and t.MOVED_QUANTITY_MU >0 and t.MOVED_QUANTITY_MU<t.PLAN_QUANTITY_MU";
            }


        }

        //加急标记
        if(jianhuojiemian.equated_Quantity!=null&& !jianhuojiemian.equated_Quantity.equals("")){
            sql+=" and pt.equated_Quantity ="+"'"+jianhuojiemian.equated_Quantity+"'";
        }

        //下发人
        if(jianhuojiemian.strBill1!=null&& jianhuojiemian.strBill1.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< jianhuojiemian.strBill1.length; i++){
                if(jianhuojiemian.strBill1.length-1==i){
                    sb.append("'"+ jianhuojiemian.strBill1[i]+"'");
                }else {
                    sb.append("'"+ jianhuojiemian.strBill1[i]+"'"+",");
                }
            }
            sql+=" and t.strBill1 in ("+sb.toString()+")";;
        }


        String sqlCount = "select count(t.id) as nums from(" + sql+") t";
        String sqlFy = "select t.* from(" + sql+") 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 Dj() {
        String sql="select distinct name from BILL_TYPE";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }

    @Override
    public Result Gz() {
        String sql="select distinct END_REGION from TASK where END_REGION is not null";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }

    @Override
    public Result Jhr() {
        String sql="select distinct  name from WORKER";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }

    @Override
    public Result Kw(String w) {
        String sql="select distinct CODE from  LOCATION where CODE like "+"'%"+w+"%'";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }

    @Override
    public Result Bj() {
        String sql="select distinct equated_Quantity  from PICK_TICKET where equated_Quantity !='' and equated_Quantity is not null";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }

    @Override
    public Result Xf() {
        String sql=" select distinct strBill1 from TASK where strBill1 is not null";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }
}
