package com.sw.common.accessory.server.controller;

import com.sw.common.accessory.server.dto.LifeResumeDto;
import com.sw.common.accessory.server.entity.LifeResume;
import com.sw.common.accessory.server.service.LifeResumeService;
import com.sw.common.core.request.RequestPage;
import com.sw.common.core.response.ResponsePage;
import com.sw.common.core.response.Result;
import com.sw.common.core.response.ResultGenerator;
import com.sw.common.core.utils.BeanUtil;
import com.sw.common.mybatisplus.entity.Page;
import com.sw.common.mybatisplus.utils.DynamicSpecifications;
import com.sw.common.mybatisplus.utils.PageUtil;
import com.sw.common.mybatisplus.utils.Specification;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;


@RestController
@RequestMapping("/lifeResume")
@Slf4j
@Api(tags = "辅材寿命追溯接口")
public class LifeResumeController {
    @Autowired
    private LifeResumeService lifeResumeService;

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

    @ApiOperation(value = "获取所有辅材寿命追溯")
    @GetMapping("/listAll")
    public Result<List<LifeResumeDto>> listAll(ServletRequest request) {
        Specification<LifeResume> specification = DynamicSpecifications.bySearchFilter(request, LifeResume.class);
        List<LifeResume> lifeResumes = lifeResumeService.findByExampleSort(specification, null);
        List<LifeResumeDto> lifeResumeDtos = lifeResumes.stream()
            .map(this::toLazyDto)
            .collect(Collectors.toList());
        return ResultGenerator.success(lifeResumeDtos);
    }

    @ApiOperation(value = "辅材寿命追溯详细信息")
    @GetMapping("/{id}")
    public Result<LifeResumeDto> get(@PathVariable @ApiParam(value="辅材寿命追溯id") Long id) {
        LifeResume lifeResume = lifeResumeService.get(id);
        if (null == lifeResume){
            return ResultGenerator.fail("该辅材寿命追溯不存在");
        }
        return ResultGenerator.success(toEagerDto(lifeResume));
    }

    @ApiOperation(value = "创建辅材寿命追溯")
    @PostMapping("/create")
    public Result<String> create(@RequestBody @Valid @ApiParam(value = "辅材寿命追溯对象") LifeResumeDto lifeResumeDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
              return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        LifeResume lifeResume = fromDto(lifeResumeDto);
        lifeResumeService.saveOrUpdate(lifeResume);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "更新辅材寿命追溯")
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Valid @ApiParam(value = "辅材寿命追溯") LifeResumeDto dto, BindingResult bindingResult) {
    if (bindingResult.hasErrors()){
        return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
    }
    if (dto.getId() == null){
        return ResultGenerator.fail("id不能为空");
    }
    LifeResume entity = lifeResumeService.get(dto.getId());
    if (entity == null){
        return ResultGenerator.fail("原信息不存在");
    }
    try {
        LifeResume entityfromFto = fromDto(dto);
        BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
        lifeResumeService.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 Long[] ids) {
        for (Long id : ids) {
            lifeResumeService.delete(id);
        }
        return ResultGenerator.success();
    }

    public LifeResumeDto toLazyDto(LifeResume entity) {
        LifeResumeDto lifeResumeDto = new LifeResumeDto();
        BeanUtils.copyProperties(entity, lifeResumeDto);
        return lifeResumeDto;
    }

    public LifeResumeDto toEagerDto(LifeResume entity) {
        LifeResumeDto lifeResumeDto = new LifeResumeDto();
        BeanUtils.copyProperties(entity, lifeResumeDto);
        return lifeResumeDto;
    }

    public LifeResume fromDto(LifeResumeDto dto) {
        LifeResume lifeResume = new LifeResume();
        BeanUtils.copyProperties(dto, lifeResume);
        return lifeResume;
    }
}