package com.xhpc.card.service.impl;

import cn.hutool.json.JSONUtil;
import com.xhpc.card.domain.*;
import com.xhpc.card.mapper.*;
import com.xhpc.card.pojo.*;
import com.xhpc.card.service.IXhpcCardService;
import com.xhpc.card.utils.MyDateUtil;
import com.xhpc.card.utils.MyPagingUtil;
import com.xhpc.common.api.CardHistoryOrderService;
import com.xhpc.common.constant.ConstantClass;
import com.xhpc.common.core.constant.Constants;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.domain.IccardInfo;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yuyang
 * @date 2022/1/20 13:44
 */
@Service
public class XhpcCardServiceImpl implements IXhpcCardService {

    @Resource
    private XhpcCardMapper xhpcCardMapper;
    @Resource
    private CardHistoryOrderService cardHistoryOrderService;
    @Resource
    private TIccardDeviceMapper tIccardDeviceMapper;
    @Resource
    private TIccardUsersMapper tIccardUsersMapper;
    @Resource
    private TIccardInfoMapper tIccardInfoMapper;
    @Resource
    private XhpcOperatorMapper xhpcOperatorMapper;
    @Resource
    private XhpcAppUserMapper xhpcAppUserMapper;
    @Resource
    private XhpcCommunityPersonnelMapper xhpcCommunityPersonnelMapper;
    @Resource
    private XhpcCustomersPersonnelMapper xhpcCustomersPersonnelMapper;
    @Resource
    private TokenService tokenService;
    @Resource
    private TIccardLogMapper tIccardLogMapper;
    @Resource
    private XhpcRechargeOrderMapper xhpcRechargeOrderMapper;
    @Resource
    private TIccardClientUsersMapper tIccardClientUsersMapper;

    @Override
    public R cardStartup(String cardno, String serialNumber, String rateModelId) {

        IccardInfo iccardInfo = xhpcCardMapper.getIccardInfoCardno(cardno);
        if (iccardInfo != null) {
            if (iccardInfo.getCardtype() == 0) {
                return R.fail(1202,"离线卡");
            }
            if (iccardInfo.getStatus() == 0) {
                return R.fail(1203,"未初始化卡");
            }
            if(iccardInfo.getStatus()==2){
                return R.fail(1204,"已锁卡");
            }
            if(iccardInfo.getStatus()==3){
                return R.fail(1205,"挂失卡");
            }
            if(iccardInfo.getStatus()==4){
                return R.fail(1206,"作废卡");
            }
        }else{
            return R.fail(1207,"无效卡");
        }
        if("".equals(rateModelId) || rateModelId==null){
            return R.fail(1208,"费率模型id为空");
        }
        //查询卡号对应的用户
        XhpcIcCardInfo xhpcIcCardInfo = xhpcCardMapper.getXhpcIcCardInfo(iccardInfo.getId().toString());
        if(xhpcIcCardInfo ==null){
            return R.fail(1200, "无效用户");
        }
        Long userId=xhpcIcCardInfo.getUserId();
        Integer userType=xhpcIcCardInfo.getUserType();
        String tenantId=xhpcIcCardInfo.getTenantId();
        Integer type =Integer.valueOf(iccardInfo.getCardtype());//改成卡授权的类型
        String grantOperator=iccardInfo.getCorpno();//改成卡授权的运营商
        R r = cardHistoryOrderService.cardStartup(userId, serialNumber, userType, tenantId, type, grantOperator, rateModelId);

        System.out.println("===============<<<<卡启动充电返回>>>>>==================="+r.getMsg());
        System.out.println("===============<<<<卡启动充电返回>>>>>==================="+r.getCode());
        return r;
    }

    @Override
    public R<ListOfAuthorizedDevices> queryDeviceList(Long currentPage, Long items) {
        //计算分页索引
        currentPage = MyPagingUtil.calculateStartIndex(currentPage, items);
        ListOfAuthorizedDevices listOfAuthorizedDevices = new ListOfAuthorizedDevices();
        listOfAuthorizedDevices.setData(new ArrayList<>());
        listOfAuthorizedDevices.setTotalItems(0);
        List<TIccardDevice> tIccardDeviceList = tIccardDeviceMapper.selectAllByPage(currentPage, items);
        if (tIccardDeviceList.isEmpty()) {
            return R.ok(listOfAuthorizedDevices);
        }
        for (TIccardDevice tIccardDevice : tIccardDeviceList) {
            ListOfAuthorizedDevices.DataDTO dataDTO = new ListOfAuthorizedDevices.DataDTO();
            dataDTO.setDeviceId(tIccardDevice.getId());
            dataDTO.setDeviceName(tIccardDevice.getDevname());
            dataDTO.setDeviceType(tIccardDevice.getDevtype());
            dataDTO.setDeviceNumber(tIccardDevice.getSerialnumber());
            Integer corpindex = tIccardDevice.getCorpindex();
            if (corpindex != null) {
                System.out.println("======corpindex===="+corpindex);
                System.out.println("======corpindex===="+corpindex);
                System.out.println("======corpindex===="+corpindex);
                XhpcOperator xhpcOperator = xhpcOperatorMapper.selectOneByCorpNoAndTenantId(String.valueOf(corpindex), tokenService.getLoginUser().getSysUser().getTenantId());
                dataDTO.setGrantOperator(xhpcOperator.getName());
            }
            dataDTO.setType(tIccardDevice.getType());
            listOfAuthorizedDevices.getData().add(dataDTO);
        }
        List<TIccardDevice> tIccardDeviceList1 = tIccardDeviceMapper.selectAll();
        listOfAuthorizedDevices.setTotalItems(tIccardDeviceList1.size());
        return R.ok(listOfAuthorizedDevices);
    }

