package com.hfkj.service.card.impl;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsUserCardMapper;
import com.hfkj.entity.BsUserCard;
import com.hfkj.entity.BsUserCardExample;
import com.hfkj.service.card.BsUserCardService;
import com.hfkj.service.hlt.HuiLianTongUnionCardService;
import com.hfkj.service.user.BsUserService;
import com.hfkj.sysenum.UserCardStatusEnum;
import com.hfkj.sysenum.UserCardTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsUserCardServiceImpl
 * @author: HuRui
 * @date: 2024/5/17
 **/
@Service("userCardService")
public class BsUserCardServiceImpl implements BsUserCardService {
    @Resource
    private BsUserCardMapper userCardMapper;
    @Resource
    private BsUserService userService;
    @Override
    public void editData(BsUserCard data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            userCardMapper.insert(data);

        } else {
            userCardMapper.updateByPrimaryKeySelective(data);
        }
    }

    @Override
    public BsUserCard getCard(String cardNo) {
        BsUserCardExample example = new BsUserCardExample();
        example.createCriteria().andCardNoEqualTo(cardNo).andStatusNotEqualTo(UserCardStatusEnum.status0.getCode());
        List<BsUserCard> list = userCardMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsUserCard getCard(Long userId) {
        BsUserCardExample example = new BsUserCardExample();
        example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(UserCardStatusEnum.status0.getCode());
        List<BsUserCard> list = userCardMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void insertUserCardBatch(List<BsUserCard> data) {
        userCardMapper.insertUserCardList(data);
    }

    @Override
    public void updateUserCardBatch(List<BsUserCard> data) {
        userCardMapper.updateUserCardList(data);
    }

    @Override
    public BsUserCard getCard(Long userId, String cardNo) {
        BsUserCardExample example = new BsUserCardExample();
        example.createCriteria()
                .andUserIdEqualTo(userId)
                .andCardNoEqualTo(cardNo)
                .andStatusNotEqualTo(UserCardStatusEnum.status0.getCode());
        List<BsUserCard> list = userCardMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsUserCard> getCardList(Long userId, UserCardTypeEnum type) {
        BsUserCardExample example = new BsUserCardExample();
        BsUserCardExample.Criteria criteria = example.createCriteria()
                .andUserIdEqualTo(userId)
                .andStatusNotEqualTo(UserCardStatusEnum.status0.getCode());
        if (type != null) {
            criteria.andTypeEqualTo(type.getCode());
        }
        example.setOrderByClause("create_time desc");
        return userCardMapper.selectByExample(example);
    }

    @Override
    public List<BsUserCard> getCardList(Map<String, Object> map) {
        BsUserCardExample example = new BsUserCardExample();
        BsUserCardExample.Criteria criteria = example.createCriteria();
        return userCardMapper.selectByExample(example);
    }

    @Override
    public void bindCard(UserCardTypeEnum type, Long userId, String cardNo) throws Exception {
        if (UserCardTypeEnum.type1.getCode() == type.getCode()) {
            if (!getCardList(userId, type).isEmpty()) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "账户已绑定工会卡，请勿重复绑定！");
            }
            // 查询工会卡信息
            JSONObject cardInfo = HuiLianTongUnionCardService.queryCardInfo(cardNo);
            if (!cardInfo.getString("respCode").equals("0000")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到工会卡信息");
            }
            JSONObject cardInfoObject = HuiLianTongUnionCardService.resolveResponse(cardInfo.getString("data"));
            if (!cardInfoObject.getBoolean("success")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, cardInfoObject.getString("message"));
            }
        }

        if (getCard(cardNo) != null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前卡号已被账户绑定，请勿重复绑定！");
        }
        BsUserCard card = new BsUserCard();
        card.setUserId(userId);
        card.setType(type.getCode());
        card.setCardNo(cardNo);
        card.setStatus(UserCardStatusEnum.status1.getCode());
        editData(card);

        // 更新登录信息
        userService.updateSession(userId);
    }

    @Override
    public void unbindCard(Long userId, String cardNo) {
        // 查询卡号
        BsUserCard card = getCard(cardNo);
        if (card == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前卡号已被账户绑定，请勿重复绑定！");
        }
        if (!card.getUserId().equals(userId)) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "账户不一致");
        }
        card.setStatus(UserCardStatusEnum.status0.getCode());
        editData(card);

        // 更新登录信息
        userService.updateSession(userId);
    }


}
