package com.sy.coinage.workshop.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sy.coinage.core.annotation.LoginNotRequired;
import com.sy.coinage.core.constants.CacheKey;
import com.sy.coinage.core.redis.RedisService;
import com.sy.coinage.core.util.DateUtils;

import com.sy.coinage.core.util.RandomUtil;
import com.sy.coinage.sys.model.Dept;
import com.sy.coinage.sys.service.IDeptService;
import com.sy.coinage.sys.service.IDictService;
import com.sy.coinage.workshop.model.*;
import com.sy.coinage.workshop.service.*;
import com.sy.coinage.workshop.vo.ScreenDeptVO;
import com.sy.coinage.workshop.vo.ScreenProportionVO;
import com.sy.coinage.workshop.vo.ScreenVO;
import com.sy.coinage.workshop.vo.ScreenWareVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(value = "大屏展示数据接口", tags = {"大屏展示数据接口"})
@RestController
@RequestMapping(value = "/screen")
public class ScreenController {

    @Autowired
    private IDeliveryPlanService deliveryPlanService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private IStockService stockService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IDeptService deptService;

    @ApiOperation(value = "大屏展示数据接口")
    @GetMapping("/get")
    @LoginNotRequired
    public ScreenVO get() {
        ScreenVO data = new ScreenVO();
        // 品名当日出入库信息
        if (redisService.hasKey("screen_day")) {
            String json = redisService.get("screen_day");
            List<ScreenWareVO> l = JSON.parseArray(json, ScreenWareVO.class);
            data.setDayList(l);
        }
        // 部门产能
        data.setDeptInfo(this.getDeptInfo());
        // 解缴任务信息
        data.setPlanList(this.getDeliveryPlanList());
        // 库存占比
        if (redisService.hasKey("screen_proportion")) {
            String json = redisService.get("screen_proportion");
            ScreenProportionVO vo = JSON.parseObject(json, ScreenProportionVO.class);
            data.setProportionInfo(vo);
        }
        // 出入库完成率
        if (redisService.hasKey("screen_securities")) {
            String json = redisService.get("screen_securities");
            ScreenDeptVO vo = JSON.parseObject(json, ScreenDeptVO.class);
            data.setSecuritiesInfo(vo);
        }
        // 总库当日出入库信息
        if (redisService.hasKey("screen_ware")) {
            String json = redisService.get("screen_ware");
            List<ScreenWareVO> l = JSON.parseArray(json, ScreenWareVO.class);
            if (l.size() < 5) {
                this.addNullWare(l, l.size());
            }
            data.setWareList(l);
        } else {
            List<ScreenWareVO> l = new ArrayList<>();
            this.addNullWare(l, 0);
        }
        return data;
    }

    private void addNullWare(List<ScreenWareVO> l, int size) {
        ScreenWareVO vo = new ScreenWareVO();
        vo.setWareCn("-");
        vo.setSecurities("-");
        vo.setQty(0);
        vo.setDayInQty(0);
        vo.setDayOutQty(0);
        for (int i = 0; i < 5 - size; i++) {
            l.add(vo);
        }
    }

    private static String[] dept = {"造币一部", "造币二部", "造币三部"};

    private ScreenDeptVO getDeptInfo() {
        Dept d = deptService.getByName("造币二部");
        ScreenDeptVO data = new ScreenDeptVO();
        // 柱图最大值
        data.setMax(1000);
        data.setName(Arrays.asList(this.dept));
        // 产能开始
        List<Double> production = new ArrayList<>();
        production.add(0D);
        String yield = this.redisService.get(CacheKey.DEPT_YIELD + d.getName());
        if (StringUtils.isNotBlank(yield) && !"null".equals(yield)) {
            production.add(Double.parseDouble(yield));
        } else {
            production.add(0D);
        }
        production.add(0D);
        data.setProduction(production);
        // 产能结束
        // 库存开始
        List<Double> inventory = new ArrayList<>();
        inventory.add(0D);
        Double deptInventory = this.getDeptInventory(Integer.parseInt(d.getRemark()));
        inventory.add(deptInventory);
        inventory.add(0D);
        data.setInventory(inventory);
        // 库存结束
        return data;
    }


    private List<List<String>> getDeliveryPlanList() {
        LambdaQueryWrapper<DeliveryPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(DeliveryPlan::getPlanDeliveryDate, DateUtils.getBeginDayOfWeek(), DateUtils.getEndDayOfWeek());
        queryWrapper.orderByAsc(DeliveryPlan::getSecurities);
        Page<DeliveryPlan> page = new Page<>(1, 20);
        List<DeliveryPlan> data = this.deliveryPlanService.list(page, queryWrapper);
        List<List<String>> list = new ArrayList<>();
        if (data != null && data.size() > 0) {
            for (DeliveryPlan d : data) {
                List<String> vo = new ArrayList<>();
                vo.add(DateUtils.toString(d.getPlanDeliveryDate()));
                vo.add(d.getSecurities());
                vo.add(String.valueOf(d.getTotalBoxNum()));
                vo.add(d.getCrownBetween());
                vo.add(d.getParBetween().replaceAll("<span class='red'>", "").replaceAll("</span>", ""));
                vo.add(d.getAddressCn());
                list.add(vo);
            }
        }
        return list;
    }

    private Double getDeptInventory(Integer wareId) {
        Double resp = 0D;
        LambdaQueryWrapper<Stock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Stock::getWareId, wareId);
        List<Stock> list = this.stockService.list(queryWrapper);
        if (list != null && list.size() > 0) {
            for (Stock stock : list) {
                if (stock.getBoxNum() != null) {
                    resp = resp + stock.getBoxNum();
                }
                if (stock.getRacketNum() != null) {
                    Product p = productService.getByCache(stock.getSecuritiesId());
                    resp = resp + stock.getRacketNum() * p.getBoxAmount();
                }
            }
        }
        return resp;
    }


}
