package com.iformall.service.impl;

import java.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.WxTags;
import com.iformall.domain.po.WxTagsGroup;
import com.iformall.domain.po.WxTagsType;
import com.iformall.domain.vo.WxTagsGroupVo;
import com.iformall.domain.vo.WxTagsTypeVo;
import com.iformall.mapper.WxTagsMapper;
import com.iformall.service.WxTagsGroupService;
import com.iformall.service.WxTagsService;
import com.iformall.service.WxTagsTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iformall.common.IdWorker;

@Service
public class WxTagsServiceImpl implements WxTagsService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxTagsMapper wxTagsMapper;

    @Autowired
    private WxTagsTypeService wxTagsTypeService;

    @Autowired
    private WxTagsGroupService wxTagsGroupService;


    @Override
    public PageInfo<WxTags> listAsPage(WxTags record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxTagsMapper.findList(record));
    }

    @Override
    public WxTags getById(Long id) {
        return wxTagsMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(WxTags record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxTagsMapper.insert(record);
        } else {
            wxTagsMapper.updateById(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        wxTagsMapper.deleteById(id);
    }

    @Override
    public List<WxTags> findList(WxTags wxTags) {
        return wxTagsMapper.findList(wxTags);
    }

    @Override
    public List<WxTagsGroupVo> listAllVo() {
        List<WxTagsGroupVo> groupVos = new ArrayList<>();
        List<WxTagsGroup> groups = wxTagsGroupService.findList(null);
        for (WxTagsGroup tg : groups) {
            WxTagsGroupVo tgvo = new WxTagsGroupVo();
            tgvo.setId(tg.getId());
            tgvo.setName(tg.getName());

            WxTagsType type = new WxTagsType();
            type.setGroupId(tg.getId());
            List<WxTagsTypeVo> typeVos = new ArrayList<>();
            List<WxTagsType> types = wxTagsTypeService.findList(type);
            for (WxTagsType tt : types) {
                WxTagsTypeVo ttvo = new WxTagsTypeVo();
                ttvo.setId(tt.getId());
                ttvo.setName(tt.getName());
                ttvo.setFlag(tt.getFlag());

                WxTags t = new WxTags();
                t.setTypeId(tt.getId());
                ttvo.setTags(findList(t));
                typeVos.add(ttvo);
            }
            tgvo.setTypes(typeVos);
            groupVos.add(tgvo);
        }
        return groupVos;
    }

}
