package com.sw.mes.sap.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.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sw.mes.core.annotation.Jinko;
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.sap.dto.MesReconciliationDto;
import com.sw.mes.sap.entity.MesReconciliation;
import com.sw.mes.sap.service.MesReconciliationService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 晶科SAP下发生产工单通信记录Controller
 * 
 * @author liru
 * @date 2023-12-29
 */
@RestController
@Jinko(value="晶科SAP下发生产工单通信记录Controller",author = "liru")
@RequestMapping("/mes/mesReconciliation")
public class MesReconciliationController
{
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private MesReconciliationService mesReconciliationService;


    @ApiOperation(value = "获取所有数据")
    @GetMapping("/listAll")
    public Result<List<MesReconciliationDto>> listAll(ServletRequest request) {
        Specification<MesReconciliation> specification = DynamicSpecifications.bySearchFilter(request, MesReconciliation.class);
        List<MesReconciliation> mesReconciliations = mesReconciliationService.findByExampleSort(specification, null);
        List<MesReconciliationDto> mesReconciliationDtos = mesReconciliations.stream()
                .map(mesReconciliation -> toLazyDto(mesReconciliation))
                .collect(Collectors.toList());
        
        return ResultGenerator.success(mesReconciliationDtos);
    }

    @ApiOperation(value = "创建数据")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "MesReconciliation对象") MesReconciliationDto MesReconciliationDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        MesReconciliation mesReconciliation = fromDto(MesReconciliationDto);
        mesReconciliationService.saveOrUpdate(mesReconciliation);
        return ResultGenerator.success();
    }
    
    @ApiOperation(value = "开始对账")
    @PostMapping("/start")
    public Result start(@RequestBody MesReconciliationDto dto) {
    	try {
			mesReconciliationService.startReconciliation(dto.getCreateTimeStart(),dto.getCreateTimeEnd());
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return ResultGenerator.success();
    }

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

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

    public MesReconciliation fromDto(MesReconciliationDto dto) {
        return modelMapper.map(dto, MesReconciliation.class);
    }
}
