package com.persagy.dmp.rwd.qrcode.controller;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.digital.client.DigitalObjectFacade;
import com.persagy.dmp.rwd.qrcode.service.QrCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 二维码管理
 * @author:linhuili
 * @date:2021/11/8
 */
@RestController
@RequestMapping("/rwd/plus/qrCode")
public class QrCodeController {

    @Autowired
    private QrCodeService qrCodeService;

    /**
     * 对象生成默认二维码
     * @param criteria
     * @return
     * @throws Exception
     */
    @PostMapping("/createDefaultQrCode")
    @Deprecated
    public CommonResult createDefaultQrCode(@RequestBody QueryCriteria criteria) throws Exception {
        String projectId = AppContext.getContext().getProjectId();
        String groupCode = AppContext.getContext().getGroupCode();
        //统计对象数据总数
        criteria.setOnlyCount(true);
        CommonResult<List<ObjectNode>> countCommonResult = DigitalObjectFacade.queryPrototype(groupCode, projectId, null, null, criteria);
        Long total = countCommonResult.getCount();
        if(total == null || total <= 0){
            return CommonResult.success("暂无可生成二维码的对象数据");
        }
        //分页处理：生成对象二维码
        int pageNum = total.intValue() / 500 + 1 ;
        for(int i=0 ; i< pageNum ; i++){
            criteria.setOnlyCount(false);
            criteria.setPage(Long.valueOf(i));
            criteria.setSize(Long.valueOf(500));
            List<ObjectNode> pageList = DigitalObjectFacade.query(groupCode, projectId, null, null, criteria);
            if(pageList.size() <= 0){
                continue;
            }
            //todo 目前文件服务暂时支持单个文件上传，后期支持批量上传后，此接口需进行优化
            qrCodeService.createDefaultQrCode(pageList);
        }
        return CommonResult.success("创建成功");
    }


}