    @Override
    public R<Object> deleteDevice(Integer deviceId) {

        if (deviceId == null) {
            return R.fail("deviceId为null");
        }
        TIccardDevice tIccardDevice = tIccardDeviceMapper.selectDeletedStatusByDeviceId(deviceId);
        if (tIccardDevice != null) {
            return R.fail(404, "指定资源不存在");
        }
        tIccardDeviceMapper.updateCardDeviceStatusIsDel(deviceId);
        //write log info
        //填充log字段
        TIccardDevice tIccardDevice1 = tIccardDeviceMapper.selectDeletedStatusByDeviceId(deviceId);
        DeviceLogInfo deviceLogInfo = new DeviceLogInfo();
        Long operatorId = tokenService.getLoginUser().getSysUser().getOperatorId();
        deviceLogInfo.setOperatorId(operatorId);
        deviceLogInfo.setOperate(10);
        DeviceLogInfo.DeviceInfoDTO deviceInfoDTO = new DeviceLogInfo.DeviceInfoDTO();
        deviceInfoDTO.setDevName(tIccardDevice1.getDevname());
        deviceInfoDTO.setDevType(tIccardDevice1.getDevtype());
        deviceInfoDTO.setSerialNumber(tIccardDevice1.getSerialnumber());
        deviceInfoDTO.setCorpIndex(Long.valueOf(tIccardDevice1.getCorpindex()));
        XhpcOperator xhpcOperator = xhpcOperatorMapper.selectOneByCorpNoAndTenantId(String.valueOf(deviceInfoDTO.getCorpIndex()), tokenService.getLoginUser().getSysUser().getTenantId());
        deviceInfoDTO.setCorpName(xhpcOperator.getName());
        deviceLogInfo.setDeviceInfo(deviceInfoDTO);
        //填充整体记录字段
        TIccardLog tIccardLog = new TIccardLog();
        tIccardLog.setUniqueid(tIccardDevice1.getSerialnumber());
        tIccardLog.setType((byte) 1);
        tIccardLog.setOperate((byte) 10);
        tIccardLog.setOperatorid(Math.toIntExact(tokenService.getLoginUser().getSysUser().getUserId()));
        String log = JSONUtil.toJsonStr(deviceLogInfo);
        tIccardLog.setLog(log);
        tIccardLogMapper.insertSelective(tIccardLog);

        tIccardDeviceMapper.deleteIccardDeviceId(deviceId);
        return R.ok();
    }

