package com.sw.mes.sto.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.sto.entity.LogStoMesInteractive;
import com.sw.mes.sto.dto.LogStoMesInteractiveDto;
import com.sw.mes.sto.service.LogStoMesInteractiveService;
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;

/**
 * 大基地直流STO调拨创建和冲销数据通信记录Controller
 *
 * @author liru
 * @date 2024-01-16
 */
@Controller
@Jinko(value="大基地直流STO调拨创建和冲销数据通信记录Controller",author = "liru")
@RequestMapping("/sto/interactive")
public class LogStoMesInteractiveController
{
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private LogStoMesInteractiveService logStoMesInteractiveService;

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

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

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

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

    public LogStoMesInteractiveDto toLazyDto(LogStoMesInteractive entity) {
        return modelMapper.map(entity, LogStoMesInteractiveDto.class);
    }

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

    public LogStoMesInteractive fromDto(LogStoMesInteractiveDto dto) {
        return modelMapper.map(dto, LogStoMesInteractive.class);
    }
}
