package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.UserCenter;
import com.hai.dao.HighUserCardMapper;
import com.hai.entity.HighOilCard;
import com.hai.entity.HighUserCard;
import com.hai.entity.HighUserCardExample;
import com.hai.enum_type.OilCardStatusEnum;
import com.hai.enum_type.UserCardType;
import com.hai.model.HighUserModel;
import com.hai.service.HighOilCardService;
import com.hai.service.HighUserCardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service("highUserCardService")
public class HighUserCardServiceImpl implements HighUserCardService {

    @Resource
    private HighUserCardMapper highUserCardMapper;

    @Resource
    private UserCenter userCenter;

    @Resource
    private HighOilCardService oilCardService;

    @Override
    public void editCard(HighUserCard highUserCard) {
        if(highUserCard.getId() == null){
            highUserCard.setCreateTime(new Date());
            highUserCard.setUpdateTime(new Date());
            highUserCard.setStatus(1);
            highUserCardMapper.insert(highUserCard);
        } else {
            highUserCard.setUpdateTime(new Date());
            highUserCardMapper.updateByPrimaryKey(highUserCard);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void bindCard(Integer type, String cardNo,Long userId) {
        HighUserCard userCard = new HighUserCard();
        userCard.setType(type);
        userCard.setUserId(userId);
        userCard.setCardNo(cardNo);
        editCard(userCard);

        if (type.equals(UserCardType.type2.getType())) {
            // 查询卡号
            HighOilCard card = oilCardService.getOilCardByCardNo(cardNo);
            if (card == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "油卡卡号错误");
            }
            if (!card.getStatus().equals(OilCardStatusEnum.status1.getStatus())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "油卡状态异常");
            }
            if (card.getBindStatus() == 1) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "油卡已被绑定，请勿重复绑定");
            }
            card.setBindTime(new Date());
            card.setBindStatus(1);
            oilCardService.editData(card);
        }
    }

    @Override
    public void delUserCard(String cardNo, Long userId) {
        HighUserCard card = getDetailByUserCardNo(userId, cardNo);
        if (card == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
        }
        card.setStatus(0);
        editCard(card);

        if (card.getType().equals(UserCardType.type2.getType())) {
            // 查询卡号
            HighOilCard oilCard = oilCardService.getOilCardByCardNo(cardNo);
            if (oilCard == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "油卡卡号错误");
            }
            oilCard.setBindTime(null);
            oilCard.setBindStatus(0);
            oilCardService.editData(oilCard);
        }
    }

    @Override
    public HighUserCard getDetailById(Long id) {
        return highUserCardMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HighUserCard> getListByUser(Long userId, Integer type) {
        HighUserCardExample example = new HighUserCardExample();
        HighUserCardExample.Criteria criteria = example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        example.setOrderByClause("create_time desc");
        return highUserCardMapper.selectByExample(example);
    }

    @Override
    public HighUserCard getDetailByUserCardNo(Long userId, String cardNo) {
        HighUserCardExample example = new HighUserCardExample();
        example.createCriteria().andUserIdEqualTo(userId).andCardNoEqualTo(cardNo).andStatusNotEqualTo(0);
        List<HighUserCard> list = highUserCardMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<HighUserCard> getDetailByCardNo(String cardNo) {
        HighUserCardExample example = new HighUserCardExample();
        example.createCriteria().andCardNoEqualTo(cardNo).andStatusNotEqualTo(0);
        return highUserCardMapper.selectByExample(example);
    }

    @Override
    public Boolean isBindHtlCard(Long userId) {
        HighUserCardExample example = new HighUserCardExample();
        example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
        List<HighUserCard> list = highUserCardMapper.selectByExample(example);
        if (list.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public HighUserCard getUserHtlCardNo(Long userId) {
        HighUserCardExample example = new HighUserCardExample();
        example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
        List<HighUserCard> list = highUserCardMapper.selectByExample(example);
        if(list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
