package com.sw.mes.yb.printer.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.BeanUtil;
import com.sw.mes.core.utils.DynamicSpecifications;
import com.sw.mes.core.utils.PageUtil;
import com.sw.mes.core.utils.RequestPage;
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.printer.entity.PrinterLog;
import com.sw.mes.yb.printer.dto.PrinterLogDto;
import com.sw.mes.yb.printer.service.PrinterLogService;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@RestController
@RequestMapping("/printerLog")
@Slf4j
@Api(description = "PrinterLog接口")
public class PrinterLogController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private PrinterLogService printerLogService;

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

	@ApiOperation(value = "获取所有PrinterLog")
	@GetMapping("/listAll")
	public Result<List<PrinterLogDto>> listAll(ServletRequest request) {
		Specification<PrinterLog> specification = DynamicSpecifications.bySearchFilter(request, PrinterLog.class);
		List<PrinterLog> printerLogs = printerLogService.findByExampleSort(specification, null);
		List<PrinterLogDto> printerLogDtos = printerLogs.stream()
					.map(printerLog -> toLazyDto(printerLog))
					.collect(Collectors.toList());
		return ResultGenerator.success(printerLogDtos);
	}

	@ApiOperation(value = "PrinterLog详细信息")
	@GetMapping("/{id}")
	public Result<PrinterLogDto> get(@PathVariable @ApiParam(value="PrinterLogid") Long id) {
		PrinterLog printerLog = printerLogService.get(id);
		if (null == printerLog)
			return ResultGenerator.fail("该PrinterLog不存在");
		return ResultGenerator.success(toEagerDto(printerLog));
	}

	@ApiOperation(value = "创建PrinterLog")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "PrinterLog对象") PrinterLogDto printerLogDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		PrinterLog printerLog = fromDto(printerLogDto);
		printerLogService.saveOrUpdate(printerLog);
		return ResultGenerator.success();
	}

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

	@ApiOperation(value = "删除PrinterLog")
	@PostMapping("/delete")
	public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestBody Long[] ids) {
		for (Long id : ids) {
				printerLogService.softDelete(id);
		}
		return ResultGenerator.success();
	}

	public PrinterLogDto toLazyDto(PrinterLog entity) {
		return modelMapper.map(entity, PrinterLogDto.class);
	}

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

	public PrinterLog fromDto(PrinterLogDto dto) {
		return modelMapper.map(dto, PrinterLog.class);
	}


}
