package com.zcloud.controller.eightwork;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.eightwork.ElectricityGasService;
import com.zcloud.service.eightwork.ElectricityService;
import com.zcloud.util.Tools;
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：受限作业
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/electricity")
public class AppElectricityController extends BaseController {

    @Autowired
    private ElectricityService electricityService;
    @Autowired
    private ElectricityGasService electricityGasService;
    /**
     * 新增
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "受限作业", instructionsOperate = "受限作业", instructionsType = "新增")
    public Object add() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        electricityService.save(pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 修改
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "受限作业", instructionsOperate = "受限作业", instructionsType = "新增")
    public Object edit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        electricityService.edit(pd);
        map.put("result", errInfo);
        return map;
    }
    /**
     * 删除
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "受限作业", instructionsOperate = "受限作业", instructionsType = "删除")
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        electricityService.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**列表
     * @param page
     * @throws Exception
     */
    @RequestMapping(value="/list")
    @ResponseBody
	@LogAnno(menuType= "手机",menuServer= "受限作业",instructionsOperate = "受限作业",instructionsType = "列表")
    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 = electricityService.list(page);	//列出electricity列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    @RequestMapping("/findById")
    @ResponseBody
    @LogAnno(menuType= "手机",menuServer= "受限作业",instructionsOperate = "受限作业",instructionsType = "获取详情")
    public Object findById() throws Exception {
        Map<String,Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = electricityService.findById(pd);
        map.put("result",errInfo);
        map.put("pd",pd);
        return map;
    }

    @RequestMapping("/listSignFinished")
    @ResponseBody
    public Object listSignFinished() throws Exception {
        Map<String,Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        PageData signs = electricityService.listSignFinished(pd);
        map.put("result",errInfo);
        map.put("signs",signs);
        return map;
    }

    @RequestMapping("/nextStep")
    @ResponseBody
    @LogAnno(menuType= "手机",menuServer= "受限作业",instructionsOperate = "受限作业",instructionsType = "通过")
    public Object nextStep(MultipartRequest request) throws Exception {
        Map<String,Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<MultipartFile> fileList = new ArrayList<MultipartFile>();
        String SIGNTIME[] = pd.getString("SIGNTIME").split(",");

        for (int i = 0; i < SIGNTIME.length; i++) {
            fileList.add(request.getFile("file" + i));
        }
        electricityService.nextStep(pd, fileList.toArray(new MultipartFile[fileList.size()]));
        map.put("result",errInfo);
        return map;
    }


    @RequestMapping("/checkWorkCount")
    @ResponseBody
    public Object checkWorkCount() throws Exception{
        Map<String,Object> map = new HashMap<>();
        String errorInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        map.put("count",electricityService.checkWorkCount(pd));
        map.put("result",errorInfo);
        return map;
    }

    @RequestMapping("/listAllMeasures")
    @ResponseBody
    public Object listAllMeasures() throws Exception {
        Map<String,Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> measuresList = electricityService.listAllMeasures();
        map.put("result",errInfo);
        map.put("measuresList",measuresList);
        return map;
    }

    @RequestMapping("/listAllMeasuresForSign")
    @ResponseBody
    public Object listAllMeasuresForSign() throws Exception {
        Map<String,Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> measuresForSignList = electricityService.listAllMeasuresForSign(pd);
        map.put("result",errInfo);
        map.put("measuresForSignList",measuresForSignList);
        return map;
    }

    @RequestMapping("/listSignFinishMeasures")
    @ResponseBody
    public Object listSignFinishMeasures() throws Exception {
        Map<String,Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> finishMeasuresList = electricityService.listSignFinishMeasures(pd);
        map.put("result",errInfo);
        map.put("finishMeasuresList",finishMeasuresList);
        return map;
    }

    /**去修改页面获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/gas/save")
    @ResponseBody
    @LogAnno(menuType= "手机",menuServer= "受限作业",instructionsOperate = "受限作业",instructionsType = "保存气体检测")
    public Object gasSave(MultipartRequest request) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<MultipartFile> fileList = new ArrayList<MultipartFile>();
        String SIGNTIME[] = pd.getString("SIGNTIME").split(",");

        for (int i = 0; i < SIGNTIME.length; i++) {
            fileList.add(request.getFile("file" + i));
        }
        electricityService.saveGas(pd,fileList.toArray(new MultipartFile[fileList.size()]));
        map.put("result", errInfo);
        return map;
    }

    /**去修改页面获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/gas/list")
    @ResponseBody
    @LogAnno(menuType= "手机",menuServer= "受限作业",instructionsOperate = "受限作业",instructionsType = "去修改页面获取数据")
    public Object gasList() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData>	varList = electricityGasService.listAll(pd);	//列出electricity列表
        map.put("varList", varList);
        map.put("result", errInfo);
        return map;
    }

    /**去修改页面获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/gas/delete")
    @ResponseBody
    @LogAnno(menuType= "手机",menuServer= "受限作业",instructionsOperate = "受限作业",instructionsType = "去修改页面获取数据")
    public Object gasDel() throws Exception{
        Map<String,String> map = new HashMap<String,String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        electricityGasService.delete(pd);
        map.put("result", errInfo);				//返回结果
        return map;
    }

    /**获取受限作业流程
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/getFlowList")
    @ResponseBody
    @LogAnno(menuType= "手机",menuServer= "受限作业",instructionsOperate = "受限作业",instructionsType = "获取受限作业流程")
    public Object getFlowList() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> flowList = electricityService.getFlowList(pd);
        map.put("flowList", flowList);
        map.put("result", errInfo);				//返回结果
        return map;
    }

}
