package com.zcloud.controller.hiddenDangerCheckStandard;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.hiddenDangerCheckStandard.CommonItemService;
import com.zcloud.service.hiddenDangerCheckStandard.CommonService;
import com.zcloud.util.*;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：隐患排查标准表（通用）
 * 作者：wangpeng
 * 时间：2024-01-25
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/hiddenDangerCheckStandardCommon")
public class CommonController extends BaseController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private CommonItemService commonItemService;

    /**
     * 新增
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/add")
    @RequiresPermissions("hiddenDangerCheckStandardCommon:add")
    @ResponseBody
    public Object add() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("COMMON_ID", this.get32UUID());
        Warden.initDate(pd);
        commonService.save(pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 删除
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("hiddenDangerCheckStandardCommon: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();
        Warden.deleteDate(pd);
        commonService.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions("hiddenDangerCheckStandardCommon: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();
        Warden.updateDate(pd);
        commonService.edit(pd);
        map.put("result", errInfo);
        return map;
    }

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

    /**
     * 去修改页面获取数据
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit")
    @RequiresPermissions("hiddenDangerCheckStandardCommon: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 = commonService.findById(pd);    //根据ID读取
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

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

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

    /**导出到excel
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/excelModel")
    public void exportExcelModel(HttpServletResponse response) throws Exception{
        FileDownload.fileDownload(response, PathUtil.getProjectpath() + Const.FILEPATHFILE + "hiddenDangerCheckStandardTemplate.xls", "隐患排查标准模板.xls");
    }

    @RequestMapping("importExcel")
    @ResponseBody
    public Object importExcel(@RequestParam(value = "FILE") MultipartFile[] excel) throws Exception{
        Map<String, String> map = new HashMap<String, String>();
        PageData pd = new PageData();
        pd = this.getPageData();
        try {
            map = commonService.importExcel(pd,excel);
            return map;
        } catch (Exception e) {
            map.put("result", "fail"); // 返回结果
            map.put("resultStr", "");
            return map;
        }
    }



    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/pageItem")
    @RequiresPermissions("hiddenDangerCheckStandardCommon:list")
    @ResponseBody
    public Object pageItem(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        page.setPd(pd);
        List<PageData> varList = commonItemService.list(page);    //列出Common列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 新增
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/addItem")
    @RequiresPermissions("hiddenDangerCheckStandardCommon:add")
    @ResponseBody
    public Object addItem() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("COMMON_ITEM_ID", this.get32UUID());
        Warden.initDate(pd);
        commonItemService.saveItem(pd);
        map.put("result", errInfo);
        return map;
    }

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

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

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