package com.sw.mes.fb.checkout.controller;

import com.sw.mes.core.dto.mes.pc.fb.FbCheckOutDataDto;
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.fb.checkout.dto.OperateCheckOutDataLogDto;
import com.sw.mes.fb.checkout.entity.OperateCheckOutDataLog;
import com.sw.mes.fb.checkout.service.OperateCheckOutDataLogService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
* @Author
* @Description： 方棒检验数据库录入记录
* @Date: Create in
* @Modified By:
*/
@RestController
@RequestMapping("/operateCheckOutDataLog")
@Slf4j
@Api(description = "OperateCheckOutDataLog接口")
public class OperateCheckOutDataLogController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private OperateCheckOutDataLogService operateCheckOutDataLogService;

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

	@ApiOperation(value = "获取所有OperateCheckOutDataLog")
	@GetMapping("/listAll")
	public Result<List<OperateCheckOutDataLogDto>> listAll(ServletRequest request) {
		Specification<OperateCheckOutDataLog> specification = DynamicSpecifications.bySearchFilter(request, OperateCheckOutDataLog.class);
		List<OperateCheckOutDataLog> operateCheckOutDataLogs = operateCheckOutDataLogService.findByExampleSort(specification, null);
		List<OperateCheckOutDataLogDto> operateCheckOutDataLogDtos = operateCheckOutDataLogs.stream()
					.map(operateCheckOutDataLog -> toLazyDto(operateCheckOutDataLog))
					.collect(Collectors.toList());
		return ResultGenerator.success(operateCheckOutDataLogDtos);
	}

	@ApiOperation(value = "OperateCheckOutDataLog详细信息")
	@GetMapping("/{id}")
	public Result<OperateCheckOutDataLogDto> get(@PathVariable @ApiParam(value="OperateCheckOutDataLogid") Long id) {
		OperateCheckOutDataLog operateCheckOutDataLog = operateCheckOutDataLogService.get(id);
		if (null == operateCheckOutDataLog)
			return ResultGenerator.fail("该OperateCheckOutDataLog不存在");
		return ResultGenerator.success(toEagerDto(operateCheckOutDataLog));
	}

	@ApiOperation(value = "创建OperateCheckOutDataLog")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "OperateCheckOutDataLog对象") OperateCheckOutDataLogDto operateCheckOutDataLogDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		OperateCheckOutDataLog operateCheckOutDataLog = fromDto(operateCheckOutDataLogDto);
		operateCheckOutDataLogService.saveOrUpdate(operateCheckOutDataLog);
		return ResultGenerator.success();
	}

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

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

	@ApiOperation(value = "方棒检验数据记录信息")
	@ApiImplicitParams({@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "search_EQ_fbSectionNo", value = "方棒晶段编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "search_EQ_ybSectionNo", value = "圆棒晶棒编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "search_EQ_checkUserName", value = "检验人", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "search_GTE_checkTime", value = "检验开始时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "search_LT_checkTime", value = "检验结束时间", dataType = "string", paramType = "query")})
	@GetMapping("/getListPage")
	public Result<ResponsePage<FbCheckOutDataDto>> getCutRecordListPage(String search_EQ_fbSectionNo, String search_EQ_ybSectionNo, String search_EQ_checkUserName,
																		String search_GTE_checkTime, String search_LT_checkTime,
																		@ApiIgnore RequestPage requestPage) {
		requestPage.setOrderField(null);
		search_EQ_fbSectionNo = StringUtils.isBlank(search_EQ_fbSectionNo) ? null : search_EQ_fbSectionNo;
		search_EQ_ybSectionNo = StringUtils.isBlank(search_EQ_ybSectionNo) ? null : search_EQ_ybSectionNo;
		search_EQ_checkUserName = StringUtils.isBlank(search_EQ_checkUserName) ? null : search_EQ_checkUserName;
		search_GTE_checkTime = StringUtils.isBlank(search_GTE_checkTime) ? null : search_GTE_checkTime;
		search_LT_checkTime = StringUtils.isBlank(search_LT_checkTime) ? null : search_LT_checkTime;
		ResponsePage<FbCheckOutDataDto> responsePage = operateCheckOutDataLogService.getListPageInfo(search_EQ_fbSectionNo, search_EQ_ybSectionNo, search_EQ_checkUserName,
				search_GTE_checkTime,search_LT_checkTime,
				PageUtil.createPageable(requestPage));
		return ResultGenerator.success(responsePage);
	}

//	@ApiOperation(value = "方棒检验数据记录信息导出")
//	@ApiImplicitParams({@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
//			@ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
//			@ApiImplicitParam(name = "search_EQ_fbSectionNo", value = "方棒晶段编号", dataType = "string", paramType = "query"),
//			@ApiImplicitParam(name = "search_EQ_ybSectionNo", value = "圆棒晶棒编号", dataType = "string", paramType = "query"),
//			@ApiImplicitParam(name = "search_EQ_checkUserName", value = "检验人", dataType = "string", paramType = "query"),
//			@ApiImplicitParam(name = "search_GTE_checkTime", value = "检验开始时间", dataType = "string", paramType = "query"),
//			@ApiImplicitParam(name = "search_LT_checkTime", value = "检验结束时间", dataType = "string", paramType = "query")})
//	@GetMapping("/export")
//	public void cutExport(String search_EQ_fbSectionNo, String search_EQ_ybSectionNo, String search_EQ_checkUserName,
//						  String search_GTE_checkTime, String search_LT_checkTime,
//						  @ApiIgnore RequestPage requestPage, HttpServletResponse response) {
//		try {
//			requestPage.setOrderField(null);
//			search_EQ_fbSectionNo = StringUtils.isBlank(search_EQ_fbSectionNo) ? null : search_EQ_fbSectionNo;
//			search_EQ_ybSectionNo = StringUtils.isBlank(search_EQ_ybSectionNo) ? null : search_EQ_ybSectionNo;
//			search_EQ_checkUserName = StringUtils.isBlank(search_EQ_checkUserName) ? null : search_EQ_checkUserName;
//			search_GTE_checkTime = StringUtils.isBlank(search_GTE_checkTime) ? null : search_GTE_checkTime;
//			search_LT_checkTime = StringUtils.isBlank(search_LT_checkTime) ? null : search_LT_checkTime;
//			List<FbCheckOutDataDto> list1 = operateCheckOutDataLogService.getListInfo(search_EQ_fbSectionNo, search_EQ_ybSectionNo, search_EQ_checkUserName,
//					search_GTE_checkTime,search_LT_checkTime);
//			EasyExcelUtils.write(response, FbCheckOutDataDto.class, "方棒检验数据记录信息", "圆棒检验数据记录信息", list1);
//		} catch (Exception e) {
//			throw new RuntimeException("方棒检验数据记录信息导出失败，错误信息：" + e.getMessage());
//		}
//	}


	public OperateCheckOutDataLogDto toLazyDto(OperateCheckOutDataLog entity) {
		return modelMapper.map(entity, OperateCheckOutDataLogDto.class);
	}

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

	public OperateCheckOutDataLog fromDto(OperateCheckOutDataLogDto dto) {
		return modelMapper.map(dto, OperateCheckOutDataLog.class);
	}


}
