package com.sw.mes.fb.aio.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.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.fb.aio.dto.CutProcessRecordDto;
import com.sw.mes.fb.aio.entity.CutProcessRecord;
import com.sw.mes.fb.aio.service.CutProcessRecordService;
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;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@RestController
@RequestMapping("/CutProcessRecord")
@Slf4j
@Api(description = "截断数据上传接口")
public class CutProcessRecordController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private CutProcessRecordService cutProcessRecordService;

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

	@ApiOperation(value = "获取所有截断数据上传")
	@GetMapping("/listAll")
	public Result<List<CutProcessRecordDto>> listAll(ServletRequest request) {
		Specification<CutProcessRecord> specification = DynamicSpecifications.bySearchFilter(request, CutProcessRecord.class);
		List<CutProcessRecord> cutProcessRecords = cutProcessRecordService.findByExampleSort(specification, null);
		List<CutProcessRecordDto> cutProcessRecordDtos = cutProcessRecords.stream()
					.map(cutProcessRecord -> toLazyDto(cutProcessRecord))
					.collect(Collectors.toList());
		return ResultGenerator.success(cutProcessRecordDtos);
	}

	@ApiOperation(value = "截断数据上传详细信息")
	@GetMapping("/{id}")
	public Result<CutProcessRecordDto> get(@PathVariable @ApiParam(value="截断数据上传id") Long id) {
		CutProcessRecord cutProcessRecord = cutProcessRecordService.get(id);
		if (null == cutProcessRecord){
			return ResultGenerator.fail("该截断数据上传不存在");
		}
		return ResultGenerator.success(toEagerDto(cutProcessRecord));
	}

	@ApiOperation(value = "创建截断数据上传")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "截断数据上传对象") CutProcessRecordDto cutProcessRecordDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		CutProcessRecord cutProcessRecord = fromDto(cutProcessRecordDto);
		cutProcessRecordService.saveOrUpdate(cutProcessRecord);
		return ResultGenerator.success();
	}

    @ApiOperation(value = "更新截断数据上传")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "截断数据上传") CutProcessRecordDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
        if (dto.getId() == null){
            return ResultGenerator.fail("id不能为空");
		}
		CutProcessRecord entity = cutProcessRecordService.get(dto.getId());
        if (entity == null){
            return ResultGenerator.fail("原信息不存在");
		}
        try {
            CutProcessRecord entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			cutProcessRecordService.saveOrUpdate(entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }

	@ApiOperation(value = "删除截断数据上传")
	@PostMapping("/delete")
	public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Integer[] ids) {
		for (Integer id : ids) {
				cutProcessRecordService.delete(id);
		}
		return ResultGenerator.success();
	}

	public CutProcessRecordDto toLazyDto(CutProcessRecord entity) {
		return modelMapper.map(entity, CutProcessRecordDto.class);
	}

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

	public CutProcessRecord fromDto(CutProcessRecordDto dto) {
		return modelMapper.map(dto, CutProcessRecord.class);
	}


}
