package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.manage.modules.material.service.IMaterialUploadService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
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
 * @version V1.0
 * @date 2019-11-23
 */
@Slf4j
@Api(tags = "素材归属标")
@RestController
@RequestMapping("/ctop/materialUpload")
public class MaterialUploadController {

    @Autowired
    private IMaterialUploadService materialUploadService;

    @PostMapping(value = "/uploadAccount")
    public Map<String, Object> uploadAccount(@RequestBody JSONObject json) {
        Map<String, Object> returnMap = new HashMap<>();
        String mediaId = json.getString("mediaId");
        JSONArray materialArray = json.getJSONArray("materialArray");
        JSONArray accountArray = json.getJSONArray("accountArray");
        try {
            if (Check.isNull(mediaId)) {
                throw new Exception("请选择项目媒体类型");
            }
            if (Check.isNull(materialArray) || Check.isNull(accountArray)) {
                throw new Exception("请选择账号或素材");
            }
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

            new Thread(() -> materialUploadService.uploadAccount(mediaId, materialArray, accountArray, sysUser.getId())).start();
            returnMap.put("success", true);
            returnMap.put("message", "开始上传文件");
            returnMap.put("code", 0);

        } catch (Exception e) {
            returnMap.put("success", false);
            returnMap.put("message", e.getMessage());
        }
        return returnMap;
    }
}
