package com.sw.mes.wms.controller;

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.wms.entity.LogWmsMesInteractive;
import com.sw.mes.wms.dto.LogWmsMesInteractiveDto;
import com.sw.mes.wms.service.LogWmsMesInteractiveService;
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 com.sw.mes.core.annotation.Jinko;
import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Controller;

/**
 * 晶科WMS下发数据通信记录Controller
 *
 * @author liru
 * @date 2024-01-08
 */
@RestController
@Jinko(value="晶科WMS下发数据通信记录Controller",author = "liru")
@RequestMapping("/wms/interactive")
public class LogWmsMesInteractiveController
{
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private LogWmsMesInteractiveService logWmsMesInteractiveService;

    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<LogWmsMesInteractiveDto>> listByPage(ServletRequest request,@ApiIgnore RequestPage requestPage) {
        Specification<LogWmsMesInteractive> specification = DynamicSpecifications.bySearchFilter(request, LogWmsMesInteractive.class);
        Page<LogWmsMesInteractive> page = logWmsMesInteractiveService.findByExample(specification, PageUtil.createPageable(requestPage));
        List<LogWmsMesInteractiveDto> 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<LogWmsMesInteractiveDto>> listAll(ServletRequest request) {
        Specification<LogWmsMesInteractive> specification = DynamicSpecifications.bySearchFilter(request, LogWmsMesInteractive.class);
        List<LogWmsMesInteractive> logWmsMesInteractives = logWmsMesInteractiveService.findByExampleSort(specification, null);
        List<LogWmsMesInteractiveDto> logWmsMesInteractiveDtos = logWmsMesInteractives.stream().map(entity -> toLazyDto(entity)).collect(Collectors.toList());
        return ResultGenerator.success(logWmsMesInteractiveDtos);
    }

    @ApiOperation(value = "根据id获取详细信息")
    @GetMapping("/{id}")
    public Result<LogWmsMesInteractiveDto> get(@PathVariable @ApiParam(value="LogWmsMesInteractiveid") Long id) {
        LogWmsMesInteractive logWmsMesInteractive = logWmsMesInteractiveService.get(id);
        if (null == logWmsMesInteractive)
            return ResultGenerator.fail("该记录不存在");
        return ResultGenerator.success(toEagerDto(logWmsMesInteractive));
    }

    @ApiOperation(value = "创建数据")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "LogWmsMesInteractive对象") LogWmsMesInteractiveDto LogWmsMesInteractiveDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        LogWmsMesInteractive logWmsMesInteractive = fromDto(LogWmsMesInteractiveDto);
            logWmsMesInteractiveService.saveOrUpdate(logWmsMesInteractive);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "更新数据")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "LogWmsMesInteractive") LogWmsMesInteractiveDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        if (dto.getId() == null)
            return ResultGenerator.fail("id不能为空");
        LogWmsMesInteractive entity = logWmsMesInteractiveService.get(dto.getId());
        if (entity == null)
            return ResultGenerator.fail("原信息不存在");
        try {
            LogWmsMesInteractive entityfromFto = fromDto(dto);
            BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
                logWmsMesInteractiveService.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 Integer[] ids) {
        for (Integer id : ids) {
                logWmsMesInteractiveService.delete(id);
        }
        return ResultGenerator.success();
    }

    public LogWmsMesInteractiveDto toLazyDto(LogWmsMesInteractive entity) {
        return modelMapper.map(entity, LogWmsMesInteractiveDto.class);
    }

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

    public LogWmsMesInteractive fromDto(LogWmsMesInteractiveDto dto) {
        return modelMapper.map(dto, LogWmsMesInteractive.class);
    }
}
