package com.sptg.device.biz.controller;


import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.FlowCard;
import com.sptg.device.api.entity.FlowCardDevice;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.device.api.feigns.FlowCardDeviceFeign;
import com.sptg.device.biz.service.FlowCardDeviceServiceInterface;
import com.sptg.device.biz.service.FlowCardServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class FLowCardDeviceController implements FlowCardDeviceFeign {
    public final FlowCardDeviceServiceInterface flowCardDeviceServiceInterface;
    public final DeviceFeign deviceFeign;
    private final FlowCardServiceInterface flowCardServiceInterface;

    @Override
    @PostMapping(value = "/flowCardDevice/getList")
    public List<FlowCardDevice> getList(FlowCardDevice flowCardDevice) {

        return this.flowCardDeviceServiceInterface.getList(flowCardDevice);
    }

    @Override
    @PostMapping(value = "/flowCardDevice/add")
    public ResultBean<Boolean> add(@RequestBody FlowCardDevice flowCardDevice) {
        flowCardDevice.setStatus(OperationConstant.ZERO);
        flowCardDevice.setCreateDate(new Date());
        Boolean b = this.flowCardDeviceServiceInterface.insert(flowCardDevice);
        return new ResultBean<>(ResultEnum.SUCCESS,b);
    }

    @Override
    @PostMapping(value = "/flowCardDevice/delete")
    public ResultBean<Boolean> delete(@RequestBody FlowCardDevice flowCardDevice) {
        flowCardDevice.setStatus(OperationConstant.MINUS_ONE);
        flowCardDevice.setUpdataDate(new Date());
        Boolean b = this.flowCardDeviceServiceInterface.updateById(flowCardDevice);
        return new ResultBean<>(ResultEnum.SUCCESS,b);
    }

    @Override
    @PostMapping(value = "/flowCardDevice/getOne")
    public FlowCardDevice getOne(@RequestBody FlowCardDevice flowCardDevice) {
        return this.flowCardDeviceServiceInterface.getOne(flowCardDevice);
    }

    @Override
    @PostMapping(value = "/flowCardDevice/selectOneById")
    public FlowCardDevice selectOneById(@RequestBody Long id) {
        return this.flowCardDeviceServiceInterface.selectById(id);
    }

    @Override
    @PostMapping(value = "/flowCardDevice/getDeviceByCardId")
    public ResultBean<Device> getDeviceByCardId(@RequestBody FlowCardDevice flowCardDevice) {
        flowCardDevice.setStatus(OperationConstant.ZERO);
        FlowCard flowCard = this.flowCardServiceInterface.selectById(flowCardDevice.getFlowCardId());
        if (flowCard != null && flowCard.getDeviceId() != null){
            Device device = this.deviceFeign.getEntityById(flowCard.getDeviceId());
            if (device != null){
                return new ResultBean<>(ResultEnum.SUCCESS,device);
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS,null);
    }
}
