package com.jsowell.pile.service.impl;

import com.alibaba.fastjson2.JSON;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.PileSimInfo;
import com.jsowell.pile.dto.QuerySimInfoDTO;
import com.jsowell.pile.mapper.PileSimInfoMapper;
import com.jsowell.pile.service.IPileSimInfoService;
import com.jsowell.pile.vo.web.SimCardInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 充电桩SIM卡信息Service业务层处理
 *
 * @author jsowell
 * @date 2022-08-26
 */
@Slf4j
@Service
public class PileSimInfoServiceImpl implements IPileSimInfoService {
	@Autowired
	private PileSimInfoMapper pileSimInfoMapper;

	/**
	 * 查询充电桩SIM卡信息
	 *
	 * @param id 充电桩SIM卡信息主键
	 * @return 充电桩SIM卡信息
	 */
	@Override
	public PileSimInfo selectPileSimInfoById(Long id) {
		return pileSimInfoMapper.selectPileSimInfoById(id);
	}

	/**
	 * 查询充电桩SIM卡信息列表
	 *
	 * @param pileSimInfo 充电桩SIM卡信息
	 * @return 充电桩SIM卡信息
	 */
	@Override
	public List<PileSimInfo> selectPileSimInfoList(PileSimInfo pileSimInfo) {
		return pileSimInfoMapper.selectPileSimInfoList(pileSimInfo);
	}

	/**
	 * 后管查询sim卡信息列表
	 * @return
	 */
	@Override
	public List<SimCardInfoVO> getSimInfoList(QuerySimInfoDTO dto) {
		String expiredTime = dto.getExpiredTime();
		if (StringUtils.equals("1", expiredTime)) {
			// 小于7天
			expiredTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addDays(new Date(), 7));
		}else if (StringUtils.equals("2", expiredTime)) {
			// 小于30天
			expiredTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addDays(new Date(), 30));
		}
		dto.setExpiredTime(expiredTime);
		List<SimCardInfoVO> simInfoList = pileSimInfoMapper.getSimInfoList(dto);
		if (CollectionUtils.isEmpty(simInfoList)) {
			return new ArrayList<>();
		}
		for (SimCardInfoVO simCardInfoVO : simInfoList) {
			String expireTime = simCardInfoVO.getExpireTime();
			if (StringUtils.isBlank(expireTime)) {
				continue;
			}
			String poorDays = DateUtils.getPoorDays(DateUtils.parseDate(expireTime), new Date());
			simCardInfoVO.setRemainingDays(poorDays);
		}
		return simInfoList;
	}

	/**
	 * 新增充电桩SIM卡信息
	 *
	 * @param pileSimInfo 充电桩SIM卡信息
	 * @return 结果
	 */
	@Override
	public int insertPileSimInfo(PileSimInfo pileSimInfo) {
		pileSimInfo.setCreateTime(DateUtils.getNowDate());
		log.info("新增充电桩SIM卡信息 PileSimInfo:{}", JSON.toJSONString(pileSimInfo));
		return pileSimInfoMapper.insertPileSimInfo(pileSimInfo);
	}

	/**
	 * 修改充电桩SIM卡信息
	 *
	 * @param pileSimInfo 充电桩SIM卡信息
	 * @return 结果
	 */
	@Override
	public int updatePileSimInfo(PileSimInfo pileSimInfo) {
		pileSimInfo.setUpdateTime(DateUtils.getNowDate());
		return pileSimInfoMapper.updatePileSimInfo(pileSimInfo);
	}

	/**
	 * 批量删除充电桩SIM卡信息
	 *
	 * @param ids 需要删除的充电桩SIM卡信息主键
	 * @return 结果
	 */
	@Override
	public int deletePileSimInfoByIds(Long[] ids) {
		return pileSimInfoMapper.deletePileSimInfoByIds(ids);
	}

	/**
	 * 删除充电桩SIM卡信息信息
	 *
	 * @param id 充电桩SIM卡信息主键
	 * @return 结果
	 */
	@Override
	public int deletePileSimInfoById(Long id) {
		return pileSimInfoMapper.deletePileSimInfoById(id);
	}

	/**
	 * 通过桩编码查询sim卡信息
	 *
	 * @param pileSn 桩编码
	 * @return
	 */
	@Override
	public SimCardInfoVO querySimCardInfoByPileSn(String pileSn) {
		return pileSimInfoMapper.querySimCardInfoByPileSn(pileSn);
	}

	/**
	 * 通过卡号批量查询sim卡信息
	 *
	 * @param iccIds 卡号
	 * @return
	 */
	@Override
	public List<PileSimInfo> selectSimInfoByIccIds(List<String> iccIds) {
		return pileSimInfoMapper.selectSimInfoByIccIds(iccIds);
	}

	/**
	 * 通过卡号查询sim卡信息
	 *
	 * @param iccId 卡号
	 * @return
	 */
	public PileSimInfo getBasicInfoByIccId(String iccId) {
		return pileSimInfoMapper.getBasicInfoByIccId(iccId);
	}
}
