package com.zcloud.controller.integratedmanagement.access;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.integratedmanagement.access.GateCarRecordService;
import com.zcloud.service.integratedmanagement.access.GateEntryExitService;
import com.zcloud.service.integratedmanagement.access.GatePeopleRecordService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.ReturnMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>GateMachineAreaController 类是系统中的一个控制器类，它继承自 BaseController。</p>
 * <p>主要职责：处理与门禁机器进出记录相关的业务逻辑操作，提供API接口给前端或其他服务调用。</p>
 *
 * @author [王轩]
 * @since [24-05-28]
 */
@Controller
@RequestMapping("/gateMachineEntryExitLog")
public class GateMachineEntryExitLogController extends BaseController {


    @Resource
    private GateEntryExitService gateEntryExitService;

    @Resource
    private GatePeopleRecordService gatePeopleRecordService;

    @Resource
    private GateCarRecordService gateCarRecordService;

    /**
     * 获取人进出记录
     * <p>
     * 该接口用于人进出记录的相关列表数据，实现了分页查询功能。
     *
     * @param page 分页参数，包含当前页码和每页显示数量。
     * @return 返回一个包含列表信息的Map对象，具体结构取决于实现逻辑。
     */
    @RequestMapping(value = "peoList")
    @ResponseBody
    @LogAnno(menuType = "综合管理", menuServer = "门禁管理", instructionsOperate = "人进出记录", instructionsType = "列表")
    public Object peoList(Page page) throws Exception {
        // 返回一个表示操作成功的Map对象，具体结构需根据实际情况定义
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        pageData.put("STARTTIME", pageData.getString("dates[0]"));
        pageData.put("ENDTIME", pageData.getString("dates[1]"));
        page.setPd(pageData);
        List<PageData> varList = gatePeopleRecordService.list(page);
        return ReturnMap.ok().put("page", page).put("varList", varList);
    }

    /**
     * 获取车进出记录
     * <p>
     * 该接口用于人进出记录的相关列表数据，实现了分页查询功能。
     *
     * @param page 分页参数，包含当前页码和每页显示数量。
     * @return 返回一个包含列表信息的Map对象，具体结构取决于实现逻辑。
     */
    @RequestMapping(value = "carList")
    @ResponseBody
    @LogAnno(menuType = "综合管理", menuServer = "门禁管理", instructionsOperate = "车进出记录", instructionsType = "列表")
    public Object carList(Page page) throws Exception {
        // 返回一个表示操作成功的Map对象，具体结构需根据实际情况定义
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        page.setPd(pageData);
        List<PageData> varList = gateCarRecordService.list(page);
        return ReturnMap.ok().put("page", page).put("varList", varList);
    }


    /**
     * 根据企业查询 闸机情况
     * <p>
     * 该接口用于人进出记录的相关列表数据，实现了分页查询功能。
     *
     * @return 返回一个包含列表信息的Map对象，具体结构取决于实现逻辑。
     */
    @RequestMapping(value = "getGateMachineCountByCid")
    @ResponseBody
    public Object getGateMachineCountByCid() throws Exception {
        // 返回一个表示操作成功的Map对象，具体结构需根据实际情况定义
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        return ReturnMap.ok().put("varList", gateEntryExitService.getGateMachineCountByCid(pageData));
    }


    @RequestMapping(value = "getBIEntryExitPeoLogList")
    @ResponseBody
    @LogAnno(menuType = "综合管理", menuServer = "门禁管理", instructionsOperate = "人进出记录", instructionsType = "列表")
    public Object getBIEntryExitPeoLogList(Page page) throws Exception {
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        page.setPd(pageData);
        List<PageData> varList = gatePeopleRecordService.getBIEntryExitPeoLogList(page);
        return ReturnMap.ok().put("page", page).put("varList", varList);
    }

    @RequestMapping(value = "getBIEntryExitCarLogList")
    @ResponseBody
    @LogAnno(menuType = "综合管理", menuServer = "门禁管理", instructionsOperate = "车进出记录", instructionsType = "列表")
    public Object getBIEntryExitCarLogList(Page page) throws Exception {
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        page.setPd(pageData);
        List<PageData> varList = gatePeopleRecordService.getBIEntryExitCarLogList(page);
        return ReturnMap.ok().put("page", page).put("varList", varList);
    }

    @RequestMapping(value = "getBIPeopleFieldCondition")
    @ResponseBody
    @LogAnno(menuType = "BI", menuServer = "门口门禁", instructionsOperate = "人进出记录", instructionsType = "统计")
    public Object getBIPeopleFieldCondition() throws Exception {
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        List<PageData> varList = gatePeopleRecordService.getBIPeopleFieldCondition(pageData);
        varList.stream().forEach(data -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String EVENT_TIME = "";
            try {
                Date parse = sdf.parse(data.getString("EVENT_TIME"));
                EVENT_TIME = sdf.format(parse);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            data.put("EVENT_TIME", EVENT_TIME);
        });
        Map<String, Long> collect = varList.stream()
                .filter(data -> StringUtils.equals(data.getString("EVENT_TIME"), DateUtil.getDay())).collect(Collectors.toList())
                .stream().collect(Collectors.groupingBy(data -> data.getString("READER"), Collectors.counting()));
        return ReturnMap.ok().put("varList", collect);
    }

    @RequestMapping(value = "getBICarFieldCondition")
    @ResponseBody
    @LogAnno(menuType = "BI", menuServer = "门口门禁", instructionsOperate = "车进出记录", instructionsType = "统计")
    public Object getBICarFieldCondition() throws Exception {
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        List<PageData> varList = gateCarRecordService.getBICarFieldCondition(pageData);
        varList.stream().forEach(data -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String EVENT_TIME = "";
            try {
                Date parse = sdf.parse(data.getString("EVENT_TIME"));
                EVENT_TIME = sdf.format(parse);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            data.put("EVENT_TIME", EVENT_TIME);
        });
        Map<String, Long> collect = varList.stream()
                .filter(data -> StringUtils.equals(data.getString("EVENT_TIME"), DateUtil.getDay())).collect(Collectors.toList())
                .stream().collect(Collectors.groupingBy(data -> data.getString("READER"), Collectors.counting()));
        return ReturnMap.ok().put("varList", collect);
    }

}
