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

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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.CircleCardInfoMapper;
import com.iotechn.unimall.data.domain.CircleCardInfo;
import com.iotechn.unimall.admin.api.card.ICircleCardInfoService;
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 CircleCardInfoServiceImpl implements ICircleCardInfoService {
    @Autowired
    private CircleCardInfoMapper circleCardInfoMapper;

    @Override
    public Boolean add(CircleCardInfo circleCardInfo) throws ServiceException {
        Date now = new Date();
        circleCardInfo.setGmtCreate(now);
        circleCardInfo.setGmtUpdate(now);
        return circleCardInfoMapper.insert(circleCardInfo) > 0;
    }

    @Override
    public Page<CircleCardInfo> list(Long circleId, Long cardId, Long commonId, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CircleCardInfo> wrapper = new EntityWrapper<CircleCardInfo>();
        if (!StringUtils.isEmpty(circleId)) {
            wrapper.eq("circle_id", circleId);
        }
        if (!StringUtils.isEmpty(cardId)) {
            wrapper.eq("card_id", cardId);
        }
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        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<CircleCardInfo> list = circleCardInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = circleCardInfoMapper.selectCount(wrapper);
        return new Page<CircleCardInfo>(list, page, limit, count);
    }

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

    @Override
    public Boolean update(CircleCardInfo circleCardInfo) throws ServiceException {
        Date now = new Date();
        circleCardInfo.setGmtUpdate(now);
        return circleCardInfoMapper.updateById(circleCardInfo) > 0;
    }

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

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