package com.zcloud.controller.depository;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.bus.HiddenCheckService;import com.zcloud.service.bus.HiddenSchemeService;import com.zcloud.service.bus.HiddenService;
import com.zcloud.service.bus.ImgFilesService;import com.zcloud.service.depository.HiddenLibraryService;
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.springframework.beans.factory.annotation.Autowired;
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.servlet.http.HttpServletResponse;
import java.util.ArrayList;import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：隐患库主表
 * 作者：luoxiaobao
 * 时间：2023-08-03
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/hiddenLibrary")
public class HiddenLibraryController extends BaseController {

    @Autowired
    private HiddenLibraryService hiddenLibraryMapper;

    @Autowired
    private HiddenService hiddenService;

    @Autowired
    private HiddenCheckService hiddenCheckService;

    @Autowired
    private ImgFilesService imgFilesService;

    @Autowired
    private HiddenSchemeService hiddenSchemeService;

    /**
     * 新增
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/init")
    @RequiresPermissions("hiddenLibrary:add")
    @ResponseBody
    public Object init() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        if(org.apache.commons.lang3.StringUtils.isBlank(pd.getString("STATUS"))) pd.put("STATUS","0");
        if (StringUtils.isBlank(pd.getString("HIDDEN_LIBRARY_ID"))){
            pd.put("HIDDEN_LIBRARY_ID", this.get32UUID());
            hiddenLibraryMapper.save(pd);
        }else {
            hiddenLibraryMapper.edit(pd);
        }
        map.put("result", errInfo);
        return map;
    }

    /**
     * 删除
     *
     * @param out
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("hiddenLibrary:del")
    @ResponseBody
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        hiddenLibraryMapper.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions("hiddenLibrary:edit")
    @ResponseBody
    public Object edit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        hiddenLibraryMapper.edit(pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions("hiddenLibrary: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());
        page.setPd(pd);
        List<PageData> varList = hiddenLibraryMapper.list(page);    //列出HiddenLibrary列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/hiddenList")
    @RequiresPermissions("hiddenLibrary:list")
    @ResponseBody
    public Object hiddenList(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        pd.put("STATE","4");
        page.setPd(pd);
        List<PageData> varList = hiddenService.libraryList(page);    //列出HiddenLibrary列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    @RequestMapping(value = "/hiddenGoEdit")
    @RequiresPermissions("hiddenLibrary:list")
    @ResponseBody
    public Object hiddenGoEdit() throws Exception{
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = hiddenService.findById(pd); // 根据ID读取
        PageData pd2 = new PageData();
        pd2.put("FOREIGN_KEY", pd.getString("HIDDEN_ID"));
        pd2.put("TYPE", 3);
        List<PageData> hImgs = imgFilesService.listAll(pd2);// 隐患图片
        pd2.put("TYPE", 4);
        List<PageData> rImgs = imgFilesService.listAll(pd2);// 整改图片
         // 查询整改方案
        PageData hs = hiddenSchemeService.findByHdId(pd);
        List<PageData> sImgs = new ArrayList<PageData>();

        if (hs != null && hs.getString("HIDDENSCHEME_ID") != null) {
            PageData pd3 = new PageData();
            pd3.put("FOREIGN_KEY", hs.getString("HIDDENSCHEME_ID"));
            pd3.put("TYPE", 8);
            sImgs = imgFilesService.listAll(pd3);// 方案图片
        }

        List<PageData> pImgs = new ArrayList<PageData>();
        if("1".equals(pd.get("HAVEPLAN"))) {
            PageData pd3 = new PageData();
            pd3.put("FOREIGN_KEY", hs.getString("HIDDEN_ID"));
            pd3.put("TYPE",22);
            pImgs = imgFilesService.listAll(pd3);//方案图片
        }

        List<PageData> checkList = hiddenCheckService.listAll(pd);
        if (checkList != null && checkList.size() > 0) {
            PageData pd3 = new PageData();
            pd3.put("TYPE", 5);
            for (PageData check : checkList) {
                pd3.put("FOREIGN_KEY", check.get("HIDDENCHECK_ID"));
                check.put("cImgs", imgFilesService.listAll(pd3));// 验收图片
            }
        }
        map.put("pd", pd);
        map.put("hs", hs);
        map.put("hImgs", hImgs);
        map.put("rImgs", rImgs);
        map.put("sImgs", sImgs);
        map.put("pImgs", pImgs);
        map.put("checkList", checkList);
        map.put("result", errInfo);
        return map;
    }


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

    /**
     * 批量删除
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/deleteAll")
    @RequiresPermissions("hiddenLibrary: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(",");
            hiddenLibraryMapper.deleteAll(ArrayDATA_IDS);
            errInfo = "success";
        } else {
            errInfo = "error";
        }
        map.put("result", errInfo);                //返回结果
        return map;
    }


    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/addToLibrary")
    @RequiresPermissions("hiddenLibrary:edit")
    @ResponseBody
    public Object addToLibrary() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        hiddenLibraryMapper.addToLibrary(pd);
        map.put("result", errInfo);
        return map;
    }

    @RequestMapping(value = "/inStorage")
    @RequiresPermissions("hiddenLibrary:edit")
    @ResponseBody
    public Object  inStorage() throws Exception{
        Map<String, Object> map = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        hiddenService.changeLibrary(pd);
        map.put("result", "success");
        return map;
    }

    @RequestMapping(value = "/inStorageByIds")
    @RequiresPermissions("hiddenLibrary:edit")
    @ResponseBody
    public Object  inStorageByIds() throws Exception{
        Map<String, Object> map = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(pd.getString("IDS"))){
            pd.put("HIDDEN_IDS", JSONArray.parseArray(pd.getString("IDS"),String.class).toArray());
        }
        hiddenService.changeLibrary(pd);
        map.put("result", "success");
        return map;
    }

    @RequestMapping("readExcel")
    @ResponseBody
    public Object exportWord(@RequestParam(value = "FILE", required = false) MultipartFile[] excel) throws Exception{
        PageData response = new PageData();
        response.put("result", "success");
        try {
            hiddenLibraryMapper.readWord(excel);
            response.put("code","0");
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code","9999");
            response.put("errorMessage",e.getMessage());
            return response;
        }
    }
}
