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

import com.sw.mes.core.context.BaseContextHandler;
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.yb.production.dto.ProductionCircleMaterielDto;
import com.sw.mes.yb.production.entity.ProductionCircleMateriel;
import com.sw.mes.yb.production.service.ProductionCircleMaterielService;
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.jpa.domain.Specification;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
* @Author mgd
* @Description：
* @Date: Create in 2021年11月9日13:43:33
* @Modified By:
*/
@RestController
@RequestMapping("/productionCircleMateriel")
@Slf4j
@Api(description = "圆棒生产配料额定重量")
public class ProductionCircleMaterielController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private ProductionCircleMaterielService productionCircleMaterielService;


	@ApiOperation(value = "获取圆棒生产配料额定重量")
	@GetMapping("/listAll")
	public Result<List<ProductionCircleMaterielDto>> listAll(ServletRequest request) {
		Specification<ProductionCircleMateriel> specification = DynamicSpecifications.bySearchFilter(request, ProductionCircleMateriel.class);
		List<ProductionCircleMateriel> productionCircleMateriels = productionCircleMaterielService.findByExampleSort(specification, null);
		List<ProductionCircleMaterielDto> productionCircleMaterielDtos = productionCircleMateriels.stream()
					.map(productionCircleMateriel -> toLazyDto(productionCircleMateriel))
					.collect(Collectors.toList());
		return ResultGenerator.success(productionCircleMaterielDtos);
	}

    @ApiOperation(value = "更新圆棒生产配料额定重量")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "圆棒生产配料列表") ProductionCircleMaterielDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        if (dto.getId() == null) {
            return ResultGenerator.fail("id不能为空");
        }
            ProductionCircleMateriel entity = productionCircleMaterielService.get(dto.getId());
        if (entity == null) {
            return ResultGenerator.fail("原信息不存在");
        }
        try {
            ProductionCircleMateriel entityFromFto = fromDto(dto);
            entityFromFto.setCreateId(Long.valueOf(BaseContextHandler.getUserID()));
            entityFromFto.setUserName(BaseContextHandler.getUsername());
            entityFromFto.setUpdateDate(new Date());
			BeanUtils.copyProperties(entityFromFto, entity, BeanUtil.getIgnoreProperties(entityFromFto, null, null));
			productionCircleMaterielService.saveOrUpdate(entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }


	public ProductionCircleMaterielDto toLazyDto(ProductionCircleMateriel entity) {
		return modelMapper.map(entity, ProductionCircleMaterielDto.class);
	}

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

	public ProductionCircleMateriel fromDto(ProductionCircleMaterielDto dto) {
		return modelMapper.map(dto, ProductionCircleMateriel.class);
	}


}
