package com.xhpc.pp.logic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhpc.common.data.up.PileStartChargingData;
import com.xhpc.common.domain.IccardInfo;
import com.xhpc.common.domain.IccardInfoExample;
import com.xhpc.common.enums.StationDeviceEnum;
import com.xhpc.iccard.mapper.IccardInfoMapper;
import com.xhpc.mapper.XhpcDeviceMessageMapper;
import com.xhpc.pp.domain.XhpcDeviceMessage;
import com.xhpc.pp.tx.ServiceParameter;
import com.xhpc.pp.tx.ServiceResult;
import com.xhpc.pp.tx.logic.ServiceLogic;
import com.xhpc.pp.utils.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.xhpc.pp.tx.ServiceResult.HEX_00;

@Lazy
@Component("PileStartChargingDataLogic")
public class PileStartChargingDataLogic implements ServiceLogic {

    private static final Logger log = LoggerFactory.getLogger(PileStartChargingDataLogic.class);

    @Resource
    private XhpcDeviceMessageMapper deviceMessageMapper;
    @Resource
    private IccardInfoMapper iccardInfoMapper;

    @Override
    public ServiceResult service(ServiceParameter sp) throws Exception {

        String resultStr = null;
        String result = ServiceResult.FAIL;
        Map<String, Object> req = sp.getParameters();
        ObjectMapper objectMapper = new ObjectMapper();
        PileStartChargingData pileStartChargingData = objectMapper.convertValue(req, PileStartChargingData.class);
        String connectorId = pileStartChargingData.getPileNo().concat(pileStartChargingData.getGunId());
        String accountOrCardNo = pileStartChargingData.getAccountOrCardNo();
        String corpNo = connectorId.substring(0, 6);
        String balance = "0000";
        String cardNo = "00000000";
        if (pileStartChargingData.getStartType().equals("01")) { //刷卡启动
            IccardInfoExample example = new IccardInfoExample();
            IccardInfoExample.Criteria criteria = example.createCriteria();
            criteria.andCardidEqualTo(accountOrCardNo).andCorpnoEqualTo(corpNo);
            List<IccardInfo> iccardInfos = iccardInfoMapper.selectByExample(example);
            if (iccardInfos.size() == 1) {
                IccardInfo iccardInfo = iccardInfos.get(0);
                cardNo = iccardInfo.getCardno();
                //todo 桩停用
                if (!iccardInfo.getCorpno().equals(corpNo)) {
                    resultStr = "06";
                } else if (HEX_00.equals(pileStartChargingData.getPasswordRequired())
                        || (HEX_00.equals(pileStartChargingData.getPasswordRequired()) && pileStartChargingData.getPassword().equals(iccardInfo.getPassword()))) {
                    int balanceInt = iccardInfo.getBalance();
                    balance = HexUtils.toHexInt(balanceInt);
                    if (balanceInt < 100) {
                        resultStr = "01";
                        result = ServiceResult.OK;
                    } else {
                        resultStr = "03";
                    }
                } else if (iccardInfo.getStatus() != 1) {
                    result = "02";
                }
            } else {
                result = "01";
            }
        }
        String remark = "充电桩主动申请起动充电";
        XhpcDeviceMessage deviceMessage = new XhpcDeviceMessage();
        deviceMessage.setType(StationDeviceEnum.PILE.getCode());
        deviceMessage.setSerialNumber(connectorId);
        deviceMessage.setRemark(remark);
        deviceMessage.setStatus(0);
        deviceMessage.setContent((String) req.get("hex"));
        deviceMessageMapper.insertByDomain(deviceMessage);
        String hex = "orderNo".concat(connectorId).concat(cardNo).concat(balance).concat(resultStr).concat(result); //todo
        // 调用订单接口
        return new ServiceResult(HexUtils.toBytes(hex), result);
    }

}
