package com.hssx.pcbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hssx.pcbms.entity.Goods;
import com.hssx.pcbms.entity.GoodsTags;
import com.hssx.pcbms.entity.Task;
import com.hssx.pcbms.entity.TaskTags;
import com.hssx.pcbms.mapper.TaskMapper;
import com.hssx.pcbms.mapper.TaskTagsMapper;
import com.hssx.pcbms.service.TaskTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.pcbms.util.HttpRespMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴涛涛
 * @since 2019-11-11
 */
@Service
public class TaskTagsServiceImpl extends ServiceImpl<TaskTagsMapper, TaskTags> implements TaskTagsService {
    @Resource
    private TaskTagsMapper taskTagsMapper;
    @Resource
    private TaskMapper taskMapper;

    @Override
    public HttpRespMsg add(TaskTags goodsTags) {
        HttpRespMsg msg = new HttpRespMsg();
        Integer count = taskTagsMapper.selectCount(new QueryWrapper<TaskTags>().eq("name", goodsTags.getName()));
        if (count > 0) {
            msg.setError("该分类标签已存在");
            return msg;
        } else {
            if (null == goodsTags.getId()) {
                taskTagsMapper.insert(goodsTags);
            } else {
                taskTagsMapper.updateById(goodsTags);
            }
        }
        return msg;
    }

    @Override
    public HttpRespMsg getListByKeyName(String keyName) {
        HttpRespMsg msg = new HttpRespMsg();
        QueryWrapper<TaskTags> qw = new QueryWrapper();
        if (keyName != null) {
            qw.like("name", keyName);
        }
        msg.data = taskTagsMapper.selectList(qw);
        return msg;
    }

    @Override
    public HttpRespMsg delTags(TaskTags goodsTags) {
        HttpRespMsg msg = new HttpRespMsg();
        Integer count = taskMapper.selectCount(new QueryWrapper<Task>().eq("tag_id", goodsTags.getId()).last("limit 1"));
        if (count > 0) {
            msg.setError("该标签下存在任务，暂不支持该操作。");
            return msg;
        }
        taskTagsMapper.deleteById(goodsTags.getId());
        return msg;
    }
}
