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

import com.sw.mes.core.context.BaseContextHandler;
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.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.sw.mes.yb.production.entity.InputFurnaceDetail;
import com.sw.mes.yb.production.dto.InputFurnaceDetailDto;
import com.sw.mes.yb.production.service.InputFurnaceDetailService;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@RestController
@RequestMapping("/productionInputFurnaceDetail")
@Slf4j
@Api(description = "灌料明细接口")
public class InputFurnaceDetailController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private InputFurnaceDetailService inputFurnaceDetailService;

	@ApiOperation(value = "分页查询灌料明细列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
		@ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
	})
	@GetMapping("/listByPage")
	public Result<ResponsePage<InputFurnaceDetailDto>> listByPage(ServletRequest request,@ApiIgnore RequestPage requestPage) {
		Specification<InputFurnaceDetail> specification = DynamicSpecifications.bySearchFilter(request, InputFurnaceDetail.class);
    	Page<InputFurnaceDetail> page = inputFurnaceDetailService.findByExample(specification, PageUtil.createPageable(requestPage));
		List<InputFurnaceDetailDto> 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<InputFurnaceDetailDto>> listAll(ServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("search_EQ_againTaskDetailId"))){
			return ResultGenerator.fail("灌料任务id不能为空");
		}
		Specification<InputFurnaceDetail> specification = DynamicSpecifications.bySearchFilter(request, InputFurnaceDetail.class);
		List<InputFurnaceDetail> inputFurnaceDetails = inputFurnaceDetailService.findByExampleSort(specification, null);
		List<InputFurnaceDetailDto> inputFurnaceDetailDtos = inputFurnaceDetails.stream()
					.map(inputFurnaceDetail -> toLazyDto(inputFurnaceDetail))
					.collect(Collectors.toList());
		return ResultGenerator.success(inputFurnaceDetailDtos);
	}

	@ApiOperation(value = "根据灌料任务Id获取所有灌料明细")
	@GetMapping("/getByTaskId")
	public Result<List<InputFurnaceDetailDto>> getByTaskId(@ApiParam(value = "灌料任务ID") Long taskId) {
		List<InputFurnaceDetail> inputFurnaceDetails = inputFurnaceDetailService.findByAgainTaskDetailId(taskId);
		List<InputFurnaceDetailDto> inputFurnaceDetailDtos = inputFurnaceDetails.stream()
				.map(inputFurnaceDetail -> toLazyDto(inputFurnaceDetail))
				.collect(Collectors.toList());
		return ResultGenerator.success(inputFurnaceDetailDtos);
	}


	@ApiOperation(value = "灌料明细详细信息")
	@GetMapping("/{id}")
	public Result<InputFurnaceDetailDto> get(@PathVariable @ApiParam(value="灌料明细id") Long id) {
		InputFurnaceDetail inputFurnaceDetail = inputFurnaceDetailService.get(id);
		if (null == inputFurnaceDetail) {
            return ResultGenerator.fail("该灌料明细不存在");
        }
		return ResultGenerator.success(toEagerDto(inputFurnaceDetail));
	}

	@ApiOperation(value = "创建灌料明细")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "灌料明细对象") InputFurnaceDetailDto inputFurnaceDetailDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		InputFurnaceDetail inputFurnaceDetail = fromDto(inputFurnaceDetailDto);
		inputFurnaceDetailService.saveOrUpdate(inputFurnaceDetail);
		return ResultGenerator.success();
	}

    @ApiOperation(value = "更新灌料明细")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "灌料明细") InputFurnaceDetailDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        if (dto.getId() == null) {
            return ResultGenerator.fail("id不能为空");
        }
            InputFurnaceDetail entity = inputFurnaceDetailService.get(dto.getId());
        if (entity == null) {
            return ResultGenerator.fail("原信息不存在");
        }
        try {
            InputFurnaceDetail entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			inputFurnaceDetailService.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 Long[] ids) {
		for (Long id : ids) {
				inputFurnaceDetailService.delete(id);
		}
		return ResultGenerator.success();
	}

	public InputFurnaceDetailDto toLazyDto(InputFurnaceDetail entity) {
		return modelMapper.map(entity, InputFurnaceDetailDto.class);
	}

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

	public InputFurnaceDetail fromDto(InputFurnaceDetailDto dto) {
		return modelMapper.map(dto, InputFurnaceDetail.class);
	}


}
