/**   
 * @Title: SendSapWarehousingStatisticsInfoYbController.java 
 * @Package com.sw.mes.webservice.controller 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author 中环 keily  
 * @date 2022年3月1日 下午4:37:12 
 * @version V1.0   
 */   
     
package com.sw.mes.webservice.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sw.mes.core.response.ResponsePage;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.core.utils.DynamicSpecifications;
import com.sw.mes.core.utils.PageUtil;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.webservice.dto.SendSapWarehousingStatisticsInfoYbDto;
import com.sw.mes.webservice.entity.SendSapWarehousingStatisticsInfoYb;
import com.sw.mes.webservice.service.SendSapWarehousingStatisticsInfoYbService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: SendSapWarehousingStatisticsInfoYbController 
 * @Description: 向sap发送的gr圆棒入库数据记录表
 * @author 中环 keily
 * @date 2022年3月1日 下午4:37:12 
 *  
 */
@RestController
@RequestMapping(value = "/SendSapWarehousingStatisticsInfoYb")
@Slf4j
@Api(description = "向sap发送的gr圆棒入库数据记录表接口", value = "向sap发送的gr圆棒入库数据记录表接口")
public class SendSapWarehousingStatisticsInfoYbController {

	@Autowired
	ModelMapper modelMapper;
	
	@Autowired
	private SendSapWarehousingStatisticsInfoYbService sendSapWarehousingStatisticsInfoYbService;
	
	@ApiOperation(value="分页查询向sap发送的gr圆棒入库数据记录表列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name="rows", value = "行数", dataType = "integer", paramType = "query"),
		@ApiImplicitParam(name="page", value = "页码", dataType = "integer", paramType = "query")
	})
	@GetMapping("/listByPage")
	public Result<ResponsePage<SendSapWarehousingStatisticsInfoYbDto>> listByPage(ServletRequest request,@ApiIgnore RequestPage requestPage) {
		Specification<SendSapWarehousingStatisticsInfoYb> specification = DynamicSpecifications.bySearchFilter(request, SendSapWarehousingStatisticsInfoYb.class);
    	Page<SendSapWarehousingStatisticsInfoYb> page = sendSapWarehousingStatisticsInfoYbService.findByExample(specification, PageUtil.createPageable(requestPage));
		List<SendSapWarehousingStatisticsInfoYbDto> dtos = page.getContent().stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
		ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
	}
	
	@ApiOperation(value = "获取所有向sap发送的gr圆棒入库数据记录表")
	@GetMapping("/listAll")
	public Result<List<SendSapWarehousingStatisticsInfoYbDto>> listAll(ServletRequest request) {
		Specification<SendSapWarehousingStatisticsInfoYb> specification = DynamicSpecifications.bySearchFilter(request, SendSapWarehousingStatisticsInfoYb.class);
		List<SendSapWarehousingStatisticsInfoYb> sendSapWarehousingStatisticsInfoYbs = sendSapWarehousingStatisticsInfoYbService.findByExampleSort(specification, null);
		List<SendSapWarehousingStatisticsInfoYbDto> sendSapWarehousingStatisticsInfoYbDtos = sendSapWarehousingStatisticsInfoYbs.stream()
					.map(sendSapWarehousingStatisticsInfoYb -> toLazyDto(sendSapWarehousingStatisticsInfoYb))
					.collect(Collectors.toList());
		return ResultGenerator.success(sendSapWarehousingStatisticsInfoYbDtos);
	}
	
	@ApiOperation(value = "sap发送的gr圆棒入库数据记录表详细信息")
	@GetMapping("/{id}")
	public Result<SendSapWarehousingStatisticsInfoYbDto> get(@PathVariable @ApiParam(value="SendSapWarehousingStatisticsInfoYbid") Integer id) {
		SendSapWarehousingStatisticsInfoYb sendSapWarehousingStatisticsInfoYb = sendSapWarehousingStatisticsInfoYbService.get(id);
		if (null == sendSapWarehousingStatisticsInfoYb)
			return ResultGenerator.fail("该sap发送的gr圆棒入库数据记录表不存在");
		return ResultGenerator.success(toEagerDto(sendSapWarehousingStatisticsInfoYb));
	}
	
	@ApiOperation(value = "创建sap发送的gr圆棒入库数据记录表")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "sap发送的gr圆棒入库数据记录表对象") SendSapWarehousingStatisticsInfoYbDto sendSapWarehousingStatisticsInfoYbDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		SendSapWarehousingStatisticsInfoYb sendSapWarehousingStatisticsInfoYb = fromDto(sendSapWarehousingStatisticsInfoYbDto);
		sendSapWarehousingStatisticsInfoYbService.saveOrUpdate(sendSapWarehousingStatisticsInfoYb);
		return ResultGenerator.success();
	}
	
	@ApiOperation(value = "更新sap发送的gr圆棒入库数据记录表")
	@PostMapping("/update")
	public Result update(@RequestBody @Valid @ApiParam(value = "sap发送的gr圆棒入库数据记录表") SendSapWarehousingStatisticsInfoYbDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        if (dto.getId() == null)
            return ResultGenerator.fail("id不能为空");
            SendSapWarehousingStatisticsInfoYb entity = sendSapWarehousingStatisticsInfoYbService.get(dto.getId());
        if (entity == null)
            return ResultGenerator.fail("原信息不存在");
        try {
            SendSapWarehousingStatisticsInfoYb entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			sendSapWarehousingStatisticsInfoYbService.saveOrUpdate(entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }
	
	@ApiOperation(value = "删除sap发送的gr圆棒入库数据记录表")
	@PostMapping("/delete")
	public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Integer[] ids) {
		for (Integer id : ids) {
				sendSapWarehousingStatisticsInfoYbService.delete(id);
		}
		return ResultGenerator.success();
	}
	
	@ApiOperation(value = "再次向sap发送的gr圆棒入库数据记录表")
	@PostMapping("/retrySend")
	public Result retrySend(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Integer[] ids) {
		try {
			StringBuilder sb = new StringBuilder();
			String msg = sendSapWarehousingStatisticsInfoYbService.retrySend(ids);
			sb.append(msg);
			return ResultGenerator.success(sb.toString(), null);
		} catch (Exception e) {
			return ResultGenerator.fail(e.getMessage());
		}
	}
	

	
	public SendSapWarehousingStatisticsInfoYbDto toLazyDto(SendSapWarehousingStatisticsInfoYb entity) {
		return modelMapper.map(entity, SendSapWarehousingStatisticsInfoYbDto.class);
	}

	public SendSapWarehousingStatisticsInfoYbDto toEagerDto(SendSapWarehousingStatisticsInfoYb entity) {
		SendSapWarehousingStatisticsInfoYbDto dto = modelMapper.map(entity, SendSapWarehousingStatisticsInfoYbDto.class);
		return dto;
	}

	public SendSapWarehousingStatisticsInfoYb fromDto(SendSapWarehousingStatisticsInfoYbDto dto) {
		return modelMapper.map(dto, SendSapWarehousingStatisticsInfoYb.class);
	}
}
