package com.kiisoo.ic.tag.service;

import com.kiisoo.ic.tag.entity.TagDO;
import com.kiisoo.ic.tag.mapper.TagDOMapper;
import com.kiisoo.ic.wx.service.QWMailListManageService;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 标签管理service
 * @Auther: yechenhao
 * @Date: 2020/4/7 0002 10:06
 * @Version: v1
 */
@Service
public class TagService {

    @Autowired
    private QWMailListManageService qwMailListManageService;

    @Autowired
    private TagDOMapper tagDOMapper;

    /**
     * 增加标签
     * @param tagDO 标签实体类
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    public Boolean addTag(TagDO tagDO) throws Exception {
        //数据库创建标签（需要返回标签id）
        Long maxId = tagDOMapper.selectMaxTagId();
        tagDO.setId(maxId+1);
        int insert = tagDOMapper.insert(tagDO);
        if (insert == 0){
            throw new Exception("创建标签service失败");
        }
        //成功则添加用户到企业微信
        qwMailListManageService.addTag(tagDO);
        return true;
    }

    /**
     * 修改标签
     * @param tagDO 标签实体类
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateTag(TagDO tagDO) throws Exception {
        //数据库创建标签（需要返回标签id）
        int update = tagDOMapper.updateById(tagDO);
        if (update == 0){
            throw new Exception("修改标签service失败");
        }
        //成功则添加用户到企业微信
        qwMailListManageService.updateTag(tagDO);
        return true;
    }

    /**
     * 删除标签
     * @param tagId 标签实体id
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteTag(Long tagId) throws Exception {
        //数据库创建标签（需要返回标签id）
        int delete = tagDOMapper.deleteById(tagId);
        if (delete == 0){
            throw new Exception("删除标签service失败");
        }
        //todo 删除标签后操作员工
        //成功则添加用户到企业微信
        qwMailListManageService.deleteTag(tagId);
        return true;
    }
}
