package com.youchain.appupdate.rest;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONArray;
import com.alibaba.druid.sql.visitor.functions.Substring;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.appupdate.ReturnJson.RLocLayout;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.repository.ItemRepository;
import com.youchain.basicdata.service.ItemService;
import com.youchain.basicdata.service.StockService;
import com.youchain.basicdata.service.impl.PointServiceImpl;
import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.repository.AgvTaskRepository;
import com.youchain.businessdata.repository.AsnDetailRepository;
import com.youchain.businessdata.repository.PickDetailRepository;
import com.youchain.businessdata.repository.TaskRepository;
import com.youchain.businessdata.service.*;
import com.youchain.businessdata.service.impl.AsnDetailServiceImpl;
import com.youchain.businessdata.service.impl.InventoryServiceImpl;
import com.youchain.businessdata.service.impl.PickDetailServiceImpl;
import com.youchain.businessdata.service.impl.TaskServiceImpl;
import com.youchain.modules.system.service.DictService;
import com.youchain.report_data.utils.PieChart;
import com.youchain.utils.AreaNameDic;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.icepear.echarts.Option;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequiredArgsConstructor
@Api(tags = "大屏数据显示")
@RequestMapping("/api/screeView")
@Slf4j
public class ScreenViewController {
    private final PointServiceImpl pointServiceImpl;
    private final AsnDetailServiceImpl asnDetailServiceImpl;
    private final PickDetailServiceImpl pickDetailServiceImpl;
    private final TaskServiceImpl taskServiceImpl;
    private final InventoryServiceImpl inventoryServiceImpl;
    private final AgvTaskRepository agvTaskRepository;
    private final PickDetailRepository pickDetailRepository;
    private final TaskRepository taskRepository;

    @PostMapping(value="/inForDay")
    @Log("每日入库")
    @ApiOperation("每日入库")
    @AnonymousAccess
    public ResponseEntity<Object> inForDay() {
        JSONArray array=new JSONArray();
        List<Object[]> l=asnDetailServiceImpl.queryIn("Day",null,"ALL");
        int value1=0;
        int value2=0;
        if(l.size()>0){
            Object[] obj=l.get(0);
            value1=obj[0]==null?0:((Double)obj[0]).intValue();
            value2=obj[1]==null?0:((Double)obj[1]).intValue();
        }
        array.add(this.formatDay("","入库","center","#ffffff","bold"));
        array.add(this.formatDay("当日任务",value1+"","center","#ffffff","bold"));
        array.add(this.formatDay("已完成",value2+"","center","#ffffff","bold"));
        return new ResponseEntity(array, HttpStatus.OK);
    }

