package com.zcloud.controller.depository;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.depository.TextLibraryService;
import com.zcloud.util.Tools;
import com.zcloud.util.Warden;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bouncycastle.jcajce.provider.symmetric.util.PBE;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 说明：文本信息
 * 作者：luoxiaobao
 * 时间：2022-12-27
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/textLibrary")
public class TextLibraryController extends BaseController {

    @Resource
    private TextLibraryService textlibraryService;

    @RequestMapping(value = "/init")
    @RequiresPermissions("textlibrary:add")
    @ResponseBody
    public Object init(@RequestParam(value = "FILE", required = false) MultipartFile[] FILE) throws Exception {
        Map<String, Object> response = new HashMap<>();
        PageData request = this.getPageData();
        textlibraryService.init(request, FILE);
        response.put("result", "success");
        return response;
    }

    @RequestMapping(value = "/list")
    @RequiresPermissions("textlibrary:list")
    @ResponseBody
    public Object list(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        List<String> labels_condition = new ArrayList<>();
        List<String> category_condition = new ArrayList<>();
        if (StringUtils.isNotEmpty(pd.getString("labels"))) {
            List<PageData> _labels = Warden.getList(pd.getString("labels"));
            List<String> labels = _labels.stream().map(n -> n.getString("BUS_LABEL_FACTORY_ID")).filter(StringUtils::isNotBlank).collect(Collectors.toList());
            if (!labels.isEmpty()) labels_condition.addAll(labels);
        }

        if (StringUtils.isNotEmpty(pd.getString("CATEGORY_LIST"))) {
            List<PageData> _category_list = Warden.getList(pd.getString("CATEGORY_LIST"));
            List<String> category_list = _category_list.stream().map(n -> n.getString("DICTIONARIES_ID")).filter(StringUtils::isNotBlank).collect(Collectors.toList());
            if (!category_list.isEmpty()) category_condition.addAll(category_list);
        }

        if (StringUtils.isNotEmpty(pd.getString("SPECIFICATION_TYPES"))) {
            List<PageData> _SPECIFICATION_TYPES = Warden.getList(pd.getString("SPECIFICATION_TYPES"));
            List<String> specification_types = _SPECIFICATION_TYPES.stream().map(n -> n.getString("DICTIONARIES_ID")).filter(StringUtils::isNotBlank).collect(Collectors.toList());
            if (!specification_types.isEmpty()) category_condition.addAll(specification_types);
        }

        if (StringUtils.isNotEmpty(pd.getString("TYPES"))) {
            List<PageData> _TYPES = Warden.getList(pd.getString("TYPES"));
            List<String> types = _TYPES.stream().map(n -> n.getString("DICTIONARIES_ID")).filter(StringUtils::isNotBlank).collect(Collectors.toList());
            if (!types.isEmpty()) category_condition.addAll(types);
        }

        if (StringUtils.isNotEmpty(pd.getString("ATTRIBUTE_LIST"))) {
            List<PageData> _TYPES = Warden.getList(pd.getString("ATTRIBUTE_LIST"));
            List<String> types = _TYPES.stream().map(n -> n.getString("DICTIONARIES_ID")).filter(StringUtils::isNotBlank).collect(Collectors.toList());
            if (!types.isEmpty()) category_condition.addAll(types);
        }

        pd.put("LABELS", labels_condition.toArray());
        pd.put("CATEGORY_IDS",category_condition.toArray());
        page.setPd(pd);
        List<PageData> varList = textlibraryService.list(page);
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 删除
     *
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("textlibrary:del")
    @ResponseBody
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("result", "success");
        try {
            PageData pd = this.getPageData();
            textlibraryService.delete(pd);
            map.put("code", "0");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "9999");
            map.put("errorMessage", e.getMessage());
            return map;
        }

    }

    /**
     * 去修改页面获取数据
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit")
    @RequiresPermissions("textlibrary:edit")
    @ResponseBody
    public Object goEdit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = this.getPageData();
        PageData data = textlibraryService.findById(pd);    //根据ID读取
        map.put("data", data);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 批量删除
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/deleteAll")
    @RequiresPermissions("textlibrary:del")
    @ResponseBody
    public Object deleteAll() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String DATA_IDS = pd.getString("DATA_IDS");
        if (Tools.notEmpty(DATA_IDS)) {
            String ArrayDATA_IDS[] = DATA_IDS.split(",");
            textlibraryService.deleteAll(ArrayDATA_IDS);
            errInfo = "success";
        } else {
            errInfo = "error";
        }
        map.put("result", errInfo);                //返回结果
        return map;
    }

    @RequestMapping("lock")
    @ResponseBody
    public Object lock() throws Exception {
        Map<String, Object> response = new HashMap<>();
        try {
            PageData request = this.getPageData();
            textlibraryService.lock(request);
            response.put("code", "0");
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", "9999");
            response.put("message", e.getMessage());
        }
        response.put("result", "success");
        return response;
    }

    @RequestMapping("top")
    @ResponseBody
    public Object top() throws Exception {
        Map<String, Object> response = new HashMap<>();
        try {
            PageData request = this.getPageData();
            textlibraryService.top(request);
            response.put("code", "0");
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", "9999");
            response.put("message", e.getMessage());
        }
        response.put("result", "success");
        return response;
    }

    @RequestMapping("updateFile")
    @ResponseBody
    public Object updateFile(@RequestParam(value = "FILE", required = false) MultipartFile[] FILE) throws Exception {
        Map<String, Object> response = new HashMap<>();
        try {
            textlibraryService.updateFile(FILE, this.getPageData());
            response.put("code", "0");
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", "9999");
            response.put("message", e.getMessage());
        }
        response.put("result", "success");
        return response;
    }

    @RequestMapping("getUpdateLog")
    @ResponseBody
    public Object getUpdateLog() throws Exception {
        Map<String, Object> response = new HashMap<>();
        try {
            response.put("list", textlibraryService.getUpdateLog(this.getPageData()));
            response.put("code", "0");
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", "9999");
            response.put("message", e.getMessage());
        }
        response.put("result", "success");
        return response;
    }

    @RequestMapping("setStatus")
    @ResponseBody
    public Object setStatus() throws Exception {
        Map<String, Object> response = new HashMap<>();
        try {
            textlibraryService.setStatus(this.getPageData());
            response.put("code", "0");
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", "9999");
            response.put("message", e.getMessage());
        }
        response.put("result", "success");
        return response;
    }

    @RequestMapping("copyToOperate")
    @ResponseBody
    public Object copyToOperate() throws Exception{
        Map<String, Object> response = new HashMap<>();
        try {
            textlibraryService.copyToOperate(this.getPageData());
            response.put("code", "0");
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", "9999");
            response.put("message", e.getMessage());
        }
        response.put("result", "success");
        return response;
    }

    @RequestMapping("getTextInfo")
    @ResponseBody
    public Object getTextInfo() throws Exception{
        Map<String, Object> response = new HashMap<>();
        try {
            PageData text_info = textlibraryService.getTextInfo(this.getPageData());
            response.put("info",text_info);
            response.put("code", "0");
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", "9999");
            response.put("message", e.getMessage());
        }
        response.put("result", "success");
        return response;
    }

    @RequestMapping("exportWord")
    public void exportWord(HttpServletResponse response) throws Exception{
        try {
           textlibraryService.exportWord(this.getPageData(),response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
