package com.sw.mes.sap.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.sap.entity.WarehouseLocation;
import com.sw.mes.sap.dto.WarehouseLocationDto;
import com.sw.mes.sap.service.WarehouseLocationService;
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;

/**
 * 晶科库存位置信息Controller
 * 
 * @author liru
 * @date 2024-01-02
 */
@Controller
@Jinko(value="晶科库存位置信息Controller",author = "liru")
@RequestMapping("/mes/location")
public class WarehouseLocationController
{
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private WarehouseLocationService warehouseLocationService;

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

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

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

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

    public WarehouseLocationDto toLazyDto(WarehouseLocation entity) {
        return modelMapper.map(entity, WarehouseLocationDto.class);
    }

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

    public WarehouseLocation fromDto(WarehouseLocationDto dto) {
        return modelMapper.map(dto, WarehouseLocation.class);
    }
}
