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

import java.util.List;

import com.iotechn.unimall.core.util.GeneratorUtil;
import com.iotechn.unimall.data.util.QRCodeUtil;
import com.iotechn.unimall.data.domain.*;
import com.iotechn.unimall.data.mapper.CardHolderInfoMapper;
import com.iotechn.unimall.data.mapper.CircleCardInfoMapper;
import com.iotechn.unimall.data.mapper.PersonalHomepageInfoMapper;
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.CardManagementInfoMapper;
import com.iotechn.unimall.admin.api.card.ICardManagementInfoService;
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 CardManagementInfoServiceImpl implements ICardManagementInfoService {
    @Autowired
    private CardManagementInfoMapper cardManagementInfoMapper;
    @Autowired
    private QRCodeUtil qrCodeUtil;
    @Autowired
    private CircleCardInfoMapper circleCardInfoMapper;
    @Autowired
    private CardHolderInfoMapper cardHolderInfoMapper;
    @Autowired
    private PersonalHomepageInfoMapper personalHomepageInfoMapper;

    @Override
    public Boolean add(CardManagementInfo cardManagementInfo) throws ServiceException {
        Date now = new Date();
        cardManagementInfo.setGmtCreate(now);
        cardManagementInfo.setGmtUpdate(now);
        cardManagementInfoMapper.insert(cardManagementInfo);
        //更新数据库默认值
        cardManagementInfo = cardManagementInfoMapper.selectById(cardManagementInfo.getId());
        //二维码
        String str = qrCodeUtil.getQRCodeImage(String.valueOf(cardManagementInfo.getId()), false, cardManagementInfo.getName(), GeneratorUtil.genUUId());
        String str1 = qrCodeUtil.getQRCodeImage(cardManagementInfo.getId() + "," + cardManagementInfo.getCommonId(), false, cardManagementInfo.getName(), GeneratorUtil.genUUId());
        cardManagementInfo.setQrCode(str);
        cardManagementInfo.setQrCodeMyself(str1);
        return cardManagementInfoMapper.updateById(cardManagementInfo) > 0;
    }

    @Override
    public Page<CardManagementInfo> list(Long commonId, String cardBusiness, String name, String post, String companyName, String location, String detailedAddress, String phone, String headSculpture, String qrCode, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CardManagementInfo> wrapper = new EntityWrapper<CardManagementInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(cardBusiness)) {
            wrapper.eq("card_business", cardBusiness);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("name", name);
        }
        if (!StringUtils.isEmpty(post)) {
            wrapper.eq("post", post);
        }
        if (!StringUtils.isEmpty(companyName)) {
            wrapper.eq("company_name", companyName);
        }
        if (!StringUtils.isEmpty(location)) {
            wrapper.eq("location", location);
        }
        if (!StringUtils.isEmpty(detailedAddress)) {
            wrapper.eq("detailed_address", detailedAddress);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(headSculpture)) {
            wrapper.eq("head_sculpture", headSculpture);
        }
        if (!StringUtils.isEmpty(qrCode)) {
            wrapper.eq("qr_code", qrCode);
        }
        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);
        List<CardManagementInfo> list = cardManagementInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            for (CardManagementInfo cardManagementInfo : list) {
                PersonalHomepageInfo personalHomepageInfo = new PersonalHomepageInfo();
                personalHomepageInfo.setCommonId(cardManagementInfo.getCommonId());
                personalHomepageInfo.setDeleteFlag(0l);
                PersonalHomepageInfo personalHomepageInfo1 = personalHomepageInfoMapper.selectOne(personalHomepageInfo);
                if (personalHomepageInfo1 != null) {
                    cardManagementInfo.setPersonalHomeId(personalHomepageInfo1.getId());
                }
            }
        }
        Integer count = cardManagementInfoMapper.selectCount(wrapper);
        return new Page<CardManagementInfo>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String id, String deleteCondition) {
        String[] ids = String.valueOf(id).split(",");
        for (String tt : ids) {
            CardManagementInfo tmp = cardManagementInfoMapper.selectById(Long.parseLong(tt));
            if (tmp != null) {
                //退出关联的圈子
                List<CircleCardInfo> list = circleCardInfoMapper.selectList(new EntityWrapper<CircleCardInfo>()
                        .eq("card_id", tmp.getId())
                        .eq("delete_flag", 0));
                if (!CollectionUtils.isEmpty(list)) {
                    for (CircleCardInfo aa : list) {
                        aa.setDeleteFlag(1l);
                        aa.setGmtUpdate(new Date());
                        circleCardInfoMapper.updateById(aa);
                    }
                }
                //从所有拥有此名片用户列表删除
                if ("1".equals(deleteCondition)) {
                    List<CardHolderInfo> cardHolderInfoList = cardHolderInfoMapper.selectList(new EntityWrapper<CardHolderInfo>()
                            .eq("card_id", tmp.getId())
                            .eq("delete_flag", 0));
                    if (!CollectionUtils.isEmpty(cardHolderInfoList)) {
                        for (CardHolderInfo aa : cardHolderInfoList) {
                            aa.setDeleteFlag(1l);
                            aa.setGmtUpdate(new Date());
                            cardHolderInfoMapper.updateById(aa);
                        }
                    }
                }
                tmp.setCommonId(-1l);
                tmp.setGmtUpdate(new Date());
                cardManagementInfoMapper.updateById(tmp);
            }
        }
        return true;
    }

    @Override
    public Boolean update(CardManagementInfo cardManagementInfo) throws ServiceException {
        Date now = new Date();
        cardManagementInfo.setGmtUpdate(now);
        return cardManagementInfoMapper.updateById(cardManagementInfo) > 0;
    }

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

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

    @Override
    public String export(Long commonId, String cardBusiness, String name, String post, String companyName, String location, String detailedAddress, String phone, String headSculpture, String qrCode, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<CardManagementInfo> wrapper = new EntityWrapper<CardManagementInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(cardBusiness)) {
            wrapper.eq("card_business", cardBusiness);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("name", name);
        }
        if (!StringUtils.isEmpty(post)) {
            wrapper.eq("post", post);
        }
        if (!StringUtils.isEmpty(companyName)) {
            wrapper.eq("company_name", companyName);
        }
        if (!StringUtils.isEmpty(location)) {
            wrapper.eq("location", location);
        }
        if (!StringUtils.isEmpty(detailedAddress)) {
            wrapper.eq("detailed_address", detailedAddress);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(headSculpture)) {
            wrapper.eq("head_sculpture", headSculpture);
        }
        if (!StringUtils.isEmpty(qrCode)) {
            wrapper.eq("qr_code", qrCode);
        }
        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<CardManagementInfo> list = cardManagementInfoMapper.selectList(wrapper);
        ExcelUtil<CardManagementInfo> util = new ExcelUtil<CardManagementInfo>(CardManagementInfo.class);
        return util.exportExcel(list, "操作日志");
    }
}
