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

import com.alibaba.fastjson.JSONObject;
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.base.factory.init.service.EquipmentService;
import com.sw.mes.yb.production.dto.LoadingFinishedDto;
import com.sw.mes.yb.production.entity.LoadingFinished;
import com.sw.mes.yb.production.service.LoadingFinishedService;
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.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;

@RestController
@RequestMapping("/fls")
@Slf4j
@Api(description = "fls数据上报接口")
public class LoadingFinishedController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private LoadingFinishedService loadingFinishedService;

    @ApiOperation(value = "分页查询配合金数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<LoadingFinishedDto>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        Specification<LoadingFinished> specification = DynamicSpecifications.bySearchFilter(request, LoadingFinished.class);
        Page<LoadingFinished> page = loadingFinishedService.findByExample(specification, PageUtil.createPageable(requestPage));
        List<LoadingFinishedDto> dtos = page.getContent().stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
        ResponsePage<LoadingFinishedDto> responsePage = new ResponsePage<>(dtos, page.getTotalElements());
        return ResultGenerator.success(responsePage);
    }

    @ApiOperation(value = "获取所有配合金数据")
    @GetMapping("/listAll")
    public Result<List<LoadingFinishedDto>> listAll(ServletRequest request) {
        Specification<LoadingFinished> specification = DynamicSpecifications.bySearchFilter(request, LoadingFinished.class);
        List<LoadingFinished> loadingFinisheds = loadingFinishedService.findByExampleSort(specification, null);
        List<LoadingFinishedDto> loadingFinishedDtos = loadingFinisheds.stream()
                .map(this::toLazyDto)
                .collect(Collectors.toList());
        return ResultGenerator.success(loadingFinishedDtos);
    }

    @ApiOperation(value = "配合金数据详细信息")
    @GetMapping("/{id}")
    public Result<LoadingFinishedDto> get(@PathVariable @ApiParam(value = "productionResistivityServiceid") Integer id) {
        LoadingFinished loadingFinished = loadingFinishedService.get(id);
        if (null == loadingFinished)
            return ResultGenerator.fail("该条数据不存在");
        return ResultGenerator.success(toEagerDto(loadingFinished));
    }

    @ApiOperation(value = "配合金上报数据")
    @PostMapping("/uploadAlloyInfo")
    public Result saveData(@RequestBody @Valid @ApiParam(value = "配合金数据") String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        if(null!=jsonObject){
            LoadingFinished loadingFinished = new LoadingFinished();
            loadingFinished.setBottleCapCode(String.valueOf(jsonObject.get("bottleCapCode")));
            loadingFinished.setDeviceNo(String.valueOf(jsonObject.get("deviceNo")));
            loadingFinished.setFeedDevNo(String.valueOf(jsonObject.get("feedDevNo")));
            loadingFinished.setTaskCode(String.valueOf(jsonObject.get("taskCode")));
            loadingFinishedService.saveOrUpdate(loadingFinished);
            return ResultGenerator.success("SUCCESS");
        }
        return ResultGenerator.fail("FAIL");
    }

    @ApiOperation(value = "fls取料完成上报数据")
    @PostMapping("/uploadAlloyFinishInfo")
    public Result saveFinishInfoData(@RequestBody @Valid @ApiParam(value = "fls取料完成上报数据") String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        if(null!=jsonObject){
            LoadingFinished loadingFinished = new LoadingFinished();
            loadingFinished.setBottleCapCode(String.valueOf(jsonObject.get("bottleCapCode")));
            loadingFinished.setDeviceNo(String.valueOf(jsonObject.get("deviceNo")));
            loadingFinished.setFeedDevNo(String.valueOf(jsonObject.get("feedDevNo")));
            loadingFinished.setTaskCode(String.valueOf(jsonObject.get("taskCode")));
            loadingFinished.setAlloyType(String.valueOf(jsonObject.get("alloyType")));
            loadingFinished.setAlloyWeight(String.valueOf(jsonObject.get("alloyWeight")));
            loadingFinishedService.saveOrUpdate(loadingFinished);
            return ResultGenerator.success("SUCCESS");
        }
        return ResultGenerator.fail("FAIL");
    }

    public LoadingFinishedDto toLazyDto(LoadingFinished entity) {
        return modelMapper.map(entity, LoadingFinishedDto.class);
    }

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

    public LoadingFinished fromDto(LoadingFinishedDto dto) {
        return modelMapper.map(dto, LoadingFinished.class);
    }
}
