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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.identity.controller.admin.scanrecord.vo.*;
import cn.iocoder.yudao.module.identity.controller.app.scanrecord.vo.FirstScanRecordRespVO;
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.ApiOperation;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

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

@Api(tags = "管理后台 - 扫码记录")
@RestController
@RequestMapping("/identity/scan-record")
@Validated
public class ScanRecordController {

    @Resource
    private ScanRecordService scanRecordService;

    @PostMapping("/create")
    @ApiOperation("创建扫码记录")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:create')")
    public CommonResult<Long> createScanRecord(@Valid @RequestBody ScanRecordCreateReqVO createReqVO) {
        return success(scanRecordService.createScanRecord(createReqVO));
    }

    @PostMapping("/createBatch")
    @ApiOperation("创建扫码记录")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:createBatch')")
    public CommonResult<Boolean> createBatchScanRecord(@Valid @RequestBody CreateFirstScanRecordReqVO params) {
        scanRecordService.createBatchScanRecord(params);
        return success(Boolean.TRUE);
    }

    @PutMapping("/update")
    @ApiOperation("更新扫码记录")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:update')")
    public CommonResult<Boolean> updateScanRecord(@Valid @RequestBody ScanRecordUpdateReqVO updateReqVO) {
        scanRecordService.updateScanRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除扫码记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:scan-record:delete')")
    public CommonResult<Boolean> deleteScanRecord(@RequestParam("id") Long id) {
        scanRecordService.deleteScanRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得扫码记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    public CommonResult<ScanRecordRespVO> getScanRecord(@RequestParam("id") Long id) {
        ScanRecordDO scanRecord = scanRecordService.getScanRecord(id);
        return success(ScanRecordConvert.INSTANCE.convert(scanRecord));
    }

    @GetMapping("/list")
    @ApiOperation("获得扫码记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    public CommonResult<List<ScanRecordRespVO>> getScanRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ScanRecordDO> list = scanRecordService.getScanRecordList(ids);
        return success(ScanRecordConvert.INSTANCE.convertList(list));
    }

    /**
     * pageNo=1
     * pageSize=10
     * goodsName=1
     * businessName=1
     * timeHorizon%5B0%5D=2023-03-01%2000%3A00%3A00
     * timeHorizon%5B1%5D=2023-04-27%2023%3A59%3A59
     *
     * @param
     * @return
     */
    @GetMapping("/timeList")
    @ApiOperation("统计时间范围内扫码次数")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    public CommonResult<Integer> getScanRecordTimeList(@RequestParam(value = "goodsName",required = false) String goodsName,
                                                                      @RequestParam(value = "belongBusinessName",required = false) String belongBusinessName,
                                                                      @RequestParam(value = "timeHorizon[0]",required = false) String timeHorizon1,
                                                                      @RequestParam(value = "timeHorizon[1]",required = false) String timeHorizon2) {
        int scanRecordListTist = scanRecordService.getScanRecordCount(goodsName, belongBusinessName, timeHorizon1, timeHorizon2);
        return success(scanRecordListTist);
    }

    @GetMapping("/count")
    @ApiOperation("查询出每日的查询次数")
    @ApiImplicitParam(name = "belongBusinessName", value = "商户名称", required = true, example = "测试商户1", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    public Map<String, Long> getScanCountToday(@RequestParam(value = "goodsName",required = false) String goodsName,
                                               @RequestParam(value = "belongBusinessName",required = false) String belongBusinessName,
                                               @RequestParam(value = "timeHorizon[0]",required = false) String timeHorizon1,
                                               @RequestParam(value = "timeHorizon[1]",required = false) String timeHorizon2) {
        Map<String, Long> scanCountToday = scanRecordService.getScanCountByDay(goodsName, belongBusinessName, timeHorizon1, timeHorizon2);
        return scanCountToday;
    }

    @GetMapping("/regionList")
    @ApiOperation("获得扫码地区列表")
    @ApiImplicitParam(name = "ids", value = "商户id", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    public CommonResult<List<String>> getBusinessRegionList(@RequestParam(value = "goodsName",required = false) Long goodsName,
                                                            @RequestParam(value = "businessName",required = false) Long businessName,
                                                            @RequestParam(value = "timeHorizon[0]",required = false) String timeHorizon1,
                                                            @RequestParam(value = "timeHorizon[1]",required = false) String timeHorizon2) {
        List<String> regionList = scanRecordService.getBusinessRegionList(goodsName, businessName, timeHorizon1, timeHorizon2);
        System.out.println("regionList = " + regionList);
        return success(regionList);
    }

    @GetMapping("/page")
    @ApiOperation("获得扫码记录分页")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    public CommonResult<PageResult<ScanRecordRespVO>> getScanRecordPage(@Valid ScanRecordPageReqVO pageVO) {
        PageResult<ScanRecordDO> pageResult = scanRecordService.getScanRecordPage(pageVO);
        return success(ScanRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出扫码记录 Excel")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:export')")
    @OperateLog(type = EXPORT)
    public void exportScanRecordExcel(@Valid ScanRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ScanRecordDO> list = scanRecordService.getScanRecordList(exportReqVO);
        // 导出 Excel
        List<ScanRecordExcelVO> datas = ScanRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "扫码记录.xls", "数据", ScanRecordExcelVO.class, datas);
    }

    @GetMapping("/getFirstScanInfoByUrlAndNumber")
    @ApiOperation("根据identityUrl 获取首次的扫码时间与首次扫码地点和扫码次数")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    @ApiImplicitParam(name = "identityUrl", value = "二维码url", required = true, example = "399403089039626147", dataTypeClass = String.class)
    public CommonResult<FirstScanRecordRespVO> getFirstScanInfoByUrlAndNumber(@RequestParam("identityUrl") String identityUrl) {
        return success(scanRecordService.getFirstScanInfoByUrlAndNumber(identityUrl));
    }

    @GetMapping("/getScanLocationAndTimeByUrl")
    @ApiOperation("根据identityUrl 获取所有的扫码时间与扫码地点")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    @ApiImplicitParam(name = "identityUrl", value = "二维码url", required = true, example = "399403089039626147", dataTypeClass = String.class)
    public CommonResult<List<ScanRecordDO>> getScanLocationAndTimeByUrl(@RequestParam("identityUrl") String identityUrl) {
        return success(scanRecordService.getScanLocationAndTimeByUrl(identityUrl));
    }

    @GetMapping("/getFirstScanInfoByBatchId")
    @ApiOperation("根据采收批次ID 获取首次的扫码时间与首次扫码地点和扫码次数")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    @ApiImplicitParam(name = "batchId", value = "采收批次ID", required = true, example = "HABTBC20230607094312PCTI95O3", dataTypeClass = String.class)
    public CommonResult<ScanRecordDO> getFirstScanInfoByBatchId(@RequestParam("batchId") String batchId) {
        ScanRecordDO firstScanInfoByBatchId = scanRecordService.getFirstScanInfoByBatchId(batchId);
        return success(firstScanInfoByBatchId);
    }

    @GetMapping("/getFirstScanInfoByUrl")
    @ApiOperation("根据identityUrl 获取首次的扫码时间与首次扫码地点")
    @PreAuthorize("@ss.hasPermission('identity:scan-record:query')")
    @ApiImplicitParam(name = "identityUrl", value = "二维码url", required = true, example = "399403089039626147", dataTypeClass = String.class)
    public CommonResult<ScanRecordDO> getFirstScanInfoByUrl(@RequestParam("identityUrl") String identityUrl) {
        ScanRecordDO firstScanInfoByUrlAndNumber = scanRecordService.getFirstScanInfoByUrl(identityUrl);
        return success(firstScanInfoByUrlAndNumber);
    }
}