    @Override
    public R<CardList> queryCardListBy(QueryConditions queryConditions) {
        //获取登录用户
        LoginUser loginUser = tokenService.getLoginUser();
        //获取登录用户类型
        SysUser sysUser = loginUser.getSysUser();
        String loginUserType = sysUser.getUserType();
        //判断登录用户类型
        //如果是运营商 01 03 都是运营商
        if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(loginUserType) || UserTypeUtil.SYS_USER_TYPE_THREE.equals(loginUserType)) {
            //设置运营商id
            queryConditions.setCorpNo(sysUser.getCorpNo());
            //平台
        } else if (UserTypeUtil.SYS_USER_TYPE_ZERO.equals(loginUserType)) {
            //平台可能会选中多个授权运营商
//            String grantOperatorIds = (String) queryConditions.getGrantOperatorIds();
//            if (!"".equals(grantOperatorIds) && grantOperatorIds != null) {
//                String[] splitGrantOperatorIds = grantOperatorIds.split(",");
//                List<String> idsList = Arrays.asList(splitGrantOperatorIds);
//                queryConditions.setGrantOperatorIds(idsList);
//            }
        }
        //获取登录用户的租户id
        String tenantId = sysUser.getTenantId();
        queryConditions.setTenantId(tenantId);
        //计算分页索引
        long startIndex = MyPagingUtil.calculateStartIndex(queryConditions.getCurrentPage(), queryConditions.getItems());
        queryConditions.setCurrentPage(startIndex);
        CardList cardList = new CardList();
        cardList.setTotalItems(0L);
        cardList.setData(new ArrayList<>());
        List<Map<String, Object>> listData = tIccardInfoMapper.selectAllBy(queryConditions);
        Long totalCount = tIccardInfoMapper.selectTotalCountBy(queryConditions);
        cardList.setTotalItems(totalCount);
        if (listData.isEmpty()) {
            return R.ok();
        }
        //1.遍历卡记录
        for (Map<String, Object> data : listData) {
            //2.封装通用数据
            CardList.DataDTO dataDTO = new CardList.DataDTO();
            dataDTO.setCardSerialNumber((String) data.get("cardID"));
            dataDTO.setCardPhysicalNumber((String) data.get("cardNo"));
            //授权商名称
            dataDTO.setGrantOperator((String) data.get("name"));
            dataDTO.setClassification((Integer) data.get("cardType"));
            dataDTO.setCardRecordId((Integer) data.get("id"));
            dataDTO.setCardType((Integer) data.get("is_platform"));
            dataDTO.setCardStatus((Integer) data.get("status"));
            dataDTO.setUserAccount((String) data.get("user_account"));
            dataDTO.setCashPledge((Integer) data.get("cashPledge"));
            dataDTO.setClassification((Integer) data.get("cardType"));
            //3.判断是否是离线卡，离线卡处理逻辑
            if (dataDTO.getClassification() == 0) {
                //离线用户类型为10
                dataDTO.setUserType(10L);
                if(data.get("userIndex")!=null){
                    int userIndex = (int) data.get("userIndex");
                    TIccardUsers tIccardUser = tIccardUsersMapper.selectByPrimaryKey(userIndex);
                    if(tIccardUser !=null){
                        String phone = tIccardUser.getPhone();
                        dataDTO.setUserAccount(phone);
                    }
                }
            } else {
                dataDTO.setUserType((Long) data.get("user_type"));
                if (data.get("user_id") != null) {
                    dataDTO.setUserId(Long.valueOf((Integer) data.get("user_id")));
                }
            }
            cardList.getData().add(dataDTO);

        }
        return R.ok(cardList);
    }

    @Override
    public R<BindCardEcho> noBindCardInfo(Long cardId) {

        BindCardEcho bindCardEcho = new BindCardEcho();
        TIccardInfo tIccardInfo = tIccardInfoMapper.selectByPrimaryKey(Math.toIntExact(cardId));
        bindCardEcho.setCardId(tIccardInfo.getCardid());
        bindCardEcho.setCardNo(tIccardInfo.getCardno());
        bindCardEcho.setCardType(tIccardInfo.getCardtype());
        //如果该卡不是联网卡
        if (tIccardInfo.getCardtype() == 0) {
            return R.fail("该卡不是联网卡");
        }
        bindCardEcho.setCardClassification(tIccardInfo.getIsPlatform());
        //如果该卡的分类是平台卡的话
        if (bindCardEcho.getCardClassification() == 0) {
            //0 C端用户 2社区用户 3 B端用户
            int[] userTypeArray = {0, 2, 3};
            List<Integer> userType = Arrays.stream(userTypeArray).boxed().collect(Collectors.toList());
            bindCardEcho.setUserType(userType);

        } else {
            //如果是运营商的话
            //2社区用户 3 B端用户
            int[] userTypeArray = {2, 3};
            List<Integer> userType = Arrays.stream(userTypeArray).boxed().collect(Collectors.toList());
            bindCardEcho.setUserType(userType);
        }
        bindCardEcho.setCashPledge(Long.valueOf(tIccardInfo.getCashpledge()));
        bindCardEcho.setCardRecordId(tIccardInfo.getId());
        return R.ok(bindCardEcho);
    }

    @Override
    public R<SearchCardResponse> searchCardUser(SearchCardUserQueryCondition queryCondition) {
        //计算分页索引
        long startIndex = MyPagingUtil.calculateStartIndex(queryCondition.getCurrentPage(), queryCondition.getItems());
        queryCondition.setCurrentPage(startIndex);
        SearchCardResponse searchCardResponse = new SearchCardResponse();
        searchCardResponse.setData(new ArrayList<>());
        searchCardResponse.setTotalItems(0L);
        //判断是平台还是运营商
        if (queryCondition.getCardClassification() == 0) {
            //(0 C端用户 2社区用户 3 B端用户)
            switch (queryCondition.getUserType()) {
                case 0:
                    List<Integer> userIdList = xhpcCardMapper.selectUserIdsByUserType(queryCondition.getUserType());
                    List<XhpcAppUser> appUserList = xhpcAppUserMapper.selectByCondition(queryCondition, userIdList);
                    Long allCount = xhpcAppUserMapper.selectCount(queryCondition, userIdList);
                    searchCardResponse.setTotalItems(allCount);
                    for (XhpcAppUser xhpcAppUser : appUserList) {
                        SearchCardResponse.DataDTO dataDTO = new SearchCardResponse.DataDTO();
                        dataDTO.setUserAccount(xhpcAppUser.getPhone());
                        dataDTO.setUserType(0);
                        searchCardResponse.getData().add(dataDTO);
                    }
                    return R.ok(searchCardResponse);
                case 2:
                    List<Integer> userIdList1 = xhpcCardMapper.selectUserIdsByUserType(queryCondition.getUserType());
                    List<XhpcCommunityPersonnel> xhpcCommunityPersonnelList = xhpcCommunityPersonnelMapper.selectByCondition(queryCondition, userIdList1);
                    Long allCount1 = xhpcCommunityPersonnelMapper.selectCount(queryCondition, userIdList1);
                    searchCardResponse.setTotalItems(allCount1);
                    for (XhpcCommunityPersonnel xhpcCommunityPersonnel : xhpcCommunityPersonnelList) {
                        SearchCardResponse.DataDTO dataDTO = new SearchCardResponse.DataDTO();
                        dataDTO.setUserAccount(xhpcCommunityPersonnel.getAccount());
                        dataDTO.setUserType(2);
                        dataDTO.setPhone(xhpcCommunityPersonnel.getPhone());
                        dataDTO.setName(xhpcCommunityPersonnel.getName());
                        searchCardResponse.getData().add(dataDTO);
                    }
                    return R.ok(searchCardResponse);
                case 3:
                    List<Integer> userIdList2 = xhpcCardMapper.selectUserIdsByUserType(queryCondition.getUserType());
                    List<XhpcCustomersPersonnel> xhpcCustomersPersonnelList = xhpcCustomersPersonnelMapper.selectByCondition(queryCondition, userIdList2);
                    Long allCount2 = xhpcCustomersPersonnelMapper.selectCount(queryCondition, userIdList2);
                    searchCardResponse.setTotalItems(allCount2);
                    for (XhpcCustomersPersonnel xhpcCustomersPersonnel : xhpcCustomersPersonnelList) {
                        SearchCardResponse.DataDTO dataDTO = new SearchCardResponse.DataDTO();
                        dataDTO.setUserAccount(xhpcCustomersPersonnel.getAccount());
                        dataDTO.setUserType(3);
                        dataDTO.setPhone(xhpcCustomersPersonnel.getPhone());
                        dataDTO.setName(xhpcCustomersPersonnel.getName());
                        searchCardResponse.getData().add(dataDTO);
                    }
                    return R.ok(searchCardResponse);
                default:
                    return R.fail("没有此用户类型");
            }
        } else if (queryCondition.getCardClassification() == 1) {
            XhpcOperator operator = xhpcOperatorMapper.selectByName(queryCondition.getGrantOperatorName());
            if (operator == null) {
                return R.fail("没有此运营商,或未填入授权运营商参数");
            }
            Long operatorId = operator.getOperatorId();
            //2社区用户 3 B端用户)
            switch (queryCondition.getUserType()) {
                case 2:
                    List<Integer> userIdList = xhpcCardMapper.selectUserIdsByUserType(queryCondition.getUserType());
                    List<XhpcCommunityPersonnel> xhpcCommunityPersonnelList = xhpcCommunityPersonnelMapper.selectByOperatorId(operatorId, queryCondition.getCurrentPage(), queryCondition.getItems(), userIdList);
                    Long allCount1 = xhpcCommunityPersonnelMapper.selectCountByOperatorId(operatorId, userIdList);
                    searchCardResponse.setTotalItems(allCount1);
                    for (XhpcCommunityPersonnel xhpcCommunityPersonnel : xhpcCommunityPersonnelList) {
                        SearchCardResponse.DataDTO dataDTO = new SearchCardResponse.DataDTO();
                        dataDTO.setUserAccount(xhpcCommunityPersonnel.getAccount());
                        dataDTO.setUserType(2);
                        searchCardResponse.getData().add(dataDTO);
                    }
                    return R.ok(searchCardResponse);
                case 3:
                    List<Integer> userIdList2 = xhpcCardMapper.selectUserIdsByUserType(queryCondition.getUserType());
                    List<XhpcCustomersPersonnel> xhpcCustomersPersonnelList = xhpcCustomersPersonnelMapper.selectByOperatorId(operatorId, queryCondition.getCurrentPage(), queryCondition.getItems(), userIdList2);
                    Long allCount2 = xhpcCustomersPersonnelMapper.selectCountByOperatorId(operatorId, userIdList2);
                    searchCardResponse.setTotalItems(allCount2);
                    for (XhpcCustomersPersonnel xhpcCustomersPersonnel : xhpcCustomersPersonnelList) {
                        SearchCardResponse.DataDTO dataDTO = new SearchCardResponse.DataDTO();
                        dataDTO.setUserAccount(xhpcCustomersPersonnel.getAccount());
                        dataDTO.setUserType(3);
                        searchCardResponse.getData().add(dataDTO);
                    }
                    return R.ok(searchCardResponse);
                default:
                    return R.fail("没有此用户类型");
            }
        }
        return R.fail("没有此卡类型");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Object> onlineCardBind(BindCardInfo bindCardInfo) {
        //check card type is online card
        if (bindCardInfo.getCardType() != 1) {
            R.fail("该卡不是联网卡");
        }
        if (bindCardInfo.getUserType() !=null || "".equals(bindCardInfo.getUserType())) {
            R.fail("请选择绑定的用户类型");
        }
        if (bindCardInfo.getUserAccount() !=null || "".equals(bindCardInfo.getUserAccount())) {
            R.fail("请选择绑定的用户");
        }
        //build an association
        XhpcIcCardInfo xhpcIcCardInfo = new XhpcIcCardInfo();
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        String tenantId = sysUser.getTenantId();
        xhpcIcCardInfo.setTenantId(tenantId);
        //获取指定用户类型的id
        switch (bindCardInfo.getUserType()) {
            case 0:
                XhpcAppUser xhpcAppUser = xhpcAppUserMapper.selectByPhone(bindCardInfo.getUserAccount());
                Long appUserId = xhpcAppUser.getAppUserId();
                xhpcIcCardInfo.setUserId(appUserId);
                break;
            case 2:
                XhpcCommunityPersonnel xhpcCommunityPersonnel = xhpcCommunityPersonnelMapper.selectByAccount(bindCardInfo.getUserAccount());
                Long communityPersonnelId = xhpcCommunityPersonnel.getCommunityPersonnelId();
                xhpcIcCardInfo.setUserId(communityPersonnelId);
                break;
            case 3:
                XhpcCustomersPersonnel xhpcCustomersPersonnel = xhpcCustomersPersonnelMapper.selectByAccount(bindCardInfo.getUserAccount());
                Long customersPersonnelId = xhpcCustomersPersonnel.getCustomersPersonnelId();
                xhpcIcCardInfo.setUserId(customersPersonnelId);
                break;
            default:
                return R.fail("没有此用户类型");
        }
        xhpcIcCardInfo.setTIccardInfoId(String.valueOf(bindCardInfo.getCardRecordId()));
        xhpcIcCardInfo.setUseStatus(1L);
        xhpcIcCardInfo.setCreateTime(new Date());
        xhpcIcCardInfo.setUserAccount(bindCardInfo.getUserAccount());
        xhpcIcCardInfo.setUserType(bindCardInfo.getUserType());
        xhpcIcCardInfo.setTenantId(tokenService.getLoginUser().getSysUser().getTenantId());
        TIccardInfo tIccardInfo = new TIccardInfo();
        tIccardInfo.setId(bindCardInfo.getCardRecordId());
        tIccardInfo.setStatus(1);
        //tIccardInfo.setCashpledge(Math.toIntExact(bindCardInfo.getCashPledge()));
        tIccardInfoMapper.updateByPrimaryKeySelective(tIccardInfo);
        xhpcCardMapper.insertSelective(xhpcIcCardInfo);
        //write log info
        tIccardInfo = tIccardInfoMapper.selectByPrimaryKey(bindCardInfo.getCardRecordId());
        TIccardLog tIccardLog = new TIccardLog();
        tIccardLog.setUniqueid(tIccardInfo.getCardno());
        tIccardLog.setType((byte) 0);
        tIccardLog.setOperate((byte) 11);
        tIccardLog.setOperatorid(Math.toIntExact(sysUser.getUserId()));
        tIccardLog.setCreatetime(new Date());
        CardLogInfo cardLogInfo = new CardLogInfo();
        cardLogInfo.setOperate(11);
        cardLogInfo.setOperatorId(sysUser.getUserId());
        CardLogInfo.CardInfoDTO cardInfoDTO = new CardLogInfo.CardInfoDTO();
        cardInfoDTO.setCardID(tIccardInfo.getCardid());
        cardInfoDTO.setCardNo(tIccardInfo.getCardno());
        cardInfoDTO.setCardType(tIccardInfo.getCardtype());
        cardInfoDTO.setPassword(tIccardInfo.getPassword());
        cardInfoDTO.setUserIndex(tIccardInfo.getUserindex());
        cardInfoDTO.setCorpNo(tIccardInfo.getCorpno());
        //cardInfoDTO.setCashPledge(tIccardInfo.getCashpledge());
        cardInfoDTO.setBalance(tIccardInfo.getBalance());
        cardInfoDTO.setStatus(tIccardInfo.getStatus());
        cardInfoDTO.setPrepay(null);
        cardLogInfo.setCardInfo(cardInfoDTO);
        String str = JSONUtil.toJsonStr(cardLogInfo);
        tIccardLog.setLog(str);
        tIccardLogMapper.insertSelective(tIccardLog);
        return R.ok();
    }

    @Override
    public R<CardUserInfo> queryWholeUserInfo(Long cardRecordId) {

        CardUserInfo cardUserInfo = new CardUserInfo();
        TIccardInfo tIccardInfo = tIccardInfoMapper.selectByPrimaryKey(Math.toIntExact(cardRecordId));
        Integer cardType = tIccardInfo.getCardtype();
        if (cardType.equals(ConstantClass.OFFLINE_CARD)) {
            cardUserInfo.setUserType(10);
            //公共部分
            cardUserInfo.setCardSerialNumber(tIccardInfo.getCardid());
            cardUserInfo.setCardPhysicalNumber(tIccardInfo.getCardno());
            cardUserInfo.setCardClassification(tIccardInfo.getIsPlatform());
            cardUserInfo.setCashPledge(tIccardInfo.getCashpledge());
            cardUserInfo.setBalance(tIccardInfo.getBalance());
            cardUserInfo.setCardType(tIccardInfo.getCardtype());
            XhpcOperator xhpcOperator = xhpcOperatorMapper.selectOneByCorpNoAndTenantId(tIccardInfo.getCorpno(), tokenService.getLoginUser().getSysUser().getTenantId());
            if(xhpcOperator !=null && xhpcOperator.getName() !=null){
                cardUserInfo.setGrantOperatorName(xhpcOperator.getName());
            }
            //离线用户账号
            TIccardUsers tIccardUser = tIccardUsersMapper.selectByPrimaryKey(tIccardInfo.getUserindex());
            cardUserInfo.setUserAccount(tIccardUser.getPhone());
            cardUserInfo.setUserAge(Integer.valueOf(tIccardUser.getUserage()));
            cardUserInfo.setUserName(tIccardUser.getUsername());
            cardUserInfo.setUserSex(Integer.valueOf(tIccardUser.getUsersex()));
            cardUserInfo.setIdCardNumber(tIccardUser.getIdnumber());
            cardUserInfo.setPhoneNumber(tIccardUser.getPhone());
            cardUserInfo.setLicencePlate(tIccardUser.getLicenceplate());
            return R.ok(cardUserInfo);
        } else {
            XhpcIcCardInfo xhpcIcCardInfo = xhpcCardMapper.getXhpcIcCardInfo(String.valueOf(tIccardInfo.getId()));
            cardUserInfo.setUserAccount(xhpcIcCardInfo.getUserAccount());
            cardUserInfo.setUserType(xhpcIcCardInfo.getUserType());
            //公共部分
            cardUserInfo.setCardSerialNumber(tIccardInfo.getCardid());
            cardUserInfo.setCardPhysicalNumber(tIccardInfo.getCardno());
            cardUserInfo.setCardClassification(tIccardInfo.getIsPlatform());
            cardUserInfo.setCashPledge(tIccardInfo.getCashpledge());
            cardUserInfo.setBalance(tIccardInfo.getBalance());
            cardUserInfo.setCardType(tIccardInfo.getCardtype());
            XhpcOperator xhpcOperator = xhpcOperatorMapper.selectOneByCorpNoAndTenantId(tIccardInfo.getCorpno(), tokenService.getLoginUser().getSysUser().getTenantId());
            if(xhpcOperator !=null && xhpcOperator.getName() !=null){
                cardUserInfo.setGrantOperatorName(xhpcOperator.getName());
            }
            return R.ok(cardUserInfo);
        }
    }

    @Override
    public R<List<Map<String, Object>>> queryOperatorUser() {

        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        String userType = sysUser.getUserType();
        if (!UserTypeUtil.SYS_USER_TYPE_ZERO.equals(userType)) {
            R.fail("该用户不是平台，不能调用此接口");
        }
        String tenantId = sysUser.getTenantId();
        List<XhpcOperator> xhpcOperators = xhpcOperatorMapper.selectAll(tenantId);
        ArrayList<Map<String, Object>> dataList = new ArrayList<>();
        if (xhpcOperators.isEmpty()) {
            return R.ok(dataList);
        }
        for (XhpcOperator xhpcOperator : xhpcOperators) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", xhpcOperator.getOperatorId());
            map.put("name", xhpcOperator.getName());
            dataList.add(map);
        }
        return R.ok(dataList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Object> reportTheLossOfCard(Integer cardId) {

        if (cardId == null) {
            return R.fail("无效cardId");
        }
        TIccardInfo tIccardInfo = tIccardInfoMapper.selectByPrimaryKey(cardId);
        if (tIccardInfo.getStatus() == 3) {
            return R.fail("该卡已经挂失");
        }
        tIccardInfo.setStatus(3);
        tIccardInfoMapper.updateByPrimaryKeySelective(tIccardInfo);
        //write log info
        tIccardInfo = tIccardInfoMapper.selectByPrimaryKey(cardId);
        TIccardLog tIccardLog = new TIccardLog();
        tIccardLog.setUniqueid(tIccardInfo.getCardno());
        tIccardLog.setType((byte) 0);
        tIccardLog.setOperate((byte) 5);
        tIccardLog.setOperatorid(Math.toIntExact(tokenService.getLoginUser().getSysUser().getUserId()));
        tIccardLog.setCreatetime(new Date());
        CardLogInfo cardLogInfo = new CardLogInfo();
        cardLogInfo.setOperate(5);
        cardLogInfo.setOperatorId(tokenService.getLoginUser().getSysUser().getUserId());
        CardLogInfo.CardInfoDTO cardInfoDTO = new CardLogInfo.CardInfoDTO();
        cardInfoDTO.setCardID(tIccardInfo.getCardid());
        cardInfoDTO.setCardNo(tIccardInfo.getCardno());
        cardInfoDTO.setCardType(tIccardInfo.getCardtype());
        cardInfoDTO.setPassword(tIccardInfo.getPassword());
        cardInfoDTO.setUserIndex(tIccardInfo.getUserindex());
        cardInfoDTO.setCorpNo(tIccardInfo.getCorpno());
        cardInfoDTO.setCashPledge(tIccardInfo.getCashpledge());
        cardInfoDTO.setBalance(tIccardInfo.getBalance());
        cardInfoDTO.setStatus(tIccardInfo.getStatus());
        cardInfoDTO.setPrepay(null);
        cardLogInfo.setCardInfo(cardInfoDTO);
        String str = JSONUtil.toJsonStr(cardLogInfo);
        tIccardLog.setLog(str);
        tIccardLogMapper.insertSelective(tIccardLog);
        return R.ok();
    }

    @Override
    public R<OperateCardsLogResponse> operateCardLog(OperateCardsLogRequest operateCardsLogRequest) {
        //计算分页索引
        long startIndex = MyPagingUtil.calculateStartIndex(operateCardsLogRequest.getCurrentPage(), operateCardsLogRequest.getItems());
        operateCardsLogRequest.setCurrentPage(startIndex);
        OperateCardsLogResponse operateCardsLogResponse = new OperateCardsLogResponse();
        operateCardsLogResponse.setData(new ArrayList<>());
        List<TIccardLog> dataList = tIccardLogMapper.selectByCondition(operateCardsLogRequest);
        if (dataList.isEmpty()) {
            return R.ok(operateCardsLogResponse);
        }
        Long totalItems = tIccardLogMapper.selectCountByCondition(operateCardsLogRequest);
        operateCardsLogResponse.setTotalItems(totalItems);
        for (TIccardLog tIccardLog : dataList) {
            OperateCardsLogResponse.DataDTO dataDTO = new OperateCardsLogResponse.DataDTO();
            String log = tIccardLog.getLog();

            CardLogInfo cardLogInfo = JSONUtil.toBean(log, CardLogInfo.class);
            CardLogInfo.CardInfoDTO cardInfo = cardLogInfo.getCardInfo();
            XhpcOperator operator =new XhpcOperator();
            if(tIccardLog.getOperate()==0){
                operator = xhpcOperatorMapper.selectOneByCorpNoAndTenantId(cardInfo.getCorpNo(), tokenService.getLoginUser().getSysUser().getTenantId());
            }else{
                operator = xhpcOperatorMapper.selectOneByCorpNoAndTenantId(cardInfo.getCorpIndex(), tokenService.getLoginUser().getSysUser().getTenantId());
            }
            if(operator !=null && operator.getName() !=null){
                dataDTO.setGrantOperatorName(operator.getName());
            }
            if(tIccardLog.getUniqueid() !=null){
                dataDTO.setCardSerialNumber(tIccardLog.getUniqueid());
            }
            dataDTO.setOperate(Integer.valueOf(tIccardLog.getOperate()));
            if(cardInfo.getCashPledge() !=null){
                dataDTO.setCashPledge(Long.valueOf(cardInfo.getCashPledge()));
            }
            Integer balance = cardInfo.getBalance();
            if (balance != null) {
                dataDTO.setBalance(Long.valueOf(balance));
            }
            dataDTO.setOperateTime(MyDateUtil.parseDateToStr(tIccardLog.getCreatetime()));
            XhpcOperator operator2 = xhpcOperatorMapper.selectByPrimaryKey(cardLogInfo.getOperatorId());
            if (operator2 != null) {
                dataDTO.setOperatorName(operator2.getName());
            } else {
                dataDTO.setOperatorName(null);
            }
            operateCardsLogResponse.getData().add(dataDTO);
        }
        return R.ok(operateCardsLogResponse);
    }

    @Override
    public R<OperateDevicesLogResponse> operateDevicesLog(OperateDevicesLogRequest operateDevicesLogRequest) {
        //计算分页索引
        long startIndex = MyPagingUtil.calculateStartIndex(operateDevicesLogRequest.getCurrentPage(), operateDevicesLogRequest.getItems());
        operateDevicesLogRequest.setCurrentPage(startIndex);
        OperateDevicesLogResponse operateDevicesLogResponse = new OperateDevicesLogResponse();
        operateDevicesLogResponse.setData(new ArrayList<>());
        List<TIccardLog> dataList = tIccardLogMapper.selectDeviceLogListByCondition(operateDevicesLogRequest);
        if (dataList.isEmpty()) {
            return R.ok(operateDevicesLogResponse);
        }
        Long totalItems = tIccardLogMapper.selectCountOfDeviceLogListByCondition(operateDevicesLogRequest);
        operateDevicesLogResponse.setTotalItems(totalItems);
        for (TIccardLog tIccardLog : dataList) {
            OperateDevicesLogResponse.DataDTO dataDTO = new OperateDevicesLogResponse.DataDTO();
            String log = tIccardLog.getLog();
            DeviceLogInfo deviceLogInfo = JSONUtil.toBean(log, DeviceLogInfo.class);
            DeviceLogInfo.DeviceInfoDTO deviceInfo = deviceLogInfo.getDeviceInfo();
            dataDTO.setDeviceSerialNumber(deviceInfo.getSerialNumber());
            dataDTO.setOperateType(Integer.valueOf(tIccardLog.getOperate()));
            dataDTO.setDeviceName(deviceInfo.getDevName());
            dataDTO.setDeviceType(deviceInfo.getDevType());
            dataDTO.setGrantOperatorName(deviceInfo.getCorpName());
            Integer operatorId = tIccardLog.getOperatorid();
            XhpcOperator operator = xhpcOperatorMapper.selectByPrimaryKey(Long.valueOf(operatorId));
            dataDTO.setOperatorName(operator.getName());
            if(tIccardLog.getCreatetime() !=null){
                dataDTO.setOperateTime(MyDateUtil.parseDateToStr(tIccardLog.getCreatetime()));
            }
            operateDevicesLogResponse.getData().add(dataDTO);
        }
        return R.ok(operateDevicesLogResponse);
    }

    @Override
    public R<QueryRechargingRecordResponse> queryRechargingRecord(QueryRechargingRecordRequest queryRechargingRecordRequest) {
        //计算分页索引
        long startIndex = MyPagingUtil.calculateStartIndex(queryRechargingRecordRequest.getCurrentPage(), queryRechargingRecordRequest.getItems());
        queryRechargingRecordRequest.setCurrentPage(startIndex);
        QueryRechargingRecordResponse queryRechargingRecordResponse = new QueryRechargingRecordResponse();
        queryRechargingRecordResponse.setData(new ArrayList<>());
        queryRechargingRecordResponse.setTotalItems(0L);
        Integer cardType = queryRechargingRecordRequest.getClassification();
        //如果卡是离线卡
        if (cardType == 0) {
            List<TIccardLog> tIccardLogList = tIccardLogMapper.selectRechargeLogByUniqueId(queryRechargingRecordRequest.getCardSerialNumber(), queryRechargingRecordRequest.getCurrentPage(), queryRechargingRecordRequest.getItems());
            if (tIccardLogList.isEmpty()) {
                R.ok(queryRechargingRecordResponse);
            }
            Long recordsCount = tIccardLogMapper.selectCountRechargeLogByUniqueId(queryRechargingRecordRequest.getCardSerialNumber());
            queryRechargingRecordResponse.setTotalItems(recordsCount);
            for (TIccardLog tIccardLog : tIccardLogList) {
                String log = tIccardLog.getLog();
                CardLogInfo cardLogInfo = JSONUtil.toBean(log, CardLogInfo.class);
                CardLogInfo.CardInfoDTO cardInfo = cardLogInfo.getCardInfo();
                //Get userAccount by userIndex
                Integer userIndex = cardInfo.getUserIndex();
                TIccardUsers tIccardUsers = tIccardUsersMapper.selectByPrimaryKey(userIndex);
                String accountStr = tIccardUsers.getPhone();
                QueryRechargingRecordResponse.DataDTO dataDTO = new QueryRechargingRecordResponse.DataDTO();
                dataDTO.setUserAccount(accountStr);
                //Get orderNumber（not found in the MySQL database）
                dataDTO.setOrderNumber(null);
                dataDTO.setRechargeOrderNumber(null);
                //Get rechargeMoney
                dataDTO.setAmount(new BigDecimal(cardInfo.getPrepay()));
                dataDTO.setRechargeMoney(Long.valueOf(cardInfo.getPrepay()));
                //Get rechargeMethod (not found in the MySQL database)
                dataDTO.setRechargeMethod("4");
                dataDTO.setOrderStatus(1);
                dataDTO.setStatus(1);
                dataDTO.setType(4);
                dataDTO.setCreateTime(MyDateUtil.parseDateToStr(tIccardLog.getCreatetime()));
                dataDTO.setSource(8);
                queryRechargingRecordResponse.getData().add(dataDTO);
            }
            return R.ok(queryRechargingRecordResponse);
        } else {
            //如果是联网卡
            //Get user id
            switch (queryRechargingRecordRequest.getUserType()) {
                case 0:
                    XhpcAppUser xhpcAppUser = xhpcAppUserMapper.selectByPhone(queryRechargingRecordRequest.getUserAccount());
                    List<XhpcRechargeOrder> xhpcRechargeOrderList = xhpcRechargeOrderMapper.selectByUserId(xhpcAppUser.getAppUserId(), queryRechargingRecordRequest.getCurrentPage(), queryRechargingRecordRequest.getItems());
                    if (xhpcRechargeOrderList.isEmpty()) {
                        return R.ok(queryRechargingRecordResponse);
                    }
                    Long recordsCount = xhpcRechargeOrderMapper.selectCountByUserId(xhpcAppUser.getAppUserId());
                    queryRechargingRecordResponse.setTotalItems(recordsCount);
                    for (XhpcRechargeOrder xhpcRechargeOrder : xhpcRechargeOrderList) {
                        QueryRechargingRecordResponse.DataDTO dataDTO = new QueryRechargingRecordResponse.DataDTO();
                        dataDTO.setUserAccount(xhpcAppUser.getPhone());
                        queryRechargeRecordToEncapsulateDTO(queryRechargingRecordResponse, xhpcRechargeOrder, dataDTO);
                    }
                    return R.ok(queryRechargingRecordResponse);
                case 2:
                    XhpcCommunityPersonnel xhpcCommunityPersonnel = xhpcCommunityPersonnelMapper.selectByAccount(queryRechargingRecordRequest.getUserAccount());
                    List<XhpcRechargeOrder> xhpcRechargeOrderList1 = xhpcRechargeOrderMapper.selectByUserId(xhpcCommunityPersonnel.getCommunityPersonnelId(), queryRechargingRecordRequest.getCurrentPage(), queryRechargingRecordRequest.getItems());
                    if (xhpcRechargeOrderList1.isEmpty()) {
                        return R.ok(queryRechargingRecordResponse);
                    }
                    Long recordsCount1 = xhpcRechargeOrderMapper.selectCountByUserId(xhpcCommunityPersonnel.getCommunityId());
                    queryRechargingRecordResponse.setTotalItems(recordsCount1);
                    for (XhpcRechargeOrder xhpcRechargeOrder : xhpcRechargeOrderList1) {
                        QueryRechargingRecordResponse.DataDTO dataDTO = new QueryRechargingRecordResponse.DataDTO();
                        dataDTO.setUserAccount(xhpcCommunityPersonnel.getAccount());
                        queryRechargeRecordToEncapsulateDTO(queryRechargingRecordResponse, xhpcRechargeOrder, dataDTO);
                    }
                    return R.ok(queryRechargingRecordResponse);
                case 3:
                    XhpcCustomersPersonnel xhpcCustomersPersonnel = xhpcCustomersPersonnelMapper.selectByAccount(queryRechargingRecordRequest.getUserAccount());
                    List<XhpcRechargeOrder> xhpcRechargeOrderList2 = xhpcRechargeOrderMapper.selectByUserId(xhpcCustomersPersonnel.getCustomersPersonnelId(), queryRechargingRecordRequest.getCurrentPage(), queryRechargingRecordRequest.getItems());
                    if (xhpcRechargeOrderList2.isEmpty()) {
                        return R.ok(queryRechargingRecordResponse);
                    }
                    Long recordsCount2 = xhpcRechargeOrderMapper.selectCountByUserId(xhpcCustomersPersonnel.getCustomersPersonnelId());
                    queryRechargingRecordResponse.setTotalItems(recordsCount2);
                    for (XhpcRechargeOrder xhpcRechargeOrder : xhpcRechargeOrderList2) {
                        QueryRechargingRecordResponse.DataDTO dataDTO = new QueryRechargingRecordResponse.DataDTO();
                        dataDTO.setUserAccount(xhpcCustomersPersonnel.getAccount());
                        queryRechargeRecordToEncapsulateDTO(queryRechargingRecordResponse, xhpcRechargeOrder, dataDTO);
                    }
                    return R.ok(queryRechargingRecordResponse);
                default:
                    return R.fail("没有此类型的用户");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Object> removeBinding(Long cardRecordId) {
        //1.find card record according card id
        //2.完成联网卡表的状态改变
        //3.完成朱老师那边的卡表状态改变
        //4.释放关联用户
        //4.写日志到log表中
        XhpcIcCardInfo xhpcIcCardInfo = xhpcCardMapper.selectBytCardId(String.valueOf(cardRecordId));
        if (xhpcIcCardInfo.getDelFlag() == 1) {
            return R.fail("该卡已经解绑，不能重复解绑");
        }
        xhpcIcCardInfo.setDelFlag(1L);
        xhpcCardMapper.updateByPrimaryKeySelective(xhpcIcCardInfo);
        TIccardInfo tIccardInfo = tIccardInfoMapper.selectByPrimaryKey(Math.toIntExact(cardRecordId));
        tIccardInfo.setStatus(0);
        tIccardInfoMapper.updateByPrimaryKeySelective(tIccardInfo);
        TIccardLog tIccardLog = new TIccardLog();
        tIccardLog.setUniqueid(tIccardInfo.getCardid());
        tIccardLog.setType((byte) 0);
        tIccardLog.setOperate((byte) 11);
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        tIccardLog.setOperatorid(Math.toIntExact(sysUser.getUserId()));
        tIccardLog.setCreatetime(new Date());
        //log部分
        CardLogInfo.CardInfoDTO cardInfoDTO = new CardLogInfo.CardInfoDTO();
        cardInfoDTO.setCardID(tIccardInfo.getCardid());
        cardInfoDTO.setCardNo(tIccardInfo.getCardno());
        cardInfoDTO.setCardType(tIccardInfo.getCardtype());
        cardInfoDTO.setPassword(tIccardInfo.getPassword());
        cardInfoDTO.setUserIndex(tIccardInfo.getUserindex());
        cardInfoDTO.setCorpNo(tIccardInfo.getCorpno());
        cardInfoDTO.setCashPledge(tIccardInfo.getCashpledge());
        cardInfoDTO.setBalance(tIccardInfo.getBalance());
        cardInfoDTO.setStatus(tIccardInfo.getStatus());
        cardInfoDTO.setPrepay(null);
        CardLogInfo cardLogInfo = new CardLogInfo();
        cardLogInfo.setCardInfo(cardInfoDTO);
        cardLogInfo.setOperate(11);
        cardLogInfo.setOperatorId(sysUser.getUserId());
        String cardLogInfoStr = JSONUtil.toJsonStr(cardLogInfo);
        tIccardLog.setLog(cardLogInfoStr);
        tIccardLogMapper.updateByPrimaryKeySelective(tIccardLog);
        return R.ok();
    }

    @Override
    public R<Object> addClientUser(TIccardClientUsers userInfo) {

        userInfo.setUsersid(null);
        tIccardClientUsersMapper.insertSelective(userInfo);
        return R.ok();
    }

    @Override
    public R<Object> updateClientUser(TIccardClientUsers userInfo) {

        tIccardClientUsersMapper.updateByPrimaryKeySelective(userInfo);
        return R.ok();
    }

    @Override
    public R<Object> deleteClientUser(Integer userId) {

        TIccardClientUsers user = tIccardClientUsersMapper.selectByPrimaryKey(userId);
        user.setDelFlag(1);
        tIccardClientUsersMapper.updateByPrimaryKey(user);
        return R.ok();
    }

    @Override
    public R<Object> queryClientUsers(queryClientUsersCondition queryCondition) {

        List<TIccardClientUsers> usersList = tIccardClientUsersMapper.selectByCondition(queryCondition);
        return R.ok(usersList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Object> recoverToReportTheLossOfCard(Integer cardId) {

        if (cardId == null) {
            return R.fail("无效cardId");
        }
        TIccardInfo tIccardInfo = tIccardInfoMapper.selectByPrimaryKey(cardId);
        //状态3的意思是该卡是挂失状态
        if (tIccardInfo.getStatus() != 3) {
            return R.fail("该卡不是挂失状态");
        }
        tIccardInfo.setStatus(1);
        tIccardInfoMapper.updateByPrimaryKeySelective(tIccardInfo);
        //write log info
        tIccardInfo = tIccardInfoMapper.selectByPrimaryKey(cardId);
        TIccardLog tIccardLog = new TIccardLog();
        tIccardLog.setUniqueid(tIccardInfo.getCardno());
        //0表示该日志是卡操作日志
        tIccardLog.setType((byte) 0);
        //表示解除挂失
        tIccardLog.setOperate((byte) 6);
        String corpno = tIccardInfo.getCorpno();
        String tenantId = tokenService.getLoginUser().getSysUser().getTenantId();
        tIccardLog.setOperatorid(Math.toIntExact(tokenService.getLoginUser().getSysUser().getUserId()));
        tIccardLog.setCreatetime(new Date());
        CardLogInfo cardLogInfo = new CardLogInfo();
        /**
         * {
         *   "cardInfo": {
         *     "cardID": "A721DCD7",
         *     "cardNo": "2020101304",
         *     "cardType": 0,
         *     "password": null,
         *     "userIndex": 0,
         *     "corpNo": "100002",
         *     "cashPledge": 0,
         *     "balance": 0,
         *     "status": 3,
         *     "prepay": 0
         *   },
         *   "operatorId": 1044,
         *   "operate": 5
         * }
         */
        cardLogInfo.setOperate(6);
        cardLogInfo.setOperatorId(tokenService.getLoginUser().getSysUser().getUserId());
        CardLogInfo.CardInfoDTO cardInfoDTO = new CardLogInfo.CardInfoDTO();
        cardInfoDTO.setCardID(tIccardInfo.getCardid());
        cardInfoDTO.setCardNo(tIccardInfo.getCardno());
        cardInfoDTO.setCardType(tIccardInfo.getCardtype());
        cardInfoDTO.setPassword(tIccardInfo.getPassword());
        cardInfoDTO.setUserIndex(tIccardInfo.getUserindex());
        cardInfoDTO.setCorpNo(tIccardInfo.getCorpno());
        cardInfoDTO.setCashPledge(tIccardInfo.getCashpledge());
        cardInfoDTO.setBalance(tIccardInfo.getBalance());
        cardInfoDTO.setStatus(tIccardInfo.getStatus());
        cardInfoDTO.setPrepay(null);
        cardLogInfo.setCardInfo(cardInfoDTO);
        String str = JSONUtil.toJsonStr(cardLogInfo);
        tIccardLog.setLog(str);
        tIccardLogMapper.insertSelective(tIccardLog);
        return R.ok();
    }

    @Override
    public R addIccardInfo(TIccardInfo iccardInfo) {
         tIccardInfoMapper.insertSelective(iccardInfo);

        return R.ok();
    }

    @Override
    public R removeIccardInfo(Integer id) {
        tIccardInfoMapper.deleteByPrimaryKey(id);
        return R.ok();
    }

    @Override
    public List<Map<String, Object>> getSerialNumberList() {
        return xhpcCardMapper.getSerialNumberList();
    }

    @Override
    public void updateXhpcTerminalAll(Integer status) {
        xhpcCardMapper.updateXhpcTerminalAll(status);
    }

    /**
     * 用于QueryRechargeRecord方法封装DTO数据
     *
     * @author WH
     * @date 2022/2/15 18:15
     * @since version-1.0
     */
    private void queryRechargeRecordToEncapsulateDTO(QueryRechargingRecordResponse queryRechargingRecordResponse, XhpcRechargeOrder xhpcRechargeOrder, QueryRechargingRecordResponse.DataDTO dataDTO) {

        dataDTO.setOrderNumber(xhpcRechargeOrder.getRechargeOrderNumber());
        dataDTO.setRechargeMethod(String.valueOf(xhpcRechargeOrder.getType()));
        dataDTO.setRechargeMoney(xhpcRechargeOrder.getAmount().longValue());
        dataDTO.setOrderStatus(xhpcRechargeOrder.getStatus());
        dataDTO.setCreateTime(MyDateUtil.parseDateToStr(xhpcRechargeOrder.getCreateTime()));
        queryRechargingRecordResponse.getData().add(dataDTO);
    }

    private void fillUserInfo(CardUserInfo cardUserInfo, Map<String, Object> userData) {

        cardUserInfo.setCardSerialNumber((String) userData.get("cardID"));
        cardUserInfo.setCardPhysicalNumber((String) userData.get("cardNo"));
        cardUserInfo.setGrantOperatorName((String) userData.get("name"));
        cardUserInfo.setCardType((Integer) userData.get("is_platform"));
        cardUserInfo.setCardClassification((Integer) userData.get("cardType"));
        cardUserInfo.setCashPledge((Integer) userData.get("cashPledge"));
    }

}
