package com.zd.exam.controller;

import com.zd.common.core.utils.FileConfigUtils;
import com.zd.common.core.utils.UrlFormatUtils;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.exam.domain.ElSafeTrain;
import com.zd.exam.service.IElSafeTrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 一体机表控制层
 *
 * @author cyl
 * @since 2021-09-13 09:09:15
 */
@RestController
@RequestMapping("/onepc")
@Api(tags = {"一体机"})
public class ElOnePcController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private IElSafeTrainService elSafeTrainService;

    @Autowired
    private FileConfigUtils fileConfigUtils;

    /**
     * 一体机获取实学习资料列表
     */
    @GetMapping("/safetrain")
    @ApiOperation(value = "一体机获取实学习资料列表")
    public TableDataInfo<ElSafeTrain> safeTrain(ElSafeTrain elSafeTrain, HttpServletRequest request) {
        startPage();
        List<ElSafeTrain> list = elSafeTrainService.selectElSafeTrainList(elSafeTrain);
        String baseUrl=request.getServerName() ;
        String cover="";

        for(ElSafeTrain safeTrain:list){
            if(baseUrl.contains("192.168")){
                cover=fileConfigUtils.getLocalUrl()+"/"+safeTrain.getCover();
            }else {
                cover=fileConfigUtils.getRemoteUrl()+"/"+safeTrain.getCover();
            }

           cover= UrlFormatUtils.getHttpsORHttpUrl(cover);
           safeTrain.setCover(cover);
        }
        return getDataTable(list);

    }


    /**
     * 一体机获取实学习资料详情
     */
    @GetMapping("/safetraindetail")
    @ApiOperation(value = "一体机获取实学习资料详情")
    public AjaxResult safeTrainDetail(ElSafeTrain elSafeTrain, HttpServletRequest request) {
        ElSafeTrain elSafe=elSafeTrainService.selectElSafeTrainById(elSafeTrain.getId());
        String baseUrl=request.getServerName() ;
        String cover="";
        if(baseUrl.contains("192.168")){
            cover=fileConfigUtils.getLocalUrl()+"/"+elSafe.getCover();
        }else {
            cover=fileConfigUtils.getRemoteUrl()+"/"+elSafe.getCover();
        }

        cover= UrlFormatUtils.getHttpsORHttpUrl(cover);
        elSafe.setCover(cover);
        return AjaxResult.success(elSafeTrainService.selectElSafeTrainById(elSafeTrain.getId()));
    }

}