    @PostMapping(value="/outForDay")
    @Log("每日出库")
    @ApiOperation("每日出库")
    @AnonymousAccess
    public ResponseEntity<Object> outForDay() {
        List<Object[]> l=pickDetailServiceImpl.queryOut("Day",null);
        int value1=0;
        int value2=0;
        if(l.size()>0){
            Object[] obj=l.get(0);
            value1=obj[0]==null?0:((Double)obj[0]).intValue();
            value2=obj[1]==null?0:((Double)obj[1]).intValue();
        }
        JSONArray array=new JSONArray();
        array.add(this.formatDay("","出库","center","#ffffff","bold"));
        array.add(this.formatDay("当日任务",""+value1,"center","#ffffff","bold"));
        array.add(this.formatDay("已完成",""+value2,"center","#ffffff","bold"));
        return new ResponseEntity(array, HttpStatus.OK);
    }
    @PostMapping(value="/kmdView")
    @Log("库满度")
    @ApiOperation("库满度")
    @AnonymousAccess
    public ResponseEntity<Object> kmdView() {
        JSONArray array=new JSONArray();
        array.add(this.formatKmd("",100));
        List<Object[]> objs=pointServiceImpl.queryPointKmd();
        HashMap<String,Double> qhtk=new HashMap<String,Double>();
        HashMap<String,Double> qhtm=new HashMap<String,Double>();
        HashMap<String,Double> zck=new HashMap<String,Double>();
        HashMap<String,Double> zcm=new HashMap<String,Double>();
        int size=objs.size();
        if(objs.size()>0){
            for(int i=0;i<objs.size();i++) {
                Object[] obj = objs.get(i);
                String areaName = obj[0].toString();
                String status = obj[1].toString();
                Double qty = Double.parseDouble(obj[2].toString());
                if (areaName.equals(AreaNameDic.OneFC)||areaName.equals(AreaNameDic.TwoFB)) {
                    qhtm.put(status, qty);
                } else if (areaName.equals(AreaNameDic.OneFK)||areaName.equals(AreaNameDic.TwoFK)) {
                    qhtk.put(status, qty);
                }
            }
        }
        if(qhtk!=null){
            Double k=qhtk.get(BaseStatus.FREE)==null?0d:qhtk.get(BaseStatus.FREE);
            Double m=qhtk.get(BaseStatus.USED)==null?0d:qhtk.get(BaseStatus.USED);
            array.add(this.formatKmd(AreaNameDic.OneFK,m*100/(k+m)));
        }
        if(qhtm!=null){
            Double k=qhtm.get(BaseStatus.FREE)==null?0d:qhtm.get(BaseStatus.FREE);
            Double m=qhtm.get(BaseStatus.USED)==null?0d:qhtm.get(BaseStatus.USED);
            array.add(this.formatKmd(AreaNameDic.OneFC,m*100/(k+m)));
        }
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("data",array);
        JSONArray colors=new JSONArray();
        colors.add("#ffffff00");
        if(size>=1) {
            colors.add("#cc0099");
        }
        if(size>=2) {
            colors.add("#99cc33");
        }
        if(size>=3) {
            colors.add("#00baff");
        }
        if(size>=4) {
            colors.add("#3de7c9");
        }
        map.put("colors",colors);
        map.put("unit","%");
        map.put("showValue",false);
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @PostMapping(value="/inOutForMonth")
    @Log("每月出入库")
    @ApiOperation("每月出入库")
    @AnonymousAccess
    public ResponseEntity<Object> inOutForMonth() {
        int rk=0;
        List<Object[]> lr=asnDetailServiceImpl.queryIn("Month",null,"ALL");
        if(lr.size()>0){
            Object[] obj=lr.get(0);
            rk=obj[0]==null?0:((Double)obj[0]).intValue();
        }
        int ck=0;
        List<Object[]> lc=pickDetailServiceImpl.queryOut("Month",null);
        if(lc.size()>0){
            Object[] obj=lc.get(0);
            ck=obj[0]==null?0:((Double)obj[0]).intValue();
        }
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("r1",(int)rk/1000);
        map.put("r2",(int)rk/100%10);
        map.put("r3",(int)rk/10%10);
        map.put("r4",(int)rk%10);
        map.put("c1",(int)ck/1000);
        map.put("c2",(int)ck/100%10);
        map.put("c3",(int)ck/10%10);
        map.put("c4",(int)ck%10);
        return new ResponseEntity(map, HttpStatus.OK);
    }
    @PostMapping(value="/inMonth")
    @Log("每月入库")
    @ApiOperation("每月入库")
    @AnonymousAccess
    public ResponseEntity<Object> inMonth() {
        return this.inOutMonth("IN");
    }

    @PostMapping(value="/outMonth")
    @Log("每月出库")
    @ApiOperation("每月出库")
    @AnonymousAccess
    public ResponseEntity<Object> outMonth() {
        return this.inOutMonth("OUT");
    }

    public ResponseEntity<Object> inOutMonth(String type) {
        JSONArray array=new JSONArray();
        List<Object[]> objs=taskServiceImpl.queryAreaMonth(type);
        HashMap<String,Double> qht=new HashMap<String,Double>();
        HashMap<String,Double> zc=new HashMap<String,Double>();
        HashMap<String,Double> mf=new HashMap<String,Double>();
        HashMap<String,Double> xj=new HashMap<String,Double>();
        double totalQty=0;
        int size=objs.size();
        if(objs.size()>0){
            for(int i=0;i<objs.size();i++) {
                Object[] obj = objs.get(i);
                String areaName = obj[0].toString();
                Double qty = Double.parseDouble(obj[1].toString());
                totalQty+=qty;
                if (areaName.equals(AreaNameDic.OneFC)||areaName.equals(AreaNameDic.TwoFB)) {
                    qht.put(areaName, qty);
                }
            }
            if(qht!=null) {
                double value=qht.get(AreaNameDic.OneFK);
                array.add(this.formatKmd("前后桶", value*100/totalQty));
            }
        }
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("data",array);

        JSONArray color=new JSONArray();
        if(size>=1) {
            color.add("#ffcc33");
        }
        if(size>=2) {
            color.add("#99cccc");
        }
        if(size>=3) {
            color.add("#3399ff");
        }
        if(size>=4) {
            color.add("#fff8dc");
        }
        map.put("color",color);
        map.put("lineWidth",15);
        map.put("radius","55%");
        map.put("activeRadius","60%");
        return new ResponseEntity(map, HttpStatus.OK);
    }
    @PostMapping(value="/inOutDay")
    @Log("每日出入库")
    @ApiOperation("每日出入库")
    @AnonymousAccess
    public ResponseEntity<Object> inOutDay(String type) {
        JSONArray array=new JSONArray();
        String date=DateUtil.formatChageDay(new Date(),"DAY",-6);
        List<Object[]> objsIn=asnDetailServiceImpl.queryIn("InOutDay", date,"ALL");
        HashMap<String,Object> mapIn=new HashMap<String,Object>();
        mapIn.put("0","0");
        if(objsIn.size()>0){
            for(int i=0;i<objsIn.size();i++) {
                Object[] obj = objsIn.get(i);
                String day = obj[0].toString();
                Double qty = Double.parseDouble(obj[1].toString());
                mapIn.put(day,qty);
            }
        }
        List<Object[]> objsOut=pickDetailServiceImpl.queryOut("InOutDay", date);
        HashMap<String,Object> mapOut=new HashMap<String,Object>();
        mapOut.put("0","0");
        if(objsOut.size()>0){
            for(int i=0;i<objsOut.size();i++) {
                Object[] obj = objsOut.get(i);
                String day = obj[0].toString();
                Double qty = Double.parseDouble(obj[1].toString());
                mapOut.put(day,qty);
            }
        }
        HashMap<String,Object> map=new HashMap<String,Object>();
        HashMap<String,Object> dataset=new HashMap<String,Object>();
        JSONArray source=new JSONArray();
        JSONArray sourceH=new JSONArray();
        sourceH.add("product");
        sourceH.add("入库");
        sourceH.add("出库");
        source.add(sourceH);
        for(int i=-6;i<=0;i++){
            String date1=DateUtil.formatChageDay(new Date(),"DAY",i);
            if(mapIn.get(date1)!=null||mapOut.get(date1)!=null){
                JSONArray sourceD=new JSONArray();
                sourceD.add(date1);
                sourceD.add(mapIn.get(date1)==null?0:mapIn.get(date1));
                sourceD.add(mapOut.get(date1)==null?0:mapOut.get(date1));
                source.add(sourceD);
            }
        }
        dataset.put("source",source);
        HashMap<String,Object> data=new HashMap<String,Object>();
        HashMap<String,Object> config=new HashMap<String,Object>();
        HashMap<String,Object> yAxis=new HashMap<String,Object>();
        HashMap<String,Object> axisLabel=new HashMap<String,Object>();
        HashMap<String,Object> xAxis=new HashMap<String,Object>();
        axisLabel.put("color","#fff");
        HashMap<String,Object> nameTextStyle=new HashMap<String,Object>();
        nameTextStyle.put("color","#fff");
        map.put("msg","");
        data.put("config",config);
        config.put("yAxis",yAxis);
        yAxis.put("axisLabel",axisLabel);
        yAxis.put("show",true);
        yAxis.put("nameTextStyle",nameTextStyle);
        config.put("xAxis",xAxis);
        xAxis.put("axisLabel",axisLabel);
        xAxis.put("type","category");
        xAxis.put("nameTextStyle",nameTextStyle);
        HashMap<String,Object> legend=new HashMap<String,Object>();
        config.put("legend",legend);
        HashMap<String,Object> grid=new HashMap<String,Object>();
        grid.put("top",20);
        grid.put("bottom",25);
        grid.put("width","auto");
        grid.put("height","auto");
        config.put("grid",grid);
        JSONArray series=new JSONArray();
        HashMap<String,Object> c1=new HashMap<String,Object>();
        c1.put("color","#fff68f");
        c1.put("type","bar");
        HashMap<String,Object> c2=new HashMap<String,Object>();
        c2.put("color","#09f");
        c2.put("type","bar");
        series.add(c1);
        series.add(c2);
        config.put("series",series);
        HashMap<String,Object> title=new HashMap<String,Object>();
        HashMap<String,Object> textStyle=new HashMap<String,Object>();
        textStyle.put("color","#fff68f");
        title.put("textStyle",textStyle);
        config.put("title",title);
        config.put("dataset",dataset);
        data.put("config",config);
        map.put("data",data);
        map.put("satus",1);
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @PostMapping(value="/inventoryView")
    @Log("库存余量")
    @ApiOperation("库存余量")
    @AnonymousAccess
    public ResponseEntity<Object> inventoryView() {
        List<Object[]> l=inventoryServiceImpl.queryItemStock();
        HashMap<String,Object> map=new HashMap<String,Object>();
        HashMap<String,Object> dataH=new HashMap<String,Object>();
        HashMap<String,Object> config=new HashMap<String,Object>();
        if(l.size()>0){
            JSONArray header=new JSONArray();
            header.add("代码");
            header.add("名称");
            header.add("托数");
            config.put("header",header);
            JSONArray data=new JSONArray();
            for(int i=0;i<l.size();i++) {
                JSONArray d=new JSONArray();
                Object[] obj = l.get(i);
                String itemCode = obj[0].toString();
                String itemName = obj[1].toString();
                String qty = obj[2].toString();
                d.add(itemCode);
                d.add(itemName);
                d.add(qty);
                data.add(d);
            }
            config.put("data",data);
            config.put("rowNum",8);
            config.put("headerBGC","#000fff82");
            config.put("oddRowBGC","#ededed33");
            config.put("evenRowBGC","#ededed33");
            dataH.put("config",config);
            map.put("msg","");
            map.put("data",dataH);
            map.put("satus","1");
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }
    @PostMapping(value="/taskPerformed")
    @Log("前后桶正在执行的任务")
    @ApiOperation("前后桶正在执行的任务")
    @AnonymousAccess
    public ResponseEntity<Object> taskPerformed() {//id【任务id】、type【任务类型】、物料号
        List<AgvTask> agvTasks = agvTaskRepository.queryAgvTaskPerformedQH();
        HashMap<String,Object> map=new HashMap<String,Object>();
        HashMap<String,Object> dataH=new HashMap<String,Object>();
        HashMap<String,Object> config=new HashMap<String,Object>();
        if(agvTasks.size()>0){
            JSONArray header=new JSONArray();
            header.add("料车");
            header.add("任务类型");
            header.add("任务ID");
            config.put("header",header);
            JSONArray data=new JSONArray();
            for (AgvTask a : agvTasks) {
                JSONArray d=new JSONArray();
                String itemCode = a.getStockCode();
                if (itemCode == null || itemCode.equals("")){
                    itemCode = "agv正在前往";
                }
                String type = a.getType().equals("ASN")? "入库":"出库";
                String id = a.getId().toString();
                d.add(itemCode);
                d.add(type);
                d.add(id);
                data.add(d);
            }
            config.put("data",data);
            config.put("rowNum",8);
            config.put("waitTime",2000);
            config.put("carousel","single");
            config.put("headerBGC","#061a3a");
            config.put("oddRowBGC","#06152f");
            config.put("evenRowBGC","#071026");
            dataH.put("config",config);
            map.put("msg","");
            map.put("data",dataH);
            map.put("satus","1");
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    public HashMap<String,Object> formatDay(String title,String content,String textAlign,String fill,String fontWeight) {
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("title",title);
        HashMap<String,Object> number=new HashMap<String,Object>();
        number.put("content",content);
        number.put("textAlign",textAlign);
        HashMap<String,Object> style=new HashMap<String,Object>();
        style.put("fill",fill);
        style.put("fontWeight",fontWeight);
        number.put("style",style);
        map.put("number",number);
        return map;
    }
    public HashMap<String,Object> formatKmd(String name,double value) {
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("name",name);
        map.put("value", NumberUtil.round(value,2));
        return map;
    }

    @PostMapping(value="/locLayout")
    @Log("库位布局")
    @ApiOperation("库位布局")
    @AnonymousAccess
    public ResponseEntity<Object> locLayout(String zoneType) {
        //0空1满2禁用3空车区满
        if(zoneType==null){
            zoneType="QHT";
        }
        List<Object[]> mins=pointServiceImpl.getPointMinxy(zoneType);
        List<RLocLayout> ls=pointServiceImpl.queryLayout(zoneType,0,0);
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("mg_top",2);
        map.put("mg_left",1);
        HashMap<String,Object> seatList=new HashMap<String,Object>();
        int type0=0;
        int type1=0;
        int type2=0;
        int type3=0;
        if(ls.size()>0){
            JSONArray seat=new JSONArray();
            Object[] obj=mins.get(0);
            double x=Double.parseDouble(obj[0].toString())-2;
            double y=Double.parseDouble(obj[1].toString())-2;
            double maxy=Double.parseDouble(obj[3].toString());
            HashMap<String,Integer> itemTypes=new HashMap<String,Integer>();
            for(int i=0;i<ls.size();i++) {
                HashMap<String,Object> d=new HashMap<String,Object>();
                RLocLayout layout = ls.get(i);
                d.put("id", layout.getId());
                d.put("gRow", (int)Math.ceil(layout.getX()-x));
                d.put("gCol", Math.abs((int)Math.ceil(layout.getY()-maxy)));
                d.put("type", layout.getType());
                d.put("code", layout.getCode());
                if (layout.getType() == 0) {
                    type0++;
                } else if (layout.getType() == 1) {
                    d.put("code", layout.getItemName());
                    type1++;
                } else if (layout.getType() == 2) {
                    type2++;
                } else if (layout.getType() == 3) {
                    type3++;
                }
                String itemType= layout.getItemType();
                if(itemType!=null) {
                    if (itemTypes.get(itemType) != null) {
                        itemTypes.put(itemType, itemTypes.get(itemType) + 1);
                    } else {
                        itemTypes.put(itemType, 1);
                    }
                }
                seat.add(d);
            }
            String movieName="";
            if(zoneType.equals("QHT")){
                movieName="前后桶点位图  "+ls.size()+"库位(空点:"+type0+",空车:"+type3+",满车:"+type1+",禁用:"+type2+")";
            }else  if(zoneType.equals("ZCKM")){
                movieName="";
            }
            map.put("movieName",movieName);//+mes);
            map.put("seatList",seat);
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @PostMapping(value="/locJxbLayout")
    @Log("机械臂库位布局")
    @ApiOperation("机械臂库位布局")
    @AnonymousAccess
    public ResponseEntity<Object> locJxbLayout(String zoneType) {
        //0空1满2禁用3空车区满
        List<Object[]> mins=pointServiceImpl.getPointMinxy(zoneType);
        List<Object[]> ts=pointServiceImpl.getZcJxb();
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("mg_top",2);
        map.put("mg_left",1);
        HashMap<String,Object> seatList=new HashMap<String,Object>();
        if(ts.size()>0){
            JSONArray seat=new JSONArray();
            Object[] obj=mins.get(0);
            double x=Double.parseDouble(obj[0].toString())-2;
            double y=Double.parseDouble(obj[1].toString())-2;
            double maxy=Double.parseDouble(obj[3].toString());
            HashMap<String,Integer> itemTypes=new HashMap<String,Integer>();
            for(Object[] o:ts) {
                HashMap<String,Object> d=new HashMap<String,Object>();
                int j=0;
//                for(Object[] o1:ts){
//                    log.info("o1:"+o1.length+"===");
//                    for(int v=0;v<o1.length;v++){
//                        log.info("v:"+obj[v]+"====="+v);
//                    }
//                }
                d.put("id", o[j++]);
                d.put("xh", o[j++]);
                d.put("code1", o[j++].toString().replace("MA1151-",""));
                d.put("code2", o[j++].toString().replace("MA1151-",""));
                d.put("status1", o[j++]);
                d.put("status2", o[j++]);
                d.put("item1", o[j++]);
                d.put("item2", o[j++]);
                Double row=Double.parseDouble(o[j++].toString());
                Double col=Double.parseDouble(o[j++].toString());
                d.put("gRow", (int)Math.ceil(row-x));
                d.put("gCol", Math.abs((int)Math.ceil(col-maxy)));
                if(d.get("status1").equals(BaseStatus.USED)||d.get("status2").equals(BaseStatus.USED)){
                    d.put("type", 1);
                }else{
                    d.put("type", 0);
                }
                seat.add(d);
            }
            map.put("movieName",null);//+mes);
            map.put("seatList",seat);
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @PostMapping(value="/itemTypePie")
    @Log("物料类型比列")
    @ApiOperation("物料类型比列")
    @AnonymousAccess
    public ResponseEntity<Object> itemTypePie(String itemType) {
        DecimalFormat df = new DecimalFormat("#.0");
        int pointCount=pointServiceImpl.getPointCoutType(itemType);
        int invCount=pointServiceImpl.getInvCoutType(itemType);
        String color0="#ededed";
        String color1="#FF0000";
        String scoreName="SCORE";
        double percentage=invCount*100*0.1*10/pointCount;
//        percentage=Math.round(percentage);
        percentage=Double.parseDouble(df.format(percentage));
        if(itemType.equals("前桶480")){
//            color0="#04CDE6";
            color1="#0CC8E6";
        }else  if(itemType.equals("前桶440")){
//            color0="#73E239";
            color1="#35B45E";
        }else  if(itemType.equals("后桶1200单洗")){
//            color0="#FEBA6B";
            color1="#F29961";
        }else  if(itemType.equals("后桶DF440单洗")){
//            color0="#E15E68";
            color1="#E53240";
        }else  if(itemType.equals("后桶1200洗干")){
//            color0="#A682E6";
            color1="#6244E6";
        }else  if(itemType.equals("后桶1400单洗")){
//            color0="#A682E6";
            color1="#8fbc8f";
        }else  if(itemType.equals("后桶DF440洗干")){
//            color0="#A682E6";
            color1="#663300";
        }else  if(itemType.equals("CK")){
//            color0="#A682E6";
            color1="#663300";
        }
        Option option= PieChart.pieStyle(color0,color1,scoreName,percentage);
        return new ResponseEntity(option, HttpStatus.OK);
    }

    @PostMapping(value="/itemTypePieQty")
    @Log("物料类型比列数量")
    @ApiOperation("物料类型比列数量")
    @AnonymousAccess
    public ResponseEntity<Object> itemTypePieQty(String itemType) {
        String invCount=pointServiceImpl.getInvCoutInfo(itemType);
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("data",invCount);
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @PostMapping(value="/areaInOutDay")
    @Log("库区出入库")
    @ApiOperation("库区出入库")
    @AnonymousAccess
    public ResponseEntity<Object> areaInOutDay(String type,String areaType) {
        List<Object[]> ts=taskServiceImpl.queryAreaDay(type,areaType);
        String invCount="0";
        if(ts.size()>0){
            Object[] obj=ts.get(0);
            if(obj[0]!=null&&obj[1]!=null) {
                invCount = obj[0] + "车" + obj[1];
            }
        }
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("data",invCount);
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @PostMapping(value="/locLayoutOld")
    @Log("库位布局-旧")
    @ApiOperation("库位布局-旧")
    @AnonymousAccess
    public ResponseEntity<Object> locLayoutOld() {
        List<RLocLayout> ls=pointServiceImpl.queryLayout("QHT",0,0);
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("width",2.6);
        map.put("height",1.95);
        map.put("mg_top",2);
        map.put("mg_left",1);
//        map.put("positionDistin",3.1);

        HashMap<String,Object> seatList=new HashMap<String,Object>();
        int type0=0;
        int type1=0;
        int type2=0;
        if(ls.size()>0){
            int colView=24;//每行显示的库位数
            int kbc=3;//4个库位一个空白格子
            int kbr=14;
            int kbcol=(int)Math.ceil(colView*1.0/kbc)-1;
            int row=(int)Math.ceil(ls.size()*1.0/colView);
            int v=0;
            int kbrow=(int)Math.ceil(row*1.0/kbr)-1;
            int coutRow=row+kbrow;
            JSONArray seat=new JSONArray();
            HashMap<String,Integer> itemTypes=new HashMap<String,Integer>();
            for(int i=1;i<=coutRow;i++) {
                for(int j=1;j<=colView+kbcol;j++){
                    HashMap<String,Object> d=new HashMap<String,Object>();
                    if((j%(kbc+1)==0)||(i%(kbr+1)==0)){
                        if((i%(kbr+1)==0)) {
                        d.put("id",v);
                        d.put("gRow",i);
                        d.put("gCol",j);
                        int type=3;
//                        if((j%(kbc+1)==0)&&(i%(kbr+1)==0)){
//                            type=5;
//                        }else if(i%(kbr+1)==0){
//                            type=3;
//                        }else if(j%(kbc+1)==0){
//                            type=4;
//                        }
                        d.put("type", type);//3横4竖5交叉
                        d.put("code","");
                        seat.add(d);
                        }
                    }else {
                        RLocLayout layout = ls.get(v++);
                        d.put("id", layout.getId());
                        d.put("gRow", i);
                        d.put("gCol", j);
                        d.put("type", layout.getType());
                        d.put("code", layout.getCode());
                        if (layout.getType() == 0) {
                            type0++;
                        } else if (layout.getType() == 1) {
                            type1++;
                        } else if (layout.getType() == 2) {
                            type2++;
                        }
                        String itemType= layout.getItemType();
                        if(itemType!=null) {
                            if (itemTypes.get(itemType) != null) {
                                itemTypes.put(itemType, itemTypes.get(itemType) + 1);
                            } else {
                                itemTypes.put(itemType, 1);
                            }
                        }
                        seat.add(d);
                        if (ls.size() <= v) {
                            break;
                        }
                    }
                }
            }
            String mes=itemTypes.toString();
            mes=mes.replace("{"," ").replace("}"," ");
            map.put("movieName","前后桶  "+ls.size()+"库位(空点:"+type0+",满车:"+type1+",禁用:"+type2+")"+mes);
            map.put("seatList",seat);
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @PostMapping(value="/inOutData")
    @Log("出入库数据")
    @ApiOperation("出入库数据")
    @AnonymousAccess
    public ResponseEntity<Object> inOutData() {
        HashMap<String,Object> data=new HashMap<String,Object>();
        //获取当天的开始时间
        DateTime beginOfDay = cn.hutool.core.date.DateUtil.beginOfDay(new Date());
        //获取当月的开始时间
        //出库
        //已经完成
        data.put("outZX",pickDetailRepository.queryOutQHData(beginOfDay.toString(), "PICK_ALL").size());
        //待执行
        data.put("outDZX",pickDetailRepository.queryOutQHData(beginOfDay.toString(), "ALLOCATE").size());
        //全部任务
        data.put("outAll",pickDetailRepository.queryOutQHAllData(beginOfDay.toString()).size());
        //入库
        //已完成 + 已取消
        data.put("inZX",taskRepository.queryInQHData(beginOfDay.toString(),"FINISH").size()+taskRepository.queryInQHData(beginOfDay.toString(),"CANCEL").size());
        //已执行的
        data.put("inDZX",taskRepository.queryInQHData(beginOfDay.toString(),"OPEN").size());
        //全部任务
        data.put("inAll",taskRepository.queryInQHALLData(beginOfDay.toString()).size());
        return new ResponseEntity(data, HttpStatus.OK);
    }
}
