package com.yc.wms.service.impl;

import com.yc.wms.bean.Liaoxiangjiankong;
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 LiaoxiangjiankongImpl implements com.yc.wms.service.Liaoxiangjiankong {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

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

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

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

        String sql="SELECT\n" +
                "    ct.code,\n" +
                "    SUM(CASE WHEN s.STATUS = 'OCCUPY'  OR s.STATUS = 'FREE'  THEN 1 ELSE 0 END) AS zs,\n" +
                "\t\tSUM(CASE WHEN s.STATUS = 'OCCUPY' AND s.agvFlag = 0 THEN 1 ELSE 0 END) AS mbzk,\n" +
                "    SUM(CASE WHEN s.STATUS = 'OCCUPY' AND s.agvFlag = 1 THEN 1 ELSE 0 END) AS mzk,\n" +
                "\t\tSUM(CASE WHEN s.STATUS = 'FREE' AND s.agvFlag = 0 THEN 1 ELSE 0 END) AS kbzk,\n" +
                "    SUM(CASE WHEN s.STATUS = 'FREE' AND s.agvFlag = 1 THEN 1 ELSE 0 END) AS kzk\n" +
                "FROM\n" +
                "    stock s\n" +
                "LEFT JOIN containerType ct ON ct.id = s.containerType\n" +
                "WHERE\n" +
                "    s.LP LIKE 'X%'\n" +
                "    AND ct.code != 'XN'";


        //类型
        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+"'";
        }

        //状态
        if(dcx.s_status!=null&& dcx.s_status.length>0){
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i< dcx.s_status.length; i++){
                if(dcx.s_status.length-1==i){
                    sb.append("'"+ dcx.s_status[i]+"'");
                }else {
                    sb.append("'"+ dcx.s_status[i]+"'"+",");
                }
            }
            sql+=" and s.STATUS in ("+sb.toString()+")";

        }

        String sqlCount = "select count(t.code) as nums from (" + sql+"\n GROUP BY ct.code\n" + ") t";
        String sqlFy =  sql+"GROUP BY ct.code\n " +
                                "ORDER BY ct.code";

        sqlFy+= " OFFSET "+(pageNum-1) * pageSize+ " ROWS FETCH NEXT "+(pageNum) * pageSize +" ROWS ONLY";

        //查询单数据
        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);
    }
}
