package org.dromara.system.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.CountData;
import org.dromara.system.domain.bo.TblCarInfoBo;
import org.dromara.system.domain.vo.CountDataVo;
import org.dromara.system.domain.vo.TblCarInfoVo;
import org.dromara.system.service.ISysDataScopeService;
import org.dromara.system.service.ITblCarInfoService;
import org.dromara.system.service.ITblCountDataService;
import org.dromara.system.service.ITblGoodsOrderService;
import org.dromara.util.MessageUtils;
import org.dromara.util.YingshiHttp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

@Validated
@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/statistic")
public class StatisticController extends TblBaseController {
    private final ITblCountDataService countDataService;

    private final ITblGoodsOrderService goodsOrderService;
    private final ISysDataScopeService dataScopeService;

    private final ITblCarInfoService carInfoService;


    @PostMapping("/passenger/flow")
    public Object sitesFlow(@RequestBody(required = false) Map<String,Object> data) {
        if(data==null) data=new HashMap<>();
        if(data.get("start")==null){
            data.put("start", DateUtil.format(DateUtil.offsetDay(DateUtil.yesterday(), -7),"yyyy-MM-dd"));
        }
        if(data.get("end")==null){
            data.put("end", DateUtil.format(DateUtil.yesterday(),"yyyy-MM-dd"));
        }

       List<DateTime> dateList= DateUtil.rangeToList(DateUtil.parse(data.get("start").toString()), DateUtil.parse(data.get("end").toString()), DateField.DAY_OF_YEAR);

        List<Object> qs = new ArrayList<>();
        dateList.forEach(date->{
            Map<String,Object> temp = new HashMap<>();
            temp.put("start",DateUtil.format(date,"yyyy-MM-dd"));
            temp.put("end",DateUtil.format(DateUtil.offsetDay(date,1),"yyyy-MM-dd"));


            Map<String,Object> d = goodsOrderService.queryByDate(temp);
            if(d==null){
                d=new HashMap<>();
                d.put("ordercount",0);
            }


            CountDataVo countDataVo = countDataService.queryByDate(temp);
            if(countDataVo==null){
                countDataVo = new CountDataVo();countDataVo.setOutcount(0);countDataVo.setIncount(0);
            }
            temp.put("flowCount",countDataVo);
            temp.put("goodsOrderCount",d);
            qs.add(temp);
        });



        return R.ok(qs);
    }
    @PostMapping("/passenger/flow1")
    public Object sitesFlow1(@RequestBody(required = false) Map<String,Object> data) {
        if(data==null) data=new HashMap<>();
        if(data.get("start")==null || data.get("end")==null ){
           return R.fail("参数不对");
        }

        Map<String,Object> temp = new HashMap<>();
        try{
            if(data.get("depts")!=null || data.get("carnum")!=null) {


                TblCarInfoBo bo = new TblCarInfoBo();
                try {
                    if (data.get("depts") != null) {
                        bo.getParams().put("depts", data.get("depts"));
                    }
                    if (data.get("carnum") != null) {
                        bo.setCarNum(data.get("carnum").toString());
                    }
                } catch (Exception e) {

                }
                getDepts(dataScopeService, bo);
                List<String> allcodes = new ArrayList<>();
                List<TblCarInfoVo> carInfos = carInfoService.queryList(bo);
                if (carInfos.size() > 0) {
                    carInfos.forEach(carInfo -> {
                        try {
                            JSONUtil.parseArray(carInfo.getDeviceInfos()).toList(JSONObject.class).forEach(deviceInfo -> {
                                if (deviceInfo.getStr("type").equals("people_count")) {
                                    allcodes.add(StrUtil.format("'{}'", deviceInfo.getStr("code")));
                                }
                            });
                        } catch (Exception e) {
                        }
                    });
                    temp.put("devicecodes", allcodes.stream().collect(Collectors.joining(",")));
                }
            }
        }catch (Exception e){}

        temp.put("start",data.get("start"));
        temp.put("end",data.get("end"));
        CountDataVo countDataVo = countDataService.queryByDate(temp);
        if(countDataVo==null){
            countDataVo = new CountDataVo();countDataVo.setOutcount(0);countDataVo.setIncount(0);
        }
        temp.put("flowCount",countDataVo);
        return R.ok(temp);
    }
}
