package com.zcloud.controller.safetyMeeting.app;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.safetyMeeting.SafetyMeetingService;
import com.zcloud.service.system.ImgFilesService;
import com.zcloud.util.*;
import org.apache.commons.io.FileUtils;
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 javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 说明：TODO
 * 作者：wangxuan
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/safetyMeeting")
public class AppSafetyMeetingController extends BaseController {

    @Resource
    private SafetyMeetingService safetyMeetingService;
    @Resource
    private ImgFilesService imgFilesService;
    @Autowired
    private Smb smb;
    @RequestMapping(value = "/list")
    // @RequiresPermissions("safetyMeeting:list")
    @ResponseBody
    public Object list(Page page) throws Exception {
        PageData pageData = this.getPageData();
        page.setPd(pageData);
        return ReturnMap.ok().put("page",page).put("varList",safetyMeetingService.appDataList(page));
    }
    /**
     * @Description: 安全例会详情
     * @Author: dearLin
     * @Date: 2024/2/6/006 14:10
     * @Param: [] []
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/goEdit")
    // @RequiresPermissions("safetyMeeting:list")
    @ResponseBody
    public Object goEdit() throws Exception {
        PageData pageData = getPageData();
        PageData meetPeopleByUId = safetyMeetingService.findMeetPeopleByUId(pageData);
        meetPeopleByUId.putAll(safetyMeetingService.findById(pageData));
        return ReturnMap.ok().put("pd", meetPeopleByUId)
                .put("people", safetyMeetingService.saftPersonList(pageData))
                .put("imgs", imgFilesService.getListByKeyAndType(pageData.getString("SAFETY_MEETING_ID"), "105"))
                .put("singImgs", imgFilesService.getListByKeyAndType(meetPeopleByUId.getString("SAFETY_MEETING_PEOPLE_ID"), "106"))
                ;
    }

    /**
     * @Description: 手机签字确认
     * @Author: dearLin
     * @Date: 2024/2/6/006 14:10
     * @Param: [] []
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/confirm")
    // @RequiresPermissions("safetyMeeting:list")
    @ResponseBody
    public Object confirm() throws Exception {
        PageData pageData = getPageData();
        pageData.put("OPERATOR", pageData.getString("USER_ID") );
        pageData.put("OPERATTIME", DateUtil.date2Str(new Date()));
        safetyMeetingService.confirmMeetingPeople(pageData);
        return ReturnMap.ok().put("pd", safetyMeetingService.findMeetPeopleByUId(pageData));
    }
    /**完成
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/upload", headers = "content-type=multipart/form-data")
    @ResponseBody
    @Transactional
    public Object finish(
            @RequestParam(value="FFILE",required=false) MultipartFile[] files
    ) throws Exception{
        PageData pageData = this.getPageData();
        String FOREIGN_KEY = pageData.getString("FOREIGN_KEY");
        String TYPE = pageData.getString("TYPE");
        String errInfo = "success";
        String uploadPath = "";
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
//                 保存文件
                File tempFile = new File(file.getOriginalFilename());
                FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);
                tempFile.delete();
                PageData pd = new PageData();
                pd = this.getPageData();

                String  ffile = DateUtil.getDays();
//        		String filePath = PathUtil.getProjectpath() + Const.FILEPATHFILE + ffile;	//文件上传路径
//        		String fileName = FileUpload.fileUp(file, filePath, this.get32UUID());				//执行上传
                String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                smb.sshSftp(file, fileName, Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile);

                pd.put("IMGFILES_ID",  this.get32UUID());
                pd.put("FILEPATH",  Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile + "/" + fileName);
                pd.put("TYPE", TYPE);
                pd.put("FOREIGN_KEY",  FOREIGN_KEY);
                imgFilesService.save(pd);

                uploadPath += Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile + "/" + fileName;
            }
        }
        return ReturnMap.ok().put("imgPath",uploadPath);
    }
}
