package com.sw.mes.yb.agv.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.yb.agv.dto.AgvDispatchDeployDto;
import com.sw.mes.yb.agv.entity.AgvDispatchDeploy;
import com.sw.mes.yb.agv.service.AgvDispatchDeployService;

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;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@RestController
@RequestMapping("/AgvDispatchDeploy")
@Slf4j
@Api(description = "AGV调度配置接口")
public class AgvDispatchDeployController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private AgvDispatchDeployService agvDispatchDeployService;

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

	@ApiOperation(value = "获取所有AGV调度配置")
	@GetMapping("/listAll")
	public Result<List<AgvDispatchDeployDto>> listAll(ServletRequest request) {
		Specification<AgvDispatchDeploy> specification = DynamicSpecifications.bySearchFilter(request, AgvDispatchDeploy.class);
		List<AgvDispatchDeploy> agvDispatchDeploys = agvDispatchDeployService.findByExampleSort(specification, null);
		List<AgvDispatchDeployDto> agvDispatchDeployDtos = agvDispatchDeploys.stream()
					.map(agvDispatchDeploy -> toLazyDto(agvDispatchDeploy))
					.collect(Collectors.toList());
		return ResultGenerator.success(agvDispatchDeployDtos);
	}

	@ApiOperation(value = "AGV调度配置详细信息")
	@GetMapping("/{id}")
	public Result<AgvDispatchDeployDto> get(@PathVariable @ApiParam(value="AGV调度配置id") Long id) {
		AgvDispatchDeploy agvDispatchDeploy = agvDispatchDeployService.get(id);
		if (null == agvDispatchDeploy)
			return ResultGenerator.fail("该AGV调度配置不存在");
		return ResultGenerator.success(toEagerDto(agvDispatchDeploy));
	}

	@ApiOperation(value = "创建AGV调度配置")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "AGV调度配置对象") AgvDispatchDeployDto agvDispatchDeployDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		AgvDispatchDeploy agvDispatchDeploy = fromDto(agvDispatchDeployDto);
		agvDispatchDeployService.saveOrUpdate(agvDispatchDeploy);
		return ResultGenerator.success();
	}

    @ApiOperation(value = "更新AGV调度配置")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "AGV调度配置") AgvDispatchDeployDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        if (dto.getId() == null)
            return ResultGenerator.fail("id不能为空");
            AgvDispatchDeploy entity = agvDispatchDeployService.get(dto.getId());
        if (entity == null)
            return ResultGenerator.fail("原信息不存在");
        try {
            AgvDispatchDeploy entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			agvDispatchDeployService.saveOrUpdate(entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }

	@ApiOperation(value = "删除AGV调度配置")
	@PostMapping("/delete")
	public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Long[] ids) {
		for (Long id : ids) {
				agvDispatchDeployService.delete(id);
		}
		return ResultGenerator.success();
	}


	@ApiOperation(value = "根据关键字查询记录(可用于验证关键字是否重复)")
	@PostMapping("/getKeyword")
	public Result getKeyword(String keyword) {
		AgvDispatchDeploy deploy = agvDispatchDeployService.getByKeyword(keyword);
		return ResultGenerator.success(deploy);
	}

	public AgvDispatchDeployDto toLazyDto(AgvDispatchDeploy entity) {
		return modelMapper.map(entity, AgvDispatchDeployDto.class);
	}

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

	public AgvDispatchDeploy fromDto(AgvDispatchDeployDto dto) {
		return modelMapper.map(dto, AgvDispatchDeploy.class);
	}


}
