package com.iotechn.unimall.admin.api.card.impl;

import java.util.List;

import com.iotechn.unimall.admin.api.card.ICardExchangeInfoService;
import com.iotechn.unimall.core.exception.AppServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.data.domain.CardExchangeInfo;
import com.iotechn.unimall.data.domain.CardHolderInfo;
import com.iotechn.unimall.data.domain.CardManagementInfo;
import com.iotechn.unimall.data.dto.goods.GroupShopDTO;
import com.iotechn.unimall.data.dto.order.OrderDTO;
import com.iotechn.unimall.data.mapper.CardExchangeInfoMapper;
import com.iotechn.unimall.data.mapper.CardHolderInfoMapper;
import com.iotechn.unimall.data.mapper.CardManagementInfoMapper;
import com.iotechn.unimall.data.model.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.data.util.ExcelUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.iotechn.unimall.core.exception.ServiceException;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

/**
 * 名片交换Service业务层处理
 *
 * @author jlb
 * @date 2023-04-12
 */
@Service
public class CardExchangeInfoServiceImpl implements ICardExchangeInfoService {
    @Autowired
    private CardExchangeInfoMapper cardExchangeInfoMapper;
    @Autowired
    private CardHolderInfoMapper cardHolderInfoMapper;
    @Autowired
    private CardManagementInfoMapper cardManagementInfoMapper;

    @Override
    public Boolean add(CardExchangeInfo cardExchangeInfo) throws ServiceException {
        Date now = new Date();
        cardExchangeInfo.setGmtCreate(now);
        cardExchangeInfo.setGmtUpdate(now);
        return cardExchangeInfoMapper.insert(cardExchangeInfo) > 0;
    }

    @Override
    public Page<CardExchangeInfo> list(Long sendId, Long receiveId, Long sendCardId, Long receiveCardId, String circleName, String status, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CardExchangeInfo> wrapper = new EntityWrapper<CardExchangeInfo>();
        if (!StringUtils.isEmpty(sendId)) {
            wrapper.eq("send_id", sendId);
        }
        if (!StringUtils.isEmpty(receiveId)) {
            wrapper.eq("receive_id", receiveId);
        }
        if (!StringUtils.isEmpty(sendCardId)) {
            wrapper.eq("send_card_id", sendCardId);
        }
        if (!StringUtils.isEmpty(receiveCardId)) {
            wrapper.eq("receive_card_id", receiveCardId);
        }
        if (!StringUtils.isEmpty(circleName)) {
            wrapper.eq("circle_name", circleName);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        wrapper.eq("delete_flag", 0);
        Integer count = cardExchangeInfoMapper.getCardExchangeCount(status,(page - 1) * limit, limit, receiveId);
        List<CardExchangeInfo> list = cardExchangeInfoMapper.getCardExchangeInfoPage(status,(page - 1) * limit, limit, receiveId);
        if (!CollectionUtils.isEmpty(list)){
            for (CardExchangeInfo cardExchangeInfo:list){
                CardManagementInfo cardManagementInfo = cardManagementInfoMapper.selectById(cardExchangeInfo.getSendCardId());
                if (cardManagementInfo != null) {
                    cardExchangeInfo.setCardManagementInfo(cardManagementInfo);
                }
            }
        }
        return new Page<CardExchangeInfo>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String id) {
        String[] ids = String.valueOf(id).split(",");
        for (String tt : ids) {
            CardExchangeInfo tmp = cardExchangeInfoMapper.selectById(Long.parseLong(tt));
            if (tmp != null) {
                tmp.setDeleteFlag(1L);
                cardExchangeInfoMapper.updateById(tmp);
            }
        }
        return true;
    }

    @Override
    public Boolean update(CardExchangeInfo cardExchangeInfo, String updateFlag) throws ServiceException {
        Date now = new Date();
        cardExchangeInfo.setGmtUpdate(now);
        //拉黑
        if ("1".equals(updateFlag)) {
            cardExchangeInfo.setStatus("7");
        }
        //拒绝
        else if ("2".equals(updateFlag)) {
            cardExchangeInfo.setStatus("3");
        }
        //接受
        else if ("3".equals(updateFlag)) {
            cardExchangeInfo.setStatus("1");
            //接收人名片夹添加数据
            CardHolderInfo cardHolderInfo = new CardHolderInfo();
            cardHolderInfo.setCardId(String.valueOf(cardExchangeInfo.getSendCardId()));
            CardManagementInfo cardManagementInfo = cardManagementInfoMapper.selectById(cardExchangeInfo.getSendCardId());
            //查看发起人名片是否删除
            if (cardManagementInfo.getCommonId() == -1) {
                throw new AppServiceException(ExceptionDefinition.ACCEPT_CARD_ERROR);
            }
            cardHolderInfo.setCommonId(cardExchangeInfo.getReceiveId());
            cardHolderInfo.setGmtCreate(now);
            cardHolderInfo.setGmtUpdate(now);
            cardHolderInfoMapper.insert(cardHolderInfo);
            //发起人名片夹添加数据
            cardHolderInfo = new CardHolderInfo();
            cardHolderInfo.setCardId(String.valueOf(cardExchangeInfo.getReceiveCardId()));
            cardHolderInfo.setCommonId(cardExchangeInfo.getSendId());
            cardHolderInfo.setGmtCreate(now);
            cardHolderInfo.setGmtUpdate(now);
            cardHolderInfoMapper.insert(cardHolderInfo);
        }
        //移除
        else {
            cardExchangeInfo.setStatus("0");
        }
        return cardExchangeInfoMapper.updateById(cardExchangeInfo) > 0;
    }

    @Override
    public CardExchangeInfo get(Long id) throws ServiceException {
        return cardExchangeInfoMapper.selectById(id);
    }

    @Override
    public String export(Long sendId, Long receiveId, Long sendCardId, Long receiveCardId, String circleName, String status, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CardExchangeInfo> wrapper = new EntityWrapper<CardExchangeInfo>();
        if (!StringUtils.isEmpty(sendId)) {
            wrapper.eq("send_id", sendId);
        }
        if (!StringUtils.isEmpty(receiveId)) {
            wrapper.eq("receive_id", receiveId);
        }
        if (!StringUtils.isEmpty(sendCardId)) {
            wrapper.eq("send_card_id", sendCardId);
        }
        if (!StringUtils.isEmpty(receiveCardId)) {
            wrapper.eq("receive_card_id", receiveCardId);
        }
        if (!StringUtils.isEmpty(circleName)) {
            wrapper.eq("circle_name", circleName);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        List<CardExchangeInfo> list = cardExchangeInfoMapper.selectList(wrapper);
        ExcelUtil<CardExchangeInfo> util = new ExcelUtil<CardExchangeInfo>(CardExchangeInfo.class);
        return util.exportExcel(list, "操作日志");
    }
}
