package com.yc.wms.service.impl;

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

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

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

        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 log.id) as Row_Num,log.id,log.RELATED_BILL,\n" +
                "log.LOG_TYPE ,bt.NAME bt_name,loc1.CODE loc1_code,loc.CODE loc_code,\n" +
                "i.CODE i_code,i.NAME i_name,i.UNIT,\n" +
                "log.OCCUR_QUANTITY*log.INC_DEC number,\n" +
                "log.OPERATOR ,log.OCCUR_TIME ,log.DESCRIPTION ,\n" +
                "ik.PROP_C1 ,ik.PROP_C2 ,ik.PROP_C3 ,ik.PROP_C5 ,ik.PROP_C8 ,\n" +
                "ik.PROP_C9 ,log.endRegion ,log.LP \n" +
                "FROM INVENTORY_LOG log\n" +
                "left join BILL_TYPE bt on bt.ID=log.BILL_TYPE_ID\n" +
                "left join LOCATION loc on loc.id=log.LOCATION_ID\n" +
                "left join LOCATION loc1 on loc1.id=log.SRCLOC_ID\n" +
                "left join ITEM_KEY ik on ik.id=log.ITEM_KEY_ID\n" +
                "left join item i on i.id=ik.ITEM_ID\n" +
                "where 1=1";

        //相关单号
        if(kucunrizhi.RELATED_BILL!=null){
            if(!kucunrizhi.RELATED_BILL.trim().equals("")){
                String[] str=kucunrizhi.RELATED_BILL.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 log.RELATED_BILL in ("+sb.toString()+")";
            }
        }

        //日志类型
        if(kucunrizhi.LOG_TYPE!=null&& kucunrizhi.LOG_TYPE.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< kucunrizhi.LOG_TYPE.length; i++){
                if(kucunrizhi.LOG_TYPE.length-1==i){
                    sb.append("'"+ kucunrizhi.LOG_TYPE[i]+"'");
                }else {
                    sb.append("'"+ kucunrizhi.LOG_TYPE[i]+"'"+",");
                }
            }
            sql+=" and log.LOG_TYPE in ("+sb.toString()+")";;
        }


        //源库位
        if(kucunrizhi.loc1_code!=null){
            if(!kucunrizhi.loc1_code.trim().equals("")){
                String[] str=kucunrizhi.loc1_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 loc1.CODE in ("+sb.toString()+")";
            }
        }

        //目标库位
        if(kucunrizhi.loc_code!=null){
            if(!kucunrizhi.loc_code.trim().equals("")){
                String[] str=kucunrizhi.loc_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 loc.CODE in ("+sb.toString()+")";
            }
        }


        //料号
        if(kucunrizhi.i_code!=null){
            if(!kucunrizhi.i_code.trim().equals("")){
                String[] str=kucunrizhi.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()+")";
            }
        }

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


        //操作员
        if(kucunrizhi.OPERATOR!=null){
            if(!kucunrizhi.OPERATOR.trim().equals("")){
                String[] str=kucunrizhi.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 log.OPERATOR in ("+sb.toString()+")";
            }
        }

        //发生时间
        if(kucunrizhi.OCCUR_TIME!=null&&kucunrizhi.OCCUR_TIME.length>0){
            String startDate = simpleDateFormat.format(kucunrizhi.OCCUR_TIME[0]);
            String endDate = simpleDateFormat.format(kucunrizhi.OCCUR_TIME[1]);
            sql+=" and log.OCCUR_TIME between"+"'"+startDate+"'"+" and "+ "'"+endDate+"'";
        }


        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 Log() {
        String sql=" select distinct LOG_TYPE from INVENTORY_LOG";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }
}
