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

import com.sw.common.accessory.api.feignCilent.YbFeignClients;
import com.sw.common.accessory.server.dto.DeterminationEchnologyDto;
import com.sw.common.accessory.server.dto.DeterminationQualityDto;
import com.sw.common.accessory.server.dto.DictionaryItemDto;
import com.sw.common.accessory.server.entity.DeterminationEchnology;
import com.sw.common.accessory.server.entity.DeterminationQuality;
import com.sw.common.accessory.server.service.DeterminationQualityService;
import com.sw.common.core.context.BaseContextHandler;
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.StringUtils;
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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/anomalyDeterminationQuality")
@Slf4j
@Api(tags = "热场退料异常判断质量评审接口")
public class DeterminationQualityController {
    @Autowired
    private DeterminationQualityService determinationQualityService;
    @Autowired
    private YbFeignClients ybFeignClients;
    @ApiOperation(value = "分页查询热场退料异常判断质量评审列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_createTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "search_LTE_createTime", value = "结束时间", dataType = "String", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<DeterminationQualityDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        Specification<DeterminationQuality> specification = DynamicSpecifications.bySearchFilter(request, DeterminationQuality.class);
        Page<DeterminationQuality> page = determinationQualityService.findByExample(specification, PageUtil.createPageable(requestPage));
        List<DeterminationQualityDto> dtos = page.getContent().stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
        ResponsePage<DeterminationQualityDto> responsePage = new ResponsePage<>(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
    }
    @ApiOperation(value = "获取所有热场退料异常判断质量评审")
    @GetMapping("/listAll")
    public Result<List<DeterminationQualityDto>> listAll(ServletRequest request) {
        Specification<DeterminationQuality> specification = DynamicSpecifications.bySearchFilter(request, DeterminationQuality.class);
        List<DeterminationQuality> determinationEchnologies = determinationQualityService.findByExampleSort(specification, null);
        List<DeterminationQualityDto> applyRecordDtos = determinationEchnologies.stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
        return ResultGenerator.success(applyRecordDtos);
    }

    @ApiOperation(value = "辅料热场退料异常判断质量评审")
    @GetMapping("/{id}")
    public Result<DeterminationQualityDto> get(@PathVariable @ApiParam(value = "辅料申请记录id") Long id) {
        DeterminationQuality DeterminationQuality = determinationQualityService.get(id);
        if (null == DeterminationQuality) {
            return ResultGenerator.fail("该辅料申请记录不存在");
        }
        return ResultGenerator.success(toEagerDto(DeterminationQuality));
    }

    @ApiOperation(value = "创建热场退料异常判断质量评审")
    @PostMapping("/create")
    public Result<String> create(@RequestBody @Valid @ApiParam(value = "辅料申请记录对象") DeterminationQualityDto DeterminationQualityDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        DeterminationQuality applyRecord = fromDto(DeterminationQualityDto);
        determinationQualityService.saveOrUpdate(applyRecord);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "更新热场退料异常判断质量评审")
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Valid @ApiParam(value = "辅料申请记录") DeterminationQualityDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        if (dto.getId() == null) {
            return ResultGenerator.fail("id不能为空");
        }
        DeterminationQuality entity = determinationQualityService.get(dto.getId());
        if (entity == null) {
            return ResultGenerator.fail("原信息不存在");
        }
        try {
            DeterminationQuality entityfromFto = fromDto(dto);
            determinationQualityService.saveOrUpdate(entityfromFto);
            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) {
            determinationQualityService.delete(id);
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "审批通过热场退料异常判断质量评审")
    @PostMapping("/confirmAudit")
    public Result<String> confirmAudit(@RequestBody @Valid @ApiParam(value = "辅料申请记录") DeterminationQualityDto dto) {
        if (dto.getId() == null) {
            return ResultGenerator.fail("id不能为空");
        }
        DeterminationQuality entity = determinationQualityService.get(dto.getId());
        if (entity == null) {
            return ResultGenerator.fail("原信息不存在");
        }
        try {
            determinationQualityService.confirmAudit(dto,entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "审批不通过热场退料异常判断质量评审")
    @PostMapping("/unConfirmAudit")
    public Result<String> unConfirmAudit(@RequestBody @Valid @ApiParam(value = "辅料申请记录") DeterminationEchnologyDto dto) {
        if (dto.getId() == null) {
            return ResultGenerator.fail("id不能为空");
        }
        DeterminationQuality entity = determinationQualityService.get(dto.getId());
        if (entity == null) {
            return ResultGenerator.fail("原信息不存在");
        }
        try {
            determinationQualityService.unConfirmAudit(dto,entity);
            return ResultGenerator.success();
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "通过ID获取辅料热场退料异常判断质量评审详情")
    @GetMapping("/findById")
    public Result<DeterminationQualityDto> findById(@RequestParam @ApiParam(value = "辅料申请记录id") Long id) {
        DeterminationQualityDto determinationQualityDto = determinationQualityService.findById(id);
        if (null == determinationQualityDto) {
            return ResultGenerator.fail("该辅料申请记录不存在");
        }
        return ResultGenerator.success(determinationQualityDto);
    }

    @ApiOperation(value = "通过热场ID获取辅料热场退料异常判断质量评审详情")
    @GetMapping("/findByAccessoryId")
    public Result<DeterminationQualityDto> findByAccessoryId(@RequestParam @ApiParam(value = "辅料申请记录id") Long id) {
        DeterminationQualityDto determinationQualityDto = determinationQualityService.findByAccessoryId(id);
        if (null == determinationQualityDto) {
            return ResultGenerator.fail("该辅料申请记录不存在");
        }
        return ResultGenerator.success(determinationQualityDto);
    }

    @ApiOperation(value = "通过入库单号获取辅料热场退料异常判断质量评审详情")
    @GetMapping("/findByAccessoryInputBillNo")
    public Result<DeterminationQualityDto> findByAccessoryInputBillNo(@RequestParam @ApiParam(value = "辅料申请记录id") String billNo) {
        DeterminationQualityDto determinationQualityDto = determinationQualityService.findByInputBillNo(billNo);
        if (null == determinationQualityDto) {
            return ResultGenerator.fail("该辅料申请记录不存在");
        }
        return ResultGenerator.success(determinationQualityDto);
    }

    public DeterminationQualityDto toLazyDto(DeterminationQuality entity) {
        DeterminationQualityDto determinationQualityDto = new DeterminationQualityDto();
        BeanUtils.copyProperties(entity, determinationQualityDto);
        return determinationQualityDto;
    }

    public DeterminationQualityDto toEagerDto(DeterminationQuality entity) {
        DeterminationQualityDto determinationQualityDto = new DeterminationQualityDto();
        BeanUtils.copyProperties(entity, determinationQualityDto);
        return determinationQualityDto;
    }

    public DeterminationQuality fromDto(DeterminationQualityDto dto) {
        DeterminationQuality determinationQuality = new DeterminationQuality();
        BeanUtils.copyProperties(dto, determinationQuality);
        return determinationQuality;
    }
}
