package cn.iocoder.yudao.module.identity.controller.app.scanrecord;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.ip.IPAddressUtils;
import cn.iocoder.yudao.framework.ip.core.utils.IPUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.identity.controller.admin.scanrecord.vo.ScanRecordRespVO;
import cn.iocoder.yudao.module.identity.controller.app.scanrecord.vo.FirstScanRecordRespVO;
import cn.iocoder.yudao.module.identity.controller.app.scanrecord.vo.ScanRecordAppPageReqVO;
import cn.iocoder.yudao.module.identity.convert.scanrecord.ScanRecordConvert;
import cn.iocoder.yudao.module.identity.dal.dataobject.scanrecord.ScanRecordDO;
import cn.iocoder.yudao.module.identity.service.scanrecord.ScanRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "APP移动端 - 扫码记录")
@RestController
@RequestMapping("/identity/scan-record")
@Validated
@Slf4j
public class ScanRecordAppController {

    @Resource
    private ScanRecordService scanRecordService;

    @GetMapping("/page")
    @PermitAll
    @ApiOperation("获得扫码记录分页")
    public CommonResult<PageResult<ScanRecordRespVO>> getRecordListForApp(@Valid ScanRecordAppPageReqVO pageReqVO) {
        PageResult<ScanRecordDO> pageResult = scanRecordService.getRecordPageForApp(pageReqVO);
        return CommonResult.success(ScanRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/createScanRecord")
    @PermitAll
    @ApiOperation("创建扫码记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identityCode", value = "标识码内容", required = true, example = "399403089039626147", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "类型", required = true, example = "WD5502TT", dataTypeClass = String.class),
            @ApiImplicitParam(name = "m", value = "查询是否断开的m值", required = true, example = "4177C6C7DDA8DA1FC14945C6C90C2F7800", dataTypeClass = String.class),
            @ApiImplicitParam(name = "mobileSystemInfo", value = "手机系统信息", required = true, example = "{}", dataTypeClass = String.class)
    })
    @OperateLog(enable = false) // 避免 请求被记录操作日志
    public CommonResult<Boolean> createScanRecord(@RequestParam("identityCode") String identityCode,
            @RequestParam(value = "type", required = false) String type, @RequestParam(value = "m", required = false) String m,
            @RequestParam(value = "mobileSystemInfo", required = false) String mobileSystemInfo,
            HttpServletRequest request) {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        log.info("创建扫码记录：identityCode:{}, type:{}, m:{}", identityCode, type, m);

        String region = null;
        String ip = IPAddressUtils.getIpAdrress(request);
        log.info("创建扫码记录：从request中获得的ip:{}", ip);
        if(ip != null) {
            try {
                region = IPUtils.search(ip);
            } catch (Exception e) {
                log.error("解析IP地址错误: ", e);
            }
        }
        log.info("获得的地址region:" + region);
        // 这里不控制插入频率  由前端控制
        scanRecordService.createByApp(identityCode, ip, region, type, m, mobileSystemInfo);
        return CommonResult.success(Boolean.TRUE);
    }

    @GetMapping("/getFirstScanInfoByUrlAPP")
    @PermitAll
    @ApiOperation("根据identityUrl 获取首次的扫码时间与首次扫码地点")
    @ApiImplicitParam(name = "identityUrl", value = "二维码url", required = true, example = "399403089039626147", dataTypeClass = String.class)
    public CommonResult<ScanRecordDO> getFirstScanInfoByUrl(@RequestParam("identityUrl") String identityUrl) {
        ScanRecordDO firstScanInfoByUrl = scanRecordService.getFirstScanInfoByUrlApp(identityUrl);
        return success(firstScanInfoByUrl);
    }

    @GetMapping("/getLastScanInfoByUrl")
    @PermitAll
    @ApiOperation("根据identityUrl 获取首次的扫码时间与首次扫码地点")
    @ApiImplicitParam(name = "identityUrl", value = "二维码url", required = true, example = "399403089039626147", dataTypeClass = String.class)
    public CommonResult<FirstScanRecordRespVO> getLastScanInfoByUrl(@RequestParam("identityUrl") String identityUrl) {
        return success(scanRecordService.getLastScanInfoByUrl(identityUrl));
    }

}
