/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.businessdata.service.impl;

import com.alibaba.fastjson.JSON;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.repository.StockRepository;
import com.youchain.businessdata.ReturnJson.RPTaskList;
import com.youchain.businessdata.domain.Inventory;
import com.youchain.businessdata.domain.ItemKey;
import com.youchain.businessdata.domain.PickDetail;
import com.youchain.businessdata.domain.Task;
import com.youchain.businessdata.inputJson.IPTask;
import com.youchain.businessdata.repository.InventoryRepository;
import com.youchain.businessdata.repository.PickDetailRepository;
import com.youchain.businessdata.repository.TaskRepository;
import com.youchain.businessdata.service.InventoryLogService;
import com.youchain.businessdata.service.ScreenMdService;
import com.youchain.businessdata.service.TaskService;
import com.youchain.businessdata.service.dto.TaskDto;
import com.youchain.businessdata.service.dto.TaskQueryCriteria;
import com.youchain.businessdata.service.mapstruct.TaskMapper;
import com.youchain.modules.system.domain.Dept;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author houjianlan
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-08-16
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class ScreenMdServiceImpl implements ScreenMdService {
    private final EntityManager entityMapper;
    public List<Object[]> queryLayout(String zoneType) {
        String sql="SELECT ls.id,ls.code,ls.X,ls.Y,i.BAR_CODE,ik.LOT,ls.DISABLED ,inv.`STATUS` inv_status,ls.`STATUS` loc_status\n" +
                "FROM mdwms.`location_storage` `ls` \n" +
                "LEFT JOIN mdwms.`stock` `sto` ON `sto`.`LocationStorage_id` = `ls`.`ID` \n" +
                "LEFT JOIN mdwms.`location` `loc` ON `loc`.`STOCK_ID` = `sto`.`ID` \n" +
                "LEFT JOIN mdwms.`inventory` `inv` ON `inv`.`LOCATION_ID` = `loc`.`ID` \n" +
                "LEFT JOIN mdwms.`item_key` `ik` ON `inv`.`ITEM_KEY_ID` = `ik`.`ID` \n" +
                "LEFT JOIN mdwms.`item` `i` ON `i`.`ID` = `ik`.`ITEM_ID` \t\t\n" +
                "WHERE `ls`.`TYPE` = '05' and ls.X is not null and ls.Y is not null and ls.x!=0 and ls.y!=0 \n" +
                "ORDER By `ls`.`CODE`";
        List<Object[]> ts= entityMapper.createNativeQuery(sql).getResultList();
        return ts;
    }

    public List<Object[]> getLocXy(String zoneType) {
        String sql="SELECT min(ls.X) minx,min(ls.Y) miny,max(ls.X) maxx,max(ls.Y) maxy " +
                "FROM mdwms.`location_storage` `ls` \n" +
                "WHERE `ls`.`TYPE` = '05' and ls.X is not null and ls.Y is not null and ls.x!=0 and ls.y!=0 \n";
        List<Object[]> ts= entityMapper.createNativeQuery(sql).getResultList();
        return ts;
    }

    @Transactional(readOnly = true)
    public int getLocStatusCount(String type){
        String sql="SELECT count(ls.id) " +
                "FROM mdwms.`location_storage` `ls` \n" +
                "LEFT JOIN mdwms.`stock` `sto` ON `sto`.`LocationStorage_id` = `ls`.`ID` \n" +
                "LEFT JOIN mdwms.`location` `loc` ON `loc`.`STOCK_ID` = `sto`.`ID` \n" +
                "LEFT JOIN mdwms.`inventory` `inv` ON `inv`.`LOCATION_ID` = `loc`.`ID` \n" +
                "WHERE `ls`.`TYPE` = '05' and ls.X is not null and ls.Y is not null and ls.x!=0 and ls.y!=0 \n";
        if(type.equals("1")){//1禁用，2满料，3AGV未送物料占用，4 空闲，5空车占用
            sql+=" and ls.DISABLED=1";
        }else if(type.equals("2")){
            sql+=" and ls.DISABLED=0 and inv.status='OPEN' and ls.status='OCCUPY'";
        }else if(type.equals("3")){
            sql+=" and ls.DISABLED=0 and inv.status='ZY' and ls.status='OCCUPY'";
        }else if(type.equals("4")){
            sql+=" and ls.DISABLED=0 and inv.status is null and ls.status='FREE'";
        }else if(type.equals("5")){
            sql+=" and ls.DISABLED=0 and inv.status is null and ls.status='OCCUPY'";
        }
        List<Object> ts= entityMapper.createNativeQuery(sql).getResultList();
        return Integer.parseInt(ts.get(0).toString());
    }

    public int getLocCount(String itemType) {
        String sql="SELECT count(ls.id) " +
                "FROM mdwms.`location_storage` `ls` \n" +
                "WHERE `ls`.`TYPE` = '05' and ls.X is not null and ls.Y is not null and ls.x!=0 and ls.y!=0 \n";
        List<Object> ts= entityMapper.createNativeQuery(sql).getResultList();
        return Integer.parseInt(ts.get(0).toString());
    }
}
