package com.yc.wms.service.impl;

import com.yc.wms.bean.Modeldcx;
import com.yc.wms.service.Zwdcx;
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 dcxImpl implements Zwdcx {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public Result QueryJson(QueryPageUtil query, Modeldcx dcx) {

        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 ct.code) as Row_Num,ct.code,s.STATUS,s.agvFlag,count(s.LP) shu\n" +
                "from stock s\n" +
                "left join containerType ct on  ct.id= s.containerType\n" +
                "where 1=1 and s.LP like 'X%'";


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

        //是否
        if(dcx.agvFlag!=null&& !dcx.agvFlag.equals("")){
            sql+=" and s.agvFlag ="+"'"+dcx.agvFlag+"'";
        }

        String sqlCount = "select count(t.code) as nums from(" + sql+"\n GROUP BY ct.code,s.STATUS,s.agvFlag \n"+
        ") t";
        String sqlFy = "select t.* from(" + sql+"GROUP BY ct.code,s.STATUS,s.agvFlag \n" +
                ") 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 Type() {
        String sql="select distinct code from containerType";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }
}
