package com.ruixuan.jiaoyang.controller;


import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruixuan.common.annotation.Log;
import com.ruixuan.common.core.controller.BaseController;
import com.ruixuan.common.core.domain.AjaxResult;
import com.ruixuan.common.core.page.TableDataInfo;
import com.ruixuan.common.enums.BusinessType;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.poi.ExcelUtil;
import com.ruixuan.jiaoyang.entity.JyMaterialList;
import com.ruixuan.jiaoyang.service.IJyMaterialListService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 素材信息Controller
 *
 * @author ruoyi
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/jy/material")
public class JyMaterialListController extends BaseController {
    private static ExecutorService uploadExecutorService = Executors.newFixedThreadPool(20);
    @Autowired
    private IJyMaterialListService jyMaterialListService;


    @GetMapping("/insertKeyWord")
    public JSONObject insertKeyWord(String type, String keyWord) {
        JSONObject json = new JSONObject();
        jyMaterialListService.insertKeyWord(type, keyWord);
        json.put("code", 0);
        json.put("message", "success");
        return json;
    }

    @GetMapping("/checkMaterial")
    public JSONObject checkMaterial(String signature) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(signature)) {
                throw new Exception("入参不能为空");
            }
            Integer count = jyMaterialListService.checkMaterial(signature);
            if (count > 0) {
                returnJson.put("data", false);
            } else {
                returnJson.put("data", true);
            }
            returnJson.put("code", 0);
        } catch (Exception e) {
            returnJson.put("code", 500);
            returnJson.put("message", e.getMessage());

        }
        return returnJson;
    }


    @PostMapping("/addMaterials")
    public JSONObject addMaterials(@RequestBody JSONObject requestJson) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(requestJson)) {
                throw new Exception("入参不能为空");
            }
            Long userId = requestJson.getLong("userId");
            if (Check.isNull(userId)) {
                throw new Exception("上传人不能为空");
            }
            String userName = requestJson.getString("userName");
            if (Check.isNull(userName)) {
                throw new Exception("上传人不能为空");
            }
            JSONArray materialList = requestJson.getJSONArray("materialList");
            if (Check.isNull(materialList)) {
                throw new Exception("上传人不能为空");
            }

            uploadExecutorService.submit(new Runnable() {
                @Override
                public void run() {
                    jyMaterialListService.addMaterials(userId, userName, materialList);
                }
            });
            returnJson.put("code", 0);
            returnJson.put("message", "提交成功");


        } catch (Exception e) {
            returnJson.put("code", 500);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }


    /**
     * 查询素材信息列表
     */
    @GetMapping("/list")
    public PageInfo<JyMaterialList> list(JyMaterialList jyMaterialList) {
        jyMaterialList.setUserId(null);
        return jyMaterialListService.selectJyMaterialListList(jyMaterialList);
    }

    @GetMapping("/getKeyWordList")
    public PageInfo<JSONObject> getKeyWordList(String pageNum, String pageSize) {
        return jyMaterialListService.getKeyWordList(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
    }

    /**
     * 修改素材信息
     */

    @Log(title = "素材信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody JyMaterialList jyMaterialList) {
        return toAjax(jyMaterialListService.updateJyMaterialList(jyMaterialList));
    }


    /**
     * 获取素材信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:list:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(jyMaterialListService.selectJyMaterialListById(id));
    }

    /**
     * 新增素材信息
     */
    @PreAuthorize("@ss.hasPermi('system:list:add')")
    @Log(title = "素材信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JyMaterialList jyMaterialList) {
        return toAjax(jyMaterialListService.insertJyMaterialList(jyMaterialList));
    }


    /**
     * 删除素材信息
     */
    @PreAuthorize("@ss.hasPermi('system:list:remove')")
    @Log(title = "素材信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jyMaterialListService.deleteJyMaterialListByIds(ids));
    }
}

