package com.sw.mes.yb.pad.controller;

import com.sw.mes.core.dto.ProductionPaintingLineDetailDto1;
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.pad.dto.ProductionPaintingLineDetailDto;
import com.sw.mes.yb.pad.entity.ProductionPaintingLineDetail;
import com.sw.mes.yb.pad.service.ProductionPaintingLineDetailService;
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("/ProductionPaintingLineDetail")
@Slf4j
@Api(description = "画线记录(晶段样片)详情表接口")
public class ProductionPaintingLineDetailController {
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private ProductionPaintingLineDetailService productionPaintingLineDetailService;

	@ApiOperation(value = "分页查询画线记录(晶段样片)详情表列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
		@ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
	})
	@GetMapping("/listByPage")
	public Result<ResponsePage<ProductionPaintingLineDetailDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
		requestPage.setOrderField("sequence");
		requestPage.setOrderDirection("ASC");
		Specification<ProductionPaintingLineDetail> specification = DynamicSpecifications.bySearchFilter(request, ProductionPaintingLineDetail.class);
    	Page<ProductionPaintingLineDetail> page = productionPaintingLineDetailService.findByExample(specification, PageUtil.createPageable(requestPage));
		List<ProductionPaintingLineDetailDto> dtos = page.getContent().stream()
                .map(entity -> toLazyDto(entity))
                .collect(Collectors.toList());
		for (ProductionPaintingLineDetailDto dto : dtos) {
			Integer type = dto.getType();
			if ( type == 0){
				dto.setSegmentType("晶段");
				continue;
			}
			if (type == 1){
				dto.setSegmentType("样片");
				continue;
			}
			if (type == 2){
				dto.setSegmentType("回收料");//20220902
				continue;
			}
			if (type == 3){
				dto.setSegmentType("直径异常回收料");
				continue;
			}

			if (type == 4){
				dto.setSegmentType("回收料");
				continue;
			}

		}
		ResponsePage responsePage = new ResponsePage(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
	}

	@ApiOperation(value = "获取所有画线记录(晶段样片)详情表")
	@GetMapping("/listAll")
	public Result<List<ProductionPaintingLineDetailDto>> listAll(ServletRequest request) {
		Specification<ProductionPaintingLineDetail> specification = DynamicSpecifications.bySearchFilter(request, ProductionPaintingLineDetail.class);
		List<ProductionPaintingLineDetail> productionPaintingLineDetails = productionPaintingLineDetailService.findByExampleSort(specification, null);
		List<ProductionPaintingLineDetailDto> productionPaintingLineDetailDtos = productionPaintingLineDetails.stream()
					.map(productionPaintingLineDetail -> toLazyDto(productionPaintingLineDetail))
					.collect(Collectors.toList());
		return ResultGenerator.success(productionPaintingLineDetailDtos);
	}

	@ApiOperation(value = "画线记录(晶段样片)详情表详细信息")
	@GetMapping("/{id}")
	public Result<ProductionPaintingLineDetailDto> get(@PathVariable @ApiParam(value="画线记录(晶段样片)详情表id") Integer id) {
		ProductionPaintingLineDetail productionPaintingLineDetail = productionPaintingLineDetailService.get(id);
		if (null == productionPaintingLineDetail) {
            return ResultGenerator.fail("该画线记录(晶段样片)详情表不存在");
        }
		return ResultGenerator.success(toEagerDto(productionPaintingLineDetail));
	}

	@ApiOperation(value = "创建画线记录(晶段样片)详情表")
	@PostMapping("/create")
	public Result create(@RequestBody @Valid @ApiParam(value = "画线记录(晶段样片)详情表对象") ProductionPaintingLineDetailDto productionPaintingLineDetailDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
		}
		ProductionPaintingLineDetail productionPaintingLineDetail = fromDto(productionPaintingLineDetailDto);
		productionPaintingLineDetailService.saveOrUpdate(productionPaintingLineDetail);
		return ResultGenerator.success();
	}

    @ApiOperation(value = "更新画线记录(晶段样片)详情表")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "画线记录(晶段样片)详情表") ProductionPaintingLineDetailDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        if (dto.getId() == null) {
            return ResultGenerator.fail("id不能为空");
        }
            ProductionPaintingLineDetail entity = productionPaintingLineDetailService.get(dto.getId());
        if (entity == null) {
            return ResultGenerator.fail("原信息不存在");
        }
        try {
            ProductionPaintingLineDetail entityfromFto = fromDto(dto);
			BeanUtils.copyProperties(entityfromFto, entity, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
			productionPaintingLineDetailService.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) {
				productionPaintingLineDetailService.delete(id);
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "获取画线明细")
	@GetMapping("/getProductionPaintingLineDetail")
	public Result getProductionPaintingLineDetail(@RequestParam(value = "drawId") Long drawId , @RequestParam(value = "type") Integer type) {
		List<ProductionPaintingLineDetail> list = null;
		switch (type){
			case 1:
				//长棒画线明细
                list = productionPaintingLineDetailService.findByOperateDrawId(drawId);
                break;
            case 2:
                //圆棒反切
                list = productionPaintingLineDetailService.findByOperateReversecutDrawId(drawId);
                break;
            case 3:
                //方棒反切
                list = productionPaintingLineDetailService.findByOperateRecutDrawIdFb(drawId);
                break;
            case 4:
                //方棒反切
                list = productionPaintingLineDetailService.findByOperateReversecutDrawId(drawId, 1, 0);
                break;
        }
        return ResultGenerator.success(list);
    }

	@ApiOperation(value = "根据编号获取画线信息")
	@PostMapping("/getPaintingLineDetail")
	public Result getPaintingLineDetail(@RequestBody List<String> crystalNo) {

		//画线明细
		List<ProductionPaintingLineDetail> bySegmentNo = productionPaintingLineDetailService.findBySegmentNos(crystalNo);
		List<ProductionPaintingLineDetailDto1> productionPaintingLineDetailDtos = bySegmentNo.stream()
				.map(productionPaintingLineDetail -> toLazyDto1(productionPaintingLineDetail))
				.collect(Collectors.toList());

		return ResultGenerator.success(productionPaintingLineDetailDtos);
	}


    @ApiOperation(value = "PAD根据晶段编号查看数据是否存在划线表/或长晶棒表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "no", value = "晶段编号", dataType = "String", paramType = "query")
	})
    @GetMapping("/findDetailByNo")
    public Result findDetailByNo(String no) {

        return ResultGenerator.success(productionPaintingLineDetailService.findDetailByNo(no));

    }


    public ProductionPaintingLineDetailDto toLazyDto(ProductionPaintingLineDetail entity) {
        return modelMapper.map(entity, ProductionPaintingLineDetailDto.class);
    }

	public ProductionPaintingLineDetailDto1 toLazyDto1(ProductionPaintingLineDetail entity) {
		return modelMapper.map(entity, ProductionPaintingLineDetailDto1.class);
	}

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

    public ProductionPaintingLineDetail fromDto(ProductionPaintingLineDetailDto dto) {
        return modelMapper.map(dto, ProductionPaintingLineDetail.class);
	}


}
