//package com.sw.mes.yb.pad.controller;
//
//import com.sw.mes.base.factory.inspect.entity.DefaultCrystalAttrs;
//import com.sw.mes.base.factory.inspect.service.DefaultCrystalAttrService;
//import com.sw.mes.base.system.entity.DictionaryItem;
//import com.sw.mes.base.system.service.DictionaryItemService;
//import com.sw.mes.core.constant.Constants;
//import com.sw.mes.core.exception.BusinessException;
//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.yb.pad.dto.ProductionBackCuttingRecordDto;
//import com.sw.mes.yb.pad.dto.RecutInfoDetailInfo;
//import com.sw.mes.yb.pad.entity.ProductionBackCuttingRecord;
//import com.sw.mes.yb.pad.entity.SectionNoDetail;
//import com.sw.mes.yb.pad.service.ProductionRecutService;
//import com.sw.mes.yb.pad.service.SectionNoDetailService;
//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.math.BigDecimal;
//import java.util.List;
//import java.util.stream.Collectors;
//
//
///**
// * @Author
// * @Description：
// * @Date: Create in
// * @Modified By:
// */
//@RestController
//@RequestMapping("/ProductionBackCuttingRecord")
//@Slf4j
//@Api(description = "圆棒返切画线接口")
//public class ProductionRecutController1 {
//    @Autowired
//    ModelMapper modelMapper;
//
//    @Autowired
//    private ProductionRecutService productionRecutService;
//
//    @ApiOperation(value = "分页查询ProductionBackCuttingRecord列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
//            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
//    })
//    @GetMapping("/listByPage")
//    public Result<ResponsePage<ProductionBackCuttingRecordDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
//        Specification<ProductionBackCuttingRecord> specification = DynamicSpecifications.bySearchFilter(request, ProductionBackCuttingRecord.class);
//        Page<ProductionBackCuttingRecord> page = productionRecutService.findByExample(specification, PageUtil.createPageable(requestPage));
//        List<ProductionBackCuttingRecordDto> dtos = page.getContent().stream()
//                .map(entity -> toLazyDto(entity))
//                .collect(Collectors.toList());
//        ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
//        return ResultGenerator.success(responsePage);
//    }
//
//    @ApiOperation(value = "获取所有ProductionBackCuttingRecord")
//    @GetMapping("/listAll")
//    public Result<List<ProductionBackCuttingRecordDto>> listAll(ServletRequest request) {
//        Specification<ProductionBackCuttingRecord> specification = DynamicSpecifications.bySearchFilter(request, ProductionBackCuttingRecord.class);
//        List<ProductionBackCuttingRecord> productionBackCuttingRecords = productionRecutService.findByExampleSort(specification, null);
//        List<ProductionBackCuttingRecordDto> productionBackCuttingRecordDtos = productionBackCuttingRecords.stream()
//                .map(productionBackCuttingRecord -> toLazyDto(productionBackCuttingRecord))
//                .collect(Collectors.toList());
//        return ResultGenerator.success(productionBackCuttingRecordDtos);
//    }
//
//    @ApiOperation(value = "ProductionBackCuttingRecord详细信息")
//    @GetMapping("/{id}")
//    public Result<ProductionBackCuttingRecordDto> get(@PathVariable @ApiParam(value = "ProductionBackCuttingRecordid") Long id) {
//        ProductionBackCuttingRecord productionBackCuttingRecord = productionRecutService.get(id);
//        if (null == productionBackCuttingRecord) {
//            return ResultGenerator.fail("该ProductionBackCuttingRecord不存在");
//        }
//        return ResultGenerator.success(toEagerDto(productionBackCuttingRecord));
//    }
//
//    @Autowired
//    private DictionaryItemService dictionaryItemService;
//    @Autowired
//    private SectionNoDetailService sectionNoDetailService;
//
//    @Autowired
//    private DefaultCrystalAttrService defaultCrystalAttrService;
//
//    @ApiOperation(value = "创建ProductionBackCuttingRecord")
//    @PostMapping("/create")
//    public Result create(@RequestBody @Valid @ApiParam(value = "ProductionBackCuttingRecord对象") ProductionBackCuttingRecordDto productionBackCuttingRecordDto, BindingResult bindingResult) {
//        if (bindingResult.hasErrors()) {
//            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
//        }
//        int segmentCount = 0;
//        for (RecutInfoDetailInfo recutInfoDetailInfo : productionBackCuttingRecordDto.getDetail()) {
//            if ("晶段".equals(recutInfoDetailInfo.getType())) {
//                segmentCount++;
//            }
//        }
//        SectionNoDetail sectionNoDetail = sectionNoDetailService.findBySectionNo(productionBackCuttingRecordDto.getSegmentNo());
//        if (sectionNoDetail == null) {
//            throw new BusinessException("晶段编号不存在");
//        }
//        List<DictionaryItem> dictionaryItems = dictionaryItemService.findByThemeValueAndItemName(Constants.WEIGHT_RATIO, sectionNoDetail.getSpecs(), true);
//        if (dictionaryItems.size() == 0) {
//            throw new BusinessException("请配置【" + sectionNoDetail.getSpecs() + "】规格的理论重量系数");
//        }
//        BigDecimal weightLossCoefficient = NumberUtil.Object2BigDecimal(dictionaryItems.get(0).getValue());
//        if (weightLossCoefficient == null || weightLossCoefficient.compareTo(new BigDecimal("1")) < 1) {
//            throw new BusinessException("【" + dictionaryItems.get(0).getValue() + "】理论重量系数格式不正确，只能为大于0的数字类型");
//        }
//        productionBackCuttingRecordDto.setBackCutType(segmentCount > 1);
//        DefaultCrystalAttrs defaultCrystalAttrList = defaultCrystalAttrService.getDefaultCrystalAttrs(sectionNoDetail.getDopant(), sectionNoDetail.getSpecs(), null);
//        productionRecutService.createBackCuttingRecord(productionBackCuttingRecordDto, weightLossCoefficient, defaultCrystalAttrList, true);
//        return ResultGenerator.success();
//    }
//
//    @ApiOperation(value = "更新ProductionBackCuttingRecord")
//    @PostMapping("/update")
//    public Result update(@RequestBody @Valid @ApiParam(value = "ProductionBackCuttingRecord") ProductionBackCuttingRecordDto dto, BindingResult bindingResult) {
//        if (bindingResult.hasErrors()) {
//            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
//        }
//        if (dto.getId() == null) {
//            return ResultGenerator.fail("id不能为空");
//        }
//        ProductionBackCuttingRecord entity = productionRecutService.get(dto.getId());
//        if (entity == null) {
//            return ResultGenerator.fail("原信息不存在");
//        }
//        try {
//            ProductionBackCuttingRecord entityfromFto = fromDto(dto);
//            BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
//            productionRecutService.saveOrUpdate(entity);
//            return ResultGenerator.success();
//        } catch (Exception e) {
//            return ResultGenerator.fail(e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "删除ProductionBackCuttingRecord")
//    @PostMapping("/delete")
//    public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Long[] ids) {
//        for (Long id : ids) {
//            productionRecutService.deleteBackInfoDetail(id);
//        }
//        return ResultGenerator.success();
//    }
//
//    @ApiOperation(value = "托盘号获取晶段信息")
//    @GetMapping("/findSegmentInfo")
//    public Result findSegmentNoInfo(@ApiParam(value = "载具编号", type = "query") @RequestParam String trayNo) {
//
//        return this.productionRecutService.findSegmentNoInfo(trayNo);
//    }
//
//
//    @ApiOperation(value = "单晶编号获取晶段信息")
//    @GetMapping("/findInfoBySectionNo")
//    public Result findSegmentNoInfoBySectionNo(@ApiParam(value = "晶段编号", type = "query") @RequestParam String sectionNo) {
//
//        return this.productionRecutService.findSegmentNoInfoBySectionNo(sectionNo);
//    }
//
//    @ApiOperation(value = "单晶编号获取返切画线记录")
//    @GetMapping("/findBySectionNo")
//    public Result<ProductionBackCuttingRecord> findBySectionNo(@ApiParam(value = "晶段编号", type = "query") @RequestParam String sectionNo) {
//
//        return ResultGenerator.success(productionRecutService.findTopBySegmentNoOrderByIdDesc(sectionNo));
//    }
//
//    @ApiOperation(value = "获取单晶编号")
//    @PostMapping("/getSegmentNo")
//    public Result getSegmentNo(@ApiParam(value = "GetSegmentNoDto", type = "query") @RequestBody ProductionBackCuttingRecordDto productionBackCuttingRecordDto) {
//        return ResultGenerator.success(this.productionRecutService.getSegmentNo(productionBackCuttingRecordDto).getData());
//    }
//
//    public ProductionBackCuttingRecordDto toLazyDto(ProductionBackCuttingRecord entity) {
//        return modelMapper.map(entity, ProductionBackCuttingRecordDto.class);
//    }
//
//    public ProductionBackCuttingRecordDto toEagerDto(ProductionBackCuttingRecord entity) {
//        ProductionBackCuttingRecordDto dto = modelMapper.map(entity, ProductionBackCuttingRecordDto.class);
//        return dto;
//    }
//
//    public ProductionBackCuttingRecord fromDto(ProductionBackCuttingRecordDto dto) {
//        return modelMapper.map(dto, ProductionBackCuttingRecord.class);
//    }
//
//
//}
