package com.zcloud.controller.keyProjects.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.keyProjects.KeyprojectCheckService;
import com.zcloud.service.keyProjects.KeyprojectHiddenService;
import com.zcloud.service.keyProjects.KeyprojectPunishService;
import com.zcloud.service.relatedparty.OutSourcedService;
import com.zcloud.service.specialoperation.PersonnelManagementService;
import com.zcloud.service.system.ImgFilesService;
import com.zcloud.util.*;
import org.apache.commons.lang.StringUtils;
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 java.util.*;

/**
 * 说明：重点工程处罚
 * 作者：luoxiaobao
 * 时间：2022-09-21
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/keyprojectpunish")
public class AppKeyprojectPunishController extends BaseController {

	@Autowired
	private KeyprojectPunishService keyprojectpunishService;
	@Autowired
	private KeyprojectHiddenService keyprojectHiddenService;
	@Autowired
	private ImgFilesService imgFilesService;

	@Autowired
	private Smb smb;


	@Autowired
	private OutSourcedService outsourcedService;

	@Autowired
	private KeyprojectCheckService keyprojectcheckService;

	@Autowired
	private PersonnelManagementService personnelmanagementService;

	@Autowired
	private SendMessageUtil sendMessageUtil;

	/**处理
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/handle")
	@ResponseBody
	@Transactional
	public Object handle(@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();
		pd.put("HANDLED", 1);

		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				String suffixName = files[i].getOriginalFilename().substring(files[i].getOriginalFilename().lastIndexOf(".")+1).toLowerCase();
				if (!"pdf".equals(suffixName) && !"jpg".equals(suffixName) && !"jpeg".equals(suffixName) && !"png".equals(suffixName) && !"mp4".equals(suffixName)) {
					map.put("result", "fail");
					map.put("exception", "上传文件格式不正确");
					return map;
				}
			}
			for (int i = 0; i < files.length; i++) {
				MultipartFile file = files[i];

				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 + ffile);
				pd.put("HANDLE_IMG",  Const.FILEPATHFILE + ffile + "/" + fileName);
			}
		}
		keyprojectpunishService.editstate(pd);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 新增
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/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();
		PageData hidden = keyprojectHiddenService.findById(pd);
		pd.put("KEYPROJECTPUNISH_ID", this.get32UUID());    //主键
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));    //添加时间
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));    //修改时间
		pd.put("ISDELETE", 0);    //是否删除
		pd.put("HANDLED", 0);
		pd.put("HIDDEN_ID", hidden.get("HIDDEN_ID"));
		pd.put("KEYPROJECTCHECK_ID", hidden.get("FOREIGN_ID"));
		pd.put("UNITS_ID", hidden.get("RECTIFICATIONDEPT"));
		pd.put("PERSONNELMANAGEMENT_ID", hidden.get("RECTIFICATIONOR"));
		keyprojectpunishService.save(pd);
		//因发送其他项目需要 提出来
		PageData keyprote = new PageData();
		keyprote.put("KEYPROJECTCHECK_ID", pd.getString("KEYPROJECTCHECK_ID"));
		keyprote = keyprojectcheckService.findById(keyprote);
		//发送处罚短信通知
		try {
			//这里发送短信  判断公司是否需要发送短信 然后判断是否有隐患  有则发送  没有不发送
			PageData outSource = new PageData();
			outSource.put("OUTSOURCED_ID", keyprote.getString("OUTSOURCED_ID"));
			outSource = outsourcedService.findById(outSource);
			if (StringUtils.isNotBlank(outSource.getString("IS_SMS")) && StringUtils.equals("0", outSource.getString("IS_SMS"))) {
				//发送短信 pd中 PERSONNELMANAGEMENT_ID 对应 `qa-gwj-regulatory`.`BUS_PERSONNELMANAGEMENT` 表中 PERSONNELMANAGEMENT_ID  获取到 PHONENUM 手机号字段 发送短信
				PageData person = new PageData();
				person.put("PERSONNELMANAGEMENT_ID", pd.getString("PERSONNELMANAGEMENT_ID"));
				person = personnelmanagementService.findById(person);

				PageData mes = new PageData();
				mes.put("templateCode", "SMS_215820677");// 短信模板编码
				mes.put("CORPINFO_ID", person.get("CORPINFO_ID"));// 企业id
				// 参数集合
				List<PageData> paramsList = new ArrayList<PageData>();
				// 第1个参数
				PageData params1 = new PageData();
				params1.put("name", "time");// 存入短信模板中的参数名称
				params1.put("value", pd.getString("REASON"));// 存入上面参数名称所传递的值
				paramsList.add(params1);
				sendMessageUtil.sendMessagesKeyproject(mes,person.getString("PHONENUM"), paramsList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//获取当前重点工程的所属企业 根据重点工程创建人获取
			PageData dataDock = new PageData();
			String corpInfoId = outsourcedService.getCorpInfoByOutsourcesCreator(pd);
			pd.put("operatingCorpId", corpInfoId);
			dataDock.putAll(pd);
			map.put("dockData", JSON.toJSONString(dataDock));
			map.put("result", errInfo);
		}
		return map;
	}

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

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

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/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();
		String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
		page.setPd(pd);
		List<PageData>	varList = keyprojectpunishService.list(page);	//列出KeyprojectPunish列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

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

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


//	/**重点工程处罚列表 包含 未选择处罚结果的隐患  已下发处罚但为反馈的隐患 不处罚的隐患
//	 * @param page
//	 * @throws Exception
//	 */
//	@RequestMapping(value="/punishlist")
//	@ResponseBody
//	public Object punishlist(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 = keyprojectpunishService.getpunishlist(page);	//列出KeyprojectCheck列表
//		map.put("varList", varList);
//		map.put("page", page);
//		map.put("result", errInfo);
//		return map;
//	}
}
