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

import java.util.List;

import com.iotechn.unimall.data.domain.CardManagementInfo;
import com.iotechn.unimall.data.domain.CommonUserInfo;
import com.iotechn.unimall.data.domain.PersonalHomepageInfo;
import com.iotechn.unimall.data.mapper.CardManagementInfoMapper;
import com.iotechn.unimall.data.mapper.CommonUserInfoMapper;
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.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.util.ExcelUtil;
import com.iotechn.unimall.data.mapper.CardHolderInfoMapper;
import com.iotechn.unimall.data.domain.CardHolderInfo;
import com.iotechn.unimall.admin.api.card.ICardHolderInfoService;
import com.iotechn.unimall.data.model.Page;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

/**
 * 名片夹Service业务层处理
 *
 * @author jlb
 * @date 2023-04-10
 */
@Service
public class CardHolderInfoServiceImpl implements ICardHolderInfoService {
    @Autowired
    private CardHolderInfoMapper cardHolderInfoMapper;
    @Autowired
    private CardManagementInfoMapper cardManagementInfoMapper;
    @Autowired
    private CommonUserInfoMapper commonUserInfoMapper;

    @Override
    public Boolean add(CardHolderInfo cardHolderInfo) throws ServiceException {
        Date now = new Date();
        cardHolderInfo.setGmtCreate(now);
        cardHolderInfo.setGmtUpdate(now);
        //个人展示的二维码 相互交换
        if (cardHolderInfo.getCardCommonId() != null) {
            CardHolderInfo cardHolderInfo1 = new CardHolderInfo();
            cardHolderInfo1.setCardId(cardHolderInfo.getMyCardId());
            cardHolderInfo1.setCommonId(cardHolderInfo.getCardCommonId());
            cardHolderInfo1.setGmtCreate(now);
            cardHolderInfo1.setGmtUpdate(now);
            cardHolderInfoMapper.insert(cardHolderInfo1);
        }
        return cardHolderInfoMapper.insert(cardHolderInfo) > 0;
    }

    @Override
    public Page<CardHolderInfo> list(Long commonId, String cardId, String classify, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CardHolderInfo> wrapper = new EntityWrapper<CardHolderInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(cardId)) {
            wrapper.eq("card_id", cardId);
        }
        if (!StringUtils.isEmpty(classify)) {
            wrapper.eq("classify", classify);
        }
        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);
        List<CardHolderInfo> list = cardHolderInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            for (CardHolderInfo cardHolderInfo : list) {
                CardManagementInfo cardManagementInfo = cardManagementInfoMapper.selectById(cardHolderInfo.getCardId());
                if (cardManagementInfo != null) {
                    cardHolderInfo.setCardManagementInfo(cardManagementInfo);
                }
                //查看是否允许分享
                CommonUserInfo commonUserInfo = commonUserInfoMapper.selectById(cardHolderInfo.getCommonId());
                if (commonUserInfo != null) {
                    cardHolderInfo.setShareCard(commonUserInfo.getShareCard());
                }
            }
        }
        Integer count = cardHolderInfoMapper.selectCount(wrapper);
        return new Page<CardHolderInfo>(list, page, limit, count);
    }

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

    @Override
    public Boolean update(CardHolderInfo cardHolderInfo) throws ServiceException {
        Date now = new Date();
        cardHolderInfo.setGmtUpdate(now);
        return cardHolderInfoMapper.updateById(cardHolderInfo) > 0;
    }

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

    @Override
    public String export(Long commonId, String cardId, String classify, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CardHolderInfo> wrapper = new EntityWrapper<CardHolderInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(cardId)) {
            wrapper.eq("card_id", cardId);
        }
        if (!StringUtils.isEmpty(classify)) {
            wrapper.eq("classify", classify);
        }
        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<CardHolderInfo> list = cardHolderInfoMapper.selectList(wrapper);
        ExcelUtil<CardHolderInfo> util = new ExcelUtil<CardHolderInfo>(CardHolderInfo.class);
        return util.exportExcel(list, "操作日志");
    }
}
