package com.zcloud.controller.eightwork;


import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.entity.Response;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.corp.CorpInfoService;
import com.zcloud.service.eightwork.EightWorkService;
import com.zcloud.service.system.DictionariesService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.fop.util.DataURLUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/eightwork")
public class AppEightWorkController extends BaseController {

    @Autowired
    private Smb smb;
    @Autowired
    private EightWorkService eightWorkService;
    @Autowired
    private DictionariesService dictionariesService;
    @Autowired
    private UsersService usersService;
    @Resource
    private CorpInfoService corpInfoService;

    /**八项作业列表
     * @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();
        List<PageData> varList = eightWorkService.list(pd);	//列出confinedspace列表
        map.put("varList", varList);
        map.put("result", errInfo);
        return map;
    }

    /**
     * description:
     * accidentType-风险点
     * hiddenLevel-隐患级别
     *
     * @return tree or list
     * @throws Exception all
     */
    @RequestMapping(value = "/getInfo")
    @ResponseBody
    public Response getInfo() throws Exception {
        Response response = Response.getResponse();
        try {
            PageData request = this.getPageData();
            if (StringUtils.isBlank(request.getString("vectors"))) throw new RuntimeException("未找到要获取的数据字典类型");
            List<String> vectors = Warden.getList(request.getString("vectors"),String.class);
            for (String vector: vectors) {
                switch (vector) {
                    case "accidentType":
                        //事故类型
                        response.put("accidentType", dictionariesService.getTreeById("cee1190ea96a4ca9b7bca81e11f0d0f8"));
                        continue;
                    default:
                }
            }
            return response.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return response.errorMessage(e.getMessage());
        }
    }

    @RequestMapping("/saveFile")
    @ResponseBody
    public Object saveFile(@RequestParam(value="file",required=false) MultipartFile file) throws Exception{
        Map<String,Object> map = new HashMap<>();
        String errorInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        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("FILE_PATH",  Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile + "/" + fileName);
        map.put("result",errorInfo);
        return map;
    }

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

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

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

    /**
     * @Description: 电子围栏 来自人员定位系统
     * @Author: dearLin
     * @Date: 2024/1/25/025 9:02
     * @Param: [com.zcloud.entity.Page] [page]
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/getPlsList")
    @ResponseBody
    public Object getPlsList(Page page) throws Exception {
        ReturnMap returnMap = new ReturnMap();
        PageData pd = new PageData();
        pd = this.getPageData();
        JSONObject body = PLSUtil.getRyRegionList(pd.getString("CORPINFO_ID"),"",1,99999,0);

        if (body != null) {
            List<JSONObject> list = body.getJSONArray("rows").toJavaList(JSONObject.class);
            returnMap.put("varList", list);
            returnMap.put("page", page);
            return returnMap;
        }
        return ReturnMap.error("查询失败");
    }

    /**获取动火作业流程
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/isInPls")
    @ResponseBody
    public Object isInPls() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = this.getPageData();
        PageData user = usersService.findById(pd);
        PageData corpData = corpInfoService.findById(user);
        if ("1".equals(corpData.getString("PERSONNEL_POSITION"))) {
            map.put("flag",true);
            map.put("result", errInfo);				//返回结果
            return map;
        }

        if (Tools.isEmpty(pd.getString("PLS_ID")) || Tools.isEmpty(user.getString("CARDNO"))) {
            return ReturnMap.ok().put("flag",true);
        }
        map.put("flag",
                PLSUtil.
                        determineWhetherThePersonIsInTheElectronicFence(
                                pd.getString("CORPINFO_ID"),
                                Integer.parseInt(user.getString("CARDNO")),
                                pd.getString("PLS_ID")));
        map.put("result", errInfo);				//返回结果
        return map;
    }

}
