package com.jsowell.pile.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.core.page.PageResponse;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.pile.domain.PileMsgRecord;
import com.jsowell.pile.dto.QueryPileDTO;
import com.jsowell.pile.mapper.PileMsgRecordMapper;
import com.jsowell.pile.service.IPileMsgRecordService;
import com.jsowell.pile.vo.web.PileCommunicationLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class PileMsgRecordServiceImpl implements IPileMsgRecordService {

	@Autowired
	private PileMsgRecordMapper pileMsgRecordMapper;

	@Override
	public void save(String pileSn, String connectorCode, String frameType, String jsonMsg, String originalMsg) {
		PileMsgRecord pileMsgRecord = PileMsgRecord.builder()
				.pileSn(pileSn)
				.connectorCode(connectorCode)
				.jsonMsg(jsonMsg)
				.frameType(frameType)
				.originalMsg(originalMsg)
				.build();
		pileMsgRecordMapper.insertSelective(pileMsgRecord);
	}

	// @Override
	// public List<PileMsgRecord> getByConnectorCodeList(List<String> connectorCodeList) {
	// 	if (CollectionUtils.isEmpty(connectorCodeList)) {
	// 		return Lists.newArrayList();
	// 	}
	// 	return pileMsgRecordMapper.getByConnectorCodeList(connectorCodeList);
	// }

	public static void main(String[] args) {
		String type = BytesUtil.bcd2Str(YKCFrameTypeCode.TRANSACTION_RECORDS_CODE.getBytes());
		System.out.println(type);
		String binary = BytesUtil.binary(YKCFrameTypeCode.TRANSACTION_RECORDS_CODE.getBytes(), 16);
		System.out.println(binary);
		String s = YKCUtils.frameType2Str(YKCFrameTypeCode.TRANSACTION_RECORDS_CODE.getBytes());
		System.out.println(s);

	}

	@Override
	public PageResponse getPileFeedList(QueryPileDTO dto) {
		// 分页
		PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
		List<PileMsgRecord> pileFeedList = pileMsgRecordMapper.getPileFeedList(dto.getPileSn());
		PageInfo<PileMsgRecord> pageInfo = new PageInfo<>(pileFeedList);

		List<PileCommunicationLogVO> list = new ArrayList<>();
		for (PileMsgRecord pileMsgRecord : pageInfo.getList()) {
			PileCommunicationLogVO vo = new PileCommunicationLogVO();
			String frameType = pileMsgRecord.getFrameType();

			String frameTypeStr = YKCFrameTypeCode.getFrameTypeStr(frameType);
			if (StringUtils.isNotBlank(frameTypeStr)) {
				vo.setDescription(frameTypeStr);
			} else {
				vo.setDescription(pileMsgRecord.getJsonMsg());
			}
			vo.setOriginalMsg(pileMsgRecord.getOriginalMsg());
			vo.setPileSn(pileMsgRecord.getPileSn());
			vo.setCreateTime(pileMsgRecord.getCreateTime());
			vo.setFrameType(frameType);
			list.add(vo);
		}

		PageResponse pageResponse = PageResponse.builder()
				.pageNum(pageInfo.getPageNum())
				.pageSize(pageInfo.getPageSize())
				.list(list)
				.total(pageInfo.getTotal())
				.pages(pageInfo.getPages())
				.build();
		return pageResponse;
	}
}
