package org.jeecg.modules.fileupload.controller;

import cn.com.ctop.common.module.service.IMaterialImageInfoService;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
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.HashMap;
import java.util.Map;

/**
 * 素材库工具接口
 *
 * @author jeecg-boot
 */
@RestController
public class FileController {
    @Autowired
    private IMaterialInfoService materialInfoService;

    @Autowired
    private IMaterialImageInfoService materialImageInfoService;

    @RequestMapping("file/check")
    public Map<String, Object> chenckFile(String code, Long projectId) {
        return materialInfoService.checkMaterialInfo(code, projectId);
    }


    @RequestMapping("file/imageCheck")
    public Map<String, Object> imageCheck(String code, String videoId) {
        return materialImageInfoService.checkMaterialInfo(code, videoId);
    }


    @PostMapping("/insertV2")
    public Map<String, Object> insertV2(@RequestBody JSONObject json) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        json.put("userId", user.getId());
        return materialInfoService.correlationUpload(json);
    }


    @PostMapping("/insertImage")
    public Map<String, Object> insertImage(@RequestBody JSONObject json) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject()
                .getPrincipal();

        json.put("userId", user.getId());
        return materialInfoService.insertImage(json);
    }


    @PostMapping("file/insert")
    public Map<String, Object> insertFile(@RequestBody JSONObject json) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject()
                .getPrincipal();

        json.put("userId", user.getId());
        return materialInfoService.insert(json);
    }


    @PostMapping("file/batchInsert")
    public Map<String, Object> batchInsert(@RequestBody JSONObject json) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject()
                    .getPrincipal();
            json.put("userId", user.getId());
            JSONArray videoArr = json.getJSONArray("videoArr");
            if (!Check.isNull(videoArr)) {
                json.remove("videoArr");

                for (int i = 0; i < videoArr.size(); i++) {
                    JSONObject video = videoArr.getJSONObject(i);
                    json.put("code", video.getString("code"));
                    json.put("url", video.getString("url"));
                    materialInfoService.insert(json);

                }
            }

            ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_SUCCESS.getCode());

        } catch (Exception e) {
            ResultMapUtils.setResultMap(resultMap, StatusCode.MATERIAL_UPLOAD_FAIL.getCode());
            e.printStackTrace();
        }
        return resultMap;
    }
}
