package com.tofly.xrtymis.custom.controller.foreign;

import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.xrtymis.custom.commons.CommonConstants;
import com.tofly.xrtymis.custom.dto.ForeignOrderDto;
import com.tofly.xrtymis.custom.dto.ForeignOrderFileBase64Dto;
import com.tofly.xrtymis.custom.service.GdflowconfigService;
import com.tofly.xrtymis.custom.service.GdglinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @author FlashWJL
 * @date 2021/12/7 13:19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/foreign")
@Api(tags = "对外（网厅、抄表平台提供接口）")
public class ForeignController {

    private final GdflowconfigService gdflowconfigService;

    private final GdglinfoService gdglinfoService;


    /**
     * 获取工单类型
     */
    @PostMapping("/getOrderType")
    @ApiOperation(value = "获取工单类型")
    public ResultRespone getOrderType(){
        ResultRespone rr =ResultRespone.success(gdflowconfigService.getOrderType());
        rr.setCode(CommonConstants.INT_ZERO);
        return rr;
    }

    /**
     * 工单登记
     */
    @PostMapping("/saveOrder")
    @ApiOperation(value = "工单登记")
    public ResultRespone saveOrder(@Valid ForeignOrderDto dto, List<MultipartFile> file){
        try {
            SecurityUtils.getUserId();
        }catch (Exception e){
            return ResultRespone.failed("请登录");
        }
        String check = gdflowconfigService.orderTypeCheck(dto);
        ResultRespone rr= null;
        if(StringUtils.isNotEmpty(check)){
            rr= ResultRespone.failed(check);
            rr.setCode(CommonConstants.INT_THREE);
            return rr;
        }
        rr = gdglinfoService.foreignSaveOrder(dto,file);
        if(rr!=null&&rr.getCode()==1) {
            rr.setCode(CommonConstants.INT_ZERO);
        }
        return rr;
    }

    /**
     * 工单登记
     */
    @PostMapping("/saveOrderFile")
    @ApiOperation(value = "工单登记")
    public ResultRespone saveOrder(@Valid @RequestBody ForeignOrderFileBase64Dto dto){
        try {
            SecurityUtils.getUserId();
        }catch (Exception e){
            return ResultRespone.failed("请登录");
        }
        String check = gdflowconfigService.orderTypeCheck(dto);
        ResultRespone rr= null;
        if(StringUtils.isNotEmpty(check)){
            rr= ResultRespone.failed(check);
            rr.setCode(CommonConstants.INT_THREE);
            return rr;
        }
        rr = gdglinfoService.foreignSaveOrderBase64(dto);
        if(rr!=null&&rr.getCode()==1) {
            rr.setCode(CommonConstants.INT_ZERO);
        }
        return rr;
    }

}
