package com.sw.mes.yb.plan.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.yb.plan.entity.PlanProductionOrderBom;
import com.sw.mes.yb.plan.dto.PlanProductionOrderBomDto;
import com.sw.mes.yb.plan.service.PlanProductionOrderBomService;
import io.swagger.annotations.*;
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;

/**
 * 订单Bom信息Controller
 * 
 * @author liru
 * @date 2023-12-31
 */
@RestController
@Jinko(value="订单Bom信息Controller",author = "liru")
@RequestMapping("/mes/bom")
public class PlanProductionOrderBomController
{
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private PlanProductionOrderBomService planProductionOrderBomService;

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

    @ApiOperation(value = "根据id获取详细信息")
    @GetMapping("/{id}")
    public Result<PlanProductionOrderBomDto> get(@PathVariable @ApiParam(value="PlanProductionOrderBomid") Long id) {
        PlanProductionOrderBom PlanProductionOrderBom = planProductionOrderBomService.get(id);
        if (null == PlanProductionOrderBom)
            return ResultGenerator.fail("该记录不存在");
        return ResultGenerator.success(toEagerDto(PlanProductionOrderBom));
    }
    
    @ApiOperation(value = "创建数据")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "PlanProductionOrderBom对象") PlanProductionOrderBomDto PlanProductionOrderBomDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        PlanProductionOrderBom PlanProductionOrderBom = fromDto(PlanProductionOrderBomDto);
        planProductionOrderBomService.saveOrUpdate(PlanProductionOrderBom);
        return ResultGenerator.success();
    }

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

    public PlanProductionOrderBomDto toLazyDto(PlanProductionOrderBom entity) {
        return modelMapper.map(entity, PlanProductionOrderBomDto.class);
    }

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

    public PlanProductionOrderBom fromDto(PlanProductionOrderBomDto dto) {
        return modelMapper.map(dto, PlanProductionOrderBom.class);
    }
}
