package com.sw.mes.fb.turnstore.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.*;
import com.sw.mes.fb.turnstore.dto.RawMaterialReceivingDetailDto;
import com.sw.mes.fb.turnstore.entity.RawMaterialReceivingDetail;
import com.sw.mes.fb.turnstore.service.RawMaterialReceivingDetailService;
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("/RawMaterialReceivingDetail")
@Slf4j
@Api(description = "原料接收详情接口")
public class RawMaterialReceivingDetailController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private RawMaterialReceivingDetailService rawMaterialReceivingDetailService;

    @ApiOperation(value = "分页查询原料接收详情列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_trayId", value = "主表id", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<RawMaterialReceivingDetailDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {

        Specification<RawMaterialReceivingDetail> specification = DynamicSpecifications.bySearchFilter(request, RawMaterialReceivingDetail.class);
        Page<RawMaterialReceivingDetail> page = rawMaterialReceivingDetailService.findByExample(specification, PageUtil.createPageable(requestPage));
        List<RawMaterialReceivingDetailDto> dtos = page.getContent().stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
        ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
    }

    @ApiOperation(value = "获取所有原料接收详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search_EQ_trayId", value = "主表id", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/listAll")
    public Result<List<RawMaterialReceivingDetailDto>> listAll(ServletRequest request) {
        Specification<RawMaterialReceivingDetail> specification = DynamicSpecifications.bySearchFilter(request, RawMaterialReceivingDetail.class);
        List<RawMaterialReceivingDetail> rawMaterialReceivingDetails = rawMaterialReceivingDetailService.findByExampleSort(specification, null);
        List<RawMaterialReceivingDetailDto> rawMaterialReceivingDetailDtos = rawMaterialReceivingDetails.stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
        return ResultGenerator.success(rawMaterialReceivingDetailDtos);
    }

    @ApiOperation(value = "原料接收详情详细信息")
    @GetMapping("/{id}")
    public Result<RawMaterialReceivingDetailDto> get(@PathVariable @ApiParam(value = "原料接收详情id") Integer id) {
        RawMaterialReceivingDetail rawMaterialReceivingDetail = rawMaterialReceivingDetailService.get(id);
        if (null == rawMaterialReceivingDetail) {
            return ResultGenerator.fail("该原料接收详情不存在");
        }
        return ResultGenerator.success(toEagerDto(rawMaterialReceivingDetail));
    }

    @ApiOperation(value = "创建原料接收详情")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "原料接收详情对象") RawMaterialReceivingDetailDto rawMaterialReceivingDetailDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        RawMaterialReceivingDetail rawMaterialReceivingDetail = fromDto(rawMaterialReceivingDetailDto);
        rawMaterialReceivingDetailService.saveOrUpdate(rawMaterialReceivingDetail);
        return ResultGenerator.success();
    }

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

    public RawMaterialReceivingDetailDto toLazyDto(RawMaterialReceivingDetail entity) {
        return modelMapper.map(entity, RawMaterialReceivingDetailDto.class);
    }

    public RawMaterialReceivingDetailDto toEagerDto(RawMaterialReceivingDetail entity) {
        return modelMapper.map(entity, RawMaterialReceivingDetailDto.class);
    }

    public RawMaterialReceivingDetail fromDto(RawMaterialReceivingDetailDto dto) {
        return modelMapper.map(dto, RawMaterialReceivingDetail.class);
    }


}
