package com.bonus.hn.bearing.capacity.backstage.manager.controller;

import com.bonus.hn.bearing.capacity.backstage.companypro.entity.TreeBean;
import com.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.AnnouncementBean;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.DownloadFileBean;
import com.bonus.hn.bearing.capacity.backstage.manager.service.AnnouncementService;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.*;
import com.bonus.hn.bearing.capacity.backstage.project.entity.FileBean;
import com.bonus.hn.bearing.capacity.backstage.project.service.ProgressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @author bonus
 */
@Api(tags = "系统公告")
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

	@Autowired
	private AnnouncementService service;
	@Resource
	private ProgressService progressService;


	@LogAnnotation
	@PostMapping("getAnnouncementList")
	@ApiOperation(value = "系统公告-列表查询")
	public R getAnnouncementList(AnnouncementBean o){
		List<AnnouncementBean> list = service.getAnnouncementList(o);
		return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
	}
	@LogAnnotation
	@PostMapping("getAnnouncementType")
	@ApiOperation(value = "系统公告-分类管理列表")
	public R getAnnouncementType(AnnouncementBean o){
		List<AnnouncementBean> list = service.getAnnouncementType(o);
		return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
	}
	@LogAnnotation
	@PostMapping("getUserList")
	@ApiOperation(value = "系统公告-人员查询")
	public R getUserList(AnnouncementBean o){
		List<AnnouncementBean> list = service.getUserList(o);
		return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
	}

	@RequestMapping(value = "addAnnouncementInfo", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "系统公告-新增")
	public AjaxRes addAnnouncementInfo(@RequestBody AnnouncementBean bean){
		AjaxRes ar = new AjaxRes();
		int cb = service.addAnnouncementInfo(bean);
		ar.setFailMsg(GlobalConst.DATA_FAIL);
		if(cb>0){
			ar.setFailMsg(GlobalConst.INIT_SUCCEED);
		}
		return  ar;
	}
	@RequestMapping(value = "categoricalManagementOfOperations", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "系统公告-新增/修改分类管理")
	public AjaxRes categoricalManagementOfOperations(@RequestBody AnnouncementBean bean){
		AjaxRes ar = new AjaxRes();
		int cb = service.categoricalManagementOfOperations(bean);
		ar.setFailMsg(GlobalConst.DATA_FAIL);
		if(cb>0){
			ar.setFailMsg(GlobalConst.INIT_SUCCEED);
		}
		return  ar;
	}

	@RequestMapping(value = "checkTitle", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "系统公告-检查公告标题是否存在")
	public String checkTitle(AnnouncementBean o){
		int num = service.checkTitle(o);
		if (num == 0){
			return "success";
		}
		return "false";
	}

	@RequestMapping(value = "getAnnouncementView", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "系统公告-公告详情")
	public AjaxRes getAnnouncementView(AnnouncementBean o){
		AjaxRes ar = new AjaxRes();
		AnnouncementBean announcementBean = service.getAnnouncementView(o);
		Map<String, Object> map = new HashMap<>(100);
		if(announcementBean !=null){
			map.put("announcementBean",announcementBean);
		}
		ar.setSucceed(map, GlobalConst.DATA_SUCCEED);
		return ar;
	}

	@LogAnnotation
	@PostMapping("delAnnouncementTypeById")
	@ApiOperation(value = "系统公告-类型管理-删除类型")
	public AjaxRes delAnnouncementTypeById(AnnouncementBean bean) {
		AjaxRes ar = new AjaxRes();
		int result  = service.delAnnouncementTypeById(bean);
		if(result==1){
			ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
		}else {
			ar.setFailMsg(GlobalConst.DATA_FAIL);
		}
		return ar;
	}
	@LogAnnotation
	@PostMapping("delAnnouncementById")
	@ApiOperation(value = "系统公告-删除公告")
	public AjaxRes delAnnouncementById(AnnouncementBean bean) {
		AjaxRes ar = new AjaxRes();
		int result  = service.delAnnouncementById(bean);
		if(result==1){
			ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
		}else {
			ar.setFailMsg(GlobalConst.DATA_FAIL);
		}
		return ar;
	}
	@PostMapping("/upload")
	@ResponseBody
	@ApiOperation(value = "系统公告-上传附件")
	public List<String> handleFileUpload(@RequestParam("file") MultipartFile[] files, FileBean b) {
		List<String> fileNames = new ArrayList<>();
		for (MultipartFile file : files) {
			if (file.isEmpty()) {
				fileNames.add("File is empty");
				continue;
			}
			try {
				// 获取文件名
				String fileName = file.getOriginalFilename();
				String mkdirsName = "announcement";
				// linux 系统路径
				String filePath = "/data/hnczl/" + mkdirsName + "/";
				String os = System.getProperty("os.name");
				if (os.toLowerCase().startsWith("win")) {
					filePath = "D://files/" + mkdirsName+"/";
				}
				String path = filePath + "/" + DateTimeHelper.getYear(new Date()) + "/" + DateTimeHelper.getMonth(new Date()) + "/"+DateTimeHelper.getDay()+ "/" + fileName;
				String imgPath = mkdirsName + "/" + DateTimeHelper.getYear(new Date()) + "/" + DateTimeHelper.getMonth(new Date()) + "/"+DateTimeHelper.getDay()+ "/" + fileName;
				File dest = new File(path);
				// 检查父级目录是否存在，不存在则创建
				if (!dest.getParentFile().exists()) {
					dest.getParentFile().mkdirs();
				}
				// 存入临时文件
				file.transferTo(dest);
				fileNames.add(fileName);

				long fileSize = file.getSize();
				// 转换为MB
				double megabytes = (double) fileSize / (1024 * 1024);
				// 格式化为两位小数
				DecimalFormat df = new DecimalFormat("#.##");
				String resultSize = df.format(megabytes);
				FileBean bean = new FileBean();
				bean.setFileName(fileName);
				bean.setFilePath(imgPath);
				bean.setFileSize(resultSize);
				bean.setUserId(UserUtil.getLoginUser().getId()+"");
				bean.setUserName(UserUtil.getLoginUser().getUsername());
				bean.setType("announcement");
				bean.setRemark(b.getRemark());
				bean.setAssociaId(b.getAssociaId());
				progressService.insertFile(bean);
			} catch (IOException e) {
				fileNames.add("Failed to upload " + file.getOriginalFilename());
			}
		}
		return fileNames;
	}


	@LogAnnotation
	@PostMapping("getFileList")
	@ApiOperation(value = "系统公告-获取文件列表")
	public R getFileList(FileBean o){
		List<FileBean> list = service.getFileList(o);
		return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
	}


	@LogAnnotation
	@PostMapping("tree")
	@ApiOperation(value = "公司-项目部树")
	public List<TreeBean> getCompanyProTree(TreeBean o) {
		return service.getCompanyProTree(o);
	}
	@LogAnnotation
	@PostMapping("personTree")
	@ApiOperation(value = "系统公告-公司-回显人员树")
	public List<TreeBean> personTree(TreeBean o) {
		return service.personTree(o);
	}


	@RequestMapping(value = "insertFileDownLoadRecord", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "下载文件记录")
	public AjaxRes insertFileDownLoadRecord(@RequestBody DownloadFileBean bean){
		AjaxRes ar = new AjaxRes();
		int cb = service.insertFileDownLoadRecord(bean);
		ar.setFailMsg(GlobalConst.DATA_FAIL);
		if(cb>0){
			ar.setFailMsg(GlobalConst.INIT_SUCCEED);
		}
		return  ar;
	}
}
