package com.securitycontrol.screen.service.impl;

import com.securitycontrol.common.core.constant.Constant;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.entity.screen.dto.AccessMgeDto;
import com.securitycontrol.entity.screen.vo.AccessMgeVo;
import com.securitycontrol.screen.mapper.AccessMgeMapper;
import com.securitycontrol.screen.service.AccessMgeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 出入管理
 *
 * @author lsun
 */
@Slf4j
@Service(value = "AccessMgeService")
public class AccessMgeServiceImpl implements AccessMgeService {
    @Resource(name = "AccessMgeMapper")
    private AccessMgeMapper mapper;

    @Override
    public AjaxResult getVehicleStatistics(AccessMgeDto dto) {
        return totalAccessData("1",dto);
    }

    @Override
    public AjaxResult getVehicleStatisticsPhoto(AccessMgeDto dto) {
        List<AccessMgeVo> list = new ArrayList<>();
        try {
            list = mapper.getVehicleStatisticsPhoto(dto);
        } catch (Exception e) {
            log.error("今日、当月车辆出入场统计图片-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public List<AccessMgeVo> getVehicleStatisticsList(AccessMgeDto dto) {
        List<AccessMgeVo> list = new ArrayList<>();
        list = getOutAndInRecord("1", dto);
        return list;
    }

    public AccessMgeVo setAccessData(AccessMgeVo vo, String[] valArr) {
        for (String str : valArr) {
            String[] splitStr = str.split("@");
            if (Objects.equals(splitStr[0], Constant.ACCESS_TIME)) {
                vo.setAccesssTime(splitStr[1]);
            } else if (Objects.equals(splitStr[0], Constant.ACCESS_TYPE)) {
                vo.setAccessType(splitStr[1]);
            }
        }
        return vo;
    }

    @Override
    public AjaxResult getPerStatistics(AccessMgeDto dto) {
        return totalAccessData("2",dto);
    }

    @Override
    public AjaxResult getPerStatisticsPhoto(AccessMgeDto dto) {
        List<AccessMgeVo> list = new ArrayList<>();
        try {
            list = mapper.getPerStatisticsPhoto(dto);
        } catch (Exception e) {
            log.error("今日、当月人员出入场统计图片-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public List<AccessMgeVo> getPerStatisticsList(AccessMgeDto dto) {
        List<AccessMgeVo> list = new ArrayList<>();
        list = getOutAndInRecord("2", dto);
        return list;
    }

    /**
     * 人员/车辆 出入场记录
     * @return List<AccessMgeVo>
     * @description
     * @author cwchen
     * @date 2024/4/19 13:33
     */
    public List<AccessMgeVo> getOutAndInRecord(String type, AccessMgeDto dto) {
        // type ： 1.车辆 2.人员
        List<AccessMgeVo> list = new ArrayList<>();
        try {
            dto.setTypeCode(Constant.PERSON_CAR_TYPE);
            dto.setModeName(Objects.equals(Constant.TYPE, type) ? Constant.CAR : Constant.PERSON);
            list = Objects.equals(Constant.TYPE, type) ? mapper.getVehicleStatisticsList(dto) : mapper.getPerStatisticsList(dto);
            for (AccessMgeVo accessMgeVo : list) {
                String basicInfo = Objects.equals(Constant.TYPE, type) ? mapper.getCarBasicInfo(accessMgeVo.getCarNum()) : mapper.getPeopleBasicInfo(accessMgeVo.getUserId());
                String data = mapper.getAccessLists(accessMgeVo.getSourceType());
                if (StringUtils.isNotBlank(basicInfo)) {
                    String[] split = basicInfo.split("@");
                    accessMgeVo.setUserName(split[0]);
                    accessMgeVo.setPhone(split[1]);
                }
                if (StringUtils.isNotEmpty(data)) {
                    String[] dataArr = data.split(",");
                    setAccessData(accessMgeVo, dataArr);
                }
            }
        } catch (Exception e) {
            log.error(Objects.equals(Constant.TYPE, type) ? "获取车辆出入场记录" : "获取人员出入场记录", e);
        }
        return list;
    }

    public AjaxResult totalAccessData(String type, AccessMgeDto dto){
        System.err.println(type);
        // type ： 1.车辆 2.人员
        List<AccessMgeVo> dataList = new ArrayList<>();
        List<AccessMgeVo> list = new ArrayList<>();
        try {
            dto.setTypeCode(Constant.PERSON_CAR_TYPE);
            dto.setModeName(Objects.equals(Constant.TYPE, type) ? Constant.CAR : Constant.PERSON);
            String[] dateArr = DateTimeHelper.getMonthFirstAndLastDay().split(" - ");
            dto.setStartDate(dateArr[0]);
            dto.setEndDate(dateArr[1]);
            AccessMgeVo vo = new AccessMgeVo();
            list = Objects.equals(Constant.TYPE, type) ? mapper.getVehicleStatisticsList(dto) : mapper.getPerStatisticsList(dto);
            for (AccessMgeVo accessMgeVo : list) {
                String data = mapper.getAccessLists(accessMgeVo.getSourceType());
                if (StringUtils.isNotEmpty(data)) {
                    String[] dataArr = data.split(",");
                    setAccessData(accessMgeVo, dataArr);
                }
            }
            // 今日入 今日出 当月入 当月出
            int jtrNum = 0, jrcNum = 0, dyrNum = 0, dycNum = 0;
            for (AccessMgeVo mgeVo : list) {
                if (Objects.equals(mgeVo.getAccessType(), Constant.ACCESS_TYPE_IN)) {
                    dyrNum++;
                    if (mgeVo.getAccesssTime().contains(DateTimeHelper.getNowDate())) {
                        jtrNum++;
                    }
                } else if (Objects.equals(mgeVo.getAccessType(), Constant.ACCESS_TYPE_OUT)) {
                    dycNum++;
                    if (mgeVo.getAccesssTime().contains(DateTimeHelper.getNowDate())) {
                        jrcNum++;
                    }
                }
            }
            vo.setJtrNum(jtrNum + "");
            vo.setJrcNum(jrcNum + "");
            vo.setDyrNum(dyrNum + "");
            vo.setDycNum(dycNum + "");
            dataList.add(vo);
        } catch (Exception e) {
            log.error(Objects.equals(Constant.TYPE, type) ? "统计车辆出入场数量" : "统计人员出入场数量", e);
        }
        return AjaxResult.success(dataList);
    }
}
