package com.youchain.appupdate.rest;

import cn.hutool.json.JSONArray;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.businessdata.service.impl.*;
import com.youchain.report_data.utils.PieChart;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


@RestController
@RequiredArgsConstructor
@Api(tags = "美的大屏数据显示")
@RequestMapping("/api/screenMdView")
@Slf4j
public class ScreenMdController {
    private final ScreenMdServiceImpl screenMdImpl;
    @PostMapping(value="/locLayout")
    @Log("库位布局")
    @ApiOperation("库位布局")
    @AnonymousAccess
    public ResponseEntity<Object> locLayout(String zoneType) {
        //0空1满2禁用3空车区满
        if(zoneType==null){
            zoneType="MD";
        }
        List<Object[]> xys=screenMdImpl.getLocXy(zoneType);
        List<Object[]> objs=screenMdImpl.queryLayout(zoneType);
        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(objs.size()>0){
            JSONArray seat=new JSONArray();
            Object[] xy=xys.get(0);
            HashMap<String,Integer> itemTypes=new HashMap<String,Integer>();
            for(Object[] obj:objs){
                double minX=Double.parseDouble(xy[0].toString());
                double minY=Double.parseDouble(xy[1].toString());
                double maxX=Double.parseDouble(xy[2].toString());
                double maxY=Double.parseDouble(xy[3].toString());
                HashMap<String,Object> d=new HashMap<String,Object>();
                Object id=obj[0];
                Object code=obj[1];
                double gRow=Double.parseDouble(obj[2].toString());
                double gCol=Double.parseDouble(obj[3].toString());
                Object barCode=obj[4];
                Object lot=obj[5];
                Object disabled=obj[6];
                String inv_status=obj[7]==null?"":obj[7].toString();
                Object loc_status=obj[8];
                String type="4";//1禁用，2满料，3AGV未送物料占用，4 空闲，5空车占用
                //禁用（红色，白×），有物料（绿，白车），4，空（紫，百空），空占用（紫，橙空）
                if(disabled.toString().equals("true")){
                    type="1";
                }else if(loc_status.equals("OCCUPY")&&inv_status.equals("OPEN")){
                    type="2";
                }else if(loc_status.equals("OCCUPY")&&inv_status.equals("ZY")){
                    type="3";
                }else if(loc_status.equals("FREE")&&inv_status.equals("")){
                    type="4";
                }else if(loc_status.equals("OCCUPY")&&inv_status.equals("")){
                    type="5";
                }
                d.put("id", obj[0]+"");
//                d.put("gRow",Math.abs((int)Math.ceil(gCol-maxY)));
//                d.put("gCol",(int)Math.ceil(gRow-minX));
                //库位靠左考上显示
                gRow=gRow-minX;
                gCol=gCol-minY;
                //重新计算最大最小XY
                maxX=maxX-minX;
                minX=0d;
                maxY=maxY-minY;
                minY=0;

                double gRow2=gCol;
                double gCol2=gRow;

                double maxX2=maxY;
                double maxY2 =maxX;


                d.put("gRow",Math.abs(gRow2-maxX2));
                d.put("gCol",gCol2);
                d.put("type", type);
                d.put("lot", lot);
                d.put("code", code);
                d.put("item_code", barCode);
                seat.add(d);
            }
            String movieName="重庆美的点位布局图";
            map.put("movieName",movieName);//+mes);
            map.put("seatList",seat);
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @PostMapping(value="/locTypePie")
    @Log("库位比列")
    @ApiOperation("库位比列")
    @AnonymousAccess
    public ResponseEntity<Object> locTypePie(String type) {
        //1禁用，2满料，3AGV未送物料占用，4 空闲，5空车占用
        DecimalFormat df = new DecimalFormat("#.0");
        int locCount=screenMdImpl.getLocCount(type);
        int sjCount=screenMdImpl.getLocStatusCount(type);
        String color0="#ededed";
        String color1="#FF0000";
        String scoreName="SCORE";
        double percentage=sjCount*100*0.1*10/locCount;
//        percentage=Math.round(percentage);
        percentage=Double.parseDouble(df.format(percentage));
        if(type.equals("1")){
//            color0="#04CDE6";
            color1="#0CC8E6";
            color1="#021334FF";
        }else if(type.equals("2")){
//            color0="#73E239";
            color1="#35B45E";
            color1="#021334FF";
        }else if(type.equals("3")){
//            color0="#FEBA6B";
            color1="#F29961";
            color1="#021334FF";
        }else if(type.equals("4")){
//            color0="#E15E68";
            color1="#E53240";
            color1="#021334FF";
        }else if(type.equals("5")){
//            color0="#A682E6";
            color1="#6244E6";
            color1="#021334FF";
        }
        Option option= PieChart.pieStyle(color0,color1,scoreName,percentage);
        return new ResponseEntity(option, HttpStatus.OK);
    }

    @PostMapping(value="/locTypePieQty")
    @Log("库位比列数量")
    @ApiOperation("库位比列数量")
    @AnonymousAccess
    public ResponseEntity<Object> locTypePieQty(String type) {
        int sjCount=screenMdImpl.getLocStatusCount(type);
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("data",sjCount);
        return new ResponseEntity(map, HttpStatus.OK);
    }
}
