package com.base.biz.controller.rpc;


import com.base.api.model.AntennaRpcDTO;
import com.base.biz.model.entity.AntennaInfo;
import com.base.biz.service.AntennaService;
import com.base.orbit.log.ServerNameEnum;
import com.base.orbit.utils.DateUtils;
import com.base.orbit.utils.IdGenerator;
import com.base.orbit.web.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/rpc/antena")
@RestController
@Slf4j
public class AntennaRpcController {

    @Resource
    private AntennaService antennaService;

    @RequestMapping(value = "/getInfoList", method = RequestMethod.POST)
    public Object getInfoList(@RequestBody AntennaRpcDTO antennaRpcDTO) {
        return Result.success(antennaService.getInfoList(antennaRpcDTO));
    }


    @PostMapping(value = "/upOrSaveAntena")
    public Boolean upOrSaveAntena(@RequestBody AntennaRpcDTO antennaRpcDTO) {
        try {
            AntennaInfo antennaInfo = new AntennaInfo();
            BeanUtils.copyProperties(antennaRpcDTO, antennaInfo);
            if (StringUtils.isEmpty(antennaInfo.getId())) {
                antennaInfo.setId(IdGenerator.getStrId());
                antennaInfo.setCreateTime(DateUtils.getNowDate());
                antennaService.save(antennaInfo);
            } else {
                antennaInfo.setUpdateTime(DateUtils.getNowDate());
                antennaService.updateById(antennaInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("save or update antenna info is error,msg is " + e.getMessage() + "!", ServerNameEnum.BASE_SERVER);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }


    @PostMapping(value = "/delAntenaInfoByIds")
    public Boolean delAntenaInfoByIds(@RequestBody List<String> ids) {
        try {
            ids.stream().forEach(id -> {
                        antennaService.removeById(id);
                    }
            );
        } catch (Exception e) {
            e.printStackTrace();
            log.error("del antenna info is error,msg is " + e.getMessage() + "!", ServerNameEnum.BASE_SERVER);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @PostMapping(value = "/selAntenaInfoByIds")
    public Object selAntenaInfoByIds(@RequestBody List<String> stationIds) {
        return antennaService.selAntenaInfoByIds(stationIds);
    }

    @PostMapping(value = "/selAllAntenaInfos")
    public Object selAllAntenaInfos() {
        return antennaService.selAllAntenaInfos();
    }
}
