package cn.iocoder.yudao.module.system.controller.app.facedeviceinfo;

import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.facedeviceinfo.vo.FaceDeviceInfoPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.facedeviceinfo.vo.FaceDeviceInfoRespVO;
import cn.iocoder.yudao.module.system.controller.admin.facedeviceinfo.vo.FaceDeviceInfoSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.facedeviceinfo.FaceDeviceInfoDO;
import cn.iocoder.yudao.module.system.service.facedeviceinfo.FaceDeviceInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;



@Tag(name = "管理后台 - 人脸设备信息关联门店")
@RestController
@RequestMapping("/t/face-device-info")
@Validated
public class AppFaceDeviceInfoController {

    @Resource
    private FaceDeviceInfoService faceDeviceInfoService;

    @Resource
    private StringRedisTemplate clearRedisTemplate;


    @GetMapping("/get")
    @Operation(summary = "获得人脸设备信息关联门店")
    @Parameter(name = "mac", description = "设备mac码", required = true, example = "1024")
    public CommonResult<FaceDeviceInfoRespVO> getFaceDeviceInfo(@RequestParam("mac") String mac) {
        FaceDeviceInfoDO faceDeviceInfo = faceDeviceInfoService.getInfo(mac);
        String today = DateUtil.date().toString("yyyyMMdd");
        FaceDeviceInfoRespVO bean = BeanUtils.toBean(faceDeviceInfo, FaceDeviceInfoRespVO.class);
        String s = clearRedisTemplate.opsForValue().get(today+faceDeviceInfo.getCarteenId());
        if(StringUtils.isNotBlank(s)){
            bean.setClear(false);
        }else {
            bean.setClear(true);
            clearRedisTemplate.opsForValue().set(today+faceDeviceInfo.getCarteenId(),"true");
            String yesterday = DateUtil.format(DateUtil.yesterday(), "yyyyMMdd");
            clearRedisTemplate.delete(yesterday+faceDeviceInfo.getCarteenId());
        }

        return success(bean);
    }


}