package org.jeecg.modules.appInterface.controller;

import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.appInterface.FileList;
import org.jeecg.modules.appInterface.ImageUtil;
import org.jeecg.modules.appInterface.ResHttpStatus;
import org.jeecg.modules.appInterface.entity.FbsWorkingStatistics;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.appInterface.service.IFbsWorkingStatisticsService;
import org.jeecg.modules.fbsPerson.entity.FbsPerson;
import org.jeecg.modules.fbsPerson.service.IFbsPersonService;
import org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList;
import org.jeecg.modules.fbsWorkshopDispatchList.service.IFbsWorkshopDispatchListService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Title: Controller
 * @Description: 工时统计接口
 * @author： jeecg-boot
 * @date：   2020-10-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/workingStatistics/fbsWorkingStatistics")
@Slf4j
public class FbsWorkingStatisticsController {
	@Autowired
	private IFbsWorkingStatisticsService fbsWorkingStatisticsService;
	 @Autowired
	 private IFbsWorkshopDispatchListService fbsWorkshopDispatchListService;
	 @Autowired
	 private IFbsPersonService fbsPersonService;
	 @Value("${jeecg.path.upload}")
	 private String upLoadPath;
	 @Value("${jeecg.path.upvedio}")
	 private String upVedio;

	 public static void main1(String[] srg) {
		 String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		 //加载JDBC驱动
		 String dbURL = "jdbc:sqlserver://47.103.119.148;DatabaseName=UFDATA_100_2019";
		 //连接服务器和数据库
		 String userName = "sa"; //默认用户名
		 String userPwd = "Wakeup@123"; //密码
		 Connection dbConn;
		 PreparedStatement ps = null;
		 ResultSet rs = null;
		 String targetPath = "D:/img/1.png";
		 try {
			 Class.forName(driverName);
			 dbConn = DriverManager.getConnection(dbURL, userName, userPwd);

			 String sql = "select * from VoucherAccessories where voucherID ='1000000003'";
			 ps = dbConn.prepareStatement(sql);
//			 ps.setInt(1, 1);
			 rs = ps.executeQuery();
			 while (rs.next()) {
				 InputStream in = rs.getBinaryStream("FileContent");
				 ImageUtil.readBin2Image(in, targetPath);
			 }

			 System.out.println("Connection Successful!");
			 //如果连接成功 控制台输出Connection  Successful!
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
	 }



	 /**
	  * 开工接口
	  * @param entity
	  * @return
	  */
	 @RequestMapping(value = "/startOperation", method = RequestMethod.POST)
	 @ResponseBody
	 public ResponseEntity<ResHttpStatus> startOperation(FbsWorkingStatistics entity){
		 ResHttpStatus res = new ResHttpStatus();
		 if(entity == null){
			 res.setCode("1");
			 res.setMsg("参数不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isNotBlank(entity.getId())){
			 entity.setId(null);
		 }
		 if(StringUtils.isBlank(entity.getPersonnelCode())){
			 res.setCode("1");
			 res.setMsg("人员编码不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getPersonnelName())){
			 res.setCode("1");
			 res.setMsg("人员名称不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getPkWorkshopDispatchList())){
			 res.setCode("1");
			 res.setMsg("派工单id不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		if(StringUtils.isBlank(entity.getRowNumber())){
			 res.setCode("1");
			 res.setMsg("行号能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }

		 QueryWrapper<FbsWorkshopDispatchList> ypffscSaleorderBQueryWrapper = new QueryWrapper<>();
		 ypffscSaleorderBQueryWrapper.eq("id", entity.getPkWorkshopDispatchList()).eq("del_flag", 0);
		 List<FbsWorkshopDispatchList> list = fbsWorkshopDispatchListService.list(ypffscSaleorderBQueryWrapper);
		 if(list.size() < 1){
			 res.setCode("1");
			 res.setMsg("该订单还没有派工"+entity.getOrderNumber());
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 FbsWorkshopDispatchList workshopDispatchList = list.get(0);
		 //0初始，1接单,2暂停，3完成
		 if(StringUtils.isNotBlank(workshopDispatchList.getState()) && !workshopDispatchList.getState().equals("0") &&
                 !workshopDispatchList.getState().equals("2")){
             res.setCode("1");
             res.setMsg("该订单已开工或已结束，请勿再次操作"+entity.getOrderNumber());
             return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
         }
		 entity.setStartDate(workshopDispatchList.getPlannedStartTime());//计划时间
		 entity.setEndDate(workshopDispatchList.getPlannedStartTime());//计划时间

		 entity.setFirstTime(new Date());//获取当前时间
		 entity.setFinalInfo("开工");
		 entity.setFinalState("1");//完结状态（1-开工，2-下岗/暂停，3-实时报工）

		 fbsWorkingStatisticsService.save(entity);
         fbsWorkshopDispatchListService.updateState("1",entity.getPkWorkshopDispatchList());

		 res.setCode("0");
		 res.setMsg("成功");
		 res.setData(entity);
		 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
	 }



	 /**
	  * 下岗/暂停
	  * @param entity
	  * @return
	  */
	 @RequestMapping(value = "/suspendOperation", method = RequestMethod.POST)
	 @ResponseBody
	 public ResponseEntity<ResHttpStatus> suspendOperation(FbsWorkingStatistics entity){
		 ResHttpStatus res = new ResHttpStatus();

		 long nd = 1000 * 24 * 60 * 60;
		 long nh = 1000 * 60 * 60;
		 long nm = 1000 * 60;

		 if(entity == null){
			 res.setCode("1");
			 res.setMsg("参数不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getId())){
			 res.setCode("1");
			 res.setMsg("报工单id不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }else{
			 FbsWorkingStatistics par = fbsWorkingStatisticsService.getById(entity.getId());
			 if(par != null){
			 	Date firstTime = par.getFirstTime();
				 long diff = new Date().getTime() - firstTime.getTime();
				 // 计算差多少天
//				 long day = diff / nd;
				 // 计算差多少小时
				 long hour = diff % nd / nh;
				 // 计算差多少分钟
				 long min = diff / nm;
				 BigDecimal aa = new BigDecimal(min).divide(new BigDecimal("60"), 2, BigDecimal.ROUND_HALF_UP);
				 entity.setWorkingHours(aa);
			 }
		 }
		 if(StringUtils.isBlank(entity.getPersonnelCode())){
			 res.setCode("1");
			 res.setMsg("人员编码不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getPersonnelName())){
			 res.setCode("1");
			 res.setMsg("人员名称不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getPkWorkshopDispatchList())){
			 res.setCode("1");
			 res.setMsg("派工单id不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
//         if(StringUtils.isBlank(entity.getOrderId())){
//             res.setCode("1");
//             res.setMsg("订单id不能为空");
//             return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
//         }
		 QueryWrapper<FbsWorkshopDispatchList> ypffscSaleorderBQueryWrapper = new QueryWrapper<>();
		 ypffscSaleorderBQueryWrapper.eq("id", entity.getPkWorkshopDispatchList()).eq("del_flag", 0);
		 List<FbsWorkshopDispatchList> list = fbsWorkshopDispatchListService.list(ypffscSaleorderBQueryWrapper);
		 if(list.size() < 1){
			 res.setCode("1");
			 res.setMsg("该订单还没有派工"+entity.getOrderNumber());
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 FbsWorkshopDispatchList workshopDispatchList = list.get(0);

         //状态（0初始，1接单,2暂停，3完成）
         if(StringUtils.isNotBlank(workshopDispatchList.getState()) && !workshopDispatchList.getState().equals("1")){
             res.setCode("1");
             res.setMsg("该订单目前不在可暂停状态，无法暂停"+entity.getOrderNumber());
             return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
         }
		 entity.setStartDate(workshopDispatchList.getPlannedStartTime());//计划时间
		 entity.setEndDate(workshopDispatchList.getPlannedStartTime());//计划时间
		 entity.setFinalTime(new Date());//获取当前时间
		 entity.setFinalInfo("下岗/暂停");
		 entity.setFinalState("2");//完结状态（1-开工，2-下岗/暂停，3-实时报工）
		 fbsWorkingStatisticsService.updateById(entity);
         fbsWorkshopDispatchListService.updateState("2",entity.getPkWorkshopDispatchList());

         res.setCode("0");
		 res.setMsg("成功");
		 res.setData(entity);
		 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
	 }

	 /**
	  * 实时报工
	  * @param entity
	  * @return
	  */
	 @RequestMapping(value = "/finishDoing", method = RequestMethod.POST)
	 @ResponseBody
	 public ResponseEntity<ResHttpStatus> finishDoing(FbsWorkingStatistics entity){
		 ResHttpStatus res = new ResHttpStatus();

		 long nd = 1000 * 24 * 60 * 60;
		 long nh = 1000 * 60 * 60;
		 long nm = 1000 * 60;

		 if(entity == null){
			 res.setCode("1");
			 res.setMsg("参数不能为空 ");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getId())){
			 res.setCode("1");
			 res.setMsg("报工单id不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 FbsWorkingStatistics par = fbsWorkingStatisticsService.getById(entity.getId());
		 if(par != null){
			if(par.getState().equals("1")){
				res.setCode("1");
				res.setMsg("该次已完成报工，请勿再次操作");
				return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
			}

			if( par.getFinalTime() == null){
				Date firstTime = par.getFirstTime();
				long diff = new Date().getTime() - firstTime.getTime();
				// 计算差多少分钟
				long min = diff / nm;
				BigDecimal aa = new BigDecimal(min).divide(new BigDecimal("60"), 2, BigDecimal.ROUND_HALF_UP);
				entity.setWorkingHours(aa);
				entity.setFinalTime(new Date());//获取当前时间

			}

		 }else{
			 res.setCode("1");
			 res.setMsg("报工单id不正确，没有查询到数据");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }

		 if(StringUtils.isBlank(entity.getPersonnelCode())){
			 res.setCode("1");
			 res.setMsg("人员编码不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getPersonnelName())){
			 res.setCode("1");
			 res.setMsg("人员名称不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }

		 if(StringUtils.isBlank(entity.getQualifiedNumber())){
			 res.setCode("1");
			 res.setMsg("合格数量不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getScrapNumber())){
			 res.setCode("1");
			 res.setMsg("报废数量不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getRefuseNumber())){
			 res.setCode("1");
			 res.setMsg("拒绝数量不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getState())){
			 res.setCode("1");
			 res.setMsg("状态不能为空0保存，1提交");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 if(StringUtils.isBlank(entity.getPkWorkshopDispatchList())){
			 res.setCode("1");
			 res.setMsg("派工单id不能为空");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
//         if(StringUtils.isBlank(entity.getOrderId())){
//             res.setCode("1");
//             res.setMsg("订单id不能为空");
//             return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
//         }
		 QueryWrapper<FbsWorkshopDispatchList> ypffscSaleorderBQueryWrapper = new QueryWrapper<>();
		 ypffscSaleorderBQueryWrapper.eq("id", entity.getPkWorkshopDispatchList()).eq("del_flag", 0);
		 List<FbsWorkshopDispatchList> list = fbsWorkshopDispatchListService.list(ypffscSaleorderBQueryWrapper);
		 if(list.size() < 1){
			 res.setCode("1");
			 res.setMsg("该订单还没有派工"+entity.getOrderNumber());
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 FbsWorkshopDispatchList workshopDispatchList = list.get(0);

         //状态（0初始，1接单,2暂停，3完成）
         if(workshopDispatchList.getState().equals("0") || workshopDispatchList.getState().equals("2")){
             res.setCode("1");
             res.setMsg("该订单目前正在进行中，无法报工"+entity.getOrderNumber());
             return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
         }

		 entity.setStartDate(workshopDispatchList.getPlannedStartTime());//计划时间
		 entity.setEndDate(workshopDispatchList.getPlannedStartTime());//计划时间

		 entity.setFinalInfo("实时报工");
		 entity.setFinalState("3");//完结状态（1-开工，2-下岗/暂停，3-实时报工）
		 fbsWorkingStatisticsService.updateById(entity);
         fbsWorkshopDispatchListService.updateState("3",entity.getPkWorkshopDispatchList());

		 if(!par.getState().equals("1")){
			 QueryWrapper<FbsWorkingStatistics> queryWrapper = new QueryWrapper<>();
			 queryWrapper.eq("pk_Workshop_Dispatch_List", entity.getPkWorkshopDispatchList()).eq("del_flag", 0);
			 List<FbsWorkingStatistics> list1 = fbsWorkingStatisticsService.list(queryWrapper);
			 BigDecimal allHors = new BigDecimal("0");
			 for (FbsWorkingStatistics o:list1){
				 allHors = allHors.add(o.getWorkingHours());
			 }
			 fbsWorkshopDispatchListService.writeBackManHoursCompleted(allHors.toString(),entity.getPkWorkshopDispatchList());

		 }

         if(entity.getState().equals("1")){
         	//把数据传入u8数据库
		 }

         res.setCode("0");
		 res.setMsg("成功");
		 res.setData(entity);
		 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
	 }

	 /**
	  * 通过人员编码查询人员信息
	  * @param userId
	  * @return
	  */
	 @RequestMapping(value = "getPersonByCode")
	 @ResponseBody
	 public ResponseEntity<ResHttpStatus> getPersonByCode(String userId){

		 ResHttpStatus res = new ResHttpStatus();
		 if(StringUtils.isBlank(userId)){
			 res.setCode("1");
			 res.setMsg("人员编码不能为空-userId");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 List<FbsPerson> entity = fbsPersonService.getPerByCode(userId);
		 if(entity.size() < 1){
			 res.setCode("1");
			 res.setMsg("人员编码不正确"+userId);
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }

		 Map<String,String> map = new HashMap<>();
		 map.put("userCode",entity.get(0).getCpersoncode());
		 map.put("realName",entity.get(0).getCpersonname());

		 res.setCode("0");
		 res.setMsg("成功");
		 res.setData(map);

		 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
	 }

	 /**
	  * 根据工单id（条码）获取工单信息
	  * @param sheetId
	  * @return
	  */
	 @RequestMapping(value = "getOrderBySheetId")
	 @ResponseBody
	 public ResponseEntity<ResHttpStatus> getOrderBySheetId(String sheetId) {

		 ResHttpStatus res = new ResHttpStatus();
		 if (StringUtils.isBlank(sheetId)) {
			 res.setCode("1");
			 res.setMsg("人员编码不能为空-userId");
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }

		 QueryWrapper<FbsWorkingStatistics> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("personnel_Code", sheetId).eq("del_flag", 0);
		 List<FbsWorkingStatistics> list = fbsWorkingStatisticsService.list(queryWrapper);
		 if(list.size() < 1){
			 res.setCode("1");
			 res.setMsg("人员编码不正确"+sheetId);
			 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);
		 }
		 FbsWorkingStatistics entity = list.get(0);
		 Map<String,Object> map = new HashMap<>();
		 map.put("orderCode",entity.getOrderNumber());
		 map.put("rowNumber",entity.getRowNumber());
		 map.put("materialCode",entity.getMaterielCode());
		 map.put("materialName",entity.getMaterielName());
		 map.put("startDate",entity.getStartDate());
		 map.put("endDate",entity.getEndDate());

		 res.setCode("0");
		 res.setMsg("成功");
		 res.setData(map);

		 return new ResponseEntity<ResHttpStatus>(res, HttpStatus.OK);

	 }

	 /**
	  * 分页列表查询
	 * @param fbsWorkingStatistics
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<IPage<FbsWorkingStatistics>> queryPageList(FbsWorkingStatistics fbsWorkingStatistics,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<FbsWorkingStatistics>> result = new Result<IPage<FbsWorkingStatistics>>();
		QueryWrapper<FbsWorkingStatistics> queryWrapper = QueryGenerator.initQueryWrapper(fbsWorkingStatistics, req.getParameterMap());
		Page<FbsWorkingStatistics> page = new Page<FbsWorkingStatistics>(pageNo, pageSize);
		IPage<FbsWorkingStatistics> pageList = fbsWorkingStatisticsService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	/**
	  *   添加
	 * @param fbsWorkingStatistics
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<FbsWorkingStatistics> add(@RequestBody FbsWorkingStatistics fbsWorkingStatistics) {
		Result<FbsWorkingStatistics> result = new Result<FbsWorkingStatistics>();
		try {
			fbsWorkingStatisticsService.save(fbsWorkingStatistics);
			result.success("添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}
	
	/**
	  *  编辑
	 * @param fbsWorkingStatistics
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<FbsWorkingStatistics> edit(@RequestBody FbsWorkingStatistics fbsWorkingStatistics) {
		Result<FbsWorkingStatistics> result = new Result<FbsWorkingStatistics>();
		FbsWorkingStatistics fbsWorkingStatisticsEntity = fbsWorkingStatisticsService.getById(fbsWorkingStatistics.getId());
		if(fbsWorkingStatisticsEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = fbsWorkingStatisticsService.updateById(fbsWorkingStatistics);
			//TODO 返回false说明什么？
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<FbsWorkingStatistics> delete(@RequestParam(name="id",required=true) String id) {
		Result<FbsWorkingStatistics> result = new Result<FbsWorkingStatistics>();
		FbsWorkingStatistics fbsWorkingStatistics = fbsWorkingStatisticsService.getById(id);
		if(fbsWorkingStatistics==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = fbsWorkingStatisticsService.removeById(id);
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<FbsWorkingStatistics> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<FbsWorkingStatistics> result = new Result<FbsWorkingStatistics>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.fbsWorkingStatisticsService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<FbsWorkingStatistics> queryById(@RequestParam(name="id",required=true) String id) {
		Result<FbsWorkingStatistics> result = new Result<FbsWorkingStatistics>();
		FbsWorkingStatistics fbsWorkingStatistics = fbsWorkingStatisticsService.getById(id);
		if(fbsWorkingStatistics==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(fbsWorkingStatistics);
			result.setSuccess(true);
		}
		return result;
	}

	 @RequestMapping(value = "getEsopFileList")
	 public Result<List<Map<String,Object>>> getEsopFileList(String code){
		 Result<List<Map<String,Object>>> result=new Result<>();
	 	 ArrayList<String> list = new ArrayList<>();
		 String par = FileList.getAllFileName(upLoadPath+code,list);

		 if(par.equals("false")){
			 result.error500("文件为空");
			 return result;
		 }

		 List<Map<String,Object>> resuleMap = new ArrayList<>();
		 try {

		 	for (String o:list){
				Map<String,Object> map = new HashMap<>();
				map.put("fileName",o);
				map.put("fileType",getFileOf(o).toLowerCase());
				resuleMap.add(map);
			}
			result.setResult(resuleMap);
			result.success("操作成功");

		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }

		 return result;
	 }

	 @RequestMapping(value = "getVedioFileList")
	 public Result<List<Map<String,Object>>> getVedioFileList(){
		 Result<List<Map<String,Object>>> result=new Result<>();
		 ArrayList<String> list = new ArrayList<>();
		 FileList.getAllFileName(upVedio,list);
		 List<Map<String,Object>> resuleMap = new ArrayList<>();
		 try {
			 Collections.sort(list); // 升序排列
			 for (String o:list){
				 Map<String,Object> map = new HashMap<>();
				 map.put("fileName",o);
				 map.put("fileType",getFileOf(o).toLowerCase());
				 String fileType = getFileOf(o).toLowerCase();
				 if(fileType.equals("jpg") || fileType.equals("jpeg") || fileType.equals("png")){
					 map.put("isImg",true);
				 }else if(fileType.equals("avi") || fileType.equals("mov") || fileType.equals("rmvb") || fileType.equals("rm")
						 || fileType.equals("flv") || fileType.equals("mp4") || fileType.equals("3gp")){
					 map.put("isImg",false);
				 }else{
					continue;
				 }
				 resuleMap.add(map);
//				 break;
			 }
			 result.setResult(resuleMap);
			 result.success("操作成功");

		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }

		 return result;
	 }


	 /**
	  * 获取文件后缀名
	  * @param fileName
	  * @return
	  */
	 public String getFileOf(String fileName) {
		 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
		 return suffix;
	 }
     public static String getFileBefore(String fileName) {
         String suffix = fileName.substring(0,fileName.lastIndexOf("."));
         return suffix;
     }

//	 public static void main(String[] args) {
//		 File file = new File("HelloWorld.java");
//		 String fileName = file.getName();
//		 System.out.println(fileName.lastIndexOf(".") + 1);
//		 System.out.println("======================");
//		 System.out.println(fileName.lastIndexOf("."));
//		 System.out.println("======================");
//		 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
//		 System.out.println(suffix);
//	 }

	 /**
	  * @Author chenchuang
	  * @Description //TODO 制造看板/产线看板： 月/日工时分布情况
	  * @Date 2020/10/28 13:20
	  * @Param []
	  * @return org.jeecg.common.api.vo.Result<java.util.Map<java.lang.String,java.lang.Object>>
	  */
	 @GetMapping(value = "/getByMaterial")
	 public Result<Map<String,Object>> getByMaterial(String type){
		 Result<Map<String,Object>> result=new Result<>();
		 try {
			 String dateString="%Y-%m";//月
			 if(type!=null&&type.equals("1")){
				 dateString="%Y-%m-%d";
			 }
			 Map<String,Object> map=fbsWorkingStatisticsService.getByMaterial(dateString);
			 result.setResult(map);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }


	 public static void main(String[] args) {
		 /*Map<String, String> map = new TreeMap<String, String>();
		 map.put("d", "44");
		 map.put("b", "2");
		 map.put("a", "3");
		 map.put("c", "4");

		 //这里将map.entrySet()转换成list
		 List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet());
		 //然后通过比较器来实现排序
		 Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
			 //升序排序
			 public int compare(Map.Entry<String, String> o1,
								Map.Entry<String, String> o2) {
				 return o1.getValue().compareTo(o2.getValue());
			 }

		 });

		 for(Map.Entry<String,String> mapping:list){
			 System.out.println(mapping.getKey()+":"+mapping.getValue());
		 }*/

//		 List<Integer> list = Arrays.asList(10,1,6,4,8,7,9,3,2,5);
//		 Collections.sort(list); // 升序排列
//		 list.forEach(n ->{System.out.print(n+", ");});
//         System.out.println(getFileBefore("sssss.jpj"));
	 }

 }
