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.BlindBoardInfoService;
import com.zcloud.service.eightwork.BlindBoardService;
import com.zcloud.util.Const;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Smb;
import com.zcloud.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
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 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/blindboard")
public class AppBlindBoardController extends BaseController {

    @Autowired
    private BlindBoardService blindboardService;
    @Autowired
    private BlindBoardInfoService blindboardinfoService;
    @Autowired
    private Smb smb;

    /**
     * 新增
     * @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();
        blindboardService.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();
        blindboardService.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();
        blindboardService.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 = blindboardService.list(page);	//列出blindboard列表
        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 = blindboardService.findById(pd);
        map.put("result",errInfo);
        map.put("pd",pd);
        map.put("boardList", blindboardinfoService.listAll(pd));
        return map;
    }

    /**上传图
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/uploadImg")
    @ResponseBody
    @Transactional
    @LogAnno(menuType= "手机",menuServer= "动土作业",instructionsOperate = "动土作业",instructionsType = "上传图片")
    public Object uploadImg(
            @RequestParam(value="FFILE",required=false) MultipartFile[] files
    ) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        if (files != null && files.length > 0) {
            MultipartFile file = files[0];

            String  ffile = DateUtil.getDays();
            String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            smb.sshSftp(file, fileName, Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile);

            map.put("path",  Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile + "/" + fileName);
        }

        map.put("result", errInfo);
        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 = blindboardService.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));
        }
        blindboardService.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",blindboardService.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 = blindboardService.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 = blindboardService.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 = blindboardService.listSignFinishMeasures(pd);
        map.put("result",errInfo);
        map.put("finishMeasuresList",finishMeasuresList);
        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 = blindboardService.getFlowList(pd);
        map.put("flowList", flowList);
        map.put("result", errInfo);				//返回结果
        return map;
    }

}
