package com.sw.mes.sap.controller;

import com.google.gson.Gson;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.dto.sap.RecycledReportSapRecordDto;
import com.sw.mes.core.enums.EnumValuesFactory;
import com.sw.mes.core.enums.MesToSapTypeEnum;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.sap.entity.RecycledReportSapRecord;
import com.sw.mes.sap.service.RecycledReportSapRecordService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.RoundingMode;


/**
 * MES报工SAP记录Controller
 */
@RestController
@Jinko(author = "yyl", value = "MES报工SAP记录Controller")
@Slf4j
@RequestMapping("/recycledReport/sapRecord")
public class RecycledReportSapRecordController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private RecycledReportSapRecordService recycledReportSapRecordService;

    @ApiOperation(value = "创建mes的余料硅料发送SAP记录数据")
    @PostMapping("/create")
    public Result create(@RequestBody @Valid @ApiParam(value = "ReportSapGoodsmvt对象") RecycledReportSapRecordDto recycledReportSapRecordDto) {
        try {
            RecycledReportSapRecord recycledReportSapRecord = fromDto(recycledReportSapRecordDto);
            Integer recordType = recycledReportSapRecord.getRecordType();
            if (MesToSapTypeEnum.余料调拨.getCode() == recordType) {
                recycledReportSapRecordService.saveRecycledRecordAllotRecord(recycledReportSapRecord);
            } else if (MesToSapTypeEnum.硅料清洗分选.getCode() == recordType) {
                recycledReportSapRecordService.saveCleanConsumeRecord(recycledReportSapRecord);
            } else if (MesToSapTypeEnum.硅料收货.getCode() == recordType) {
                recycledReportSapRecordService.saveCleanGroupRecord(recycledReportSapRecord);
            } else if (MesToSapTypeEnum.硅料成品调拨.getCode() == recordType) {
                recycledReportSapRecordService.saveCleanGroupAllotRecord(recycledReportSapRecord);
            } else if (MesToSapTypeEnum.刀剖余料调拨籽晶仓.getCode() == recordType) {
                recycledReportSapRecordService.saveRecycledNoAutoAllotRecord(recycledReportSapRecord);
            }else if (MesToSapTypeEnum.截断余料调拨籽晶仓.getCode() == recordType) {
                recycledReportSapRecordService.saveJdRecycledNoAutoAllotRecord(recycledReportSapRecord);
            }else if (MesToSapTypeEnum.其他调拨.getCode() == recordType) {
                recycledReportSapRecordService.saveOtherRecycledNoAutoAllotRecord(recycledReportSapRecord);
            }
        } catch (Exception e) {
            log.error("创建mes的余料硅料发送SAP记录数据异常:" + e.getMessage());
        }
        return ResultGenerator.success();
    }

    public RecycledReportSapRecord fromDto(RecycledReportSapRecordDto dto) {
        RecycledReportSapRecord recycledReportSapRecord = modelMapper.map(dto, RecycledReportSapRecord.class);
        recycledReportSapRecord.setChargItem(new Gson().toJson(dto.getChargItems()));
        return recycledReportSapRecord;
    }
}
