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

import java.util.List;

import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.AppServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.data.domain.CardManagementInfo;
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.CardClassifyInfoMapper;
import com.iotechn.unimall.data.domain.CardClassifyInfo;
import com.iotechn.unimall.admin.api.card.ICardClassifyInfoService;
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 CardClassifyInfoServiceImpl implements ICardClassifyInfoService {
    @Autowired
    private CardClassifyInfoMapper cardClassifyInfoMapper;

    @Override
    public Boolean add(CardClassifyInfo cardClassifyInfo) throws ServiceException {
        Date now = new Date();
        cardClassifyInfo.setGmtCreate(now);
        cardClassifyInfo.setGmtUpdate(now);
        List<CardClassifyInfo> cardClassifyInfoList = cardClassifyInfoMapper.selectList(new EntityWrapper<CardClassifyInfo>()
                .eq("common_id", cardClassifyInfo.getCommonId())
                .eq("delete_flag", 0)
                .eq("circle_name", cardClassifyInfo.getCircleName()));
        if (cardClassifyInfoList.size() > 0) {
            throw new AppServiceException(ExceptionDefinition.CLASSIFY_REPEAT_ERROR);
        }
        return cardClassifyInfoMapper.insert(cardClassifyInfo) > 0;
    }

    @Override
    public Page<CardClassifyInfo> list(Long commonId, String circleName, Long topMarking, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CardClassifyInfo> wrapper = new EntityWrapper<CardClassifyInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(circleName)) {
            wrapper.eq("circle_name", circleName);
        }
        if (!StringUtils.isEmpty(topMarking)) {
            wrapper.eq("top_marking", topMarking);
        }
        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).orderBy("top_marking", false).orderBy("gmt_create", true);
        List<CardClassifyInfo> list = cardClassifyInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = cardClassifyInfoMapper.selectCount(wrapper);
        return new Page<CardClassifyInfo>(list, page, limit, count);
    }

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

    @Override
    public Boolean top(CardClassifyInfo cardClassifyInfo) throws ServiceException {
        CardClassifyInfo cardClassifyInfo1 = cardClassifyInfoMapper.selectById(cardClassifyInfo.getId());
        List<CardClassifyInfo> cardClassifyInfoList = cardClassifyInfoMapper.selectList(new EntityWrapper<CardClassifyInfo>()
                .eq("common_id", cardClassifyInfo1.getCommonId())
                .eq("delete_flag", 0));
        if (!CollectionUtils.isEmpty(cardClassifyInfoList)) {
            for (CardClassifyInfo aa : cardClassifyInfoList) {
                aa.setTopMarking(0l);
                cardClassifyInfoMapper.updateById(aa);
            }
        }
        cardClassifyInfo1.setTopMarking(1l);
        cardClassifyInfo1.setGmtUpdate(new Date());
        return cardClassifyInfoMapper.updateById(cardClassifyInfo1) > 0;
    }

    @Override
    public Boolean topDefault(CardClassifyInfo cardClassifyInfo) throws ServiceException {
        List<CardClassifyInfo> cardClassifyInfoList = cardClassifyInfoMapper.selectList(new EntityWrapper<CardClassifyInfo>()
                .eq("common_id", cardClassifyInfo.getCommonId())
                .eq("delete_flag", 0));
        if (!CollectionUtils.isEmpty(cardClassifyInfoList)) {
            for (CardClassifyInfo aa : cardClassifyInfoList) {
                aa.setTopMarking(0l);
                cardClassifyInfoMapper.updateById(aa);
            }
        }
        return true;
    }

    @Override
    public Boolean update(CardClassifyInfo cardClassifyInfo) throws ServiceException {
        Date now = new Date();
        cardClassifyInfo.setGmtUpdate(now);
        return cardClassifyInfoMapper.updateById(cardClassifyInfo) > 0;
    }

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

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