package com.sw.mes.yb.production.controller;

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.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
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.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import com.sw.mes.yb.production.entity.ProcessNoDiameter;
import com.sw.mes.yb.production.dto.ProcessNoDiameterDto;
import com.sw.mes.yb.production.service.ProcessNoDiameterService;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@RestController
@RequestMapping("/ProcessNoDiameter")
@Slf4j
@Api(description = "12小时未等径接口")
public class ProcessNoDiameterController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private ProcessNoDiameterService processNoDiameterService;

	@ApiOperation(value = "分页查询12小时未等径列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
		@ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
	})
	@GetMapping("/listByPage")
	public Result<ResponsePage<ProcessNoDiameterDto>> listByPage(ServletRequest request,@ApiIgnore RequestPage requestPage) {
		Specification<ProcessNoDiameter> specification = DynamicSpecifications.bySearchFilter(request, ProcessNoDiameter.class);
    	Page<ProcessNoDiameter> page = processNoDiameterService.findByExample(specification, PageUtil.createPageable(requestPage));
		List<ProcessNoDiameterDto> dtos = page.getContent().stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
		ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
	}

	@ApiOperation(value = "获取所有12小时未等径")
	@GetMapping("/listAll")
	public Result<List<ProcessNoDiameterDto>> listAll(ServletRequest request) {
		Specification<ProcessNoDiameter> specification = DynamicSpecifications.bySearchFilter(request, ProcessNoDiameter.class);
		List<ProcessNoDiameter> processNoDiameters = processNoDiameterService.findByExampleSort(specification, null);
		List<ProcessNoDiameterDto> processNoDiameterDtos = processNoDiameters.stream()
					.map(processNoDiameter -> toLazyDto(processNoDiameter))
					.collect(Collectors.toList());
		return ResultGenerator.success(processNoDiameterDtos);
	}

	@ApiOperation(value = "12小时未等径详细信息")
	@GetMapping("/{id}")
	public Result<ProcessNoDiameterDto> get(@PathVariable @ApiParam(value="12小时未等径id") Long id) {
		ProcessNoDiameter processNoDiameter = processNoDiameterService.get(id);
		if (null == processNoDiameter){
			return ResultGenerator.fail("该12小时未等径不存在");
		}
		return ResultGenerator.success(toEagerDto(processNoDiameter));
	}

	@ApiOperation(value = "创建12小时未等径")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "12小时未等径对象") ProcessNoDiameterDto processNoDiameterDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		ProcessNoDiameter processNoDiameter = fromDto(processNoDiameterDto);
		processNoDiameterService.saveOrUpdate(processNoDiameter);
		return ResultGenerator.success();
	}

    @ApiOperation(value = "更新12小时未等径")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "12小时未等径") ProcessNoDiameterDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
        if (dto.getId() == null){
            return ResultGenerator.fail("id不能为空");
		}
		ProcessNoDiameter entity = processNoDiameterService.get(dto.getId());
        if (entity == null){
            return ResultGenerator.fail("原信息不存在");
		}
        try {
            ProcessNoDiameter entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			processNoDiameterService.saveOrUpdate(entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }

	@ApiOperation(value = "删除12小时未等径")
	@PostMapping("/delete")
	public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Integer[] ids) {
		for (Integer id : ids) {
				processNoDiameterService.delete(id);
		}
		return ResultGenerator.success();
	}

	public ProcessNoDiameterDto toLazyDto(ProcessNoDiameter entity) {
		return modelMapper.map(entity, ProcessNoDiameterDto.class);
	}

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

	public ProcessNoDiameter fromDto(ProcessNoDiameterDto dto) {
		return modelMapper.map(dto, ProcessNoDiameter.class);
	}


}
