package com.sw.mes.yb.alloy.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.DynamicSpecifications;
import com.sw.mes.core.utils.PageUtil;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.yb.alloy.service.FixLogicHisLogService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
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;

import com.sw.mes.yb.alloy.entity.FixLogic;
import com.sw.mes.yb.alloy.dto.FixLogicDto;
import com.sw.mes.yb.alloy.service.FixLogicService;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@RestController
@RequestMapping("/fixLogic")
@Slf4j
@Api(description = "修正逻辑接口")
public class FixLogicController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private FixLogicService fixLogicService;
    @Autowired
    private FixLogicHisLogService fixLogicHisLogService;

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

    @ApiOperation(value = "修正逻辑详细信息")
    @GetMapping("/{id}")
    public Result<FixLogicDto> get(@PathVariable @ApiParam(value = "修正逻辑id") Integer id) {
        FixLogic fixLogic = fixLogicService.get(id);
        if (null == fixLogic)
            return ResultGenerator.fail("该修正逻辑不存在");
        return ResultGenerator.success(toEagerDto(fixLogic));
    }

    @ApiOperation(value = "创建修正逻辑")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "修正逻辑对象") FixLogicDto fixLogicDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        FixLogic fixLogic = fromDto(fixLogicDto);
        List<FixLogic> fixLogicList = fixLogicService.findAllByFixLogic(fixLogic);
        if (null != fixLogicList && fixLogicList.size() != 0) {
            return ResultGenerator.fail("修正逻辑信息已存在!!!");
        }
        fixLogicService.saveOrUpdate(fixLogic);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "更新修正逻辑")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid @ApiParam(value = "修正逻辑") FixLogicDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        fixLogicService.update(dto);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "删除修正逻辑")
    @PostMapping("/delete")
    public Result remove(@ApiParam(value = "id集合,逗号分隔,如2,4", type = "query") @RequestParam Integer[] ids) {
        for (Integer id : ids) {
            fixLogicService.delete(id);
        }
        fixLogicHisLogService.deleteByFixLogicHisIds(ids);

        return ResultGenerator.success();
    }

    public FixLogicDto toLazyDto(FixLogic entity) {
        return modelMapper.map(entity, FixLogicDto.class);
    }

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

    public FixLogic fromDto(FixLogicDto dto) {
        return modelMapper.map(dto, FixLogic.class);
    }


}
