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.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.production.entity.EquipmentWorkHoursLog;
import com.sw.mes.yb.production.dto.EquipmentWorkHoursLogDto;
import com.sw.mes.yb.production.service.EquipmentWorkHoursLogService;


/**
* @Author
* @Description��
* @Date: Create in
* @Modified By:
*/
@RestController
@RequestMapping("/productionEquipmentWorkHoursLog")
@Slf4j
@Api(description = "���ػ�������ʱ��¼�ӿ�")
public class EquipmentWorkHoursLogController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private EquipmentWorkHoursLogService equipmentWorkHoursLogService;

	@ApiOperation(value = "��ҳ��ѯ���ػ�������ʱ��¼�б�")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rows", value = "����", dataType = "integer", paramType = "query"),
		@ApiImplicitParam(name = "page", value = "ҳ��", dataType = "integer", paramType = "query"),
	})
	@GetMapping("/listByPage")
	public Result<ResponsePage<EquipmentWorkHoursLogDto>> listByPage(ServletRequest request,@ApiIgnore RequestPage requestPage) {
		Specification<EquipmentWorkHoursLog> specification = DynamicSpecifications.bySearchFilter(request, EquipmentWorkHoursLog.class);
    	Page<EquipmentWorkHoursLog> page = equipmentWorkHoursLogService.findByExample(specification, PageUtil.createPageable(requestPage));
		List<EquipmentWorkHoursLogDto> 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<EquipmentWorkHoursLogDto>> listAll(ServletRequest request) {
		Specification<EquipmentWorkHoursLog> specification = DynamicSpecifications.bySearchFilter(request, EquipmentWorkHoursLog.class);
		List<EquipmentWorkHoursLog> equipmentWorkHoursLogs = equipmentWorkHoursLogService.findByExampleSort(specification, null);
		List<EquipmentWorkHoursLogDto> equipmentWorkHoursLogDtos = equipmentWorkHoursLogs.stream()
					.map(equipmentWorkHoursLog -> toLazyDto(equipmentWorkHoursLog))
					.collect(Collectors.toList());
		return ResultGenerator.success(equipmentWorkHoursLogDtos);
	}

	@ApiOperation(value = "���ػ�������ʱ��¼��ϸ��Ϣ")
	@GetMapping("/{id}")
	public Result<EquipmentWorkHoursLogDto> get(@PathVariable @ApiParam(value="���ػ�������ʱ��¼id") Long id) {
		EquipmentWorkHoursLog equipmentWorkHoursLog = equipmentWorkHoursLogService.get(id);
		if (null == equipmentWorkHoursLog) {
            return ResultGenerator.fail("�ù��ػ�������ʱ��¼������");
        }
		return ResultGenerator.success(toEagerDto(equipmentWorkHoursLog));
	}

	@ApiOperation(value = "�������ػ�������ʱ��¼")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "���ػ�������ʱ��¼����") EquipmentWorkHoursLogDto equipmentWorkHoursLogDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		EquipmentWorkHoursLog equipmentWorkHoursLog = fromDto(equipmentWorkHoursLogDto);
		equipmentWorkHoursLogService.saveOrUpdate(equipmentWorkHoursLog);
		return ResultGenerator.success();
	}

    @ApiOperation(value = "���¹��ػ�������ʱ��¼")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "���ػ�������ʱ��¼") EquipmentWorkHoursLogDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        if (dto.getId() == null) {
            return ResultGenerator.fail("id����Ϊ��");
        }
            EquipmentWorkHoursLog entity = equipmentWorkHoursLogService.get(dto.getId());
        if (entity == null) {
            return ResultGenerator.fail("ԭ��Ϣ������");
        }
        try {
            EquipmentWorkHoursLog entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			equipmentWorkHoursLogService.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) {
				equipmentWorkHoursLogService.delete(id);
		}
		return ResultGenerator.success();
	}

	public EquipmentWorkHoursLogDto toLazyDto(EquipmentWorkHoursLog entity) {
		return modelMapper.map(entity, EquipmentWorkHoursLogDto.class);
	}

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

	public EquipmentWorkHoursLog fromDto(EquipmentWorkHoursLogDto dto) {
		return modelMapper.map(dto, EquipmentWorkHoursLog.class);
	}


}
