package com.mortals.xhx.busiz.web;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.MD5Util;
import com.mortals.framework.util.SecurityUtil;
import com.mortals.xhx.module.face.service.FacePlanService;
import com.mortals.xhx.module.hik.door.model.req.door.DoorEventReq;
import com.mortals.xhx.module.hik.door.model.rsp.door.DoorEventDataInfo;
import com.mortals.xhx.module.hik.door.service.IHikDoorService;
import com.mortals.xhx.module.hik.event.model.rsp.EventInfo;
import com.mortals.xhx.module.hik.event.service.IHikEventService;
import com.mortals.xhx.module.hik.face.model.rsp.plan.PlanRecognInfo;
import com.mortals.xhx.module.hik.person.model.req.org.OrgListReq;
import com.mortals.xhx.module.hik.person.model.req.person.PersonReq;
import com.mortals.xhx.module.hik.person.model.rsp.org.OrgDataInfo;
import com.mortals.xhx.module.hik.person.model.rsp.person.PersonDataInfo;
import com.mortals.xhx.module.hik.person.service.IHikPersonService;
import com.mortals.xhx.module.realtime.model.RealtimeDataflowStatEntity;
import com.mortals.xhx.module.realtime.model.RealtimeDataflowStatQuery;
import com.mortals.xhx.module.realtime.service.RealtimeDataflowStatService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 设备
 *
 * @author: zxfei
 * @date: 2021/8/24 20:28
 */
@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {

    @Autowired
    private IHikPersonService hikPersonService;
    @Autowired
    private IHikDoorService hikDoorService;
    @Autowired
    private IHikEventService hikEventService;
    @Autowired
    private FacePlanService facePlanService;
    @Autowired
    private RealtimeDataflowStatService realtimeDataflowStatService;


    @GetMapping("webservice")
    public String webservice() {
        log.info("测试");
        return "ok";
    }

    @PostMapping("getPersonList")
    public String getPersonList(@RequestBody PersonReq personReq) {
        Rest<PersonDataInfo> personList = hikPersonService.getPersonList(personReq);
        return JSON.toJSONString(personList);
    }

    @PostMapping("getOrgList")
    public String getOrgList(@RequestBody OrgListReq orgListReq) {
        Rest<OrgDataInfo> orgList = hikPersonService.getOrgList(orgListReq);
        return JSON.toJSONString(orgList);
    }

    @PostMapping("getDoorEvents")
    public String getDoorEvents(@RequestBody DoorEventReq doorEventReq) {
        Rest<DoorEventDataInfo> doorEvents = hikDoorService.getDoorEvents(doorEventReq);
        return JSON.toJSONString(doorEvents);
    }

    @PostMapping("getEvents")
    public String getEvents() {
        Rest<List<EventInfo>> events = hikEventService.getEvents();
        return JSON.toJSONString(events);
    }


    @PostMapping("addStrangePlan")
    public String addStrangePlan() {
        Rest<List<PlanRecognInfo>> strangerPlanByDay = facePlanService.createStrangerPlanByDay();
        return JSON.toJSONString(strangerPlanByDay);
    }


    @PostMapping("mockStatData")
    public String mockStatData() {
        ArrayList<RealtimeDataflowStatEntity> statEntities = new ArrayList<>();
        //模拟构造过去人流数据
        DateTime startDateTime = DateUtil.parseDate("2023-03-01");
        DateTime endDateTime = DateUtil.parseDate("2023-05-10");
        Integer subDay = Integer.parseInt(String.valueOf(DateUtil.betweenDay(startDateTime, endDateTime, false)));


        for (int i = 0; i <= subDay; i++) {
            DateTime date = DateUtil.offsetDay(startDateTime, i);
            boolean weekend = DateUtil.isWeekend(date);
            if (weekend) {
                //周末 默认数据
                for (int j = 0; j <= 23; j++) {
                    RealtimeDataflowStatEntity statEntity = new RealtimeDataflowStatEntity();
                    statEntity.initAttrValue();
                    statEntity.setSiteId(1L);
                    statEntity.setSiteName("宜宾市民中心");
                    int year = DateUtil.year(date);
                    int month = DateUtil.month(date) + 1;
                    int day = DateUtil.dayOfMonth(date);
                    statEntity.setYear(year);
                    statEntity.setMonth(month);
                    statEntity.setDay(day);
                    statEntity.setHour(j);
                    statEntity.setCreateUserId(1L);
                    statEntity.setCreateTime(DateUtil.offsetHour(date, j));
                    statEntities.add(statEntity);
                }
            } else {
                for (int j = 0; j <= 23; j++) {
                    RealtimeDataflowStatEntity statEntity = new RealtimeDataflowStatEntity();
                    statEntity.initAttrValue();
                    statEntity.setSiteId(1L);
                    statEntity.setSiteName("宜宾市民中心");
                    if(j>8&&j<18){
                        //随机500~1200人
                        int total = RandomUtil.randomInt(400, 1200);
                        statEntity.setPersonSum(total);
                        int strangeNum = RandomUtil.randomInt(380, total);
                        statEntity.setRecoginzeSum(total - strangeNum);
                        statEntity.setStrangerSum(strangeNum);
                    }
                    int year = DateUtil.year(date);
                    int month = DateUtil.month(date) + 1;
                    int day = DateUtil.dayOfMonth(date);
                    statEntity.setYear(year);
                    statEntity.setMonth(month);
                    statEntity.setDay(day);
                    statEntity.setHour(j);
                    statEntity.setCreateUserId(1L);

                    statEntity.setCreateTime(DateUtil.offsetHour(date, j));
                    statEntities.add(statEntity);
                }
            }
        }

        log.info("新增数量：{}", statEntities.size());
        realtimeDataflowStatService.save(statEntities);

        return "ok";
    }


    public static void main(String[] args) throws Exception {

        String str="admin";
        System.out.println(SecurityUtil.md5DoubleEncoding(str));

        byte[] md5s = SecurityUtil.getDigest(str, "MD5");
        byte[] md5s1 = SecurityUtil.getDigest(md5s, "MD5");

        System.out.println(new String(md5s1));

/*
        String md5D = MD5Util.strToMd5(MD5Util.ge(str));
        System.out.println(md5D);
        String s = SecurityUtil.encodeHex(md5D.getBytes());
        System.out.println(s);
*/


        System.out.println(HexUtil.encodeHex(md5s1));



    }


}
