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

import java.util.*;

import com.iotechn.unimall.data.domain.*;
import com.iotechn.unimall.data.mapper.*;
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.admin.api.card.ICircleManagementInfoService;
import com.iotechn.unimall.data.model.Page;
import org.springframework.transaction.annotation.Transactional;

/**
 * 圈子管理Service业务层处理
 *
 * @author jlb
 * @date 2023-04-10
 */
@Service
public class CircleManagementInfoServiceImpl implements ICircleManagementInfoService {
    @Autowired
    private CircleManagementInfoMapper circleManagementInfoMapper;
    @Autowired
    private CircleCardInfoMapper circleCardInfoMapper;
    @Autowired
    private CardManagementInfoMapper cardManagementInfoMapper;
    @Autowired
    private CommonUserInfoMapper commonUserInfoMapper;
    @Autowired
    private CardHolderInfoMapper cardHolderInfoMapper;
    @Autowired
    private CardExchangeInfoMapper cardExchangeInfoMapper;

    @Override
    public Boolean add(CircleManagementInfo circleManagementInfo) throws ServiceException {
        Date now = new Date();
        circleManagementInfo.setGmtCreate(now);
        circleManagementInfo.setGmtUpdate(now);
        //添加圈子状态默认未审核
        circleManagementInfo.setStatus(0l);
        return circleManagementInfoMapper.insert(circleManagementInfo) > 0;
    }

    @Override
    public Page<CircleManagementInfo> list(Long commonId, String circleName, String circleLabel, Long status, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CircleManagementInfo> wrapper = new EntityWrapper<CircleManagementInfo>();
        if (!StringUtils.isEmpty(circleName)) {
            wrapper.like("circle_name", circleName);
        }
        if (!StringUtils.isEmpty(circleLabel)) {
            wrapper.eq("circle_label", circleLabel);
        }
        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);
        List<CircleManagementInfo> list = circleManagementInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            for (CircleManagementInfo circleManagementInfo : list) {
                List<CircleCardInfo> circleCardInfoList = circleCardInfoMapper.selectList(new EntityWrapper<CircleCardInfo>()
                        .eq("circle_id", circleManagementInfo.getId())
                        .eq("delete_flag", 0));
                circleManagementInfo.setCardNum(circleCardInfoList.size());
                List<CircleCardInfo> circleCardInfoList1 = circleCardInfoMapper.selectList(new EntityWrapper<CircleCardInfo>()
                        .eq("circle_id", circleManagementInfo.getId())
                        .eq("common_id",commonId)
                        .eq("delete_flag", 0));
                if (circleCardInfoList1.size()>0){
                    circleManagementInfo.setAddedFlag(1l);
                } else {
                    circleManagementInfo.setAddedFlag(0l);
                }
            }
        }
        Collections.sort(list, new Comparator<CircleManagementInfo>() {
            @Override
            public int compare(CircleManagementInfo o1, CircleManagementInfo o2) {
                return o1.getAddedFlag() > o2.getAddedFlag() ? -1 : (o1.getAddedFlag() == o2.getAddedFlag()) ? 0 : 1;
            }
        });
        Integer count = circleManagementInfoMapper.selectCount(wrapper);
        return new Page<CircleManagementInfo>(list, page, limit, count);
    }

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

    @Override
    public Boolean update(CircleManagementInfo circleManagementInfo) throws ServiceException {
        Date now = new Date();
        circleManagementInfo.setGmtUpdate(now);
        return circleManagementInfoMapper.updateById(circleManagementInfo) > 0;
    }

    @Override
    public CircleManagementInfo get(Long id, Long commonId) throws ServiceException {
        CircleManagementInfo circleManagementInfo = circleManagementInfoMapper.selectById(id);
        CircleCardInfo circleCardInfo = new CircleCardInfo();
        circleCardInfo.setCommonId(commonId);
        circleCardInfo.setCircleId(circleManagementInfo.getId());
        circleCardInfo.setDeleteFlag(0l);
        CircleCardInfo circleCardInfo1 = circleCardInfoMapper.selectOne(circleCardInfo);
        //我加入该圈子的名片
        if (circleCardInfo1 != null) {
            circleManagementInfo.setCircleCardInfo(circleCardInfo1);
        }
        List<CircleCardInfo> circleCardInfoList = circleCardInfoMapper.selectList(new EntityWrapper<CircleCardInfo>()
                .eq("circle_id", circleManagementInfo.getId())
                .eq("delete_flag", 0));
        circleManagementInfo.setCardNum(circleCardInfoList.size());
        return circleManagementInfo;
    }

    @Override
    public Page<CardManagementInfo> circleCardlist(Long commonId, Long id, String status, Integer page, Integer limit) throws ServiceException {
        Wrapper<CardManagementInfo> wrapper = new EntityWrapper<CardManagementInfo>();
        wrapper.eq("delete_flag", 0);
        Integer count = cardManagementInfoMapper.getCardManagementCount(status,(page - 1) * limit, limit, commonId,id);
        List<CardManagementInfo> list = cardManagementInfoMapper.getCardManagementPage(status,(page - 1) * limit, limit, commonId,id);
        return new Page<CardManagementInfo>(list, page, limit, count);
    }

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