package com.yc.wms.service.impl;

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

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

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

        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 inv.id) as Row_Num,\n" +
                "max(inv.id) as id,\n" +
                "max(loc.code) as locCode,--库位号\n" +
                "max(item.code) as itemCode,--料号\n" +
                "max(item.name) as itemName,--描述\n" +
                "max(inv.BAR_CODE) as barCode,--序列号\n" +
                "max(inv.STATUS) as status,--状态\n" +
                "max(inv.LOCATION_ID) as LOCATION_ID,--库位ID\n" +
                "max(inv.ITEM_KEY_ID) as ITEM_KEY_ID,--料号属性ID\n" +
                "sum(inv.QUANTITY) as quantity,\t--库存数量\n" +
                "sum(inv.QUEUED_QUANTITY) as queuedQuantity,--占用数量\n" +
                "max(ik.PROP_C1) as propC1,--批次号\n" +
                "max(ik.PROP_C2) as propC2,--LOC\n" +
                "max(ik.PROP_C3) as propC3,--特殊标记\n" +
                "max(ik.PROP_C4) as propC4,--部门\n" +
                "max(ik.PROP_C5) as propC5,--供应商编号\n" +
                "max(ik.PROP_C6) as propC6,--申请人编号\n" +
                "max(ik.PROP_C7) as propC7,--申请人\n" +
                "max(ik.PROP_C8) as propC8,--暂存原因\n" +
                "max(ik.PROP_C9) as propC9,--工厂\n" +
                "max(inv.SOI) as soi,--入库单号\n" +
                "max(ik.PROP_D1)  prop_D1,--生产日期\n" +
                "max(ik.PROP_D2)  prop_D2,--到日期\n" +
                "max(z.name) as kqName,--库区\n" +
                "max(z.ID) as ZONE_ID,--库区ID\n" +
                "\n" +
                "case \n" +
                "\twhen max(inv.coun_Plan) is null then 0\n" +
                "\telse  1\n" +
                "end as lock,--是否盘点\n" +
                "case \n" +
                "\twhen max(z.NAME) in('7#堆垛区','3#货架区','3#料箱区') \n" +
                "then max(ls1.name) else max(ls.name) end as LSname,--AGV库位\n" +
                "max(item.Be_SAP_Item) Be_SAP_Item, --是否SAP料号\n" +
                "max(item.VOLUME) VOLUME,--体积\n" +
                "max(item.BE_WEIGHT) BE_WEIGHT,--是否危化品\n" +
                "max(item.SHIP_RULES) SHIP_RULES,--库存价值等级\n" +
                "max(item.FEE_TYPE) FEE_TYPE,--OA料号\n" +
                "max(item.SINGLE_ITEM) SINGLE_ITEM,--是否批号管理\n" +
                "max(item.BE_MARK_WEIGHT) BE_MARK_WEIGHT,--是否序列号\n" +
                "max(item.UNIT) UNIT,--单位\n" +
                "max(item.STR_EXTEND1) STR_EXTEND1,--备注\n" +
                "max(item.STR_EXTEND2) STR_EXTEND2,--币种\n" +
                "max(item.STR_EXTEND3) STR_EXTEND3,--标准价\n" +
                "max(item.STR_EXTEND4) STR_EXTEND4,--库存单位\n" +
                "ISNULL(stock1.agvFlag, 0) agvFlag,--是否在GAV仓\n" +
                "loc.id locId ,--库位id\n" +
                "max(lts.NAME) as lts_name,--库位类型\n" +
                "item.ISBOM validity,--是否有效期管理\n" +
                "CONVERT(varchar(100), min(inv.STORAGE_DATE), 20) as storageDate,--库存日期\n" +
                " max(inv.moveStatus) moveStatus,--工作站\n" +
                "inv.statuss inv_status,--退料标记\n" +
                "MAX(stock1.WEIGHT) as WEIGHT --重量\n" +
                "\n" +
                "from INVENTORY inv\n" +
                "left join ZONE kq on kq.id=inv.warehouse_Area_ID \n" +
                "left join LOCATION loc on loc.id=inv.LOCATION_ID\n" +
                "left join LOCATIONS lts on lts.id=loc.LOCATIONS_ID\n" +
                "left join STOCK stock1 on stock1.id=loc.stock\n" +
                "left join LOCATION_STORAGE ls1 on ls1.ID=stock1.locationStorage_ID\n" +
                "left join ZONE z on z.ID =loc.ZONE_ID\n" +
                "left join ITEM_KEY ik on ik.id=inv.ITEM_KEY_ID\n" +
                "left join ITEM item on item.id=ik.ITEM_ID\n" +
                "left join STOCK stock on stock.ID=inv.LP\n" +
                "left join LOCATION_STORAGE ls on ls.ID=stock.locationStorage_ID\n" +
                "where 1=1 and inv.QUANTITY>0  and item.Be_SAP_Item=1 and lts.NAME in('存货','不良品') and inv.status in('已入库','整理中','冻结','质检中')";

        //库位号
        if(kucunhuizong.locCode!=null){
            if(!kucunhuizong.locCode.trim().equals("")){
                String[] str=kucunhuizong.locCode.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(kucunhuizong.itemCode!=null){
            if(!kucunhuizong.itemCode.trim().equals("")){
                String[] str=kucunhuizong.itemCode.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 item.code in ("+sb.toString()+")";
            }
        }

        //物料描述
        if(kucunhuizong.itemName!=null){
            if(!kucunhuizong.itemName.trim().equals("")){
                sql+=" and item.name like "+"'%"+kucunhuizong.itemName.trim()+"%'";
            }
        }

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

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

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

        //批次号
        if(kucunhuizong.propC1!=null){
            if(!kucunhuizong.propC1.trim().equals("")){
                String[] str=kucunhuizong.propC1.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 ik.PROP_C3 in ("+sb.toString()+")";
            }
        }

        //特殊标记
        if(kucunhuizong.propC3!=null){
            if(!kucunhuizong.propC3.trim().equals("")){
                sql+=" and item.name like "+"'%"+kucunhuizong.propC3.trim()+"%'";
            }
        }

        //存货日期
        if(kucunhuizong.storageDate!=null&&kucunhuizong.storageDate.length>0){
            String startDate = simpleDateFormat.format(kucunhuizong.storageDate[0]);
            String endDate = simpleDateFormat.format(kucunhuizong.storageDate[1]);
            sql+=" and inv.STORAGE_DATE between "+"'"+startDate+"'"+" and "+ "'"+endDate+"'";
        }

        //供应商号
        if(kucunhuizong.propC5!=null){
            if(!kucunhuizong.propC5.trim().equals("")){

                sql+=" and ik.PROP_C5 = "+"'"+kucunhuizong.propC5.trim()+"'";
            }
        }

        //是否盘点
        if(kucunhuizong.coun_Plan!=null&& !kucunhuizong.coun_Plan.equals("")){
            if (Integer.parseInt(kucunhuizong.coun_Plan)==0) {
                sql+=" and inv.coun_Plan is null";
            }else {
                sql+=" and inv.coun_Plan is not null";
            }
        }

        //是否批次号管理
        if(kucunhuizong.SINGLE_ITEM!=null&& !kucunhuizong.SINGLE_ITEM.equals("")){

            if (Integer.parseInt(kucunhuizong.SINGLE_ITEM)==0) {
                sql+=" and ik.PROP_C1 is null";
            }else {
                sql+=" and ik.PROP_C1 is not null";
            }
        }

        //是否有效期管理
        if(kucunhuizong.validity!=null&& !kucunhuizong.validity.equals("")){

            sql+=" and item.ISBOM = "+"'"+kucunhuizong.validity+"'";
        }

        //退料标记
        if(kucunhuizong.statuss!=null&& !kucunhuizong.statuss.equals("")){

            sql+=" and inv.statuss = "+"'"+kucunhuizong.statuss+"'";
        }


        String sqlCount = "select count(t.id) as nums from(" + sql+"\n " +
                "group by inv.id,item.id,loc.id,ik.PROP_C1,ik.PROP_C2,ik.PROP_C3,ik.PROP_C5,ik.PROP_C7,inv.STATUS,item.isBOM,inv.statuss,stock1.agvFlag"+") t";
        String sqlFy = "select t.* from(" + sql+"\n " +
                "group by inv.id,item.id,loc.id,ik.PROP_C1,ik.PROP_C2,ik.PROP_C3,ik.PROP_C5,ik.PROP_C7,inv.STATUS,item.isBOM,inv.statuss,stock1.agvFlag"+") 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 zt() {
        String sql="select distinct  STATUS from INVENTORY";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new HashMap<>());
        return Result.success(list);
    }

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