package com.sw.mes.production.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.production.dto.TruncationReccutDetailsDto;
import com.sw.mes.production.entity.TruncationReccut;
import com.sw.mes.production.entity.TruncationReccutDetails;
import com.sw.mes.production.service.TruncationReccutDetailsService;
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.Map;
import java.util.stream.Collectors;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@RestController
@RequestMapping("/truncationReccutDetails")
@Slf4j
@Api(description = "截断反切详情接口")
public class TruncationReccutDetailsController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private TruncationReccutDetailsService truncationReccutDetailsService;

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

    @ApiOperation(value = "截断反切详情详细信息")
    @GetMapping("/{id}")
    public Result<TruncationReccutDetailsDto> get(@PathVariable @ApiParam(value = "截断反切详情id") Long id) {
        TruncationReccutDetails truncationReccutDetails = truncationReccutDetailsService.get(id);
        if (null == truncationReccutDetails)
            return ResultGenerator.fail("该截断反切详情不存在");
        return ResultGenerator.success(toEagerDto(truncationReccutDetails));
    }

    @ApiOperation(value = "创建截断反切详情")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "截断反切详情对象") TruncationReccutDetailsDto truncationReccutDetailsDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        TruncationReccutDetails truncationReccutDetails = fromDto(truncationReccutDetailsDto);
        truncationReccutDetailsService.saveOrUpdate(truncationReccutDetails);
        return ResultGenerator.success();
    }

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

    @ApiOperation(value = "PAD根据晶段编号查看数据是否存在划线表/或长晶棒表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "no", value = "晶段编号", dataType = "String", paramType = "query")
    })
    @GetMapping("/findDetailByNo")
    public Result<Map<String, Object>> findDetailByNo(String no) {
        return ResultGenerator.success(truncationReccutDetailsService.findBySectionNo(no));
    }


    public TruncationReccutDetailsDto toLazyDto(TruncationReccutDetails entity) {
        return modelMapper.map(entity, TruncationReccutDetailsDto.class);
    }

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

    public TruncationReccutDetails fromDto(TruncationReccutDetailsDto dto) {
        return modelMapper.map(dto, TruncationReccutDetails.class);
    }


}
