package com.youchain.appupdate.rest;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONArray;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.businessdata.repository.PickDetailRepository;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * @author 92525
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "大屏数据显示")
@RequestMapping("/api/screeView")
@Slf4j
public class ScreenViewController {
    private final AsnDetailServiceImpl asnDetailServiceImpl;
    private final PickDetailServiceImpl pickDetailServiceImpl;
    private final TaskServiceImpl taskServiceImpl;
    private final InventoryServiceImpl inventoryServiceImpl;
    private final PickDetailRepository pickDetailRepository;
    private final TaskService taskService;

    @PostMapping(value = "/outForDay")
    @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 = "/inventoryView")
    @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);
    }

    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 = "/inOutData")
    @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());
        return new ResponseEntity(data, HttpStatus.OK);
    }
}
