package org.jeecg.modules.fbsWorkshopDispatchList.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.fbsInventory.entity.FbsInventory;
import org.jeecg.modules.fbsInventory.service.IFbsInventoryService;
import org.jeecg.modules.fbsMomOrder.entity.FbsMomOrder;
import org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail;
import org.jeecg.modules.fbsMomOrder.service.IFbsMomOrderService;
import org.jeecg.modules.fbsMomOrder.service.IFbsMomOrderdetailService;
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 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.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.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-13
 * @version： V1.0
 */
@RestController
@RequestMapping("/fbsWorkshopDispatchList/fbsWorkshopDispatchList")
@Slf4j
public class FbsWorkshopDispatchListController {
	@Autowired
	private IFbsWorkshopDispatchListService fbsWorkshopDispatchListService;

	 @Autowired
	 IFbsPersonService iFbsPersonService;

	 @Autowired
	 IFbsMomOrderService iFbsMomOrderService;

	 @Autowired
	 IFbsInventoryService iFbsInventoryService;
	 @Autowired
	 IFbsMomOrderdetailService iFbsMomOrderdetailService;



	/**
	  * 分页列表查询
	 * @param fbsWorkshopDispatchList
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<IPage<FbsWorkshopDispatchList>> queryPageList(FbsWorkshopDispatchList fbsWorkshopDispatchList,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<FbsWorkshopDispatchList>> result = new Result<IPage<FbsWorkshopDispatchList>>();
		String start=fbsWorkshopDispatchList.getStart();
		String end=fbsWorkshopDispatchList.getEnd();
		if(StringUtils.isNotBlank(start)){
			fbsWorkshopDispatchList.setStart(null);
		}
		if(StringUtils.isNotBlank(end)){
			fbsWorkshopDispatchList.setEnd(null);
		}
		String state=fbsWorkshopDispatchList.getState();
		if(StringUtils.isNotBlank(state)){
			fbsWorkshopDispatchList.setState(null);
		}
		String personnelIdU8=fbsWorkshopDispatchList.getPersonnelIdU8();
		if(StringUtils.isNotBlank(personnelIdU8)&&personnelIdU8.equals("null")){
			fbsWorkshopDispatchList.setPersonnelIdU8(null);
		}
		QueryWrapper<FbsWorkshopDispatchList> queryWrapper = QueryGenerator.initQueryWrapper(fbsWorkshopDispatchList, req.getParameterMap());
		queryWrapper.lambda().eq(FbsWorkshopDispatchList::getDelFlag,"0");
		if(StringUtils.isNotBlank(start)&&StringUtils.isNotBlank(end)&&!start.equals("null")&&!end.equals("null")){
			queryWrapper.ge("DATE_FORMAT(date,'%Y-%m-%d')",start);
			queryWrapper.le("DATE_FORMAT(date,'%Y-%m-%d')",end);
		}
		if(StringUtils.isNotBlank(state)&&!state.equals("null")){
			if(state.equals("4")){
				queryWrapper.lambda().eq(FbsWorkshopDispatchList::getState,"3");
				queryWrapper.apply("latest_completed_quantity <> number_of_tasks");
			}else if(state.equals("3")){
				queryWrapper.lambda().eq(FbsWorkshopDispatchList::getState,"3");
				queryWrapper.apply("latest_completed_quantity = number_of_tasks");
			}else{
				queryWrapper.lambda().eq(FbsWorkshopDispatchList::getState,state);
			}
		}
		queryWrapper.lambda().orderByDesc(FbsWorkshopDispatchList::getCreateTime);
		Page<FbsWorkshopDispatchList> page = new Page<FbsWorkshopDispatchList>(pageNo, pageSize);
		IPage<FbsWorkshopDispatchList> pageList = fbsWorkshopDispatchListService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	/**
	  *   添加
	 * @param fbsWorkshopDispatchList
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<FbsWorkshopDispatchList> add(@RequestBody FbsWorkshopDispatchList fbsWorkshopDispatchList) {
		Result<FbsWorkshopDispatchList> result = new Result<FbsWorkshopDispatchList>();
		try {
			fbsWorkshopDispatchListService.save(fbsWorkshopDispatchList);
			result.success("添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}
	
	/**
	  *  编辑
	 * @param fbsWorkshopDispatchList
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<FbsWorkshopDispatchList> edit(@RequestBody FbsWorkshopDispatchList fbsWorkshopDispatchList) {
		Result<FbsWorkshopDispatchList> result = new Result<FbsWorkshopDispatchList>();
		FbsWorkshopDispatchList fbsWorkshopDispatchListEntity = fbsWorkshopDispatchListService.getById(fbsWorkshopDispatchList.getId());
		if(fbsWorkshopDispatchListEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = fbsWorkshopDispatchListService.updateById(fbsWorkshopDispatchList);
			//TODO 返回false说明什么？
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}

	 @PutMapping(value = "/editR")
	 public Result<FbsWorkshopDispatchList> editR(@RequestBody FbsWorkshopDispatchList fbsWorkshopDispatchList) {
		 Result<FbsWorkshopDispatchList> result = new Result<FbsWorkshopDispatchList>();
		 FbsWorkshopDispatchList fbsWorkshopDispatchListEntity = fbsWorkshopDispatchListService.getById(fbsWorkshopDispatchList.getId());
		 if(fbsWorkshopDispatchListEntity==null) {
			 result.error500("未找到对应实体");
		 }else {
			 boolean ok = fbsWorkshopDispatchListService.updateById(fbsWorkshopDispatchList);
			 //根据订单号和产品查询对应的单据已近派工了多少数量
			 LambdaQueryWrapper<FbsWorkshopDispatchList> lambdaQueryWrapper=new LambdaQueryWrapper<>();
			 lambdaQueryWrapper.eq(FbsWorkshopDispatchList::getProductionOrderNumberId,fbsWorkshopDispatchList.getProductionOrderNumberId());
			 lambdaQueryWrapper.eq(FbsWorkshopDispatchList::getProductId,fbsWorkshopDispatchList.getProductId());
			 lambdaQueryWrapper.eq(FbsWorkshopDispatchList::getDelFlag,'0');
			 List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.list(lambdaQueryWrapper);
			 BigDecimal bigDecimal=new BigDecimal("0.0");
			 //获取总数量
			 if(fbsWorkshopDispatchListList!=null&&fbsWorkshopDispatchListList.size()>0){
				 for(FbsWorkshopDispatchList dispatchList:fbsWorkshopDispatchListList){
					 bigDecimal=bigDecimal.add(dispatchList.getNumberOfTasks());
				 }
			 }
			 //计划数量减去所有派工数量 获取剩余数量
			 fbsWorkshopDispatchList.setRemainingQuantity(fbsWorkshopDispatchList.getPlannedQuantity().subtract(bigDecimal));
			 fbsWorkshopDispatchListService.updateById(fbsWorkshopDispatchList);
			 //TODO 返回false说明什么？
			 if(ok) {
				 result.success("修改成功!");
			 }
		 }

		 return result;
	 }
	
	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<FbsWorkshopDispatchList> delete(@RequestParam(name="id",required=true) String id) {
		Result<FbsWorkshopDispatchList> result = new Result<FbsWorkshopDispatchList>();
		FbsWorkshopDispatchList fbsWorkshopDispatchList = fbsWorkshopDispatchListService.getById(id);
		if(fbsWorkshopDispatchList==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = fbsWorkshopDispatchListService.removeById(id);
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<FbsWorkshopDispatchList> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<FbsWorkshopDispatchList> result = new Result<FbsWorkshopDispatchList>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.fbsWorkshopDispatchListService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<FbsWorkshopDispatchList> queryById(@RequestParam(name="id",required=true) String id) {
		Result<FbsWorkshopDispatchList> result = new Result<FbsWorkshopDispatchList>();
		FbsWorkshopDispatchList fbsWorkshopDispatchList = fbsWorkshopDispatchListService.getById(id);
		if(fbsWorkshopDispatchList==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(fbsWorkshopDispatchList);
			result.setSuccess(true);
		}
		return result;
	}

  /**
      * 导出excel
   *
   * @param request
   * @param response
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
      // Step.1 组装查询条件
      QueryWrapper<FbsWorkshopDispatchList> queryWrapper = null;
      try {
          String paramsStr = request.getParameter("paramsStr");
          if (oConvertUtils.isNotEmpty(paramsStr)) {
              String deString = URLDecoder.decode(paramsStr, "UTF-8");
              FbsWorkshopDispatchList fbsWorkshopDispatchList = JSON.parseObject(deString, FbsWorkshopDispatchList.class);
              queryWrapper = QueryGenerator.initQueryWrapper(fbsWorkshopDispatchList, request.getParameterMap());
          }
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      }

      //Step.2 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      List<FbsWorkshopDispatchList> pageList = fbsWorkshopDispatchListService.list(queryWrapper);
      //导出文件名称
      mv.addObject(NormalExcelConstants.FILE_NAME, "车间派工单列表");
      mv.addObject(NormalExcelConstants.CLASS, FbsWorkshopDispatchList.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("车间派工单列表数据", "导出人:Jeecg", "导出信息"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
  }

  /**
      * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<FbsWorkshopDispatchList> listFbsWorkshopDispatchLists = ExcelImportUtil.importExcel(file.getInputStream(), FbsWorkshopDispatchList.class, params);
              for (FbsWorkshopDispatchList fbsWorkshopDispatchListExcel : listFbsWorkshopDispatchLists) {
                  fbsWorkshopDispatchListService.save(fbsWorkshopDispatchListExcel);
              }
              return Result.ok("文件导入成功！数据行数：" + listFbsWorkshopDispatchLists.size());
          } catch (Exception e) {
              log.error(e.getMessage());
              return Result.error("文件导入失败！");
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.ok("文件导入失败！");
  }

  /**
  * @Author chenchuang
  * @Description //TODO 删除全部
  * @Date 2020/10/14 15:51
  * @Param [id]
  * @return org.jeecg.common.api.vo.Result<org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList>
  */
	 @DeleteMapping(value = "/deleteAndChild")
	 public Result<FbsWorkshopDispatchList> deleteAndChild(@RequestParam(name="id",required=true) String id) {
		 Result<FbsWorkshopDispatchList> result = new Result<FbsWorkshopDispatchList>();
		 FbsWorkshopDispatchList fbsWorkshopDispatchList = fbsWorkshopDispatchListService.getById(id);
		 if(fbsWorkshopDispatchList==null) {
			 result.error500("未找到对应实体");
		 }else {
			 fbsWorkshopDispatchList.setDelFlag("1");
			 boolean ok = fbsWorkshopDispatchListService.updateById(fbsWorkshopDispatchList);
			 if(ok) {
				 result.success("删除成功!");
			 }
		 }

		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 查询U8人员信息
	 * @Date 2020/10/16 16:18
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsPerson.entity.FbsPerson>>
	 */
	 @GetMapping(value = "/getPerAndDerp")
	 public Result<List<FbsPerson>> getPerAndDerp(){
	 	Result<List<FbsPerson>> result =new Result<>();
		 try {
			 List<FbsPerson> fbsPersonList=iFbsPersonService.getPerAndDerp();
			 result.setResult(fbsPersonList);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	  * @Author chenchuang
	  * @Description //TODO U8生产订单查询
	  * @Date 2020/10/16 16:18
	  * @Param []
	  * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsPerson.entity.FbsPerson>>
	  */
	 @GetMapping(value = "/getFbsMomOrder")
	 public Result<List<FbsMomOrderdetail>> getFbsMomOrder(){
		 Result<List<FbsMomOrderdetail>> result =new Result<>();
		 try {
//		 	 QueryWrapper<FbsMomOrder> fbsMomOrderQueryWrapper=new QueryWrapper<>();
//		 	 fbsMomOrderQueryWrapper.apply("substring( MoCode, 3, 2 ) = substring( CONVERT ( VARCHAR ( 100 ), GETDATE ( ), 112 ), 3, 2 )");
//		 	 fbsMomOrderQueryWrapper.lambda().orderByDesc(FbsMomOrder::getCreatetime);
//			 List<FbsMomOrder> fbsMomOrderList=iFbsMomOrderService.list(fbsMomOrderQueryWrapper);
//			 result.setResult(fbsMomOrderList);
			 FbsMomOrderdetail fbsMomOrderdetail=new FbsMomOrderdetail();
			 List<FbsMomOrderdetail> fbsMomOrderdetailList= iFbsMomOrderdetailService.getMonthListStatre(fbsMomOrderdetail);
			 //剩余数量为0的remove
			 getFbsMomOrderdetailList(fbsMomOrderdetailList);
			 result.setResult(fbsMomOrderdetailList);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 public void getFbsMomOrderdetailList(List<FbsMomOrderdetail> fbsMomOrderdetailList){
		 for(int i=0; i<fbsMomOrderdetailList.size(); i++){
		 	LambdaQueryWrapper<FbsWorkshopDispatchList> fbsWorkshopDispatchListLambdaQueryWrapper=new LambdaQueryWrapper<>();
		 	fbsWorkshopDispatchListLambdaQueryWrapper.eq(FbsWorkshopDispatchList::getDelFlag,"0");
		 	fbsWorkshopDispatchListLambdaQueryWrapper.eq(FbsWorkshopDispatchList::getProductionOrderNumberName,fbsMomOrderdetailList.get(i).getMocode());
		 	fbsWorkshopDispatchListLambdaQueryWrapper.eq(FbsWorkshopDispatchList::getProductId,fbsMomOrderdetailList.get(i).getCinvcode());
			 fbsWorkshopDispatchListLambdaQueryWrapper.eq(FbsWorkshopDispatchList::getRemainingQuantity,0.0);
		 	List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.list(fbsWorkshopDispatchListLambdaQueryWrapper);
		 	if(fbsWorkshopDispatchListList!=null&&fbsWorkshopDispatchListList.size()>0){
					fbsMomOrderdetailList.remove(fbsMomOrderdetailList.get(i));
					i--;
			}

		 }
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 根据生产订单id查询产品
	 * @Date 2020/10/16 19:03
	 * @Param [moId]
	 * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsInventory.entity.FbsInventory>>
	 */
	 @GetMapping(value = "/getInventoryGetByOrderId")
	 public Result<List<FbsInventory>> getInventoryGetByOrderId(String moId){
		 Result<List<FbsInventory>> result =new Result<>();
		 try {
			 List<FbsInventory> fbsInventoryList=iFbsInventoryService.getInventoryGetByOrderId(moId);
			 result.setResult(fbsInventoryList);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }

		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 首页报表
	 * @Date 2020/10/17 12:13
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList>>
	 */
	 @GetMapping(value = "/getSumDispatchReport")
	 public Result<List<FbsWorkshopDispatchList>> getSumDispatchReport(FbsWorkshopDispatchList fbsWorkshopDispatchList){
		 Result<List<FbsWorkshopDispatchList>> result=new Result<>();
		 try {
			 FbsMomOrderdetail fbsMomOrderdetail=new FbsMomOrderdetail();
			 if(StringUtils.isNotBlank(fbsWorkshopDispatchList.getProductionOrderNumberName())&&fbsWorkshopDispatchList.getProductionOrderNumberName().equals("null")){
				 fbsWorkshopDispatchList.setProductionOrderNumberName(null);
			 }
			 fbsMomOrderdetail.setMocode(fbsWorkshopDispatchList.getProductionOrderNumberName());
			 //未审核数据
			 List<FbsMomOrderdetail> fbsMomOrderdetailList= iFbsMomOrderdetailService.getMonthListStatre(fbsMomOrderdetail);
			 //已审核当月数据
			 List<FbsMomOrderdetail> fbsMomOrderdetailList2= iFbsMomOrderdetailService.getMonthListStatreReviewed(fbsMomOrderdetail);
			 fbsMomOrderdetailList.addAll(fbsMomOrderdetailList2);

			 List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.getSumDispatch(fbsMomOrderdetailList);
			 result.setResult(fbsWorkshopDispatchListList);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 @GetMapping(value = "/export")
	 public Result<List<FbsWorkshopDispatchList>> export(String month){
		 Result<List<FbsWorkshopDispatchList>> result=new Result<>();
		 try {
		 	//erp数据
			 List<FbsMomOrderdetail> fbsMomOrderdetailList= iFbsMomOrderdetailService.getMonthListNotStatre(month);
			 //派工数据
			 List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.export(fbsMomOrderdetailList,month);
			 result.setResult(fbsWorkshopDispatchListList);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }


	 /**
	 * @Author chenchuang
	 * @Description //TODO 制造看板/产线看板： 月/日工时分布情况
	 * @Date 2020/10/19 14:12
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList>>
	 */
	 @GetMapping(value = "/getProductHoursMonthReport")
	 public Result<Map<String,Object>> getProductHoursMonthReport(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=fbsWorkshopDispatchListService.getProductHoursMonthReport(dateString);
			 result.setResult(map);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 制造看板/产线看板：月/日质量异常分类
	 * @Date 2020/10/19 16:34
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.Map<java.lang.String,java.lang.Object>>
	 */
	 @GetMapping(value = "/getAbnormalHoursMonthReport")
	 public Result<Map<String,Object>> getAbnormalHoursMonthReport(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=fbsWorkshopDispatchListService.getAbnormalHoursMonthReport(dateString);
			 result.setResult(map);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 产线看板：当日质量异常分类
	 * @Date 2020/10/27 11:35
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.Map<java.lang.String,java.lang.Object>>
	 */
	 @GetMapping(value = "/getAbnormalHoursMonthReportSameDay")
	 public Result<Map<String,Object>> getAbnormalHoursMonthReportSameDay(){
		 Result<Map<String,Object>> result=new Result<>();
		 try {
			 Map<String,Object> map=fbsWorkshopDispatchListService.getAbnormalHoursMonthReportSameDay();
			 result.setResult(map);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 人员出勤率
	 * @Date 2020/10/21 14:20
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.List<java.util.Map<java.lang.String,java.lang.Object>>>
	 */
	 @GetMapping(value = "/getAttendance")
	 public Result<List<Map<String,Object>>> getAttendance(){
		 Result<List<Map<String,Object>>> result=new Result<>();
		 try {
			 List<Map<String,Object>> map=fbsWorkshopDispatchListService.getAttendance();
			 result.setResult(map);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 标准工时与实际工时对比
	 * @Date 2020/10/21 23:07
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList>>
	 */
	 @GetMapping(value = "/getWorkingHoursContrast")
	 public Result<List<FbsWorkshopDispatchList>> getWorkingHoursContrast(){
		 Result<List<FbsWorkshopDispatchList>> result=new Result<>();
		 try {
			 List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.getWorkingHoursContrast();
			 result.setResult(fbsWorkshopDispatchListList);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO  制造看板/产线看板:月/日生产效率
	 * @Date 2020/10/22 14:09
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.lang.String>
	 */
	 @GetMapping(value = "/getProductionEfficiency")
	 public Result<String> getProductionEfficiency(String type){
		 Result<String> result=new Result<>();
		 try {
		 	 String dateString="%Y-%m";
		 	 if(type!=null&&type.equals("1")){
		 	 	dateString="%Y-%m-%d";
			 }
			 String str=fbsWorkshopDispatchListService.getProductionEfficiency(dateString);
			 if(StringUtils.isNotBlank(str)){
			 	result.setResult(str);
			 }else{
			 	result.setResult("0");
			 }
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 产线看板：当日达成率
	 * @Date 2020/10/22 15:38
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.Map<java.lang.String,java.lang.Object>>
	 */
	 @GetMapping(value = "/getAchievementRate")
	 public Result<Map<String,Object>> getAchievementRate(){
		 Result<Map<String,Object>> result=new Result<>();
		 try {
			 Map<String,Object> map=fbsWorkshopDispatchListService.getAchievementRate();
			 result.setResult(map);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 产线看板：当日考勤
	 * @Date 2020/10/26 15:05
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.Map<java.lang.String,java.lang.Object>>
	 */
	 @GetMapping(value = "/getAttendanceOfTheDay")
	 public Result<Map<String,Object>> getAttendanceOfTheDay(){
		 Result<Map<String,Object>> result=new Result<>();
		 try {
			 Map<String,Object> map=fbsWorkshopDispatchListService.getAttendanceOfTheDay();
			 result.setResult(map);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 产线看板：当前生产信息
	 * @Date 2020/10/26 16:58
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList>>
	 */
	 @GetMapping(value = "/getDoingListByOrder")
	 public Result<List<FbsWorkshopDispatchList>> getDoingListByOrder(){
		 Result<List<FbsWorkshopDispatchList>> result=new Result<>();

		 try {
			 //派工数据及完工信息
			 List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.getProductList();
			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			 if(fbsWorkshopDispatchListList!=null){
                 List<String> stringList=new ArrayList<>();//存储库存产品id(code)
                 for(FbsWorkshopDispatchList dispatchList:fbsWorkshopDispatchListList){
                     	if(StringUtils.isNotBlank(dispatchList.getProductId())){
                        	stringList.add(dispatchList.getProductId());
                    	}
                    	if(dispatchList.getEndDate()!=null){
							dispatchList.setEndDate(sdf.parse(sdf.format(dispatchList.getEndDate())));
						}
                	}
                if(stringList.size()>0){
                     //根据库存产品id查询U8库存产品获取规格
                    List<FbsInventory> fbsInventoryList=iFbsInventoryService.getListByIds(stringList);
                    //循环当前数据跟U8数据对应获取数据
                    for(FbsWorkshopDispatchList dispatchList:fbsWorkshopDispatchListList){
                        for(FbsInventory fbsInventory:fbsInventoryList){
                            //判断寻找对应的库存产品id
                            if(dispatchList.getProductId().equals(fbsInventory.getCinvcode())){
                                //获取规格
                                dispatchList.setCinvstd(fbsInventory.getCinvstd());
                            }
                        }
                    }
                }
                result.setResult(fbsWorkshopDispatchListList);
                 result.success("操作成功");
             }else{
                 result.setResult(new ArrayList<>());
                 result.success("未查询到数据");
             }
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.success("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 产线看板：生产计划
	 * @Date 2020/10/30 11:08
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList>>
	 */
	@GetMapping(value = "/getListNotStarted")
	 public Result<List<FbsWorkshopDispatchList>> getListNotStarted(){
		 Result<List<FbsWorkshopDispatchList>> result=new Result<>();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		String format = sdf.format(new Date());
//		 QueryWrapper<FbsWorkshopDispatchList> queryWrapper=new QueryWrapper<>();
//		queryWrapper.lambda().eq(FbsWorkshopDispatchList::getDelFlag,"0");
//		queryWrapper.lambda().eq(FbsWorkshopDispatchList::getState,"0");
//		queryWrapper.eq("date_format( date, '%Y-%m-%d' )",format);
//		queryWrapper.lambda().orderByDesc(FbsWorkshopDispatchList::getDate);
//		 List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.list(queryWrapper);
		List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.getListWhereStartEnd();

		 result.setResult(fbsWorkshopDispatchListList);
		 result.success("操作成功");
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 制造看板： 项目进度完工百分比
	 * @Date 2020/10/31 16:13
	 * @Param []
	 * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList>>
	 */
	 @GetMapping(value = "/getProjectScheduleCompletion")
	 public Result<List<FbsWorkshopDispatchList>> getProjectScheduleCompletion(){
		 Result<List<FbsWorkshopDispatchList>> result=new Result<>();
		 try {
			 List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.getProjectScheduleCompletion();
			 result.setResult(fbsWorkshopDispatchListList);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 派工单后台批量新增
	 * @Date 2020/11/3 11:21
	 * @Param [fbsWorkshopDispatchList]
	 * @return org.jeecg.common.api.vo.Result<org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList>
	 */
	 @PostMapping(value = "/workSaveBatch")
	 public Result<FbsWorkshopDispatchList> workSaveBatch(@RequestBody FbsWorkshopDispatchList fbsWorkshopDispatchList){
		 Result<FbsWorkshopDispatchList> result=new Result<>();
		 try {
			 fbsWorkshopDispatchListService.workSaveBatch(fbsWorkshopDispatchList);
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

	 /**
	 * @Author chenchuang
	 * @Description //TODO 根据订单号和产品查询对应的单据已近派工了多少数量
	 * @Date 2020/11/18 13:32
	 * @Param [fbsWorkshopDispatchList]
	 * @return org.jeecg.common.api.vo.Result<java.lang.String>
	 */
	 @GetMapping(value = "/getDispatchNumberByOrderIdProductId")
	 public Result<String> getDispatchNumberByOrderIdProductId(FbsWorkshopDispatchList fbsWorkshopDispatchList){
		 Result<String> result=new Result<>();

		 try {
		 	//根据订单号和产品查询对应的单据已近派工了多少数量
			 LambdaQueryWrapper<FbsWorkshopDispatchList> lambdaQueryWrapper=new LambdaQueryWrapper<>();
			 lambdaQueryWrapper.eq(FbsWorkshopDispatchList::getProductionOrderNumberId,fbsWorkshopDispatchList.getProductionOrderNumberId());
			 lambdaQueryWrapper.eq(FbsWorkshopDispatchList::getProductId,fbsWorkshopDispatchList.getProductId());
			 lambdaQueryWrapper.eq(FbsWorkshopDispatchList::getDelFlag,'0');
			 List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListService.list(lambdaQueryWrapper);
			 BigDecimal bigDecimal=new BigDecimal("0.0");
			 //获取总数量
			 if(fbsWorkshopDispatchListList!=null&&fbsWorkshopDispatchListList.size()>0){
                for(FbsWorkshopDispatchList dispatchList:fbsWorkshopDispatchListList){
                    bigDecimal=bigDecimal.add(dispatchList.getNumberOfTasks());
                }
             }
			 result.setResult(bigDecimal.doubleValue()+"");
			 result.success("操作成功");
		 } catch (Exception e) {
			 e.printStackTrace();
			 result.error500("操作失败："+e.getMessage());
		 }
		 return result;
	 }

}
