package com.base.biz.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.biz.mapper.AntennaInfoMapper;
import com.base.biz.mapper.StationInfoMapper;
import com.base.biz.model.dto.StationInfoDTO;
import com.base.biz.model.dto.request.QueryStationAddDTO;
import com.base.biz.model.dto.request.StationInfoDTOListReq;
import com.base.biz.model.dto.request.StationInfoDTOSetReq;
import com.base.biz.model.dto.response.StationTypeListDTO;
import com.base.biz.model.entity.AntennaInfo;
import com.base.biz.model.entity.StationInfo;
import com.base.biz.model.entity.TypeListBiz;
import com.base.biz.model.enums.MissionTypeEnums;
import com.base.biz.model.enums.RelayTypeEnums;
import com.base.biz.model.enums.ResourceTypeEnums;
import com.base.biz.model.enums.TransportableEnums;
import com.base.orbit.utils.BizUtils;
import com.base.orbit.web.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class StationService extends ServiceImpl<StationInfoMapper, StationInfo> {

    @Resource
    StationInfoMapper stationInfoMapper;

    public List<StationInfoDTO> getStationInfoDTOList(StationInfoDTOListReq req) {
        List<StationInfo> stationInfoList = stationInfoMapper.getStationInfoDTOList(req);
        return BizUtils.copyEntityToDto(StationInfoDTO.class, stationInfoList);
    }
    public List<StationInfoDTO> getStationListByStationIdSet(StationInfoDTOSetReq req) {
        List<StationInfo> stationInfoList = stationInfoMapper.getStationInfoDTOSet(req);
        return BizUtils.copyEntityToDto(StationInfoDTO.class, stationInfoList);
    }


    public List<StationInfoDTO> getAllList() {
        List<StationInfo> list = this.list();
        return BizUtils.copyEntityToDto(StationInfoDTO.class, list);
    }

    /**
     * 保留小数点后两位
     *
     * @param val
     * @return
     */
    public static String doubleCastStr(String val) {
        BigDecimal bg = new BigDecimal(val);
        bg = bg.setScale(2, RoundingMode.HALF_UP);
        return bg.toString();
    }

    public StationInfo getStationInfoById(String stationId) {
        StationInfo stationInfoList = stationInfoMapper.selectById(stationId);
        return stationInfoList;
    }

    public StationTypeListDTO queryStationTypeListDTO() throws Exception{
        StationTypeListDTO stationTypeListDTO = new StationTypeListDTO();
        stationTypeListDTO.setZjxnl(TypeListBiz.getTypeList(RelayTypeEnums.values()));
        stationTypeListDTO.setRwlx(TypeListBiz.getTypeList(MissionTypeEnums.values()));
        stationTypeListDTO.setZylx(TypeListBiz.getTypeList(ResourceTypeEnums.values()));
        stationTypeListDTO.setTransportable(TypeListBiz.getTypeList(TransportableEnums.values()));
        return stationTypeListDTO;
    }

    public List<StationInfo> selListByResourceType(String resourceType) {
        List<StationInfo> list = stationInfoMapper.selListByResourceType(resourceType);
        return list;
    }

    public void addStation(QueryStationAddDTO queryStationAddDTO) {
        StationInfo stationInfo = new StationInfo();
        BeanUtils.copyProperties(queryStationAddDTO, stationInfo);
        stationInfo.setId(UUID.randomUUID().toString());
        stationInfo.setCreateTime(new Date());
        stationInfoMapper.insert(stationInfo);
    }

    public void updateStation(QueryStationAddDTO queryStationAddDTO) {
        StationInfo stationInfo = new StationInfo();
        BeanUtils.copyProperties(queryStationAddDTO, stationInfo);
        stationInfoMapper.updateById(stationInfo);
    }

    public Result deleteStation(List<String> ids) {
        int i = stationInfoMapper.deleteBatchIds(ids);
        if (i > 0) {
            return Result.success();
        }
        return Result.error();
    }
}
