package com.sw.mes.yb.wms.controller;

import com.sw.mes.base.system.entity.InventoryManageInfo;
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.yb.scheduled.LineScribeMarkDataScheduling;
import com.sw.mes.core.dto.mes.pc.yb.InventoryManageInfoDto;
import com.sw.mes.base.system.service.InventoryManageInfoService;
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.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@RestController
@RequestMapping("/InventoryManageInfo")
@Slf4j
@Api(description = "立库库存管理接口")
public class InventoryManageInfoController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private InventoryManageInfoService inventoryManageInfoService;

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

    @ApiOperation(value = "获取所有InventoryManageInfo")
    @GetMapping("/listAll")
    public Result<List<InventoryManageInfoDto>> listAll(ServletRequest request) {
        Specification<InventoryManageInfo> specification = DynamicSpecifications.bySearchFilter(request, InventoryManageInfo.class);
        List<InventoryManageInfo> inventoryManageInfos = inventoryManageInfoService.findByExampleSort(specification, null);
        List<InventoryManageInfoDto> inventoryManageInfoDtos = inventoryManageInfos.stream()
                .map(inventoryManageInfo -> toLazyDto(inventoryManageInfo))
                .collect(Collectors.toList());
        return ResultGenerator.success(inventoryManageInfoDtos);
    }

    @ApiOperation(value = "InventoryManageInfo详细信息")
    @GetMapping("/{id}")
    public Result<InventoryManageInfoDto> get(@PathVariable @ApiParam(value = "InventoryManageInfoid") Long id) {
        InventoryManageInfo inventoryManageInfo = inventoryManageInfoService.get(id);
        if (null == inventoryManageInfo)
            return ResultGenerator.fail("该InventoryManageInfo不存在");
        return ResultGenerator.success(toEagerDto(inventoryManageInfo));
    }

    @Autowired
    private LineScribeMarkDataScheduling lineScribeMarkDataScheduling;

    @ApiOperation(value = "创建InventoryManageInfo")
    @PostMapping("/push")
    public void push(@RequestBody @Valid @ApiParam(value = "InventoryManageInfo对象") InventoryManageInfoDto inventoryManageInfoDto, BindingResult bindingResult) {
        lineScribeMarkDataScheduling.lineScribeMarkDataScheduling();
    }

    @ApiOperation(value = "创建InventoryManageInfo")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "InventoryManageInfo对象") InventoryManageInfoDto inventoryManageInfoDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        InventoryManageInfo inventoryManageInfo = fromDto(inventoryManageInfoDto);
        inventoryManageInfoService.saveOrUpdate(inventoryManageInfo);
        return ResultGenerator.success();
    }

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

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

    public InventoryManageInfoDto toLazyDto(InventoryManageInfo entity) {
        return modelMapper.map(entity, InventoryManageInfoDto.class);
    }

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

    public InventoryManageInfo fromDto(InventoryManageInfoDto dto) {
        return modelMapper.map(dto, InventoryManageInfo.class);
    }


}
