package io.renren.modules.sys.controller.algs;

import io.minio.errors.*;
import io.renren.common.utils.MinIoUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.algs.AlgTrain;
import io.renren.modules.sys.entity.algs.AlgorithmModel;
import io.renren.modules.sys.entity.algs.Approval;
import io.renren.modules.sys.entity.algs.FileTest;
import io.renren.modules.sys.service.AlgTrainService;
import io.renren.modules.sys.service.AlgsModelsService;
import io.renren.modules.sys.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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 java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/algs/approval")
public class approvalController {
    @Autowired
    ApprovalService approvalService;
    @Autowired
    AlgsModelsService algsModelsService;
    @Autowired
    AlgTrainService algTrainService;
    /**
     * 所有列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page=approvalService.queryPageOnUse(params);
        return R.ok().put("page",page);
    }

    /**
     * Description:通过申请
     * @param approvalId
     * @return
     */
    @GetMapping("/approve")
    @Transactional
    public R approve(String approvalId){
        Approval approval=approvalService.getById(Long.parseLong(approvalId));
        System.out.println(approval);
        approval.setApprovalStatus((byte) 1);//将审批状态改为已通过
        AlgorithmModel model=algsModelsService.getById(approval.getModelId());
        model.setIsApprovaled((byte) 1);//将model表的状态改为已通过申请
        AlgTrain algTrain=algTrainService.getById(approval.getAlgorithmTrainingId());
        algTrain.setMissStatus((byte) 4);//将算法训练的状态改为已发布
        algsModelsService.update(model);
        algTrainService.update(algTrain);
        if(approvalService.update(approval))
            return R.ok();
        else return R.error();
    }

    /**
     * Description:拒绝申请
     * @param approvalId
     * @return
     */
    @GetMapping("/refuse")
    public R refuse(String approvalId) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
        Approval approval=approvalService.getById(Long.parseLong(approvalId));
        approval.setApprovalStatus((byte) -1);//将审批状态改为已拒绝
        AlgTrain algTrain=algTrainService.getById(approval.getAlgorithmTrainingId());
        algTrain.setMissStatus((byte) 6);//将算法训练的状态改为未通过申请

        //将算法库中的缓存文件删除
        List<FileTest> fileList=MinIoUtils.listFiles("algmodel","model"+approval.getModelId());
        for(FileTest file:fileList){
            MinIoUtils.deleteFile("algmodel","model"+approval.getModelId()+"/"+file.getFilename());
        }
        algsModelsService.removeById(approval.getModelId());
        algTrainService.update(algTrain);
        if(approvalService.update(approval))
            return R.ok();
        else return R.error();

    }
}
